Skip to content

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

[Metabot] Chat internal format improvements (#77301)

Sloan Sparger6912a50e8530
diff --git a/bun.lock b/bun.lock
index bc9d46200c5..09d3bae7e5c 100644
--- a/bun.lock
+++ b/bun.lock
@@ -98,6 +98,7 @@
"dompurify": "^3.4.11",
"echarts": "6.1.0",
"events": "^3.3.0",
+ "eventsource-parser": "^3.0.6",
"fast-text-encoding": "^1.0.6",
"formik": "^2.4.5",
"frimousse": "^0.3.0",
@@ -204,6 +205,7 @@
"@rspack/dev-server": "^2.0.1",
"@rspack/plugin-react-refresh": "^2.0.0",
"@sinonjs/fake-timers": "^9.1.2",
+ "@stardazed/streams-text-encoding": "^1.0.2",
"@storybook/addon-a11y": "^8.5.0",
"@storybook/addon-actions": "^8.5.0",
"@storybook/addon-essentials": "^8.5.0",
@@ -1501,6 +1503,8 @@
"@snowplow/tracker-core": ["@snowplow/[email protected]", "", { "dependencies": { "tslib": "^2.3.0", "uuid": "^3.4.0" } }, "sha512-EsOs8HlWPp3SIFn3TSG5Eo8l/QEU0ZyHVIlvmoQT3fOdsWZVrTK9IlrWi/sA0S43tVxpHKLuiEKgmQIrQXuVyw=="],
+ "@stardazed/streams-text-encoding": ["@stardazed/[email protected]", "", {}, "sha512-f2Z15BId3t44a/u21yYSGXFAkCyKocmAyduoAy7swnZ4xIfbaZlOWsgly/jDNNOuj6hYQN72UaBRe3Z/tOHfqg=="],
+
"@storybook/addon-a11y": ["@storybook/[email protected]", "", { "dependencies": { "@storybook/addon-highlight": "8.5.0", "@storybook/test": "8.5.0", "axe-core": "^4.2.0", "vitest-axe": "^0.1.0" } }, "sha512-dTKlnhOaDsAXxkmHz7m6/qb98IENoaXTCG3fXo2iwJ1xT27fZF+i8fz8oQprLAN5r7xlnz66ARJvEIKJ+Lxjgw=="],
"@storybook/addon-actions": ["@storybook/[email protected]", "", { "dependencies": { "@storybook/global": "^5.0.0", "@types/uuid": "^9.0.1", "dequal": "^2.0.2", "polished": "^4.2.2", "uuid": "^9.0.0" } }, "sha512-6CW9+17rk5eNx6I8EKqCxRKtsJFTR/lHL+xiJ6/iBWApIm8sg63vhXvUTJ58UixmIkT5oLh0+ESNPh+x10D8fw=="],
diff --git a/e2e/support/helpers/e2e-metabot-helpers.ts b/e2e/support/helpers/e2e-metabot-helpers.ts
index cd25837be7e..cc926fe84b3 100644
--- a/e2e/support/helpers/e2e-metabot-helpers.ts
+++ b/e2e/support/helpers/e2e-metabot-helpers.ts
@@ -1,5 +1,11 @@
import type { StaticResponse } from "cypress/types/net-stubbing";
+import type {
+ FinishReason,
+ MessageMetadata,
+ SSEEvent,
+} from "metabase/api/ai-streaming/sse-types";
+
import { appBar } from "./e2e-ui-elements-helpers";
export function metabotChatSidebar() {
@@ -77,6 +83,88 @@ export function lastChatMessage() {
return chatMessages().last();
}
+const lifecycleStartFor = (events: SSEEvent[]): SSEEvent[] => {
+ const first = events[0]?.type;
+ return first === "start" || first === "start-step"
+ ? []
+ : [{ type: "start", messageId: "mock-message" }, { type: "start-step" }];
+};
+
+const lifecycleFinishFor = (events: SSEEvent[]): (SSEEvent | "[DONE]")[] => {
+ const last = events.at(-1)?.type;
+ const tail: (SSEEvent | "[DONE]")[] = [];
+ if (last !== "finish-step" && last !== "finish") {
+ tail.push({ type: "finish-step" });
+ }
+ if (last !== "finish") {
+ tail.push({ type: "finish", finishReason: "stop" });
+ }
+ tail.push("[DONE]");
+ return tail;
+};
+
+/**
+ * Serialize Metabot v2 SSE parts into a `text/event-stream` response body.
+ *
+ * Accepts each part as a positional argument; a part is either one event or an
+ * array of events (e.g. `metabotTextPart`, which expands to start/delta/end).
+ * Arguments are flattened one level, so parts compose without spreading:
+ * createMetabotSSEBody(
+ * metabotTextPart("Here is the link"),
+ * metabotDataPart("navigate_to", path),
+ * )
+ *
+ * Each event is emitted as a `data: {JSON}\n\n` chunk, wrapped in the backend
+ * lifecycle to match real server output:
+ * `start` → `start-step` → ...<parts>... → `finish-step` → `finish` → `[DONE]`
+ * A lifecycle event supplied at the head or tail is preserved rather than
+ * duplicated, so a custom `finish` (e.g. `finishReason: "error"`) flows through.
+ */
+export const createMetabotSSEBody = (
+ ...parts: Array<SSEEvent | SSEEvent[]>
+): string => {
+ const events = parts.flat();
+ return [
+ ...lifecycleStartFor(events),
+ ...events,
+ ...lifecycleFinishFor(events),
+ ]
+ .map((event) => {
+ const payload = typeof event === "string" ? event : JSON.stringify(event);
+ return `data: ${payload}\n\n`;
+ })
+ .join("");
+};
+
+/** A streamed assistant text message, emitted as start/delta/end events. */
+export const metabotTextPart = (text: string, id = "text-0"): SSEEvent[] => [
+ { type: "text-start", id },
+ { type: "text-delta", id, delta: text },
+ { type: "text-end", id },
+];
+
+/** A `data-{subtype}` part, e.g. `metabotDataPart("state", { queries: {} })`. */
+export const metabotDataPart = (subtype: string, data: unknown): SSEEvent => ({
+ type: `data-${subtype}`,
+ data,
+});
+
+/** A streamed error message. */
+export const metabotErrorPart = (errorText: string): SSEEvent => ({
+ type: "error",
+ errorText,
+});
+
+/** The trailing finish event; carries the finish reason and usage metadata. */
+export const metabotFinishPart = (
+ finishReason: FinishReason = "stop",
+ messageMetadata?: MessageMetadata,
+): SSEEvent => ({
+ type: "finish",
+ finishReason,
+ ...(messageMetadata ? { messageMetadata } : {}),
+});
+
export const mockMetabotResponse = (response: StaticResponse) => {
return cy
.intercept("POST", "/api/metabot/agent-streaming", (req) => {
diff --git a/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx
index c99ffc99467..74a2689e7b3 100644
--- a/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/metabot-question.cy.spec.tsx
@@ -29,16 +29,29 @@ const adHocQuestionPath = `/question#${btoa(
}),
)}`;
-const metabotResponse = `0:"Here is the [question link](${adHocQuestionPath})"`;
-const metabotResponseWithNavigateTo = `${metabotResponse}
-2:{"type":"navigate_to","version":1,"value":"${adHocQuestionPath}"}`;
-
-const metabotRetryResponse = `0:"Retry: Here is the [question link](${adHocQuestionPath})"`;
-
-// Captured navigate_to event verbatim from a real embedding Metabot response
+const metabotResponse = H.createMetabotSSEBody(
+ H.metabotTextPart(`Here is the [question link](${adHocQuestionPath})`),
+);
+const metabotResponseWithNavigateTo = H.createMetabotSSEBody(
+ H.metabotTextPart(`Here is the [question link](${adHocQuestionPath})`),
+ H.metabotDataPart("navigate_to", adHocQuestionPath),
+);
+
+const metabotRetryResponse = H.createMetabotSSEBody(
+ H.metabotTextPart(`Retry: Here is the [question link](${adHocQuestionPath})`),
+);
+
+// navigate_to response captured from a real embedding Metabot session
// (database id swapped from 2 to 1 to match this env's Sample Database).
-const metabotResponseWithSqlEditor = `0:"I'll direct you to the SQL editor for the Sample Database so you can write a query for the most popular products!"
-2:{"type":"navigate_to","version":1,"value":"/question#eyJkYXRhc2V0X3F1ZXJ5Ijp7ImRhdGFiYXNlIjoxLCJ0eXBlIjoibmF0aXZlIiwibmF0aXZlIjp7InF1ZXJ5IjoiIn19fQ=="}`;
+const metabotResponseWithSqlEditor = H.createMetabotSSEBody(
+ H.metabotTextPart(
+ "I'll direct you to the SQL editor for the Sample Database so you can write a query for the most popular products!",
+ ),
+ H.metabotDataPart(
+ "navigate_to",
+ "/question#eyJkYXRhc2V0X3F1ZXJ5Ijp7ImRhdGFiYXNlIjoxLCJ0eXBlIjoibmF0aXZlIiwibmF0aXZlIjp7InF1ZXJ5IjoiIn19fQ==",
+ ),
+);
describe("scenarios > embedding-sdk > metabot-question", () => {
const setup = (response: string) => {
diff --git a/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsx
index 8e59d2b0427..2d5fd2dee64 100644
--- a/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsx
+++ b/e2e/test-component/scenarios/embedding-sdk/use-metabot.cy.spec.tsx
@@ -36,8 +36,10 @@ const adHocQuestionPathProducts = buildAdHocPath({
});
const buildNavigateToResponse = (path: string) =>
- `0:"Here is the [question link](${path})"
-2:{"type":"navigate_to","version":1,"value":"${path}"}`;
+ H.createMetabotSSEBody(
+ H.metabotTextPart(`Here is the [question link](${path})`),
+ H.metabotDataPart("navigate_to", path),
+ );
type MetabotConsumerProps = {
prompts: string[];
diff --git a/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.ts b/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.ts
index f3986109372..b83ab193d49 100644
--- a/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.ts
+++ b/e2e/test/scenarios/embedding/sdk-iframe-embedding/custom-elements-api.cy.spec.ts
@@ -528,9 +528,10 @@ describe("scenarios > embedding > sdk iframe embedding > custom elements api", (
}),
)}`;
- const metabotResponse = `0:"Here is the [question link](${adHocQuestionPath})"`;
- const metabotResponseWithNavigateTo = `${metabotResponse}
- 2:{"type":"navigate_to","version":1,"value":"${adHocQuestionPath}"}`;
+ const metabotResponseWithNavigateTo = H.createMetabotSSEBody(
+ H.metabotTextPart(`Here is the [question link](${adHocQuestionPath})`),
+ H.metabotDataPart("navigate_to", adHocQuestionPath),
+ );
it("should allow to save a new question", () => {
H.mockMetabotResponse({
diff --git a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts b/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts
index 3c83deeba9e..8748b5ff6b1 100644
--- a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts
+++ b/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts
@@ -152,12 +152,10 @@ describe("Metabot Query Builder", () => {
// Response helpers
const mockNavigateToResponse = (path: string) =>
- `2:{"type":"navigate_to","version":1,"value":"${path}"}
-d:{"finishReason":"stop","usage":{"promptTokens":100,"completionTokens":10}}`;
+ H.createMetabotSSEBody(H.metabotDataPart("navigate_to", path));
const mockTextOnlyResponse = (text: string) =>
- `0:"${text}"
-d:{"finishReason":"stop","usage":{"promptTokens":100,"completionTokens":10}}`;
+ H.createMetabotSSEBody(H.metabotTextPart(text));
const mockGeneratedEntityResponse = (datasetQuery: unknown) => {
const value = {
@@ -167,9 +165,10 @@ const mockGeneratedEntityResponse = (datasetQuery: unknown) => {
query: { id: "query-1", query: datasetQuery },
display: "table",
};
- return `2:{"type":"generated_entity","version":1,"value":${JSON.stringify(value)}}
-d:{"finishReason":"stop","usage":{"promptTokens":100,"completionTokens":10}}`;
+ return H.createMetabotSSEBody(H.metabotDataPart("generated_entity", value));
};
-const mockErrorResponse = `3:"Anthropic API key expired or invalid"
-d:{"finishReason":"error","usage":{}}`;
+const mockErrorResponse = H.createMetabotSSEBody(
+ H.metabotErrorPart("Anthropic API key expired or invalid"),
+ H.metabotFinishPart("error"),
+);
diff --git a/e2e/test/scenarios/metabot/metabot.cy.spec.ts b/e2e/test/scenarios/metabot/metabot.cy.spec.ts
index 7f6ebc19e81..232332101d0 100644
--- a/e2e/test/scenarios/metabot/metabot.cy.spec.ts
+++ b/e2e/test/scenarios/metabot/metabot.cy.spec.ts
@@ -68,8 +68,7 @@ describe("Metabot UI", () => {
cy.log("test on message shorter than prompt");
H.mockMetabotResponse({
statusCode: 200,
- body: `0:"${loremIpsum.repeat(5)}"
-d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ body: H.createMetabotSSEBody(H.metabotTextPart(loremIpsum.repeat(5))),
});
H.sendMetabotMessage("You really mean that?");
cy.log("scroll new prompt to top of the scroll area");
@@ -87,8 +86,7 @@ d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
H.mockMetabotResponse({
statusCode: 200,
- body: `0:"${loremIpsum.repeat(50)}"
-d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ body: H.createMetabotSSEBody(H.metabotTextPart(loremIpsum.repeat(50))),
});
H.sendMetabotMessage("Keep going...");
@@ -103,8 +101,7 @@ d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
it("should open metabot to the bottom of the conversation when reopened with message history", () => {
H.mockMetabotResponse({
statusCode: 200,
- body: `0:"${loremIpsum.repeat(5)}"
-d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ body: H.createMetabotSSEBody(H.metabotTextPart(loremIpsum.repeat(5))),
});
H.openMetabotViaSearchButton();
H.sendMetabotMessage("Who is your favorite?");
@@ -252,9 +249,15 @@ describe("Metabot in full-app embedding", () => {
});
});
-const whoIsYourFavoriteResponse = `0:"You, but don't tell anyone."
-2:{"type":"state","version":1,"value":{"queries":{}}}
-d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`;
-
-const apiKeyInvalidResponse = `3:"Anthropic API key expired or invalid"
-d:{"finishReason":"error","usage":{}}`;
+const whoIsYourFavoriteResponse = H.createMetabotSSEBody(
+ H.metabotTextPart("You, but don't tell anyone."),
+ H.metabotDataPart("state", { queries: {} }),
+ H.metabotFinishPart("stop", {
+ usage: { inputTokens: 4916, outputTokens: 8, totalTokens: 4924 },
+ }),
+);
+
+const apiKeyInvalidResponse = H.createMetabotSSEBody(
+ H.metabotErrorPart("Anthropic API key expired or invalid"),
+ H.metabotFinishPart("error"),
+);
diff --git a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
index f76aa4a50a2..b38b8349671 100644
--- a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
+++ b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
@@ -241,9 +241,13 @@ describe("Native SQL generation", () => {
// Response helpers
const mockCodeEditResponse = (sql: string) =>
- `2:{"type":"code_edit","version":1,"value":{"buffer_id":"qb","mode":"rewrite","value":"${sql}"}}
-d:{"finishReason":"stop","usage":{"promptTokens":100,"completionTokens":10}}`;
+ H.createMetabotSSEBody(
+ H.metabotDataPart("code_edit", {
+ buffer_id: "qb",
+ mode: "rewrite",
+ value: sql,
+ }),
+ );
const mockTextOnlyResponse = (text: string) =>
- `0:"${text}"
-d:{"finishReason":"stop","usage":{"promptTokens":100,"completionTokens":10}}`;
+ H.createMetabotSSEBody(H.metabotTextPart(text));
diff --git a/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.ts b/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.ts
index 816679ac749..0091811b9ff 100644
--- a/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.ts
+++ b/e2e/test/scenarios/metabot/transforms-codegen.cy.spec.ts
@@ -502,8 +502,9 @@ const createMockTransformSuggestionResponse = (
text: string,
transformJSON: string,
) => {
- return `0:"${text}"
-2:{"type":"transform_suggestion","version":1,"value":${transformJSON}}
-2:{"type":"state","version":1,"value":{}}
-d:{"finishReason":"stop","usage":{}}`;
+ return H.createMetabotSSEBody(
+ H.metabotTextPart(text),
+ H.metabotDataPart("transform_suggestion", JSON.parse(transformJSON)),
+ H.metabotDataPart("state", {}),
+ );
};
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.clj b/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.clj
index fe2af84a044..a31684cdc9d 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_analytics/conversations.clj
@@ -155,7 +155,7 @@
[rows]
(let [conversation-ids (map :id rows)
messages-by-conv (when (seq conversation-ids)
- (->> (t2/select [:model/MetabotMessage :conversation_id :data]
+ (->> (t2/select [:model/MetabotMessage :conversation_id :data :data_version]
:conversation_id [:in conversation-ids]
{:where [:= :deleted_at nil]})
(group-by :conversation_id)))]
diff --git a/enterprise/backend/src/metabase_enterprise/metabot_analytics/queries.clj b/enterprise/backend/src/metabase_enterprise/metabot_analytics/queries.clj
index 6db0acd0375..5b3708bca76 100644
--- a/enterprise/backend/src/metabase_enterprise/metabot_analytics/queries.clj
+++ b/enterprise/backend/src/metabase_enterprise/metabot_analytics/queries.clj
@@ -4,19 +4,23 @@
tool call."
(:require
[metabase.driver.util :as driver.u]
+ [metabase.metabot.schema.v2 :as schema.v2]
[metabase.metabot.tools :as metabot.tools]
[metabase.sql-tools.core :as sql-tools]
[metabase.util.log :as log]))
(set! *warn-on-reflection* true)
-(defn- tool-output->structured
- "Pull `:structured-output` (or its snake-case alias) out of a persisted
- `tool-output` block's `:result`. Returns nil if the result lacks structured
- data — that signals a tool that errored out before validating."
- [result]
- (when (map? result)
- (or (:structured-output result) (:structured_output result))))
+(defn- tool-part->structured
+ "Pull the structured output out of a v2 tool part's `:output`. New writes
+ canonicalize the key to `:structured_output`; rows migrated from v1 keep
+ their original result map, which may use kebab-case `:structured-output`
+ instead. Returns nil if the output lacks structured data — that signals a
+ tool that errored out before validating."
+ [part]
+ (let [output (:output part)]
+ (when (map? output)
+ (or (:structured_output output) (:structured-output output)))))
(defn- referenced-table-names
"Use Macaw to extract distinct referenced table names from a SQL string.
@@ -44,13 +48,11 @@
[tool-name]
(= tool-name "construct_notebook_query"))
-(defn- successful-tool-output?
- "True when `output-block` exists, is not marked errored, and carries
- structured output."
- [output-block]
- (and output-block
- (not (:error output-block))
- (tool-output->structured (:result output-block))))
+(defn- successful-tool-part?
+ "True when the call resolved successfully and carries structured output."
+ [part]
+ (and (= "output-available" (:state part))
+ (some? (tool-part->structured part))))
(defn- sql-for-tool-call
"Best-effort SQL text for a successful SQL tool call. Prefer the structured
@@ -64,10 +66,10 @@
nil)))
(defn- sql-query-row
- [{:keys [message-id]} tool-name input-block structured database-id]
- (let [sql (sql-for-tool-call tool-name (:arguments input-block) structured)]
+ [{:keys [message-id]} tool-name part structured database-id]
+ (let [sql (sql-for-tool-call tool-name (:input part) structured)]
{:tool tool-name
- :call_id (:id input-block)
+ :call_id (:toolCallId part)
:message_id message-id
:query_id (:query-id structured)
:query_type "sql"
@@ -78,10 +80,10 @@
:tables (referenced-table-names database-id sql)}))
(defn- notebook-query-row
- [{:keys [message-id]} tool-name input-block structured query database-id]
+ [{:keys [message-id]} tool-name part structured query database-id]
(let [chart-type (:chart-type structured)]
{:tool tool-name
- :call_id (:id input-block)
+ :call_id (:toolCallId part)
:message_id message-id
:query_id (:query-id structured)
:query_type "notebook"
@@ -91,34 +93,29 @@
:database_id database-id
:tables []}))
-(defn- query-block-pair->row
- "Build a generated-query row from an in-app metabot tool-input/tool-output
- pair. Returns nil for tools we don't track or for failed/missing outputs
- (errored rows are filtered out at the source — v1 doesn't render them)."
- [{:keys [message-id]} input-block output-block]
- (let [tool-name (:function input-block)]
+(defn- query-part->row
+ "Build a generated-query row from a v2 tool part. Returns nil for tools we
+ don't track or for failed/unresolved calls (errored rows are filtered out at
+ the source — v1 doesn't render them)."
+ [{:keys [message-id]} part]
+ (let [tool-name (schema.v2/tool-part-name part)]
(when (and (query-generation-tool? tool-name)
- (successful-tool-output? output-block))
- (let [structured (tool-output->structured (:result output-block))
+ (successful-tool-part? part))
+ (let [structured (tool-part->structured part)
query (:query structured)
database-id (or (:database structured) (:database query))]
(if (notebook-tool? tool-name)
- (notebook-query-row {:message-id message-id} tool-name input-block structured query database-id)
- (sql-query-row {:message-id message-id} tool-name input-block structured database-id))))))
+ (notebook-query-row {:message-id message-id} tool-name part structured query database-id)
+ (sql-query-row {:message-id message-id} tool-name part structured database-id))))))
(defn- message->generated-queries
- "Extract generated-query rows from a single `MetabotMessage`. Pairs each
- `tool-input` block with its matching `tool-output` block by `:id`."
+ "Extract generated-query rows from a single `MetabotMessage`'s v2 tool parts."
[message]
- (let [blocks (:data message)
- outputs-by-id (->> blocks
- (filter #(= "tool-output" (:type %)))
- (into {} (map (juxt :id identity))))
- ctx {:message-id (:id message)}]
+ (let [ctx {:message-id (:id message)}]
(into []
- (comp (filter #(= "tool-input" (:type %)))
- (keep #(query-block-pair->row ctx % (get outputs-by-id (:id %)))))
- blocks)))
+ (comp (filter schema.v2/tool-part?)
+ (keep #(query-part->row ctx %)))
+ (:data message))))
(defn messages->generated-queries
"Walk a seq of `MetabotMessage` instances and return a flat vector of
@@ -128,20 +125,16 @@
[messages]
(into [] (mapcat message->generated-queries) messages))
-(defn- tool-input-block? [block tool-names]
- (and (= "tool-input" (:type block))
- (if (set? tool-names)
- (contains? tool-names (:function block))
- (= tool-names (:function block)))))
-
(defn count-tool-invocations
- "Count `tool-input` blocks across a seq of `MetabotMessage` instances.
- `tool-names` may be a single tool-name string or a set of tool-name
- strings — a block counts if its `:function` matches."
+ "Count v2 tool parts across a seq of `MetabotMessage` instances, regardless
+ of how the call resolved. `tool-names` may be a single tool-name string or
+ a set of tool-name strings."
[messages tool-names]
- (transduce
- (comp (mapcat :data)
- (filter #(tool-input-block? % tool-names)))
- (completing (fn [acc _] (inc acc)))
- 0
- messages))
+ (let [counted? (if (set? tool-names) tool-names #{tool-names})]
+ (transduce
+ (comp (mapcat :data)
+ (filter schema.v2/tool-part?)
+ (filter (comp counted? schema.v2/tool-part-name)))
+ (completing (fn [acc _] (inc acc)))
+ 0
+ messages)))
diff --git a/enterprise/backend/test/metabase_enterprise/metabot/api_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/api_test.clj
index af3af53194a..597cd1c609c 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot/api_test.clj
@@ -10,7 +10,8 @@
[metabase.metabot.settings :as metabot.settings]
[metabase.metabot.test-util :as mut]
[metabase.premium-features.core :as premium-features]
- [metabase.test :as mt]))
+ [metabase.test :as mt]
+ [metabase.util.json :as json]))
(set! *warn-on-reflection* true)
@@ -38,13 +39,20 @@
:conversation_id (str (random-uuid))
:history []
:state {}})
- lines (str/split-lines response)
- error-line (first (filter #(str/starts-with? % "3:") lines))]
+ events (->> (str/split-lines response)
+ (filter #(str/starts-with? % "data: "))
+ (remove #(= "data: [DONE]" %))
+ (map #(json/decode+kw (subs % 6))))
+ error-event (first (filter #(= "error" (:type %)) events))]
(is (false? @llm-called?)
"the LLM must never be called once the instance limit is exceeded")
- (is (some? error-line))
- (is (str/includes? error-line "ai_usage_limit_reached"))
- (is (str/includes? error-line (ee.metabot.settings/metabot-quota-reached-message))))))))
+ (is (some? error-event)
+ "an SSE error event is streamed")
+ (is (= (ee.metabot.settings/metabot-quota-reached-message)
+ (:errorText error-event))
+ "the quota message is the error event's errorText")
+ (is (str/includes? response "ai_usage_limit_reached")
+ "the ai_usage_limit_reached code surfaces on the finish event"))))))
;; the limit check is TTL-memoized process-wide; bust it so the now-removed temp limit
;; doesn't leak a stale "exceeded" verdict into other tests
(finally
diff --git a/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.clj
index 7045f832a23..0d3d2800ab7 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot_analytics/api_test.clj
@@ -22,7 +22,8 @@
ip-address (assoc :ip_address ip-address))))
(defn- insert-message!
- [{:keys [conversation-id created-at role profile-id total-tokens data deleted-at]}]
+ [{:keys [conversation-id created-at role profile-id total-tokens data data-version deleted-at]
+ :or {data-version 2}}]
(first (t2/insert-returning-pks!
:model/MetabotMessage
(cond-> {:conversation_id conversation-id
@@ -30,6 +31,7 @@
:profile_id profile-id
:total_tokens total-tokens
:data data
+ :data_version data-version
:external_id (str (random-uuid))}
created-at (assoc :created_at created-at)
deleted-at (assoc :deleted_at deleted-at)))))
@@ -107,37 +109,37 @@
:role "user"
:profile-id "ignored-user-model"
:total-tokens 3
- :data [{:role "user" :content "hello"}]})
+ :data [{:type "text" :text "hello"}]})
(insert-message! {:conversation-id convo-1
:created-at jan-3
:role "assistant"
:profile-id "nlq"
:total-tokens 7
- :data [{:role "assistant" :content "hi"}]})
+ :data [{:type "text" :text "hi"}]})
(insert-message! {:conversation-id convo-2
:created-at jan-4
:role "user"
:profile-id "ignored-user-model"
:total-tokens 2
- :data [{:role "user" :content "question"}]})
+ :data [{:type "text" :text "question"}]})
(insert-message! {:conversation-id convo-2
:created-at jan-5
:role "assistant"
:profile-id "internal"
:total-tokens 11
- :data [{:role "assistant" :content "answer"}]})
+ :data [{:type "text" :text "answer"}]})
(insert-message! {:conversation-id convo-2
:created-at jan-5
:role "assistant"
:profile-id "internal"
:total-tokens 13
- :data [{:role "assistant" :content "follow-up"}]})
+ :data [{:type "text" :text "follow-up"}]})
(insert-message! {:conversation-id convo-3
:created-at jan-4
:role "assistant"
:profile-id "deleted-model"
:total-tokens 99
- :data [{:role "assistant" :content "deleted"}]
+ :data [{:type "text" :text "deleted"}]
:deleted-at jan-5})
;; convo-1 sums across usage rows; convo-2 also has a NULL-cache row
;; (predates the cache-token columns); convo-3 has no usage rows at all.
@@ -264,7 +266,7 @@
:role "assistant"
:profile-id profile-id
:total-tokens tokens
- :data [{:role "assistant" :content "hi"}]}))]
+ :data [{:type "text" :text "hi"}]}))]
(try
(insert-conversation! {:conversation-id convo-a :user-id user-a
:created-at jan-1 :ip-address "10.0.0.3"})
@@ -416,7 +418,7 @@
:role "user"
:profile-id "ignored-user-profile"
:total-tokens 4
- :data [{:role "user" :content "hello"}]})
+ :data [{:type "text" :text "hello"}]})
(insert-message! {:conversation-id conversation-id
:created-at jan-2
:role "assistant"
@@ -463,7 +465,7 @@
:role "user"
:profile-id "ignored-user-model"
:total-tokens 3
- :data [{:role "user" :content "show me orders"}]})
+ :data [{:type "text" :text "show me orders"}]})
;; successful create_sql_query — should appear in :queries
(insert-message! {:conversation-id conversation-id
:created-at jan-2
@@ -471,32 +473,28 @@
:profile-id "internal"
:total-tokens 20
:data [{:type "text" :text "Sure, here it is."}
- {:type "tool-input"
- :id "call-success"
- :function "create_sql_query"
- :arguments {:database_id (mt/id) :sql_query sql}}
- {:type "tool-output"
- :id "call-success"
- :result {:output "<result>...</result>"
- :structured-output {:query-id "qid-success"
- :query-content sql
- :query {:database (mt/id)
- :type :native
- :native {:query sql}}
- :database (mt/id)}}}]})
- ;; failed create_sql_query (only :output, no :structured-output) — should NOT appear in :queries
+ {:type "tool-create_sql_query"
+ :toolCallId "call-success"
+ :state "output-available"
+ :input {:database_id (mt/id) :sql_query sql}
+ :output {:output "<result>...</result>"
+ :structured_output {:query-id "qid-success"
+ :query-content sql
+ :query {:database (mt/id)
+ :type :native
+ :native {:query sql}}
+ :database (mt/id)}}}]})
+ ;; failed create_sql_query (only :output, no structured output) — should NOT appear in :queries
(insert-message! {:conversation-id conversation-id
:created-at jan-3
:role "assistant"
:profile-id "internal"
:total-tokens 10
- :data [{:type "tool-input"
- :id "call-failed"
- :function "create_sql_query"
- :arguments {:database_id (mt/id) :sql_query "SELEKT bad"}}
- {:type "tool-output"
- :id "call-failed"
- :result {:output "<result>SQL query construction failed.</result>"}}]})
+ :data [{:type "tool-create_sql_query"
+ :toolCallId "call-failed"
+ :state "output-available"
+ :input {:database_id (mt/id) :sql_query "SELEKT bad"}
+ :output {:output "<result>SQL query construction failed.</result>"}}]})
(let [response (mt/user-http-request :crowberto :get 200
(format "ee/metabot-analytics/conversations/%s" conversation-id))
queries (:queries response)]
@@ -514,16 +512,16 @@
(finally
(delete-conversations! [conversation-id])))))))
-(defn- search-input-block
+(defn- search-part
[call-id]
- {:type "tool-input" :id call-id :function "search" :arguments {:q "foo"}})
-
-(defn- search-output-block
- [call-id]
- {:type "tool-output" :id call-id :result {:output "<result>...</result>"}})
+ {:type "tool-search"
+ :toolCallId call-id
+ :state "output-available"
+ :input {:q "foo"}
+ :output {:output "<result>...</result>"}})
(defn- with-search-count-fixture!
- "Seed conversations with varying numbers of search tool-input blocks so we
+ "Seed conversations with varying numbers of search tool parts so we
can assert both list and detail `:search_count` behavior without perturbing
the existing list fixture."
[thunk]
@@ -563,24 +561,26 @@
:role "assistant"
:profile-id "internal"
:total-tokens 10
- :data [(search-input-block "call-a")
- (search-output-block "call-a")]})
+ :data [(search-part "call-a")]})
(insert-message! {:conversation-id convo-two
:created-at jan-2
:role "assistant"
:profile-id "internal"
:total-tokens 10
- :data [{:type "tool-input" :id "call-x" :function "analyze_chart" :arguments {}}
- (search-input-block "call-b")
- (search-output-block "call-b")]})
- ;; convo-errored: a single search whose tool-output is marked errored — should still count.
+ :data [{:type "tool-analyze_chart" :toolCallId "call-x"
+ :state "input-available" :input {}}
+ (search-part "call-b")]})
+ ;; convo-errored: a single search whose call errored — should still count.
(insert-message! {:conversation-id convo-errored
:created-at jan-3
:role "assistant"
:profile-id "internal"
:total-tokens 4
- :data [(search-input-block "call-err")
- {:type "tool-output" :id "call-err" :error "boom"}]})
+ :data [{:type "tool-search"
+ :toolCallId "call-err"
+ :state "output-error"
+ :input {:q "foo"}
+ :errorText "boom"}]})
(thunk {:test-user-id test-user-id
:convo-none convo-none
:convo-two convo-two
@@ -611,13 +611,13 @@
(is (= expected (:search_count response))
(format "expected search_count=%d for %s" expected convo))))))))
-(defn- query-tool-input-block
+(defn- query-tool-part
[call-id tool-name]
- {:type "tool-input" :id call-id :function tool-name :arguments {}})
-
-(defn- query-tool-output-block
- [call-id]
- {:type "tool-output" :id call-id :result {:output "ok"}})
+ {:type (str "tool-" tool-name)
+ :toolCallId call-id
+ :state "output-available"
+ :input {}
+ :output {:output "ok"}})
(defn- with-query-count-fixture!
"Seed conversations that exercise `:query_count` for every query-generation
@@ -654,8 +654,7 @@
:role "assistant"
:profile-id "internal"
:total-tokens 5
- :data [(search-input-block "call-s")
- (search-output-block "call-s")]})
+ :data [(search-part "call-s")]})
;; convo-mixed: one search + create_sql_query in msg 1, then one edit_sql_query
;; and one construct_notebook_query in msg 2 — three counted queries.
(insert-message! {:conversation-id convo-mixed
@@ -663,31 +662,24 @@
:role "assistant"
:profile-id "internal"
:total-tokens 10
- :data [(search-input-block "call-mixed-s")
- (search-output-block "call-mixed-s")
- (query-tool-input-block "call-a" "create_sql_query")
- (query-tool-output-block "call-a")]})
+ :data [(search-part "call-mixed-s")
+ (query-tool-part "call-a" "create_sql_query")]})
(insert-message! {:conversation-id convo-mixed
:created-at mar-2
:role "assistant"
:profile-id "internal"
:total-tokens 10
- :data [(query-tool-input-block "call-b" "edit_sql_query")
- (query-tool-output-block "call-b")
- (query-tool-input-block "call-c" "construct_notebook_query")
- (query-tool-output-block "call-c")]})
+ :data [(query-tool-part "call-b" "edit_sql_query")
+ (query-tool-part "call-c" "construct_notebook_query")]})
;; convo-edits: search + edit + replace
(insert-message! {:conversation-id convo-edits
:created-at mar-3
:role "assistant"
:profile-id "internal"
:total-tokens 4
- :data [(search-input-block "call-edits-s")
- (search-output-block "call-edits-s")
- (query-tool-input-block "call-e" "edit_sql_query")
- (query-tool-output-block "call-e")
- (query-tool-input-block "call-r" "replace_sql_query")
- (query-tool-output-block "call-r")]})
+ :data [(search-part "call-edits-s")
+ (query-tool-part "call-e" "edit_sql_query")
+ (query-tool-part "call-r" "replace_sql_query")]})
(thunk {:test-user-id test-user-id
:convo-none convo-none
:convo-mixed convo-mixed
diff --git a/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj
index 01c8786d2eb..041ebece27f 100644
--- a/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/metabot_analytics/queries_test.clj
@@ -15,6 +15,15 @@
(mt/with-dynamic-fn-redefs [analytics.queries/referenced-table-names (fn [_db _sql] tables)]
(thunk)))
+(defn- tool-part
+ "A stored v2 tool part in `output-available` state."
+ [tool-name call-id input output]
+ {:type (str "tool-" tool-name)
+ :toolCallId call-id
+ :state "output-available"
+ :input input
+ :output output})
+
;;; ------------------------- happy paths -------------------------
(deftest create-sql-query-row-test
@@ -22,20 +31,16 @@
(fn []
(let [rows (analytics.queries/messages->generated-queries
[{:id 7
- :data [{:type "tool-input"
- :id "call-1"
- :function "create_sql_query"
- :arguments {:database_id 1
- :sql_query "SELECT * FROM orders JOIN products ON ..."}}
- {:type "tool-output"
- :id "call-1"
- :result {:output "<result>...</result>"
- :structured-output {:query-id "qid-1"
- :query-content "SELECT * FROM orders JOIN products ON ..."
- :query {:database 1
- :type :native
- :native {:query "SELECT * FROM orders JOIN products ON ..."}}
- :database 1}}}]}])]
+ :data [(tool-part "create_sql_query" "call-1"
+ {:database_id 1
+ :sql_query "SELECT * FROM orders JOIN products ON ..."}
+ {:output "<result>...</result>"
+ :structured_output {:query-id "qid-1"
+ :query-content "SELECT * FROM orders JOIN products ON ..."
+ :query {:database 1
+ :type :native
+ :native {:query "SELECT * FROM orders JOIN products ON ..."}}
+ :database 1}})]}])]
(is (= 1 (count rows)))
(let [row (first rows)]
(is (= "create_sql_query" (:tool row)))
@@ -53,20 +58,16 @@
(fn []
(let [rows (analytics.queries/messages->generated-queries
[{:id 8
- :data [{:type "tool-input"
- :id "call-2"
- :function "edit_sql_query"
- :arguments {:query_id "qid-1"
- :checklist "..."
- :edits [{:old_string "1" :new_string "2"}]}}
- {:type "tool-output"
- :id "call-2"
- :result {:structured-output {:query-id "qid-1"
- :query-content "SELECT 2 FROM orders"
- :query {:database 1
- :type :native
- :native {:query "SELECT 2 FROM orders"}}
- :database 1}}}]}])
+ :data [(tool-part "edit_sql_query" "call-2"
+ {:query_id "qid-1"
+ :checklist "..."
+ :edits [{:old_string "1" :new_string "2"}]}
+ {:structured_output {:query-id "qid-1"
+ :query-content "SELECT 2 FROM orders"
+ :query {:database 1
+ :type :native
+ :native {:query "SELECT 2 FROM orders"}}
+ :database 1}})]}])
row (first rows)]
(is (= "edit_sql_query" (:tool row)))
(is (= "SELECT 2 FROM orders" (:sql row)))
@@ -77,20 +78,16 @@
(fn []
(let [rows (analytics.queries/messages->generated-queries
[{:id 9
- :data [{:type "tool-input"
- :id "call-3"
- :function "replace_sql_query"
- :arguments {:query_id "qid-1"
- :checklist "..."
- :new_query "SELECT 3"}}
- {:type "tool-output"
- :id "call-3"
- :result {:structured-output {:query-id "qid-1"
- :query-content "SELECT 3"
- :query {:database 1
- :type :native
- :native {:query "SELECT 3"}}
- :database 1}}}]}])
+ :data [(tool-part "replace_sql_query" "call-3"
+ {:query_id "qid-1"
+ :checklist "..."
+ :new_query "SELECT 3"}
+ {:structured_output {:query-id "qid-1"
+ :query-content "SELECT 3"
+ :query {:database 1
+ :type :native
+ :native {:query "SELECT 3"}}
+ :database 1}})]}])
row (first rows)]
(is (= "replace_sql_query" (:tool row)))
(is (= "SELECT 3" (:sql row)))))))
@@ -100,19 +97,15 @@
(fn []
(let [legacy-query {:database 7 :type :query :query {:source-table 5}}
rows (analytics.queries/messages->generated-queries
- ;; Notebook tool persists structured-output WITHOUT a top-level
+ ;; Notebook tool persists structured output WITHOUT a top-level
;; :database key — the db id lives inside :query.
[{:id 10
- :data [{:type "tool-input"
- :id "call-4"
- :function "construct_notebook_query"
- :arguments {:reasoning "show me orders"
- :query {:query_type "raw"
- :source {:table_id 5}}}}
- {:type "tool-output"
- :id "call-4"
- :result {:structured-output {:query-id "qid-2"
- :query legacy-query}}}]}])
+ :data [(tool-part "construct_notebook_query" "call-4"
+ {:reasoning "show me orders"
+ :query {:query_type "raw"
+ :source {:table_id 5}}}
+ {:structured_output {:query-id "qid-2"
+ :query legacy-query}})]}])
row (first rows)]
(is (= "construct_notebook_query" (:tool row)))
(is (= "notebook" (:query_type row)))
@@ -133,60 +126,64 @@
:source-table 5}]}
rows (analytics.queries/messages->generated-queries
[{:id 11
- :data [{:type "tool-input"
- :id "call-5"
- :function "construct_notebook_query"
- :arguments {:reasoning "test"
- :query {:query_type "raw" :source {:table_id 5}}}}
- {:type "tool-output"
- :id "call-5"
- :result {:structured-output {:query-id "qid-3"
- :query mbql5}}}]}])
+ :data [(tool-part "construct_notebook_query" "call-5"
+ {:reasoning "test"
+ :query {:query_type "raw" :source {:table_id 5}}}
+ {:structured_output {:query-id "qid-3"
+ :query mbql5}})]}])
row (first rows)]
(is (= "notebook" (:query_type row)))
(is (= mbql5 (:mbql row)))
(is (= 1 (:database_id row))))))))
+(deftest migrated-rows-use-kebab-structured-output-alias-test
+ (testing "rows migrated from v1 carry :structured-output (kebab) inside :output; the extractor reads both"
+ (with-stubbed-tables! []
+ (fn []
+ (let [rows (analytics.queries/messages->generated-queries
+ [{:id 17
+ :data [(tool-part "create_sql_query" "call-kebab"
+ {:database_id 1 :sql_query "SELECT 1"}
+ {:output "<result>...</result>"
+ :structured-output {:query-id "qid-k"
+ :query-content "SELECT 1"
+ :database 1}})]}])]
+ (is (= ["qid-k"] (map :query_id rows))))))))
+
;;; ------------------------- filtered-out cases -------------------------
-(deftest tool-input-without-output-is-filtered-test
+(deftest unresolved-tool-part-is-filtered-test
(with-stubbed-tables! []
(fn []
(let [rows (analytics.queries/messages->generated-queries
[{:id 12
- :data [{:type "tool-input"
- :id "orphan"
- :function "create_sql_query"
- :arguments {:database_id 1 :sql_query "SELECT 1"}}]}])]
+ :data [{:type "tool-create_sql_query"
+ :toolCallId "orphan"
+ :state "input-available"
+ :input {:database_id 1 :sql_query "SELECT 1"}}]}])]
(is (= [] rows))))))
-(deftest tool-output-with-error-is-filtered-test
+(deftest errored-tool-part-is-filtered-test
(with-stubbed-tables! []
(fn []
(let [rows (analytics.queries/messages->generated-queries
[{:id 13
- :data [{:type "tool-input"
- :id "call-err"
- :function "create_sql_query"
- :arguments {:database_id 1 :sql_query "SELECT 1"}}
- {:type "tool-output"
- :id "call-err"
- :error "exploded"}]}])]
+ :data [{:type "tool-create_sql_query"
+ :toolCallId "call-err"
+ :state "output-error"
+ :input {:database_id 1 :sql_query "SELECT 1"}
+ :errorText "exploded"}]}])]
(is (= [] rows))))))
-(deftest tool-output-without-structured-is-filtered-test
- (testing "tools that hit a validation error return :output but no :structured-output"
+(deftest tool-part-without-structured-is-filtered-test
+ (testing "tools that hit a validation error return :output but no structured output"
(with-stubbed-tables! []
(fn []
(let [rows (analytics.queries/messages->generated-queries
[{:id 14
- :data [{:type "tool-input"
- :id "call-bad"
- :function "create_sql_query"
- :arguments {:database_id 1 :sql_query "SELEKT bad"}}
- {:type "tool-output"
- :id "call-bad"
- :result {:output "<result>SQL query construction failed.</result>"}}]}])]
+ :data [(tool-part "create_sql_query" "call-bad"
+ {:database_id 1 :sql_query "SELEKT bad"}
+ {:output "<result>SQL query construction failed.</result>"})]}])]
(is (= [] rows)))))))
(deftest non-query-tool-calls-are-skipped-test
@@ -194,110 +191,45 @@
(fn []
(let [rows (analytics.queries/messages->generated-queries
[{:id 15
- :data [{:type "tool-input"
- :id "call-search"
- :function "search"
- :arguments {:q "foo"}}
- {:type "tool-output"
- :id "call-search"
- :result {:rows [1 2 3]}}
- {:type "tool-input"
- :id "call-sql"
- :function "create_sql_query"
- :arguments {:database_id 1 :sql_query "SELECT 1 FROM orders"}}
- {:type "tool-output"
- :id "call-sql"
- :result {:structured-output {:query-id "qid-x"
- :query-content "SELECT 1 FROM orders"
- :query {:database 1 :type :native :native {:query "SELECT 1 FROM orders"}}
- :database 1}}}]}])]
+ :data [(tool-part "search" "call-search" {:q "foo"} {:output "rows"})
+ (tool-part "create_sql_query" "call-sql"
+ {:database_id 1 :sql_query "SELECT 1 FROM orders"}
+ {:structured_output {:query-id "qid-x"
+ :query-content "SELECT 1 FROM orders"
+ :query {:database 1 :type :native :native {:query "SELECT 1 FROM orders"}}
+ :database 1}})]}])]
(is (= 1 (count rows)))
(is (= "create_sql_query" (-> rows first :tool)))))))
-(deftest slackbot-shape-blocks-are-filtered-test
- (testing "legacy slackbot rows wrote :_type 'TOOL_CALL' blocks with no recoverable structured-output;
- the :type filter excludes them and the extractor yields nothing for those historical rows"
+(deftest non-tool-parts-are-skipped-test
+ (testing "text and data parts pass through the extractor without producing rows"
(with-stubbed-tables! []
(fn []
- (let [rows (analytics.queries/messages->generated-queries
- [{:id 16
- :data [{:role "assistant"
- :_type "TOOL_CALL"
- :tool_calls [{:id "slack-call-1"
- :name "create_sql_query"
- :arguments {:database_id 1 :sql_query "SELECT 1"}}]}
- {:role "tool"
- :_type "TOOL_RESULT"
- :tool_call_id "slack-call-1"
- :content "<result>...</result>"}]}])]
- (is (= [] rows)))))))
-
-(deftest slackbot-native-shape-blocks-are-extracted-test
- (testing "going-forward slackbot rows are persisted via finalize-assistant-turn!, so they carry
- the same :type 'tool-input'/'tool-output' block shape as in-app rows and the
- analytics extractor handles them identically"
- (with-stubbed-tables! ["orders"]
- (fn []
- (let [data [{:type "tool-input"
- :id "call-search"
- :function "search"
- :arguments {:query "orders"}}
- {:type "tool-output"
- :id "call-search"
- :result {:output "<result>orders</result>"}}
- {:type "tool-input"
- :id "call-sql"
- :function "create_sql_query"
- :arguments {:database_id 1 :sql_query "SELECT 1 FROM orders"}}
- {:type "tool-output"
- :id "call-sql"
- :result {:output "<result>sql</result>"
- :structured-output {:query-id "qid-slack"
- :query-content "SELECT 1 FROM orders"
- :query {:database 1
- :type :native
- :native {:query "SELECT 1 FROM orders"}}
- :database 1}}}]
- message {:id 200 :data data}]
- (testing "count-tool-invocations reaches tool names directly by :function"
- (is (= 1 (analytics.queries/count-tool-invocations [message] "search")))
- (is (= 1 (analytics.queries/count-tool-invocations
- [message] metabot.tools/query-generation-tool-names))))
- (testing "messages->generated-queries surfaces the SQL tool call"
- (let [rows (analytics.queries/messages->generated-queries [message])]
- (is (= 1 (count rows)))
- (is (= "create_sql_query" (-> rows first :tool)))
- (is (= "qid-slack" (-> rows first :query_id)))
- (is (= "SELECT 1 FROM orders" (-> rows first :sql))))))))))
+ (is (= [] (analytics.queries/messages->generated-queries
+ [{:id 16
+ :data [{:type "text" :text "hi"}
+ {:type "data-navigate_to" :data "/question/1"}]}])))))))
;;; ------------------------- aggregation -------------------------
(deftest flattens-across-messages-preserving-order-test
(with-stubbed-tables! ["t"]
(fn []
- (let [base-output (fn [qid sql]
- {:type "tool-output"
- :id (str "call-" qid)
- :result {:structured-output {:query-id qid
- :query-content sql
- :query {:database 1
- :type :native
- :native {:query sql}}
- :database 1}}})
- base-input (fn [qid sql]
- {:type "tool-input"
- :id (str "call-" qid)
- :function "create_sql_query"
- :arguments {:database_id 1 :sql_query sql}})
- rows (analytics.queries/messages->generated-queries
- [{:id 100
- :data [(base-input "a" "SELECT 1")
- (base-output "a" "SELECT 1")]}
- {:id 101
- :data [(base-input "b" "SELECT 2")
- (base-output "b" "SELECT 2")
- (base-input "c" "SELECT 3")
- (base-output "c" "SELECT 3")]}])]
+ (let [sql-part (fn [qid sql]
+ (tool-part "create_sql_query" (str "call-" qid)
+ {:database_id 1 :sql_query sql}
+ {:structured_output {:query-id qid
+ :query-content sql
+ :query {:database 1
+ :type :native
+ :native {:query sql}}
+ :database 1}}))
+ rows (analytics.queries/messages->generated-queries
+ [{:id 100
+ :data [(sql-part "a" "SELECT 1")]}
+ {:id 101
+ :data [(sql-part "b" "SELECT 2")
+ (sql-part "c" "SELECT 3")]}])]
(is (= ["a" "b" "c"] (map :query_id rows)))
(is (= [100 101 101] (map :message_id rows)))))))
@@ -309,33 +241,29 @@
;;; ------------------------- count-tool-invocations -------------------------
(deftest count-tool-invocations-test
- (testing "sums matching tool-input blocks across messages, including errored calls"
- ;; Mix within-message and across-messages, plus an errored and an unrelated tool.
+ (testing "sums matching tool parts across messages, regardless of how the call resolved"
(is (= 4 (analytics.queries/count-tool-invocations
- [{:id 1 :data [{:type "tool-input" :function "search" :id "a"}
- {:type "tool-output" :id "a" :error "boom"} ; errored still counts
- {:type "tool-input" :function "search" :id "b"}]}
- {:id 2 :data [{:type "tool-input" :function "create_sql_query" :id "c"} ; ignored tool
- {:type "tool-input" :function "search" :id "d"}
- {:type "tool-input" :function "search" :id "e"}]}]
+ [{:id 1 :data [{:type "tool-search" :toolCallId "a" :state "output-error"
+ :input {} :errorText "boom"} ; errored still counts
+ {:type "tool-search" :toolCallId "b" :state "input-available"
+ :input {}}]}
+ {:id 2 :data [(tool-part "create_sql_query" "c" {} {}) ; ignored tool
+ (tool-part "search" "d" {} {})
+ (tool-part "search" "e" {} {})]}]
"search"))))
- (testing "returns 0 for inputs that contain no matching tool-input blocks"
+ (testing "returns 0 for inputs that contain no matching tool parts"
(are [messages] (zero? (analytics.queries/count-tool-invocations messages "search"))
[]
[{:id 1 :data nil}]
[{:id 1 :data []}]
[{:id 1 :data [{:type "text" :text "hi"}]}]
- [{:id 1 :data [{:type "tool-input" :function "create_sql_query" :id "x"}]}]
- ;; Slackbot-shaped blocks lack `:type` and are intentionally skipped.
- [{:id 1 :data [{:role "assistant"
- :_type "TOOL_CALL"
- :tool_calls [{:id "slack-1" :name "search"}]}]}]))
- (testing "accepts a set of tool names; a block counts if its :function is in the set"
+ [{:id 1 :data [(tool-part "create_sql_query" "x" {} {})]}]))
+ (testing "accepts a set of tool names; a part counts if its name is in the set"
(is (= 5 (analytics.queries/count-tool-invocations
- [{:id 1 :data [{:type "tool-input" :function "create_sql_query" :id "a"}
- {:type "tool-input" :function "edit_sql_query" :id "b"}
- {:type "tool-input" :function "construct_notebook_query" :id "c"}]}
- {:id 2 :data [{:type "tool-input" :function "replace_sql_query" :id "d"}
- {:type "tool-input" :function "create_sql_query" :id "e"}
- {:type "tool-input" :function "search" :id "f"}]}] ; non-query tool
+ [{:id 1 :data [(tool-part "create_sql_query" "a" {} {})
+ (tool-part "edit_sql_query" "b" {} {})
+ (tool-part "construct_notebook_query" "c" {} {})]}
+ {:id 2 :data [(tool-part "replace_sql_query" "d" {} {})
+ (tool-part "create_sql_query" "e" {} {})
+ (tool-part "search" "f" {} {})]}] ; non-query tool
metabot.tools/query-generation-tool-names)))))
diff --git a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx
index 418f1c2b8ce..92482bc7c57 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.tsx
@@ -10,7 +10,7 @@ import { Stack } from "metabase/ui";
import S from "./MetabotQuestion.module.css";
const isQuestionNavigationMessage = (message: MetabotChatMessage) =>
- message.type === "data_part" && message.part.type === "navigate_to";
+ message.type === "data_part" && message.part.type === "data-navigate_to";
export function MetabotChatHistory() {
const metabot = useMetabotAgent();
diff --git a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx
index a55c2d47b56..f7750eb05a2 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotChatHistory.unit.spec.tsx
@@ -27,7 +27,7 @@ describe("MetabotChatHistory", () => {
id: "1",
role: "agent",
type: "data_part",
- part: { type: "navigate_to", version: 1, value: "/question#abc" },
+ part: { type: "data-navigate_to", data: "/question#abc" },
},
]),
});
diff --git a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotQuestion.stories.tsx b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotQuestion.stories.tsx
index 8184d938cb6..70fdeb83493 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotQuestion.stories.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/metabot/MetabotQuestion.stories.tsx
@@ -59,9 +59,15 @@ export const RedirectReaction = {
msw: {
handlers: [
mockStreamResponse([
- `0:"Here is the [question link](${MOCK_AD_HOC_QUESTION_ID})"`,
- `2:{"type":"navigate_to","version":1,"value":"${MOCK_AD_HOC_QUESTION_ID}"}
-`,
+ { type: "text-start", id: "t1" },
+ {
+ type: "text-delta",
+ id: "t1",
+ delta: `Here is the [question link](${MOCK_AD_HOC_QUESTION_ID})`,
+ },
+ { type: "text-end", id: "t1" },
+ { type: "data-navigate_to", data: MOCK_AD_HOC_QUESTION_ID },
+ { type: "finish", finishReason: "stop" },
]),
],
},
diff --git a/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.tsx b/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.tsx
index c4c51c7bbbd..566e5f07228 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.tsx
@@ -160,7 +160,7 @@ function getCachedChartComponent(
type PublicChatMessage =
| Extract<MetabotChatMessage, { type: "text" }>
| (Extract<MetabotChatMessage, { type: "data_part" }> & {
- part: { type: "navigate_to" };
+ part: { type: "data-navigate_to" };
});
const isPublicMessage = (
@@ -169,7 +169,7 @@ const isPublicMessage = (
): message is PublicChatMessage =>
message.type === "text" ||
(message.type === "data_part" &&
- message.part.type === "navigate_to" &&
+ message.part.type === "data-navigate_to" &&
finalNavigateToIds.has(message.id));
const mapMessage = (
@@ -189,9 +189,9 @@ const mapMessage = (
({ id, role: "agent", type: "text", message }) as const,
)
.with(
- { role: "agent", type: "data_part", part: { type: "navigate_to" } },
+ { role: "agent", type: "data_part", part: { type: "data-navigate_to" } },
({ id, part }) => {
- const questionPath = part.value;
+ const questionPath = part.data;
const Chart = authConfig
? getCachedChartComponent(questionPath, cache, authConfig)
: FallbackChartComponent;
diff --git a/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.unit.spec.tsx b/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.unit.spec.tsx
index db0d0841bfc..32c87943bc4 100644
--- a/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.unit.spec.tsx
+++ b/enterprise/frontend/src/embedding-sdk-ee/metabot/hooks/use-metabot.unit.spec.tsx
@@ -251,9 +251,8 @@ describe("useMetabot", () => {
agentId: "omnibot",
type: "data_part",
part: {
- type: "navigate_to",
- version: 1,
- value: "/question#base64",
+ type: "data-navigate_to",
+ data: "/question#base64",
},
} as any),
);
@@ -352,14 +351,14 @@ describe("useMetabot", () => {
metabotActions.addAgentMessage({
agentId: "omnibot",
type: "data_part",
- part: { type: "navigate_to", version: 1, value: "/question#1a" },
+ part: { type: "data-navigate_to", data: "/question#1a" },
} as any),
);
store.dispatch(
metabotActions.addAgentMessage({
agentId: "omnibot",
type: "data_part",
- part: { type: "navigate_to", version: 1, value: "/question#1b" },
+ part: { type: "data-navigate_to", data: "/question#1b" },
} as any),
);
store.dispatch(
@@ -374,7 +373,7 @@ describe("useMetabot", () => {
metabotActions.addAgentMessage({
agentId: "omnibot",
type: "data_part",
- part: { type: "navigate_to", version: 1, value: "/question#2" },
+ part: { type: "data-navigate_to", data: "/question#2" },
} as any),
);
});
@@ -407,7 +406,7 @@ describe("useMetabot", () => {
it("forwards the message to the underlying agent request", async () => {
const agentSpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
setup({ ui: <TestSubmit /> });
@@ -418,7 +417,7 @@ describe("useMetabot", () => {
});
it("does not open the metabot sidebar (preventOpenSidebar)", async () => {
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
// default setup() forces omnibot.visible = true; override so we can
// observe whether submitMessage would flip it back on.
const { store } = setup({
@@ -447,7 +446,7 @@ describe("useMetabot", () => {
});
it("resolves to undefined even though agent.submitInput returns an action", async () => {
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
const onResolved = jest.fn();
setup({ ui: <TestSubmit onResolved={onResolved} /> });
@@ -478,9 +477,8 @@ describe("useMetabot", () => {
agentId: "omnibot",
type: "data_part",
part: {
- type: "navigate_to",
- version: 1,
- value: "/question#base64",
+ type: "data-navigate_to",
+ data: "/question#base64",
},
} as any),
);
@@ -498,9 +496,8 @@ describe("useMetabot", () => {
agentId: "omnibot",
type: "data_part",
part: {
- type: "navigate_to",
- version: 1,
- value: "/question#base64",
+ type: "data-navigate_to",
+ data: "/question#base64",
},
} as any),
);
@@ -540,7 +537,7 @@ describe("useMetabot", () => {
metabotActions.addAgentMessage({
agentId: "omnibot",
type: "data_part",
- part: { type: "navigate_to", version: 1, value: "/question#abc" },
+ part: { type: "data-navigate_to", data: "/question#abc" },
} as any),
);
});
@@ -565,7 +562,7 @@ describe("useMetabot", () => {
metabotActions.addAgentMessage({
agentId: "omnibot",
type: "data_part",
- part: { type: "navigate_to", version: 1, value: "/question#xyz" },
+ part: { type: "data-navigate_to", data: "/question#xyz" },
} as any),
);
});
diff --git a/enterprise/frontend/src/embedding-sdk-package/components/public/MetabotQuestion/MetabotQuestion.stories.tsx b/enterprise/frontend/src/embedding-sdk-package/components/public/MetabotQuestion/MetabotQuestion.stories.tsx
index 1a842865936..3a2fd770bba 100644
--- a/enterprise/frontend/src/embedding-sdk-package/components/public/MetabotQuestion/MetabotQuestion.stories.tsx
+++ b/enterprise/frontend/src/embedding-sdk-package/components/public/MetabotQuestion/MetabotQuestion.stories.tsx
@@ -18,9 +18,15 @@ export default {
msw: {
handlers: [
mockStreamResponse([
- `0:"Here is the [question link](${MOCK_AD_HOC_QUESTION_ID})"`,
- `2:{"type":"navigate_to","version":1,"value":"${MOCK_AD_HOC_QUESTION_ID}"}
-`,
+ { type: "text-start", id: "t1" },
+ {
+ type: "text-delta",
+ id: "t1",
+ delta: `Here is the [question link](${MOCK_AD_HOC_QUESTION_ID})`,
+ },
+ { type: "text-end", id: "t1" },
+ { type: "data-navigate_to", data: MOCK_AD_HOC_QUESTION_ID },
+ { type: "finish", finishReason: "stop" },
]),
],
},
diff --git a/frontend/src/embedding-sdk-shared/test/mocks/mock-metabot-response.ts b/frontend/src/embedding-sdk-shared/test/mocks/mock-metabot-response.ts
index d8ff6cc8c8e..784fc4bc96c 100644
--- a/frontend/src/embedding-sdk-shared/test/mocks/mock-metabot-response.ts
+++ b/frontend/src/embedding-sdk-shared/test/mocks/mock-metabot-response.ts
@@ -3,17 +3,18 @@ import { http } from "msw";
export const MOCK_AD_HOC_QUESTION_ID =
"/question#eyJkYXRhc2V0X3F1ZXJ5IjogeyJkYXRhYmFzZSI6IDEsICJ0eXBlIjogInF1ZXJ5IiwgInF1ZXJ5IjogeyJzb3VyY2UtdGFibGUiOiAiY2FyZF9fMSJ9fSwgImRpc3BsYXkiOiAidGFibGUiLCAiZGlzcGxheUlzTG9ja2VkIjogdHJ1ZSwgInZpc3VhbGl6YXRpb25fc2V0dGluZ3MiOiB7fX0=";
-export const mockStreamResponse = (chunks: string[]) => {
+export const mockStreamResponse = (events: object[]) => {
return http.post("*/api/metabot/agent-streaming", () => {
const encoder = new TextEncoder();
+ const lines = [...events.map((event) => JSON.stringify(event)), "[DONE]"];
const stream = new ReadableStream({
start(controller) {
let i = 0;
const pushNext = () => {
- if (i < chunks.length) {
- controller.enqueue(encoder.encode(`${chunks[i]}\n`));
+ if (i < lines.length) {
+ controller.enqueue(encoder.encode(`data: ${lines[i]}\n\n`));
i++;
setTimeout(pushNext, 100);
} else {
diff --git a/frontend/src/metabase/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snap b/frontend/src/metabase/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snap
index cc4fb489316..77b00bcfc8a 100644
--- a/frontend/src/metabase/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snap
+++ b/frontend/src/metabase/api/ai-streaming/__snapshots__/process-stream.unit.spec.ts.snap
@@ -5,13 +5,13 @@ exports[`processChatResponse should be able to process a valid stream 1`] = `
"aborted": false,
"data": [
{
- "type": "state",
- "value": {
+ "data": {
"queries": {},
},
- "version": 1,
+ "type": "data-state",
},
],
+ "finishReason": "stop",
"history": [
{
"content": "You, but don't tell anyone.",
@@ -21,7 +21,7 @@ exports[`processChatResponse should be able to process a valid stream 1`] = `
"role": "assistant",
"tool_calls": [
{
- "arguments": "",
+ "arguments": "{}",
"id": "x",
"name": "x",
},
@@ -33,56 +33,15 @@ exports[`processChatResponse should be able to process a valid stream 1`] = `
"tool_call_id": "x",
},
],
- "parts": [
- {
- "code": "0",
- "name": "text",
- "value": "You, but don't tell anyone.",
- },
- {
- "code": "2",
- "name": "data",
- "value": {
- "type": "state",
- "value": {
- "queries": {},
- },
- "version": 1,
- },
- },
- {
- "code": "9",
- "name": "tool_call",
- "value": {
- "args": "",
- "toolCallId": "x",
- "toolName": "x",
- },
- },
- {
- "code": "a",
- "name": "tool_result",
- "value": {
- "result": "",
- "toolCallId": "x",
- },
- },
- {
- "code": "d",
- "name": "finish_message",
- "value": {
- "finishReason": "stop",
- "usage": {
- "completionTokens": 8,
- "promptTokens": 4916,
- },
- },
+ "messageMetadata": {
+ "usage": {
+ "inputTokens": 4916,
+ "outputTokens": 8,
+ "totalTokens": 4924,
},
- ],
- "text": "You, but don't tell anyone.",
+ },
"toolCalls": [
{
- "args": "",
"state": "result",
"toolCallId": "x",
"toolName": "x",
@@ -94,14 +53,13 @@ exports[`processChatResponse should be able to process a valid stream 1`] = `
exports[`processChatResponse should resolve with partial response for aborted requests 1`] = `
{
- "aborted": false,
+ "aborted": true,
"data": [
{
- "type": "state",
- "value": {
+ "data": {
"testing": 123,
},
- "version": 1,
+ "type": "data-state",
},
],
"history": [
@@ -110,25 +68,6 @@ exports[`processChatResponse should resolve with partial response for aborted re
"role": "assistant",
},
],
- "parts": [
- {
- "code": "0",
- "name": "text",
- "value": "Partial response",
- },
- {
- "code": "2",
- "name": "data",
- "value": {
- "type": "state",
- "value": {
- "testing": 123,
- },
- "version": 1,
- },
- },
- ],
- "text": "Partial response",
"toolCalls": [],
}
`;
diff --git a/frontend/src/metabase/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snap b/frontend/src/metabase/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snap
index 46e8b3d9cef..376107f40be 100644
--- a/frontend/src/metabase/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snap
+++ b/frontend/src/metabase/api/ai-streaming/__snapshots__/requests.unit.spec.ts.snap
@@ -5,11 +5,10 @@ exports[`ai requests aiStreamingQuery should return full result of a successful
"aborted": false,
"data": [
{
- "type": "state",
- "value": {
+ "data": {
"queries": {},
},
- "version": 1,
+ "type": "data-state",
},
],
"history": [
@@ -18,36 +17,6 @@ exports[`ai requests aiStreamingQuery should return full result of a successful
"role": "assistant",
},
],
- "parts": [
- {
- "code": "0",
- "name": "text",
- "value": "You, but don't tell anyone.",
- },
- {
- "code": "2",
- "name": "data",
- "value": {
- "type": "state",
- "value": {
- "queries": {},
- },
- "version": 1,
- },
- },
- {
- "code": "d",
- "name": "finish_message",
- "value": {
- "finishReason": "stop",
- "usage": {
- "completionTokens": 8,
- "promptTokens": 4916,
- },
- },
- },
- ],
- "text": "You, but don't tell anyone.",
"toolCalls": [],
}
`;
diff --git a/frontend/src/metabase/api/ai-streaming/process-stream.ts b/frontend/src/metabase/api/ai-streaming/process-stream.ts
index 99fa3c6024c..5c88165ef30 100644
--- a/frontend/src/metabase/api/ai-streaming/process-stream.ts
+++ b/frontend/src/metabase/api/ai-streaming/process-stream.ts
@@ -1,213 +1,72 @@
-import { isMatching, match } from "ts-pattern";
-import _ from "underscore";
+import { P, isMatching, match } from "ts-pattern";
-import type { JSONValue, MetabotHistory } from "metabase-types/api";
+import type { MetabotHistory } from "metabase-types/api";
import {
type KnownDataPart,
- dataPartSchema,
- finishPartSchema,
- knownDataPartTypes,
- startPartSchema,
- toolCallPartSchema,
- toolResultPartSchema,
+ dataEventSchema,
+ isKnownDataPart,
+ toolInputAvailableSchema,
+ toolOutputAvailableSchema,
+ toolOutputErrorSchema,
} from "./schemas";
-
-const StreamingPartTypeRegistry = {
- TEXT: "0",
- DATA: "2",
- ERROR: "3",
- FINISH_MESSAGE: "d",
- START_MESSAGE: "f",
- TOOL_CALL: "9",
- TOOL_RESULT: "a",
-} as const;
-
-const StreamingPartTypes = Object.values(StreamingPartTypeRegistry);
-
-type StreamingPartType = (typeof StreamingPartTypes)[number];
-
-/**
- * Concatenates all the chunks into a single Uint8Array
- */
-function concatChunks(chunks: Uint8Array[]): Uint8Array {
- const totalLength = chunks.reduce((sum, chunk) => sum + chunk.length, 0);
- const concatenatedChunks = new Uint8Array(totalLength);
-
- let offset = 0;
- for (const chunk of chunks) {
- concatenatedChunks.set(chunk, offset);
- offset += chunk.length;
- }
-
- return concatenatedChunks;
-}
-
-function parseDataStreamPart(line: string) {
- const firstSeparatorIndex = line.indexOf(":");
- if (firstSeparatorIndex === -1) {
- throw new Error("Failed to parse stream string. No separator found.");
- }
-
- const prefix = line.slice(0, firstSeparatorIndex);
- if (!StreamingPartTypes.includes(prefix as StreamingPartType)) {
- console.warn(`Received invalid message code: ${prefix}`);
- return;
- }
-
- const code = prefix as StreamingPartType;
- const textValue = line.slice(firstSeparatorIndex + 1);
- const jsonValue: JSONValue = JSON.parse(textValue);
-
- return match(code)
- .with(StreamingPartTypeRegistry.TEXT, (code) => ({
- code,
- name: "text" as const,
- value: jsonValue,
- }))
- .with(StreamingPartTypeRegistry.DATA, (code) => ({
- code,
- name: "data" as const,
- value: dataPartSchema.validateSync(jsonValue, { strict: true }),
- }))
- .with(StreamingPartTypeRegistry.TOOL_CALL, (code) => ({
- code,
- name: "tool_call" as const,
- value: toolCallPartSchema.validateSync(jsonValue, { strict: true }),
- }))
- .with(StreamingPartTypeRegistry.TOOL_RESULT, (code) => ({
- code,
- name: "tool_result" as const,
- value: toolResultPartSchema.validateSync(jsonValue, { strict: true }),
- }))
- .with(StreamingPartTypeRegistry.ERROR, (code) => ({
- code,
- name: "error" as const,
- value: jsonValue,
- }))
- .with(StreamingPartTypeRegistry.FINISH_MESSAGE, (code) => ({
- code,
- name: "finish_message" as const,
- value: finishPartSchema.validateSync(jsonValue, { strict: true }),
- }))
- .with(StreamingPartTypeRegistry.START_MESSAGE, (code) => ({
- code,
- name: "start_message" as const,
- value: startPartSchema.validateSync(jsonValue, { strict: true }),
- }))
- .exhaustive();
-}
-
-type ParsedStreamPart = Exclude<ReturnType<typeof parseDataStreamPart>, void>;
-type ParsedStreamPartName = ParsedStreamPart["name"];
-
-function isKnownDataPart(streamPart: ParsedStreamPart): streamPart is Omit<
- Extract<ParsedStreamPart, { name: "data" }>,
- "value"
-> & {
- value: KnownDataPart;
-} {
- return (
- streamPart.name === "data" &&
- knownDataPartTypes.includes(streamPart.value.type)
- );
-}
-
-type AccumulatedStreamParts = {
- toolCalls: (
- | { toolCallId: string; toolName: string; state: "call" }
- | { toolCallId: string; toolName: string; state: "result"; value: unknown }
- )[];
- text: null | string;
- data: unknown[];
- parts: ParsedStreamPart[];
- history: MetabotHistory;
-};
-
-function accumulateStreamParts(streamParts: ParsedStreamPart[]) {
- const acc: AccumulatedStreamParts = {
- toolCalls: [],
- data: [],
- text: null,
- parts: streamParts,
- history: [],
- };
-
- return streamParts.reduce((acc, streamPart, index) => {
- if (streamPart.name === "text") {
- const lastStreamPart = streamParts[index - 1];
- acc.text = `${acc.text ?? ""}${streamPart.value}`;
- if (lastStreamPart?.name === "text") {
- const historyEntry = acc.history.pop();
- acc.history.push({
- ...historyEntry,
- content: historyEntry.content + streamPart.value,
- });
- } else {
- acc.history.push({ role: "assistant", content: streamPart.value });
- }
- }
- if (streamPart.name === "data") {
- acc.data = acc.data.concat(streamPart.value);
- }
- if (streamPart.name === "tool_call") {
- acc.toolCalls.push({ ...streamPart.value, state: "call" });
- acc.history.push({
- role: "assistant",
- tool_calls: [
- {
- id: streamPart.value.toolCallId,
- name: streamPart.value.toolName,
- arguments: streamPart.value.args,
- },
- ],
- });
- }
- if (streamPart.name === "tool_result") {
- const toolCallId = streamPart.value.toolCallId;
- const index = acc.toolCalls.findIndex((v) => v.toolCallId === toolCallId);
-
- if (index === -1) {
- throw new Error(
- "Tool Results must be preceded by the tool call with the same toolCallId",
- );
- }
-
- acc.toolCalls[index] = {
- ...acc.toolCalls[index],
- state: "result",
- value: streamPart.value.result,
- };
- acc.history.push({
- role: "tool",
- content: streamPart.value.result,
- tool_call_id: streamPart.value.toolCallId,
- });
- }
-
- return acc;
- }, acc);
-}
-
-type StreamPartValue<name extends ParsedStreamPartName> = Extract<
- ParsedStreamPart,
- { name: name }
->["value"];
+import { parseSSEStream } from "./sse-stream";
+import type {
+ FinishReason,
+ MessageMetadata,
+ StartEvent,
+ ToolInputAvailableEvent,
+ ToolInputStartEvent,
+ ToolOutputAvailableEvent,
+ ToolOutputErrorEvent,
+} from "./sse-types";
+
+type ToolCall =
+ | { toolCallId: string; toolName: string; state: "call" }
+ | {
+ toolCallId: string;
+ toolName: string;
+ state: "result";
+ value: unknown;
+ error?: string;
+ };
+
+type DataPart = { type: string; data: unknown };
+
+export type StreamedError = { errorText: string };
export type AIStreamingConfig = {
- onStartMessagePart?: (part: StreamPartValue<"start_message">) => void;
- onTextPart?: (part: StreamPartValue<"text">) => void;
+ onStart?: (event: StartEvent) => void;
+ onTextPart?: (delta: string) => void;
// callback is only called if this version of the client is aware of the received data part type
onDataPart?: (part: KnownDataPart) => void;
- onToolCallPart?: (part: StreamPartValue<"tool_call">) => void;
- onToolResultPart?: (part: StreamPartValue<"tool_result">) => void;
- onError?: (error: StreamPartValue<"error">) => void;
+ onToolInputStart?: (event: ToolInputStartEvent) => void;
+ onToolInputAvailable?: (event: ToolInputAvailableEvent) => void;
+ onToolResultPart?: (event: ToolOutputAvailableEvent) => void;
+ onToolErrorPart?: (event: ToolOutputErrorEvent) => void;
+ onError?: (error: StreamedError) => void;
+ onMessageMetadata?: (metadata: MessageMetadata) => void;
};
-export interface ProcessedChatResponse extends AccumulatedStreamParts {
+export interface ProcessedChatResponse {
aborted: boolean;
+ toolCalls: ToolCall[];
+ history: MetabotHistory;
+ data: DataPart[];
+ messageMetadata?: MessageMetadata;
+ finishReason?: FinishReason;
}
+const requireToolCall = (toolCalls: ToolCall[], toolCallId: string) => {
+ const index = toolCalls.findIndex((tc) => tc.toolCallId === toolCallId);
+ if (index === -1) {
+ throw new Error(
+ "Tool Results must be preceded by the tool call with the same toolCallId",
+ );
+ }
+ return index;
+};
+
/**
* Processes a stream that follows our AI response protocol and notifies
* the appropriate handlers as text, data, tool call, etc. parts come in.
@@ -219,76 +78,127 @@ export async function processChatResponse(
stream: ReadableStream<Uint8Array>,
config: AIStreamingConfig,
): Promise<ProcessedChatResponse> {
- const parsedStreamParts: ParsedStreamPart[] = [];
- const reader = stream.getReader();
- const decoder = new TextDecoder();
- let chunks: Uint8Array[] = [];
-
- let aborted = false;
- while (!aborted) {
- try {
- const { value: chunk } = await reader.read();
-
- if (chunk) {
- chunks.push(chunk);
- if (chunk[chunk.length - 1] !== "\n".charCodeAt(0)) {
- // if the last character is not a newline, we have not read the whole JSON value
- continue;
- }
- }
-
- if (chunks.length === 0) {
- break; // we have reached the end of the stream
- }
-
- const concatenatedChunks = concatChunks(chunks);
- chunks = [];
-
- const streamParts = _.compact(
- decoder
- .decode(concatenatedChunks, { stream: true })
- .split("\n")
- .filter((line) => line !== "") // splitting leaves an empty string at the end
- .map(parseDataStreamPart),
- );
-
- for (const streamPart of streamParts) {
- parsedStreamParts.push(streamPart);
+ const result: ProcessedChatResponse = {
+ aborted: false,
+ toolCalls: [],
+ history: [],
+ data: [],
+ };
- if (streamPart.name === "start_message") {
- config.onStartMessagePart?.(streamPart.value);
- }
- if (streamPart.name === "text") {
- config.onTextPart?.(streamPart.value);
- }
- if (streamPart.name === "data") {
- if (isKnownDataPart(streamPart)) {
- config.onDataPart?.(streamPart.value);
+ try {
+ for await (const event of parseSSEStream(stream)) {
+ match(event)
+ .with({ type: "start" }, (e) => {
+ config.onStart?.(e);
+ })
+ .with({ type: "text-delta" }, (e) => {
+ config.onTextPart?.(e.delta);
+ const lastEntry = result.history.at(-1);
+ if (lastEntry?.role === "assistant" && "content" in lastEntry) {
+ lastEntry.content += e.delta;
} else {
- console.warn("Skipping unknown data part:", streamPart);
+ result.history.push({ role: "assistant", content: e.delta });
}
- }
- if (streamPart.name === "tool_call") {
- config.onToolCallPart?.(streamPart.value);
- }
- if (streamPart.name === "tool_result") {
- config.onToolResultPart?.(streamPart.value);
- }
- if (streamPart.name === "error") {
- config.onError?.(streamPart.value);
- }
- }
- } catch (err) {
- if (isMatching({ name: "AbortError" }, err)) {
- aborted = true;
- } else {
- throw err;
- }
+ })
+ .with({ type: "tool-input-start" }, (e) => {
+ config.onToolInputStart?.(e);
+ })
+ .with({ type: "tool-input-available" }, (e) => {
+ toolInputAvailableSchema.validateSync(e, { strict: true });
+ config.onToolInputAvailable?.(e);
+ result.toolCalls.push({
+ toolCallId: e.toolCallId,
+ toolName: e.toolName,
+ state: "call",
+ });
+ result.history.push({
+ role: "assistant",
+ tool_calls: [
+ {
+ id: e.toolCallId,
+ name: e.toolName,
+ arguments: JSON.stringify(e.input),
+ },
+ ],
+ });
+ })
+ .with({ type: "tool-output-available" }, (e) => {
+ toolOutputAvailableSchema.validateSync(e, { strict: true });
+ const index = requireToolCall(result.toolCalls, e.toolCallId);
+ config.onToolResultPart?.(e);
+ result.toolCalls[index] = {
+ ...result.toolCalls[index],
+ state: "result",
+ value: e.output,
+ };
+ result.history.push({
+ role: "tool",
+ content: e.output,
+ tool_call_id: e.toolCallId,
+ });
+ })
+ .with({ type: "tool-output-error" }, (e) => {
+ toolOutputErrorSchema.validateSync(e, { strict: true });
+ const index = requireToolCall(result.toolCalls, e.toolCallId);
+ config.onToolErrorPart?.(e);
+ result.toolCalls[index] = {
+ ...result.toolCalls[index],
+ state: "result",
+ value: undefined,
+ error: e.errorText,
+ };
+ result.history.push({
+ role: "tool",
+ content: e.errorText,
+ tool_call_id: e.toolCallId,
+ });
+ })
+ .with({ type: "error" }, (e) => {
+ config.onError?.({ errorText: e.errorText });
+ })
+ .with({ type: "message-metadata" }, (e) => {
+ result.messageMetadata = e.messageMetadata;
+ config.onMessageMetadata?.(e.messageMetadata);
+ })
+ .with({ type: "finish" }, (e) => {
+ if (e.finishReason) {
+ result.finishReason = e.finishReason;
+ }
+ // finish chunk's usage supersedes any prior mid-stream snapshot
+ if (e.messageMetadata) {
+ result.messageMetadata = e.messageMetadata;
+ config.onMessageMetadata?.(e.messageMetadata);
+ }
+ })
+ .with({ type: P.string.startsWith("data-") }, (e) => {
+ dataEventSchema.validateSync(e, { strict: true });
+ const dataPart: DataPart = { type: e.type, data: e.data };
+ result.data.push(dataPart);
+
+ if (isKnownDataPart(dataPart)) {
+ config.onDataPart?.(dataPart);
+ } else {
+ console.warn("Skipping unknown data part:", dataPart);
+ }
+ })
+ .otherwise(() => {
+ // NOTE: allowed events not yet handled
+ // lifecycle: start-step, finish-step, abort
+ // text: text-start, text-end
+ // tool: tool-input-delta, tool-input-error, tool-approval-request,
+ // tool-output-denied
+ // reasoning: reasoning-start, reasoning-delta, reasoning-end
+ // sources: source-url, source-document
+ // files: file
+ });
+ }
+ } catch (err) {
+ if (isMatching({ name: "AbortError" }, err)) {
+ result.aborted = true;
+ } else {
+ throw err;
}
}
- return {
- ...accumulateStreamParts(parsedStreamParts),
- aborted,
- };
+ return result;
}
diff --git a/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts b/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts
index a2cddf61317..da33e96f222 100644
--- a/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts
+++ b/frontend/src/metabase/api/ai-streaming/process-stream.unit.spec.ts
@@ -1,12 +1,18 @@
import { processChatResponse } from "./process-stream";
-import { createMockReadableStream } from "./test-utils";
+import { parseSSEStream } from "./sse-stream";
+import type { SSEEvent } from "./sse-types";
+import { createMockReadableStream, createMockSSEStream } from "./test-utils";
const getMockedCallbacks = () => ({
+ onStart: jest.fn(),
onTextPart: jest.fn(),
onDataPart: jest.fn(),
- onToolCallPart: jest.fn(),
+ onToolInputStart: jest.fn(),
+ onToolInputAvailable: jest.fn(),
onToolResultPart: jest.fn(),
+ onToolErrorPart: jest.fn(),
onError: jest.fn(),
+ onMessageMetadata: jest.fn(),
});
const expectNoStreamedError = {
@@ -15,17 +21,25 @@ const expectNoStreamedError = {
},
};
-const mockSuccessStreamData = [
- `0:"You, but don't tell anyone."`,
- `2:{"type":"state","version":1,"value":{"queries":{}}}`,
- `9:{"toolCallId":"x","toolName":"x","args":""}`,
- `a:{"toolCallId":"x","result":""}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+const mockSuccessEvents: SSEEvent[] = [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "You, but don't tell anyone." },
+ { type: "text-end", id: "t1" },
+ { type: "data-state", data: { queries: {} } },
+ { type: "tool-input-available", toolCallId: "x", toolName: "x", input: {} },
+ { type: "tool-output-available", toolCallId: "x", output: "" },
+ {
+ type: "finish",
+ finishReason: "stop",
+ messageMetadata: {
+ usage: { inputTokens: 4916, outputTokens: 8, totalTokens: 4924 },
+ },
+ },
];
-const getMockSuccessStream = () =>
- createMockReadableStream(mockSuccessStreamData);
+const getMockSuccessStream = () => createMockSSEStream(mockSuccessEvents);
-const getMockErrorStream = () => createMockReadableStream([`3:{}`]);
+const getMockErrorStream = () =>
+ createMockSSEStream([{ type: "error", errorText: "boom" }]);
describe("processChatResponse", () => {
it("should be able to process a valid stream", async () => {
@@ -41,21 +55,36 @@ describe("processChatResponse", () => {
await processChatResponse(mockSuccessStream, config);
expect(config.onTextPart).toHaveBeenCalled();
expect(config.onDataPart).toHaveBeenCalled();
- expect(config.onToolCallPart).toHaveBeenCalled();
+ expect(config.onToolInputAvailable).toHaveBeenCalled();
expect(config.onToolResultPart).toHaveBeenCalled();
+ expect(config.onMessageMetadata).toHaveBeenCalled();
expect(config.onError).not.toHaveBeenCalled();
const mockErrorStream = getMockErrorStream();
- try {
- await processChatResponse(mockErrorStream, config);
- } catch (_) {}
- expect(config.onError).toHaveBeenCalled();
+ await processChatResponse(mockErrorStream, config);
+ expect(config.onError).toHaveBeenCalledWith({ errorText: "boom" });
+ });
+
+ it("should surface a typed error's code via finish messageMetadata", async () => {
+ const config = getMockedCallbacks();
+ const result = await processChatResponse(
+ createMockSSEStream([
+ { type: "error", errorText: "nope" },
+ {
+ type: "finish",
+ finishReason: "error",
+ messageMetadata: { errorCode: "ai_usage_limit_reached" },
+ },
+ ]),
+ config,
+ );
+ expect(config.onError).toHaveBeenCalledWith({ errorText: "nope" });
+ expect(result.messageMetadata?.errorCode).toBe("ai_usage_limit_reached");
});
it("should ignore unknown data parts", async () => {
- const mockStream = createMockReadableStream([
- `2:{"type":"__some_futurist_data__","version":1,"value":"hi"}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ const mockStream = createMockSSEStream([
+ { type: "data-__some_futurist_data__", data: "hi" },
]);
const config = getMockedCallbacks();
@@ -65,65 +94,136 @@ describe("processChatResponse", () => {
expect(config.onDataPart).not.toHaveBeenCalled();
// we should keep track of the info in the result
expect(result.data).toEqual([
- { type: "__some_futurist_data__", value: "hi", version: 1 },
+ { type: "data-__some_futurist_data__", data: "hi" },
]);
});
- it("should ignore unknown part types", async () => {
- const mockStream = createMockReadableStream([`x:"UNKNOWN PART TYPE"`]);
+ it("should ignore unhandled event types", async () => {
+ const mockStream = createMockSSEStream([
+ { type: "reasoning-start", id: "r1" } as unknown as SSEEvent,
+ ]);
const config = getMockedCallbacks();
await expect(processChatResponse(mockStream, config)).resolves.toBeTruthy();
expect(config.onError).not.toHaveBeenCalled();
});
- it("should error if there is no part type", async () => {
- const mockStream = createMockReadableStream([`data-without-part-type`]);
- await expect(
- processChatResponse(mockStream, expectNoStreamedError),
- ).rejects.toBeTruthy();
+ it("should skip unparseable events without throwing", async () => {
+ const mockStream = createMockReadableStream([`data: {bad json\n`]);
+ const config = getMockedCallbacks();
+ await expect(processChatResponse(mockStream, config)).resolves.toBeTruthy();
+ expect(config.onError).not.toHaveBeenCalled();
+ });
+
+ it("should record a tool-output-error against the matching tool call", async () => {
+ const config = getMockedCallbacks();
+ const result = await processChatResponse(
+ createMockSSEStream([
+ {
+ type: "tool-input-available",
+ toolCallId: "x",
+ toolName: "x",
+ input: {},
+ },
+ { type: "tool-output-error", toolCallId: "x", errorText: "boom" },
+ ]),
+ config,
+ );
+
+ expect(config.onToolErrorPart).toHaveBeenCalledWith({
+ type: "tool-output-error",
+ toolCallId: "x",
+ errorText: "boom",
+ });
+ expect(result.toolCalls).toEqual([
+ {
+ toolCallId: "x",
+ toolName: "x",
+ state: "result",
+ value: undefined,
+ error: "boom",
+ },
+ ]);
+ expect(result.history).toContainEqual({
+ role: "tool",
+ content: "boom",
+ tool_call_id: "x",
+ });
+ });
+
+ it("should call onToolInputStart for tool-input-start events", async () => {
+ const config = getMockedCallbacks();
+ await processChatResponse(
+ createMockSSEStream([
+ { type: "tool-input-start", toolCallId: "x", toolName: "x" },
+ ]),
+ config,
+ );
+ expect(config.onToolInputStart).toHaveBeenCalledWith({
+ type: "tool-input-start",
+ toolCallId: "x",
+ toolName: "x",
+ });
+ });
+
+ it("should surface a standalone message-metadata event", async () => {
+ const config = getMockedCallbacks();
+ const metadata = {
+ usage: { inputTokens: 1, outputTokens: 2, totalTokens: 3 },
+ };
+ const result = await processChatResponse(
+ createMockSSEStream([
+ { type: "message-metadata", messageMetadata: metadata },
+ ]),
+ config,
+ );
+
+ expect(config.onMessageMetadata).toHaveBeenCalledWith(metadata);
+ expect(result.messageMetadata).toEqual(metadata);
});
it("should error if a tool result is returned without a preceding tool call", async () => {
- const mockStream = createMockReadableStream([
- `a:{"toolCallId":"x","result":""}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ const mockStream = createMockSSEStream([
+ { type: "tool-output-available", toolCallId: "x", output: "" },
]);
await expect(
processChatResponse(mockStream, expectNoStreamedError),
).rejects.toBeTruthy();
});
- it("should handle messages across multiple chunks", async () => {
- const mockStream = createMockReadableStream(
- [
- `0:"You, but `,
- `don't tell anyone."\n`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}\n`,
- ],
- { disableAutoInsertNewLines: true },
- );
+ it("should stitch text deltas into a single history entry", async () => {
+ const mockStream = createMockSSEStream([
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "You, but " },
+ { type: "text-delta", id: "t1", delta: "don't tell anyone." },
+ { type: "text-end", id: "t1" },
+ ]);
const result = await processChatResponse(mockStream, expectNoStreamedError);
- expect(result.text).toEqual("You, but don't tell anyone.");
expect(result.history).toEqual([
{ content: "You, but don't tell anyone.", role: "assistant" },
]);
});
it("should resolve with partial response for aborted requests", async () => {
- const mockStream = createMockReadableStream(
- [
- `0:"Partial response"`,
- `2:{"type":"state","version":1,"value":{"testing":123}}`,
- ],
- {
- streamOptions: {
- pull() {
- throw new DOMException("Stream aborted", "AbortError");
- },
- },
+ const partialEvents: SSEEvent[] = [
+ { type: "text-delta", id: "t1", delta: "Partial response" },
+ { type: "data-state", data: { testing: 123 } },
+ ];
+ const encoder = new TextEncoder();
+ let index = 0;
+ const mockStream = new ReadableStream<Uint8Array>({
+ pull(controller) {
+ if (index < partialEvents.length) {
+ controller.enqueue(
+ encoder.encode(
+ `data: ${JSON.stringify(partialEvents[index++])}\n\n`,
+ ),
+ );
+ } else {
+ throw new DOMException("Stream aborted", "AbortError");
+ }
},
- );
+ });
const config = getMockedCallbacks();
const result = await processChatResponse(mockStream, config);
@@ -139,7 +239,7 @@ describe("processChatResponse", () => {
await expect(
processChatResponse(
- createMockReadableStream([`0:"Starting response"`], {
+ createMockSSEStream([], {
streamOptions: {
async start() {
throw error;
@@ -151,3 +251,28 @@ describe("processChatResponse", () => {
).rejects.toThrow(error);
});
});
+
+describe("parseSSEStream", () => {
+ it("should stop at the [DONE] sentinel and ignore trailing events", async () => {
+ const encoder = new TextEncoder();
+ const stream = new ReadableStream<Uint8Array>({
+ start(controller) {
+ const encode = (payload: string) =>
+ controller.enqueue(encoder.encode(`data: ${payload}\n\n`));
+ encode(JSON.stringify({ type: "text-delta", id: "t1", delta: "hi" }));
+ encode("[DONE]");
+ encode(
+ JSON.stringify({ type: "text-delta", id: "t1", delta: "ignored" }),
+ );
+ controller.close();
+ },
+ });
+
+ const events: SSEEvent[] = [];
+ for await (const event of parseSSEStream(stream)) {
+ events.push(event);
+ }
+
+ expect(events).toEqual([{ type: "text-delta", id: "t1", delta: "hi" }]);
+ });
+});
diff --git a/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts b/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts
index 6576f6a1b14..dd3c65a2ed4 100644
--- a/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts
+++ b/frontend/src/metabase/api/ai-streaming/requests.unit.spec.ts
@@ -6,7 +6,8 @@ import {
aiStreamingQuery,
findMatchingInflightAiStreamingRequests,
} from "./requests";
-import { mockStreamedEndpoint } from "./test-utils";
+import type { SSEEvent } from "./sse-types";
+import { mockEndpoint, mockStreamedEndpoint } from "./test-utils";
const endpoint = "/some-streamed-endpoint";
const fakeBasename = "http://example.com";
@@ -19,7 +20,7 @@ describe("ai requests", () => {
const fetchSpy = jest.spyOn(global, "fetch");
mockStreamedEndpoint(endpoint, {
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
await aiStreamingQuery({ url: endpoint, body: {} });
@@ -35,7 +36,7 @@ describe("ai requests", () => {
it("should return full result of a successful request", async () => {
mockStreamedEndpoint(endpoint, {
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
const result = await aiStreamingQuery({ url: endpoint, body: {} });
expect(result).toMatchSnapshot();
@@ -43,19 +44,25 @@ describe("ai requests", () => {
it("should call callbacks for relevant chunk types", async () => {
mockStreamedEndpoint(endpoint, {
- textChunks: [
- `0:"Testing"`,
- `2:{"type":"state","version":1,"value":{}}`,
- `9:{"toolCallId":"x","toolName":"x","args":""}`,
- `a:{"toolCallId":"x","result":""}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":1,"completionTokens":1}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "Testing" },
+ { type: "text-end", id: "t1" },
+ { type: "data-state", data: {} },
+ {
+ type: "tool-input-available",
+ toolCallId: "x",
+ toolName: "x",
+ input: {},
+ },
+ { type: "tool-output-available", toolCallId: "x", output: "" },
],
});
const successCbs = {
onTextPart: jest.fn(),
onDataPart: jest.fn(),
- onToolCallPart: jest.fn(),
+ onToolInputAvailable: jest.fn(),
onToolResultPart: jest.fn(),
onError: jest.fn(),
};
@@ -63,22 +70,18 @@ describe("ai requests", () => {
await aiStreamingQuery({ url: endpoint, body: {} }, successCbs);
expect(successCbs.onTextPart).toHaveBeenCalled();
expect(successCbs.onDataPart).toHaveBeenCalled();
- expect(successCbs.onToolCallPart).toHaveBeenCalled();
+ expect(successCbs.onToolInputAvailable).toHaveBeenCalled();
expect(successCbs.onToolResultPart).toHaveBeenCalled();
expect(successCbs.onError).not.toHaveBeenCalled();
mockStreamedEndpoint(endpoint, {
- textChunks: [
- `3:{}`, // error after finish to trigger all callbacks
- ],
+ events: [{ type: "error", errorText: "boom" }],
});
const failureCbs = {
onError: jest.fn(),
};
- try {
- await aiStreamingQuery({ url: endpoint, body: {} }, failureCbs);
- } catch (_) {}
+ await aiStreamingQuery({ url: endpoint, body: {} }, failureCbs);
expect(failureCbs.onError).toHaveBeenCalled();
});
@@ -110,9 +113,7 @@ describe("ai requests", () => {
});
it("throw error if no response", async () => {
- mockStreamedEndpoint(endpoint, {
- textChunks: undefined,
- });
+ mockEndpoint(endpoint, async () => new Response(null, { status: 202 }));
await expect(
aiStreamingQuery({ url: endpoint, body: {} }),
).rejects.toThrow(/No response/);
@@ -134,7 +135,7 @@ describe("ai requests", () => {
describe("in-flight request tracking", () => {
it("should register/unregister with inflight requests on a successful request", async () => {
mockStreamedEndpoint(endpoint, {
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
expect(findMatchingInflightAiStreamingRequests(endpoint).length).toBe(
0,
@@ -176,8 +177,9 @@ describe("ai requests", () => {
});
});
-const whoIsYourFavoriteResponse = [
- `0:"You, but don't tell anyone."`,
- `2:{"type":"state","version":1,"value":{"queries":{}}}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+const whoIsYourFavoriteResponse: SSEEvent[] = [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "You, but don't tell anyone." },
+ { type: "text-end", id: "t1" },
+ { type: "data-state", data: { queries: {} } },
];
diff --git a/frontend/src/metabase/api/ai-streaming/schemas.ts b/frontend/src/metabase/api/ai-streaming/schemas.ts
index e921e7d87ff..76c29d4d9a8 100644
--- a/frontend/src/metabase/api/ai-streaming/schemas.ts
+++ b/frontend/src/metabase/api/ai-streaming/schemas.ts
@@ -8,22 +8,41 @@ import type {
SuggestedTransform,
} from "metabase-types/api";
-export const dataPartSchema = Yup.object({
- type: Yup.string().required(),
- version: Yup.number().required(),
- value: Yup.mixed(),
+export const toolInputAvailableSchema = Yup.object({
+ toolCallId: Yup.string().required(),
+ toolName: Yup.string().required(),
+ input: Yup.mixed().defined(),
+});
+
+export const toolOutputAvailableSchema = Yup.object({
+ toolCallId: Yup.string().required(),
+ output: Yup.mixed().defined(),
+});
+
+export const toolOutputErrorSchema = Yup.object({
+ toolCallId: Yup.string().required(),
+ errorText: Yup.string().required(),
+});
+
+export const dataEventSchema = Yup.object({
+ type: Yup.string()
+ .required()
+ .test("data-prefix", 'type must start with "data-"', (val) =>
+ val ? val.startsWith("data-") : false,
+ ),
+ data: Yup.mixed().required(),
});
export const knownDataPartTypes = [
- "navigate_to",
- "state",
- "todo_list",
- "code_edit",
- "transform_suggestion",
- "generated_entity",
- "adhoc_viz",
- "static_viz",
-];
+ "data-navigate_to",
+ "data-state",
+ "data-todo_list",
+ "data-code_edit",
+ "data-transform_suggestion",
+ "data-generated_entity",
+ "data-adhoc_viz",
+ "data-static_viz",
+] as const satisfies readonly KnownDataPart["type"][];
export type AdhocVizValue = {
query: unknown;
@@ -52,40 +71,17 @@ export type GeneratedCard = {
export type GeneratedEntity = GeneratedCard;
export type KnownDataPart =
- | { type: "navigate_to"; version: 1; value: string }
- | { type: "state"; version: 1; value: Record<string, any> }
- | { type: "todo_list"; version: 1; value: MetabotTodoItem[] }
- | { type: "transform_suggestion"; version: 1; value: SuggestedTransform }
- | { type: "code_edit"; version: 1; value: MetabotCodeEdit }
- | { type: "generated_entity"; version: 1; value: GeneratedEntity }
- | { type: "adhoc_viz"; version: 1; value: AdhocVizValue }
- | { type: "static_viz"; version: 1; value: StaticVizValue };
+ | { type: "data-navigate_to"; data: string }
+ | { type: "data-state"; data: Record<string, unknown> }
+ | { type: "data-todo_list"; data: MetabotTodoItem[] }
+ | { type: "data-transform_suggestion"; data: SuggestedTransform }
+ | { type: "data-code_edit"; data: MetabotCodeEdit }
+ | { type: "data-generated_entity"; data: GeneratedEntity }
+ | { type: "data-adhoc_viz"; data: AdhocVizValue }
+ | { type: "data-static_viz"; data: StaticVizValue };
-export const toolCallPartSchema = Yup.object({
- toolCallId: Yup.string().required(),
- toolName: Yup.string().required(),
- args: Yup.string(),
-});
-
-export const toolResultPartSchema = Yup.object({
- toolCallId: Yup.string().required(),
- result: Yup.mixed(),
-});
-
-export const finishPartSchema = Yup.object({
- finishReason: Yup.string()
- .oneOf([
- "stop",
- "length",
- "content_filter",
- "tool_calls",
- "error",
- "other",
- "unknown",
- ])
- .required(),
-});
-
-export const startPartSchema = Yup.object({
- messageId: Yup.string().required(),
-});
+export const isKnownDataPart = (part: {
+ type: string;
+ data: unknown;
+}): part is KnownDataPart =>
+ (knownDataPartTypes as readonly string[]).includes(part.type);
diff --git a/frontend/src/metabase/api/ai-streaming/sse-stream.ts b/frontend/src/metabase/api/ai-streaming/sse-stream.ts
new file mode 100644
index 00000000000..fc217e5470a
--- /dev/null
+++ b/frontend/src/metabase/api/ai-streaming/sse-stream.ts
@@ -0,0 +1,45 @@
+import { EventSourceParserStream } from "eventsource-parser/stream";
+
+import type { SSEEvent } from "./sse-types";
+
+/**
+ * Parse a Server-Sent Events stream into typed SSE events.
+ * Yields parsed JSON event objects and terminates on the `[DONE]` sentinel.
+ *
+ * A line that fails to parse is logged and skipped rather than thrown, so a
+ * single malformed event can't tear down an otherwise-healthy live stream.
+ */
+export async function* parseSSEStream(
+ stream: ReadableStream<Uint8Array>,
+): AsyncGenerator<SSEEvent> {
+ const eventStream = stream
+ // TypeScript v5.9 issue - cast works around TS Uint8Array generic variance issue: https://github.com/microsoft/TypeScript/issues/62240
+ .pipeThrough(
+ new TextDecoderStream() as unknown as ReadableWritablePair<
+ string,
+ Uint8Array<ArrayBufferLike>
+ >,
+ )
+ .pipeThrough(new EventSourceParserStream());
+
+ const reader = eventStream.getReader();
+
+ try {
+ while (true) {
+ const { value, done } = await reader.read();
+
+ if (done || value.data === "[DONE]") {
+ return;
+ }
+
+ try {
+ yield JSON.parse(value.data) as SSEEvent;
+ } catch {
+ console.warn("Skipping unparseable SSE event:", value.data);
+ }
+ }
+ } finally {
+ await reader.cancel().catch(() => {});
+ reader.releaseLock();
+ }
+}
diff --git a/frontend/src/metabase/api/ai-streaming/sse-types.ts b/frontend/src/metabase/api/ai-streaming/sse-types.ts
new file mode 100644
index 00000000000..91c3c1c7487
--- /dev/null
+++ b/frontend/src/metabase/api/ai-streaming/sse-types.ts
@@ -0,0 +1,132 @@
+// Typed view of the AI SDK v5/v6 SSE wire protocol (UIMessageChunk), mirroring
+// the backend's `metabase.metabot.schema.v2/ui-message-chunk`. Outer chunk keys
+// are camelCase (spec-faithful); `data-*` payloads keep Metabase snake_case.
+
+export type TokenUsage = {
+ inputTokens: number;
+ outputTokens: number;
+ totalTokens: number;
+};
+
+export type MessageMetadata = {
+ usage?: TokenUsage;
+ usageByModel?: Record<string, TokenUsage>;
+ // a typed error's code, ridden on the trailing `finish` event so the client
+ // can branch on it (e.g. the usage-limit upgrade prompt)
+ errorCode?: string;
+};
+
+export type ProviderMetadata = Record<string, Record<string, unknown>>;
+
+export type FinishReason =
+ | "stop"
+ | "length"
+ | "content-filter"
+ | "tool-calls"
+ | "error"
+ | "other";
+
+// lifecycle events
+export type StartEvent = { type: "start"; messageId?: string };
+export type StartStepEvent = { type: "start-step" };
+export type FinishStepEvent = { type: "finish-step" };
+export type FinishEvent = {
+ type: "finish";
+ finishReason?: FinishReason;
+ messageMetadata?: MessageMetadata;
+};
+
+// usage events
+export type MessageMetadataEvent = {
+ type: "message-metadata";
+ messageMetadata: MessageMetadata;
+};
+
+// text events
+export type TextStartEvent = {
+ type: "text-start";
+ id: string;
+ providerMetadata?: ProviderMetadata;
+};
+export type TextDeltaEvent = {
+ type: "text-delta";
+ id: string;
+ delta: string;
+ providerMetadata?: ProviderMetadata;
+};
+export type TextEndEvent = {
+ type: "text-end";
+ id: string;
+ providerMetadata?: ProviderMetadata;
+};
+
+// tool events
+export type ToolInputStartEvent = {
+ type: "tool-input-start";
+ toolCallId: string;
+ toolName: string;
+ providerExecuted?: boolean;
+ providerMetadata?: ProviderMetadata;
+ dynamic?: boolean;
+ title?: string;
+};
+export type ToolInputDeltaEvent = {
+ type: "tool-input-delta";
+ toolCallId: string;
+ inputTextDelta: string;
+};
+export type ToolInputAvailableEvent = {
+ type: "tool-input-available";
+ toolCallId: string;
+ toolName: string;
+ input: unknown;
+ providerExecuted?: boolean;
+ providerMetadata?: ProviderMetadata;
+ dynamic?: boolean;
+ title?: string;
+};
+export type ToolOutputAvailableEvent = {
+ type: "tool-output-available";
+ toolCallId: string;
+ output: unknown;
+ providerExecuted?: boolean;
+ providerMetadata?: ProviderMetadata;
+ dynamic?: boolean;
+ preliminary?: boolean;
+};
+export type ToolOutputErrorEvent = {
+ type: "tool-output-error";
+ toolCallId: string;
+ errorText: string;
+ providerExecuted?: boolean;
+ providerMetadata?: ProviderMetadata;
+ dynamic?: boolean;
+};
+
+// error event
+export type ErrorEvent = { type: "error"; errorText: string };
+
+// data events use "data-{subtype}" naming convention
+export type DataEvent = {
+ type: `data-${string}`;
+ id?: string;
+ data: unknown;
+ transient?: boolean;
+};
+
+export type SSEEvent =
+ | StartEvent
+ | StartStepEvent
+ | FinishStepEvent
+ | FinishEvent
+ | MessageMetadataEvent
+ | TextStartEvent
+ | TextDeltaEvent
+ | TextEndEvent
+ | ToolInputStartEvent
+ | ToolInputDeltaEvent
+ | ToolInputAvailableEvent
+ | ToolOutputAvailableEvent
+ | ToolOutputErrorEvent
+ | ErrorEvent
+ | DataEvent;
diff --git a/frontend/src/metabase/api/ai-streaming/test-utils.ts b/frontend/src/metabase/api/ai-streaming/test-utils.ts
index f8c235d8c2a..172aa1ccabb 100644
--- a/frontend/src/metabase/api/ai-streaming/test-utils.ts
+++ b/frontend/src/metabase/api/ai-streaming/test-utils.ts
@@ -1,10 +1,69 @@
import { defer } from "metabase/utils/promise";
+import type { SSEEvent } from "./sse-types";
+
export function createPauses<Count extends number>(count: Count) {
const pauses = new Array(count).fill(null).map(() => defer());
return pauses as ReturnType<typeof defer>[] & { length: Count };
}
+const lifecycleStartFor = ([first]: SSEEvent[]): SSEEvent[] =>
+ first?.type === "start" || first?.type === "start-step"
+ ? []
+ : [{ type: "start", messageId: "mock-message" }, { type: "start-step" }];
+
+const lifecycleFinishFor = (events: SSEEvent[]) => {
+ const last = events.at(-1)?.type;
+ return [
+ ...(last === "finish-step" || last === "finish"
+ ? []
+ : [{ type: "finish-step" }]),
+ ...(last === "finish" ? [] : [{ type: "finish" }]),
+ "[DONE]",
+ ];
+};
+
+/**
+ * Create a mock SSE stream from an array of event objects.
+ *
+ * Each event is encoded as `data: {JSON}\n\n`; string entries (like "[DONE]") as
+ * `data: {string}\n\n`.
+ *
+ * For array inputs, the full backend lifecycle is wrapped around the provided
+ * events to match real server output:
+ * `start` → `start-step` → ...<your events>... → `finish-step` → `finish` → `[DONE]`
+ * Any lifecycle event the caller already supplies at the head or tail is
+ * preserved and not duplicated, so tests can pass a custom `start` (with a
+ * specific `messageId`) or `finish` (with `messageMetadata`) and have it flow
+ * through unchanged. Async generator inputs control their own lifecycle.
+ */
+export function createMockSSEStream(
+ events: SSEEvent[] | AsyncGenerator<SSEEvent | string, void, unknown>,
+ options?: {
+ streamOptions?: Partial<ConstructorParameters<typeof ReadableStream>[0]>;
+ },
+): ReadableStream<Uint8Array> {
+ const source = Array.isArray(events)
+ ? [...lifecycleStartFor(events), ...events, ...lifecycleFinishFor(events)]
+ : events;
+
+ return new ReadableStream<Uint8Array>({
+ async start(controller) {
+ const encoder = new TextEncoder();
+ try {
+ for await (const event of source) {
+ const payload =
+ typeof event === "string" ? event : JSON.stringify(event);
+ controller.enqueue(encoder.encode(`data: ${payload}\n\n`));
+ }
+ } finally {
+ controller.close();
+ }
+ },
+ ...(options?.streamOptions ?? {}),
+ });
+}
+
export function createMockReadableStream(
textChunks: string[] | AsyncGenerator<string, void, unknown>,
options?: {
@@ -65,56 +124,58 @@ export function mockEndpoint<T extends Response>(
});
}
-export type MockStreamedEndpointParams =
- | {
- textChunks: string[] | undefined;
- stream?: undefined;
- waitForResponse?: boolean;
- }
- | {
- textChunks?: undefined;
- stream: ReadableStream<any>;
- waitForResponse?: boolean;
- };
+export type MockStreamedEndpointParams = {
+ events?: SSEEvent[];
+ textChunks?: string[];
+ stream?: ReadableStream<any>;
+ waitForResponse?: boolean;
+};
export function mockStreamedEndpoint(
url: string,
- { textChunks, stream, waitForResponse = false }: MockStreamedEndpointParams,
+ {
+ events,
+ textChunks,
+ stream,
+ waitForResponse = false,
+ }: MockStreamedEndpointParams,
) {
const responseGate = waitForResponse ? defer() : null;
const mock = mockEndpoint(url, async (init) => {
await responseGate?.promise;
- const body =
+ const source =
stream ||
- (textChunks && createMockReadableStream(textChunks)) ||
- undefined;
-
- // make stream abortable
- if (body) {
- let activeReader: ReadableStreamDefaultReader<any> | null = null;
- const originalGetReader = body.getReader.bind(body);
-
- body.getReader = function () {
- activeReader = originalGetReader();
- const originalRead = activeReader.read.bind(activeReader);
-
- // Race the read with the abort promise
- activeReader.read = async function () {
- return Promise.race([
- originalRead(),
- new Promise<never>((_, reject) => {
- init?.signal?.addEventListener("abort", () => {
- reject(new DOMException("Stream aborted", "AbortError"));
- });
- }),
- ]);
- };
-
- return activeReader;
- };
+ (events && createMockSSEStream(events)) ||
+ (textChunks && createMockReadableStream(textChunks));
+
+ if (!source) {
+ throw new Error(
+ "mockStreamedEndpoint requires one of `stream`, `events`, or `textChunks`",
+ );
}
+ // make the stream abortable
+ const reader = source.getReader();
+ const abortPromise = new Promise<never>((_, reject) => {
+ init?.signal?.addEventListener("abort", () => {
+ reject(new DOMException("Stream aborted", "AbortError"));
+ });
+ });
+ const body = new ReadableStream({
+ async pull(controller) {
+ const { value, done } = await Promise.race([
+ reader.read(),
+ abortPromise,
+ ]);
+ if (done) {
+ controller.close();
+ } else {
+ controller.enqueue(value);
+ }
+ },
+ });
+
return { status: 202, ok: true, body, headers: new Headers() } as any;
});
diff --git a/frontend/src/metabase/metabot/components/AIQuestionAnalysisButton.unit.spec.tsx b/frontend/src/metabase/metabot/components/AIQuestionAnalysisButton.unit.spec.tsx
index 37e4a478a6e..cb846a7f381 100644
--- a/frontend/src/metabase/metabot/components/AIQuestionAnalysisButton.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/AIQuestionAnalysisButton.unit.spec.tsx
@@ -14,9 +14,14 @@ import { AIQuestionAnalysisButton } from "./AIQuestionAnalysisButton";
const mockAgentEndpoint = () =>
mockStreamedEndpoint("/api/metabot/agent-streaming", {
- textChunks: [
- `0:"Here is an analysis of the chart."`,
- `d:{"finishReason":"stop","usage":{"promptTokens":100,"completionTokens":10}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ {
+ type: "text-delta",
+ id: "t1",
+ delta: "Here is an analysis of the chart.",
+ },
+ { type: "text-end", id: "t1" },
],
});
diff --git a/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsx
index aa7ac48bd85..23cf6b2fa6a 100644
--- a/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotAsk/MetabotAsk.unit.spec.tsx
@@ -40,7 +40,7 @@ describe("MetabotAsk", () => {
it("replaces the greeting with the conversation after sending a message", async () => {
setup({ ui: <MetabotAsk /> });
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
expect(await screen.findByText(greetingTitle)).toBeInTheDocument();
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsx
index 698ef2243be..841296f0428 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsx
@@ -31,31 +31,31 @@ export const AgentDataPartMessage = ({
debug,
}: AgentDataPartMessageProps) =>
match(message)
- .with({ part: { type: "todo_list" } }, ({ part }) => (
- <AgentTodoListMessage todos={part.value} />
+ .with({ part: { type: "data-todo_list" } }, ({ part }) => (
+ <AgentTodoListMessage todos={part.data} />
))
- .with({ part: { type: "transform_suggestion" } }, (msg) => (
+ .with({ part: { type: "data-transform_suggestion" } }, (msg) => (
<AgentSuggestionMessage message={msg} readonly={readonly} />
))
- .with({ part: { type: "navigate_to" } }, ({ part }) => {
+ .with({ part: { type: "data-navigate_to" } }, ({ part }) => {
const sourcePills = (
<NavigateToTablePills
- path={part.value}
+ path={part.data}
messageId={readonly ? undefined : message.externalId}
/>
);
return (
<Stack gap="md">
- {debug && <NavigateToDataPart type={part.type} path={part.value} />}
+ {debug && <NavigateToDataPart type={part.type} path={part.data} />}
{sourcePills}
</Stack>
);
})
- .with({ part: { type: "code_edit" } }, ({ part, metadata }) => {
+ .with({ part: { type: "data-code_edit" } }, ({ part, metadata }) => {
const sourcePills = (
<CodeEditTablePills
- value={part.value}
+ value={part.data}
buffer={metadata?.codeEditBuffer}
messageId={readonly ? undefined : message.externalId}
/>
@@ -63,31 +63,33 @@ export const AgentDataPartMessage = ({
return (
<Stack gap="md">
- {debug && <CodeEditDataPart type={part.type} value={part.value} />}
+ {debug && <CodeEditDataPart type={part.type} value={part.data} />}
{sourcePills}
</Stack>
);
})
.with(
- { part: { type: "generated_entity", value: { type: "card" } } },
+ { part: { type: "data-generated_entity", data: { type: "card" } } },
({ part }) => (
<Stack gap="md">
- {debug && <DataPartJsonCard type={part.type} value={part.value} />}
- <MetabotInlineChart value={part.value} />
+ {debug && <DataPartJsonCard type={part.type} value={part.data} />}
+ <MetabotInlineChart value={part.data} />
</Stack>
),
)
- .with({ part: { type: "adhoc_viz" } }, ({ part }) =>
- debug ? <DataPartJsonCard type={part.type} value={part.value} /> : null,
+ .with({ part: { type: "data-adhoc_viz" } }, ({ part }) =>
+ debug ? <DataPartJsonCard type={part.type} value={part.data} /> : null,
)
- .with({ part: { type: "static_viz" } }, ({ part }) =>
- debug ? <DataPartJsonCard type={part.type} value={part.value} /> : null,
+ .with({ part: { type: "data-static_viz" } }, ({ part }) =>
+ debug ? <DataPartJsonCard type={part.type} value={part.data} /> : null,
)
.exhaustive((msg: unknown) => {
console.warn("AgentDataPartMessage received an unexpected value:", msg);
return null;
});
+const formatPartType = (type: string) => type.replace(/^data-/, "");
+
const DataPartJsonCard = ({
type,
value,
@@ -119,7 +121,7 @@ const DataPartJsonCard = ({
>
<Flex align="center">
<Icon name="document" c="text-secondary" mr="sm" />
- <Text fw="bold">{type}</Text>
+ <Text fw="bold">{formatPartType(type)}</Text>
</Flex>
<ActionIcon
h="sm"
@@ -169,7 +171,7 @@ const NavigateToDataPart = ({ type, path }: { type: string; path: string }) => (
>
<Flex align="center">
<Icon name="document" c="text-secondary" mr="sm" />
- <Text fw="bold">{type}</Text>
+ <Text fw="bold">{formatPartType(type)}</Text>
</Flex>
<ActionIcon
component={ForwardRefLink}
@@ -208,7 +210,7 @@ const CodeEditDataPart = ({
>
<Flex align="center" gap="sm">
<Icon name="document" c="text-secondary" />
- <Text fw="bold">{type}</Text>
+ <Text fw="bold">{formatPartType(type)}</Text>
<Text c="text-secondary">{t`Buffer ID: ${value.buffer_id}`}</Text>
<Badge variant="light" size="sm">
{value.mode}
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsx
index 0f6da0bb874..c5c40d7adbc 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.tsx
@@ -45,7 +45,7 @@ import type {
import S from "./MetabotAgentSuggestionMessage.module.css";
export type SuggestionMessage = Omit<MetabotAgentDataPartMessage, "part"> & {
- part: Extract<MetabotDataPart, { type: "transform_suggestion" }>;
+ part: Extract<MetabotDataPart, { type: "data-transform_suggestion" }>;
};
const PreviewContent = ({
@@ -126,7 +126,7 @@ export const AgentSuggestionMessage = ({
const [hasAppliedInContext, setHasAppliedInContext] = useState(false);
const suggestedTransform: MetabotSuggestedTransform = {
- ...message.part.value,
+ ...message.part.data,
active: true,
suggestionId: message.metadata?.suggestionId ?? message.id,
};
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.unit.spec.tsx
index 23d1c2c5e2e..4ce0f7cf50e 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentSuggestionMessage.unit.spec.tsx
@@ -56,9 +56,8 @@ const createMockTransformSuggestionMessage = (overrides: {
role: "agent",
type: "data_part",
part: {
- type: "transform_suggestion",
- version: 1,
- value: value,
+ type: "data-transform_suggestion",
+ data: value,
},
metadata: {
editorTransform: overrides.payload.editorTransform,
diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
index 644792d4762..a1929797349 100644
--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx
@@ -41,20 +41,20 @@ import { MetabotFeedbackModal } from "./MetabotFeedbackModal";
const isUserVisibleDataPart = (part: MetabotDataPart): boolean =>
match(part)
- .with({ type: "todo_list" }, () => true)
- .with({ type: "transform_suggestion" }, () => true)
- .with({ type: "navigate_to" }, () => true)
- .with({ type: "code_edit" }, () => true)
- .with({ type: "generated_entity" }, () => true)
- .with({ type: "adhoc_viz" }, () => false)
- .with({ type: "static_viz" }, () => false)
+ .with({ type: "data-todo_list" }, () => true)
+ .with({ type: "data-transform_suggestion" }, () => true)
+ .with({ type: "data-navigate_to" }, () => true)
+ .with({ type: "data-code_edit" }, () => true)
+ .with({ type: "data-generated_entity" }, () => true)
+ .with({ type: "data-adhoc_viz" }, () => false)
+ .with({ type: "data-static_viz" }, () => false)
.exhaustive();
const isUserVisibleDataPartMessage = (
message: MetabotAgentDataPartMessage,
): boolean =>
match(message)
- .with({ part: { type: "code_edit" } }, ({ metadata }) => {
+ .with({ part: { type: "data-code_edit" } }, ({ metadata }) => {
return metadata?.codeEditBuffer?.source.database_id != null;
})
.otherwise(({ part }) => isUserVisibleDataPart(part));
diff --git a/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx b/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
index b45f3e9474f..c0278fb90ea 100644
--- a/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
+++ b/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
@@ -32,11 +32,7 @@ const responseHasCodeEdit = (action: SubmitInputResult) => {
return (
isFulfilled(action) &&
action.payload.data?.processedResponse.data.some(
- (dp) =>
- typeof dp === "object" &&
- dp !== null &&
- "type" in dp &&
- (dp as { type: string }).type === "code_edit",
+ (dp) => dp.type === "data-code_edit",
)
);
};
diff --git a/frontend/src/metabase/metabot/state/actions.ts b/frontend/src/metabase/metabot/state/actions.ts
index 426050384e2..a30993a6373 100644
--- a/frontend/src/metabase/metabot/state/actions.ts
+++ b/frontend/src/metabase/metabot/state/actions.ts
@@ -59,6 +59,7 @@ export const {
setPendingMessageExternalId,
setProfileOverride,
toolCallStart,
+ toolCallArgs,
toolCallEnd,
setMetabotReqIdOverride,
setDebugMode,
@@ -404,14 +405,14 @@ export const sendAgentRequest = createAsyncThunk<
match(part)
// only update the convo state if the request is successful
- .with({ type: "state" }, (part) => (state = part.value))
- .with({ type: "todo_list" }, (part) => {
+ .with({ type: "data-state" }, (part) => (state = part.data))
+ .with({ type: "data-todo_list" }, (part) => {
pushDataPart({ type: "data_part", part });
})
- .with({ type: "code_edit" }, (part) => {
- dispatch(addSuggestedCodeEdit({ ...part.value, active: true }));
+ .with({ type: "data-code_edit" }, (part) => {
+ dispatch(addSuggestedCodeEdit({ ...part.data, active: true }));
- if (part.value.buffer_id === "qb") {
+ if (part.data.buffer_id === "qb") {
dispatch(setIsNativeEditorOpen(true));
}
pushDataPart({
@@ -420,24 +421,24 @@ export const sendAgentRequest = createAsyncThunk<
metadata: {
codeEditBuffer: findCodeEditBuffer(
request.context,
- part.value.buffer_id,
+ part.data.buffer_id,
),
},
});
})
- .with({ type: "navigate_to" }, (part) => {
- dispatch(setNavigateToPath(part.value));
+ .with({ type: "data-navigate_to" }, (part) => {
+ dispatch(setNavigateToPath(part.data));
if (!isEmbeddingSdk()) {
- dispatch(push(part.value) as UnknownAction);
+ dispatch(push(part.data) as UnknownAction);
}
pushDataPart({ type: "data_part", part });
})
- .with({ type: "transform_suggestion" }, (part) => {
+ .with({ type: "data-transform_suggestion" }, (part) => {
const suggestionId = nanoid();
const suggestedTransform = {
- ...part.value,
- id: part.value.id || undefined,
+ ...part.data,
+ id: part.data.id || undefined,
active: true,
suggestionId,
};
@@ -455,36 +456,74 @@ export const sendAgentRequest = createAsyncThunk<
});
})
.with(
- { type: "generated_entity" },
- { type: "adhoc_viz" },
- { type: "static_viz" },
+ { type: "data-generated_entity" },
+ { type: "data-adhoc_viz" },
+ { type: "data-static_viz" },
(part) => {
pushDataPart({ type: "data_part", part });
},
)
.exhaustive();
},
- onStartMessagePart: function handleStartMessagePart(part) {
+ onStart: function handleStart(event) {
+ if (event.messageId) {
+ dispatch(
+ setPendingMessageExternalId({
+ agentId,
+ externalId: event.messageId,
+ }),
+ );
+ }
+ },
+ onTextPart: function handleTextPart(delta) {
+ dispatch(addAgentTextDelta({ agentId, text: delta }));
+ },
+ onToolInputStart: function handleToolInputStart(event) {
dispatch(
- setPendingMessageExternalId({
+ toolCallStart({
+ toolCallId: event.toolCallId,
+ toolName: event.toolName,
agentId,
- externalId: part.messageId,
}),
);
},
- onTextPart: function handleTextPart(part) {
- dispatch(addAgentTextDelta({ agentId, text: String(part) }));
+ onToolInputAvailable: function handleToolInputAvailable(event) {
+ dispatch(
+ toolCallArgs({
+ toolCallId: event.toolCallId,
+ toolName: event.toolName,
+ args: JSON.stringify(event.input),
+ agentId,
+ }),
+ );
},
- onToolCallPart: function handleToolCallPart(part) {
- dispatch(toolCallStart({ ...part, agentId }));
+ onToolResultPart: function handleToolResultPart(event) {
+ dispatch(
+ toolCallEnd({
+ toolCallId: event.toolCallId,
+ result:
+ typeof event.output === "string"
+ ? event.output
+ : JSON.stringify(event.output),
+ agentId,
+ }),
+ );
},
- onToolResultPart: function handleToolResultPart(part) {
- dispatch(toolCallEnd({ ...part, agentId }));
+ onToolErrorPart: function handleToolErrorPart(event) {
+ dispatch(
+ toolCallEnd({
+ toolCallId: event.toolCallId,
+ result: event.errorText,
+ isError: true,
+ agentId,
+ }),
+ );
},
- onError: function handleError(part) {
- streamedError = isMatching({ message: P.string }, part)
- ? part
- : { message: String(part) };
+ onError: function handleError(error) {
+ // the `error` chunk carries only the message; a typed error's code
+ // rides the trailing `finish` event's messageMetadata, folded in at
+ // rejection time below
+ streamedError = { message: error.errorText };
},
},
);
@@ -494,13 +533,16 @@ export const sendAgentRequest = createAsyncThunk<
}
if (streamedError) {
+ // a typed error's code arrives on finish.messageMetadata, after the
+ // `error` chunk — fold it in so the display branch can match on it
+ streamedError.type = response.messageMetadata?.errorCode;
return rejectWithValue({
type: "error",
conversation_id: request.conversation_id,
shouldRetry: true,
error: streamedError,
display: isMatching(
- { "error-code": "ai_usage_limit_reached", message: P.string },
+ { type: "ai_usage_limit_reached", message: P.string },
streamedError,
)
? // special case where we want to show the returned error from the backend
diff --git a/frontend/src/metabase/metabot/state/reducer-utils.ts b/frontend/src/metabase/metabot/state/reducer-utils.ts
index 99e0cdf20d1..bb317d50526 100644
--- a/frontend/src/metabase/metabot/state/reducer-utils.ts
+++ b/frontend/src/metabase/metabot/state/reducer-utils.ts
@@ -3,7 +3,10 @@ import { merge } from "icepick";
import type { WritableDraft } from "immer";
import { match } from "ts-pattern";
-import { METABOT_PROFILE_OVERRIDES } from "metabase/metabot/constants";
+import {
+ METABOT_PROFILE_OVERRIDES,
+ TOOL_CALL_MESSAGES,
+} from "metabase/metabot/constants";
import { uuid } from "metabase/utils/uuid";
import type {
@@ -11,6 +14,7 @@ import type {
MetabotAgentTurnDisplayError,
MetabotAgentTurnError,
MetabotConverstationState,
+ MetabotDebugToolCallMessage,
MetabotState,
} from "./types";
import { createMessageId } from "./utils";
@@ -19,6 +23,39 @@ export type ConvoPayloadAction<
Value extends Record<string, any> = Record<string, any>,
> = PayloadAction<{ agentId: MetabotAgentId } & Value>;
+export const findLastToolCallMessage = (
+ convo: WritableDraft<MetabotConverstationState>,
+ toolCallId: string,
+) =>
+ convo.messages.findLast(
+ (m): m is MetabotDebugToolCallMessage =>
+ m.type === "tool_call" && m.id === toolCallId,
+ );
+
+export const pushNewToolCall = (
+ convo: WritableDraft<MetabotConverstationState>,
+ {
+ toolCallId,
+ toolName,
+ args,
+ }: { toolCallId: string; toolName: string; args?: string },
+) => {
+ convo.messages.push({
+ id: toolCallId,
+ role: "agent",
+ type: "tool_call",
+ name: toolName,
+ args,
+ status: "started",
+ });
+ convo.activeToolCalls.push({
+ id: toolCallId,
+ name: toolName,
+ message: TOOL_CALL_MESSAGES[toolName],
+ status: "started",
+ });
+};
+
export const getRequestConversation = (
state: WritableDraft<MetabotState>,
action: {
diff --git a/frontend/src/metabase/metabot/state/reducer.ts b/frontend/src/metabase/metabot/state/reducer.ts
index 656befcd436..b743f4a307b 100644
--- a/frontend/src/metabase/metabot/state/reducer.ts
+++ b/frontend/src/metabase/metabot/state/reducer.ts
@@ -11,7 +11,7 @@ import type {
SuggestedTransform,
} from "metabase-types/api";
-import { type MetabotProfileId, TOOL_CALL_MESSAGES } from "../constants";
+import type { MetabotProfileId } from "../constants";
import { sendAgentRequest } from "./actions";
import {
@@ -20,8 +20,10 @@ import {
appendAgentTurnErrored,
convoReducer,
createConversation,
+ findLastToolCallMessage,
getMetabotInitialState,
getRequestConversation,
+ pushNewToolCall,
resetReactionState,
} from "./reducer-utils";
import type {
@@ -137,39 +139,58 @@ export const metabot = createSlice({
}>,
) => {
const { toolCallId, toolName, args } = action.payload;
- convo.messages.push({
- id: toolCallId,
- role: "agent",
- type: "tool_call",
- name: toolName,
- args,
- status: "started",
- });
- convo.activeToolCalls.push({
- id: toolCallId,
- name: toolName,
- message: TOOL_CALL_MESSAGES[toolName],
- status: "started",
- });
+ // idempotent: both tool-input-start and tool-input-available are
+ // able to signal the start of a tool call
+ if (convo.activeToolCalls.some((tc) => tc.id === toolCallId)) {
+ return;
+ }
+ pushNewToolCall(convo, { toolCallId, toolName, args });
+ },
+ ),
+ toolCallArgs: convoReducer(
+ (
+ convo,
+ action: ConvoPayloadAction<{
+ toolCallId: string;
+ toolName: string;
+ args: string;
+ }>,
+ ) => {
+ const { toolCallId, toolName, args } = action.payload;
+ const existingMsg = findLastToolCallMessage(convo, toolCallId);
+ if (existingMsg) {
+ // if toolCallStart was called (tool-input-start event is optional)
+ // update the existing tool call record to include the args received
+ existingMsg.args = args;
+ } else {
+ pushNewToolCall(convo, { toolCallId, toolName, args });
+ }
},
),
toolCallEnd: convoReducer(
(
convo,
- action: ConvoPayloadAction<{ toolCallId: string; result?: any }>,
+ action: ConvoPayloadAction<{
+ toolCallId: string;
+ result?: string;
+ isError?: boolean;
+ }>,
) => {
convo.activeToolCalls = convo.activeToolCalls.map((tc) =>
tc.id === action.payload.toolCallId ? { ...tc, status: "ended" } : tc,
);
// Update the message in messages array with result for debug history
- const message = convo.messages.findLast(
- (msg) =>
- msg.type === "tool_call" && msg.id === action.payload.toolCallId,
+ const message = findLastToolCallMessage(
+ convo,
+ action.payload.toolCallId,
);
- if (message?.type === "tool_call") {
+ if (message) {
message.status = "ended";
message.result = action.payload.result;
+ if (action.payload.isError) {
+ message.is_error = true;
+ }
}
},
),
diff --git a/frontend/src/metabase/metabot/state/selectors.ts b/frontend/src/metabase/metabot/state/selectors.ts
index bedf9623797..97c3728e727 100644
--- a/frontend/src/metabase/metabot/state/selectors.ts
+++ b/frontend/src/metabase/metabot/state/selectors.ts
@@ -136,7 +136,7 @@ export const getFinalNavigateToMessageIdsPerTurn = createSelector(
new Set(
splitByTurn(messages).flatMap((turn) => {
const lastNav = turn.findLast(
- (m) => m.type === "data_part" && m.part.type === "navigate_to",
+ (m) => m.type === "data_part" && m.part.type === "data-navigate_to",
);
return lastNav ? [lastNav.id] : [];
}),
diff --git a/frontend/src/metabase/metabot/state/types.ts b/frontend/src/metabase/metabot/state/types.ts
index a934840f953..7ad6877c456 100644
--- a/frontend/src/metabase/metabot/state/types.ts
+++ b/frontend/src/metabase/metabot/state/types.ts
@@ -8,7 +8,7 @@ import type {
MetabotTransformInfo,
} from "metabase-types/api";
-export type MetabotDataPart = Exclude<KnownDataPart, { type: "state" }>;
+export type MetabotDataPart = Exclude<KnownDataPart, { type: "data-state" }>;
export type MetabotDataPartMetadata = {
codeEditBuffer?: MetabotCodeEditorBufferContext;
diff --git a/frontend/src/metabase/metabot/tests/context.unit.spec.tsx b/frontend/src/metabase/metabot/tests/context.unit.spec.tsx
index df3d78ad0cd..93d10032d27 100644
--- a/frontend/src/metabase/metabot/tests/context.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/context.unit.spec.tsx
@@ -50,7 +50,7 @@ describe("metabot > context", () => {
it("should send along default context", async () => {
setup();
const agentSpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
await enterChatMessage("Who is your favorite?");
@@ -78,7 +78,7 @@ describe("metabot > context", () => {
]);
const agentSpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
await enterChatMessage("Who is your favorite?");
@@ -92,7 +92,7 @@ describe("metabot > context", () => {
it("should allow components to register additional context", async () => {
const agentSpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
const TestComponent = () => {
@@ -142,7 +142,7 @@ describe("metabot > context", () => {
);
const agentSpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
const SqlFixContextRegistration = () => {
diff --git a/frontend/src/metabase/metabot/tests/convo-state.unit.spec.tsx b/frontend/src/metabase/metabot/tests/convo-state.unit.spec.tsx
index 5cfda808d95..38d08b3f410 100644
--- a/frontend/src/metabase/metabot/tests/convo-state.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/convo-state.unit.spec.tsx
@@ -4,7 +4,7 @@ import { getMetabotRequestState } from "metabase/metabot/state";
import {
assertConversation,
- createMockReadableStream,
+ createMockSSEStream,
createPauses,
enterChatMessage,
erroredResponse,
@@ -20,11 +20,11 @@ describe("metabot > convo state", () => {
getMetabotRequestState(store.getState(), "omnibot");
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `2:{"type":"state","version":1,"value":{"queries":{}}}\n`;
+ yield { type: "data-state", data: { queries: {} } };
expect(getConvoReqState()).toEqual({});
- yield `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`;
+ yield { type: "finish", finishReason: "stop" };
})(),
),
});
@@ -40,8 +40,8 @@ describe("metabot > convo state", () => {
getMetabotRequestState(store.getState(), "omnibot");
mockAgentEndpoint({
- textChunks: [
- `2:{"type":"state","version":1,"value":{"queries":{}}}`,
+ events: [
+ { type: "data-state", data: { queries: {} } },
...erroredResponse,
],
});
@@ -57,10 +57,12 @@ describe("metabot > convo state", () => {
getMetabotRequestState(store.getState(), "omnibot");
mockAgentEndpoint({
- textChunks: [
- `0:"here ya go"`,
- `2:{"type":"state","version":1,"value":{"testing":123}}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "here ya go" },
+ { type: "text-end", id: "t1" },
+ { type: "data-state", data: { testing: 123 } },
+ { type: "finish", finishReason: "stop" },
],
});
await enterChatMessage("gimme state plz");
@@ -72,11 +74,11 @@ describe("metabot > convo state", () => {
const [pause1] = createPauses(1);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `0:"blah blah blah"\n`;
+ yield { type: "text-delta", id: "t1", delta: "blah blah blah" };
await pause1.promise;
- yield `0:"something something"\n`;
+ yield { type: "text-delta", id: "t1", delta: "something something" };
})(),
),
});
@@ -99,9 +101,9 @@ describe("metabot > convo state", () => {
const [pause1] = createPauses(1);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `2:{"type":"state","version":1,"value":{"testing":123}}`;
+ yield { type: "data-state", data: { testing: 123 } };
await pause1.promise;
})(),
),
diff --git a/frontend/src/metabase/metabot/tests/errors.unit.spec.tsx b/frontend/src/metabase/metabot/tests/errors.unit.spec.tsx
index 32083f9a0a6..de35e4b56f2 100644
--- a/frontend/src/metabase/metabot/tests/errors.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/errors.unit.spec.tsx
@@ -99,7 +99,7 @@ describe("metabot > errors", () => {
it("should show the backend message for admin quota limit errors", async () => {
setup();
- mockAgentEndpoint({ textChunks: adminQuotaLimitErroredResponse });
+ mockAgentEndpoint({ events: adminQuotaLimitErroredResponse });
await enterChatMessage("Who is your favorite?");
@@ -112,7 +112,7 @@ describe("metabot > errors", () => {
it("should mask streamed errors with a generic message", async () => {
setup();
- mockAgentEndpoint({ textChunks: erroredResponse });
+ mockAgentEndpoint({ events: erroredResponse });
await enterChatMessage("Who is your favorite?");
@@ -128,7 +128,7 @@ describe("metabot > errors", () => {
it("should not show a user error when an AbortError is triggered", async () => {
setup();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
@@ -156,7 +156,7 @@ describe("metabot > errors", () => {
expect(await input()).toHaveTextContent("Who is your favorite?");
mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
await enterChatMessage("Who is your favorite?");
await assertConversation([
@@ -167,7 +167,7 @@ describe("metabot > errors", () => {
it("should rewind the previous prompt on next submit if last response contained a stream-level error", async () => {
setup();
- mockAgentEndpoint({ textChunks: erroredResponse });
+ mockAgentEndpoint({ events: erroredResponse });
await enterChatMessage("first prompt");
await assertConversation([
@@ -176,7 +176,7 @@ describe("metabot > errors", () => {
]);
const retrySpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
await enterChatMessage("new first prompt");
diff --git a/frontend/src/metabase/metabot/tests/experimental.unit.spec.tsx b/frontend/src/metabase/metabot/tests/experimental.unit.spec.tsx
index bc0ac82f24d..177ad2c801e 100644
--- a/frontend/src/metabase/metabot/tests/experimental.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/experimental.unit.spec.tsx
@@ -9,12 +9,24 @@ describe("metabot > experimental", () => {
describe("debug mode", () => {
const mockResponse = () => {
mockAgentEndpoint({
- textChunks: [
- `0:"Before"`,
- `9:{"toolCallId":"debug_test","toolName":"debug_test","args":""}`,
- `a:{"toolCallId":"debug_test","result":""}`,
- `0:"After"`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "Before" },
+ { type: "text-end", id: "t1" },
+ {
+ type: "tool-input-available",
+ toolCallId: "debug_test",
+ toolName: "debug_test",
+ input: {},
+ },
+ {
+ type: "tool-output-available",
+ toolCallId: "debug_test",
+ output: "",
+ },
+ { type: "text-start", id: "t2" },
+ { type: "text-delta", id: "t2", delta: "After" },
+ { type: "text-end", id: "t2" },
],
});
};
diff --git a/frontend/src/metabase/metabot/tests/feedback.unit.spec.ts b/frontend/src/metabase/metabot/tests/feedback.unit.spec.ts
index fd9ed8f458d..de26bfe58de 100644
--- a/frontend/src/metabase/metabot/tests/feedback.unit.spec.ts
+++ b/frontend/src/metabase/metabot/tests/feedback.unit.spec.ts
@@ -17,7 +17,7 @@ import {
const setupWithNegativeFeedback = async () => {
setup();
const feedbackEndpoint = mockFeedbackEndpoint();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
const lastMessage = (await lastChatMessage())!;
@@ -54,7 +54,7 @@ describe("metabot > feedback", () => {
it("should present the user an option to provide feedback", async () => {
setup();
const feedbackEndpoint = mockFeedbackEndpoint();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
const lastMessage = (await lastChatMessage())!;
@@ -123,7 +123,7 @@ describe("metabot > feedback", () => {
it("should submit positive feedback", async () => {
setup();
const feedbackEndpoint = mockFeedbackEndpoint();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
const lastMessage = (await lastChatMessage())!;
diff --git a/frontend/src/metabase/metabot/tests/history.unit.spec.tsx b/frontend/src/metabase/metabot/tests/history.unit.spec.tsx
index 23b6be5e9f2..f749aa36fc7 100644
--- a/frontend/src/metabase/metabot/tests/history.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/history.unit.spec.tsx
@@ -11,7 +11,7 @@ import {
import {
assertConversation,
- createMockReadableStream,
+ createMockSSEStream,
createPauses,
enterChatMessage,
hideMetabot,
@@ -29,7 +29,7 @@ describe("metabot > history", () => {
setup();
const { sendResponse } = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
waitForResponse: true,
});
await enterChatMessage("Who is your favorite?");
@@ -39,7 +39,7 @@ describe("metabot > history", () => {
).toBeInTheDocument();
const agentSpy = mockAgentEndpoint({
- textChunks: [],
+ events: [],
});
await enterChatMessage("Hi!");
const reqBody = await lastReqBody(agentSpy);
@@ -52,7 +52,7 @@ describe("metabot > history", () => {
it("should not clear history when metabot is hidden or opened", async () => {
const { store } = setup();
const agentSpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
await enterChatMessage("Who is your favorite?");
@@ -69,10 +69,12 @@ describe("metabot > history", () => {
it("should merge text chunks in the history", async () => {
const { store } = setup();
mockAgentEndpoint({
- textChunks: [
- `0:"You, but "`,
- `0:"don't tell anyone."`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "You, but " },
+ { type: "text-delta", id: "t1", delta: "don't tell anyone." },
+ { type: "text-end", id: "t1" },
+ { type: "finish", finishReason: "stop" },
],
});
@@ -92,7 +94,7 @@ describe("metabot > history", () => {
it("should clear history when the user hits the reset button", async () => {
const { store } = setup();
const getState = () => getMetabotConversation(store.getState(), "omnibot");
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
await assertConversation([
@@ -170,9 +172,14 @@ describe("metabot > history", () => {
const [pause1] = createPauses(1);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `9:{"toolCallId":"test","toolName":"test","args":""}`;
+ yield {
+ type: "tool-input-available",
+ toolCallId: "test",
+ toolName: "test",
+ input: {},
+ };
await pause1.promise;
})(),
),
@@ -184,7 +191,7 @@ describe("metabot > history", () => {
{ content: "hi", role: "user" },
{
role: "assistant",
- tool_calls: [{ arguments: "", id: "test", name: "test" }],
+ tool_calls: [{ arguments: "{}", id: "test", name: "test" }],
},
{
content: "Tool execution interrupted by user",
diff --git a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx b/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
index d9c0ab4cd5e..a671db315f6 100644
--- a/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/message.unit.spec.tsx
@@ -2,7 +2,7 @@ import { screen } from "__support__/ui";
import {
assertConversation,
- createMockReadableStream,
+ createMockSSEStream,
createPauses,
enterChatMessage,
input,
@@ -18,7 +18,7 @@ describe("metabot > message", () => {
setup();
const { sendResponse } = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
waitForResponse: true,
});
@@ -39,7 +39,7 @@ describe("metabot > message", () => {
it("should be able to send a message via send button", async () => {
setup();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?", false);
expect(await input()).toHaveTextContent("Who is your favorite?");
@@ -55,12 +55,12 @@ describe("metabot > message", () => {
const [pause1] = createPauses(1);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `0:"You, but "\n`;
+ yield { type: "text-delta", id: "t1", delta: "You, but " };
await pause1.promise;
- yield `0:"don't tell anyone."\n`;
- yield `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`;
+ yield { type: "text-delta", id: "t1", delta: "don't tell anyone." };
+ yield { type: "finish", finishReason: "stop" };
})(),
),
});
diff --git a/frontend/src/metabase/metabot/tests/multi-convo.unit.spec.tsx b/frontend/src/metabase/metabot/tests/multi-convo.unit.spec.tsx
index 302e4ff16f1..c82a1b1cdce 100644
--- a/frontend/src/metabase/metabot/tests/multi-convo.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/multi-convo.unit.spec.tsx
@@ -51,18 +51,20 @@ describe("multi-convo support", () => {
// ACT
mockAgentEndpoint({
- textChunks: [
- `0:"Test 1 response"`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "Test 1 response" },
+ { type: "text-end", id: "t1" },
],
});
const msg1 = { ...input, message: "test1", agentId: "test_1" } as const;
await store.dispatch(submitInput(msg1));
mockAgentEndpoint({
- textChunks: [
- `0:"Test 2 response"`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t2" },
+ { type: "text-delta", id: "t2", delta: "Test 2 response" },
+ { type: "text-end", id: "t2" },
],
});
const msg2 = { ...input, message: "test2", agentId: "test_2" } as const;
@@ -94,9 +96,10 @@ describe("multi-convo support", () => {
it("should be able to reset a conversation", async () => {
const { hook, store } = setup({ agentIds: ["test_1"] });
mockAgentEndpoint({
- textChunks: [
- `0:"response"`,
- `d:{"finishReason":"stop","usage":{"promptTokens":1,"completionTokens":1}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "response" },
+ { type: "text-end", id: "t1" },
],
});
await store.dispatch(
diff --git a/frontend/src/metabase/metabot/tests/query-builder-code-edit.unit.spec.tsx b/frontend/src/metabase/metabot/tests/query-builder-code-edit.unit.spec.tsx
index 80c0f63e33c..30ba8b5bbfe 100644
--- a/frontend/src/metabase/metabot/tests/query-builder-code-edit.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/query-builder-code-edit.unit.spec.tsx
@@ -79,12 +79,11 @@ describe("query builder code edits from omnibot", () => {
mockedAiStreamingQuery.mockImplementation(async (request, callbacks) => {
requestBody = request.body;
- callbacks?.onStartMessagePart?.({ messageId: "msg_test_code_edit" });
+ callbacks?.onStart?.({ type: "start", messageId: "msg_test_code_edit" });
callbacks?.onTextPart?.("Reviewing the query.");
callbacks?.onDataPart?.({
- type: "code_edit",
- version: 1,
- value: {
+ type: "data-code_edit",
+ data: {
buffer_id: "qb",
mode: "rewrite",
value: SUGGESTED_SQL,
@@ -95,8 +94,6 @@ describe("query builder code edits from omnibot", () => {
aborted: false,
toolCalls: [],
data: [],
- text: null,
- parts: [],
history: [],
};
});
@@ -185,7 +182,7 @@ describe("query builder code edits from omnibot", () => {
expect.objectContaining({
type: "data_part",
externalId: "msg_test_code_edit",
- part: expect.objectContaining({ type: "code_edit" }),
+ part: expect.objectContaining({ type: "data-code_edit" }),
}),
]),
);
diff --git a/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts b/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts
index fc8b9a5c0df..d3e1fc38155 100644
--- a/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts
+++ b/frontend/src/metabase/metabot/tests/reducer.unit.spec.ts
@@ -296,4 +296,116 @@ describe("metabot reducer", () => {
expect(getRequestConversation(state, action)).toBe(convo);
});
});
+
+ describe("tool calls", () => {
+ const agentId = "omnibot" as const;
+
+ it("toolCallStart is idempotent for the same toolCallId", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "x",
+ toolName: "analyze_data",
+ }),
+ );
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "x",
+ toolName: "analyze_data",
+ }),
+ );
+
+ const convo = store.getState().metabot.conversations.omnibot;
+ expect(convo?.messages).toHaveLength(1);
+ expect(convo?.activeToolCalls).toHaveLength(1);
+ });
+
+ it("toolCallArgs updates the existing tool-call message when toolCallStart preceded it", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "x",
+ toolName: "analyze_data",
+ }),
+ );
+ store.dispatch(
+ metabotActions.toolCallArgs({
+ agentId,
+ toolCallId: "x",
+ toolName: "analyze_data",
+ args: '{"foo":1}',
+ }),
+ );
+
+ const convo = store.getState().metabot.conversations.omnibot;
+ expect(convo?.messages).toEqual([
+ expect.objectContaining({
+ id: "x",
+ type: "tool_call",
+ args: '{"foo":1}',
+ status: "started",
+ }),
+ ]);
+ expect(convo?.activeToolCalls).toHaveLength(1);
+ });
+
+ it("toolCallArgs creates a tool-call message when no tool-input-start preceded it", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallArgs({
+ agentId,
+ toolCallId: "x",
+ toolName: "analyze_data",
+ args: '{"foo":1}',
+ }),
+ );
+
+ const convo = store.getState().metabot.conversations.omnibot;
+ expect(convo?.messages).toEqual([
+ expect.objectContaining({
+ id: "x",
+ type: "tool_call",
+ args: '{"foo":1}',
+ status: "started",
+ }),
+ ]);
+ expect(convo?.activeToolCalls).toHaveLength(1);
+ });
+
+ it("toolCallEnd marks the tool-call message as errored", () => {
+ const store = createTestStore();
+ store.dispatch(
+ metabotActions.toolCallStart({
+ agentId,
+ toolCallId: "x",
+ toolName: "analyze_data",
+ }),
+ );
+ store.dispatch(
+ metabotActions.toolCallEnd({
+ agentId,
+ toolCallId: "x",
+ result: "boom",
+ isError: true,
+ }),
+ );
+
+ const convo = store.getState().metabot.conversations.omnibot;
+ expect(convo?.messages).toEqual([
+ expect.objectContaining({
+ id: "x",
+ type: "tool_call",
+ status: "ended",
+ result: "boom",
+ is_error: true,
+ }),
+ ]);
+ expect(convo?.activeToolCalls).toEqual([
+ expect.objectContaining({ id: "x", status: "ended" }),
+ ]);
+ });
+ });
});
diff --git a/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx b/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx
index a39e59149a0..a889fef0a18 100644
--- a/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/tool-calls.unit.spec.tsx
@@ -4,7 +4,7 @@ import { screen, waitFor } from "__support__/ui";
import {
assertConversation,
- createMockReadableStream,
+ createMockSSEStream,
createPauses,
enterChatMessage,
input,
@@ -20,15 +20,25 @@ describe("metabot > tool calls", () => {
const [pause1, pause2] = createPauses(2);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `9:{"toolCallId":"x","toolName":"analyze_data","args":""}\n`;
- yield `a:{"toolCallId":"x","result":""}\n`;
+ yield {
+ type: "tool-input-available",
+ toolCallId: "x",
+ toolName: "analyze_data",
+ input: {},
+ };
+ yield { type: "tool-output-available", toolCallId: "x", output: "" };
await pause1.promise;
- yield `9:{"toolCallId":"y","toolName":"analyze_chart","args":""}\n`;
- yield `a:{"toolCallId":"y","result":""}\n`;
+ yield {
+ type: "tool-input-available",
+ toolCallId: "y",
+ toolName: "analyze_chart",
+ input: {},
+ };
+ yield { type: "tool-output-available", toolCallId: "y", output: "" };
await pause2.promise;
- yield `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`;
+ yield { type: "finish", finishReason: "stop" };
})(),
),
});
@@ -64,17 +74,27 @@ describe("metabot > tool calls", () => {
const [pause1, pause2, pause3] = createPauses(3);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `9:{"toolCallId":"x","toolName":"analyze_data","args":""}\n`;
- yield `a:{"toolCallId":"x","result":""}\n`;
+ yield {
+ type: "tool-input-available",
+ toolCallId: "x",
+ toolName: "analyze_data",
+ input: {},
+ };
+ yield { type: "tool-output-available", toolCallId: "x", output: "" };
await pause1.promise;
- yield `0:"Hey."`;
+ yield { type: "text-delta", id: "t1", delta: "Hey." };
await pause2.promise;
- yield `9:{"toolCallId":"y","toolName":"analyze_chart","args":""}\n`;
- yield `a:{"toolCallId":"y","result":""}\n`;
+ yield {
+ type: "tool-input-available",
+ toolCallId: "y",
+ toolName: "analyze_chart",
+ input: {},
+ };
+ yield { type: "tool-output-available", toolCallId: "y", output: "" };
await pause3.promise;
- yield `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`;
+ yield { type: "finish", finishReason: "stop" };
})(),
),
});
@@ -119,12 +139,20 @@ describe("metabot > tool calls", () => {
it("should start a new message if there's tool calls between streamed text parts", async () => {
setup();
mockAgentEndpoint({
- textChunks: [
- `0:"Response 1"`,
- `9:{"toolCallId":"x","toolName":"x","args":""}`,
- `a:{"toolCallId":"x","result":""}`,
- `0:"Response 2"`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "Response 1" },
+ { type: "text-end", id: "t1" },
+ {
+ type: "tool-input-available",
+ toolCallId: "x",
+ toolName: "x",
+ input: {},
+ },
+ { type: "tool-output-available", toolCallId: "x", output: "" },
+ { type: "text-start", id: "t2" },
+ { type: "text-delta", id: "t2", delta: "Response 2" },
+ { type: "text-end", id: "t2" },
],
});
await enterChatMessage("Request");
@@ -141,11 +169,11 @@ describe("metabot > tool calls", () => {
const [pause1] = createPauses(1);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `0:"You, but "\n`;
+ yield { type: "text-delta", id: "t1", delta: "You, but " };
await pause1.promise;
- yield `0:"don't tell anyone."\n`;
+ yield { type: "text-delta", id: "t1", delta: "don't tell anyone." };
})(),
),
});
@@ -154,7 +182,7 @@ describe("metabot > tool calls", () => {
await userEvent.click(await stopResponseButton());
pause1.resolve();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
await assertConversation([
["user", "Who is your favorite?"],
@@ -170,11 +198,11 @@ describe("metabot > tool calls", () => {
const [pause1] = createPauses(1);
mockAgentEndpoint({
- stream: createMockReadableStream(
+ stream: createMockSSEStream(
(async function* () {
- yield `0:"You, but "\n`;
+ yield { type: "text-delta", id: "t1", delta: "You, but " };
await pause1.promise;
- yield `0:"don't tell anyone."\n`;
+ yield { type: "text-delta", id: "t1", delta: "don't tell anyone." };
})(),
),
});
@@ -183,7 +211,7 @@ describe("metabot > tool calls", () => {
await userEvent.type(await input(), "{Escape}");
pause1.resolve();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
await assertConversation([
["user", "Who is your favorite?"],
diff --git a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx
index 540ce66b43a..923f7e76521 100644
--- a/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx
+++ b/frontend/src/metabase/metabot/tests/ui.unit.spec.tsx
@@ -72,7 +72,7 @@ describe("metabot > ui", () => {
it("should show empty state ui if conversation is empty", async () => {
setup();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
expect(
await screen.findByTestId("metabot-empty-chat-info"),
@@ -157,10 +157,16 @@ describe("metabot > ui", () => {
it("should render markdown for messages", async () => {
setup();
mockAgentEndpoint({
- textChunks: [
- `0:"# You, but don't tell anyone."`,
- `2:{"type":"state","version":1,"value":{"queries":{}}}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ {
+ type: "text-delta",
+ id: "t1",
+ delta: "# You, but don't tell anyone.",
+ },
+ { type: "text-end", id: "t1" },
+ { type: "data-state", data: { queries: {} } },
+ { type: "finish", finishReason: "stop" },
],
});
@@ -228,7 +234,7 @@ describe("metabot > ui", () => {
it("should present the user an option to retry a response", async () => {
setup();
- mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });
+ mockAgentEndpoint({ events: whoIsYourFavoriteResponse });
await enterChatMessage("Who is your favorite?");
const lastMessage = await lastChatMessage();
@@ -241,7 +247,12 @@ describe("metabot > ui", () => {
it("should successfully rewind a response", async () => {
setup();
mockAgentEndpoint({
- textChunks: [`0:"Let me think..."`, ...whoIsYourFavoriteResponse],
+ events: [
+ { type: "text-start", id: "t0" },
+ { type: "text-delta", id: "t0", delta: "Let me think..." },
+ { type: "text-end", id: "t0" },
+ ...whoIsYourFavoriteResponse,
+ ],
});
await enterChatMessage("Who is your favorite?");
@@ -250,10 +261,12 @@ describe("metabot > ui", () => {
expect(beforeMessages).toHaveTextContent(/You, but don't tell anyone./);
mockAgentEndpoint({
- textChunks: [
- `0:"The answer is always you."`,
- `2:{"type":"state","version":1,"value":{"queries":{}}}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+ events: [
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "The answer is always you." },
+ { type: "text-end", id: "t1" },
+ { type: "data-state", data: { queries: {} } },
+ { type: "finish", finishReason: "stop" },
],
});
await userEvent.click(
@@ -272,9 +285,8 @@ describe("metabot > ui", () => {
setup();
mockAgentEndpoint({
- textChunks: [
- `3:"Anthropic API key expired or invalid"`,
- `d:{"finishReason":"error","usage":{}}`,
+ events: [
+ { type: "error", errorText: "Anthropic API key expired or invalid" },
],
});
@@ -345,7 +357,7 @@ describe("metabot > ui", () => {
];
setup({ promptSuggestions: prompts });
const agentSpy = mockAgentEndpoint({
- textChunks: whoIsYourFavoriteResponse,
+ events: whoIsYourFavoriteResponse,
});
expect(
diff --git a/frontend/src/metabase/metabot/tests/utils.tsx b/frontend/src/metabase/metabot/tests/utils.tsx
index 23a6f5364d2..df8183ace25 100644
--- a/frontend/src/metabase/metabot/tests/utils.tsx
+++ b/frontend/src/metabase/metabot/tests/utils.tsx
@@ -14,9 +14,11 @@ import {
waitFor,
within,
} from "__support__/ui";
+import type { SSEEvent } from "metabase/api/ai-streaming/sse-types";
import {
type MockStreamedEndpointParams,
createMockReadableStream,
+ createMockSSEStream,
createPauses,
mockStreamedEndpoint,
} from "metabase/api/ai-streaming/test-utils";
@@ -40,7 +42,7 @@ import {
} from "../state";
import { getMetabotInitialState } from "../state/reducer-utils";
-export { createMockReadableStream, createPauses };
+export { createMockReadableStream, createMockSSEStream, createPauses };
export const mockAgentEndpoint = (params: MockStreamedEndpointParams) =>
mockStreamedEndpoint("/api/metabot/agent-streaming", params);
@@ -137,22 +139,31 @@ export const lastReqBody = async (
};
// Common mock response fixtures
-export const whoIsYourFavoriteResponse = [
- `f:{"messageId":"msg_test_favorite"}`,
- `0:"You, but don't tell anyone."`,
- `2:{"type":"state","version":1,"value":{"queries":{}}}`,
- `d:{"finishReason":"stop","usage":{"promptTokens":4916,"completionTokens":8}}`,
+export const whoIsYourFavoriteResponse: SSEEvent[] = [
+ { type: "start", messageId: "msg_test_favorite" },
+ { type: "text-start", id: "t1" },
+ { type: "text-delta", id: "t1", delta: "You, but don't tell anyone." },
+ { type: "text-end", id: "t1" },
+ { type: "data-state", data: { queries: {} } },
];
-export const erroredResponse = [
- `3:"Anthropic API key expired or invalid"`,
- `d:{"finishReason":"error","usage":{}}`,
+export const erroredResponse: SSEEvent[] = [
+ { type: "error", errorText: "Anthropic API key expired or invalid" },
];
-// Admin-configured quota exceeded — carries ai_usage_limit_reached error-code
-export const adminQuotaLimitErroredResponse = [
- `3:{"message":"You have reached your AI usage limit for the current period. Please contact your administrator.","error-code":"ai_usage_limit_reached"}`,
- `d:{"finishReason":"error","usage":{}}`,
+// Admin-configured quota exceeded — the error code rides on the trailing
+// `finish` event's messageMetadata
+export const adminQuotaLimitErroredResponse: SSEEvent[] = [
+ {
+ type: "error",
+ errorText:
+ "You have reached your AI usage limit for the current period. Please contact your administrator.",
+ },
+ {
+ type: "finish",
+ finishReason: "error",
+ messageMetadata: { errorCode: "ai_usage_limit_reached" },
+ },
];
type DefaultMetabotOverrides = {
diff --git a/frontend/test/jest-setup.js b/frontend/test/jest-setup.js
index a65ab17ba61..79865c67519 100644
--- a/frontend/test/jest-setup.js
+++ b/frontend/test/jest-setup.js
@@ -1,7 +1,12 @@
import { webcrypto } from "crypto";
import { TextDecoder, TextEncoder } from "util";
-import { ReadableStream } from "web-streams-polyfill";
+import { TextDecoderStream } from "@stardazed/streams-text-encoding";
+import {
+ ReadableStream,
+ TransformStream,
+ WritableStream,
+} from "web-streams-polyfill";
import "cross-fetch/polyfill";
import "raf/polyfill";
import "jest-canvas-mock";
@@ -66,6 +71,14 @@ global.TextDecoder = TextDecoder;
// replace node's ReadableStream what one that matches what is in the browser
global.ReadableStream = ReadableStream;
+// jsdom lacks the web-streams transform/writable + TextDecoderStream that the
+// SSE consumer pipes through; use the same polyfill family so pipeThrough's
+// brand checks line up (stardazed's TextDecoderStream builds on global
+// TransformStream, and EventSourceParserStream extends it)
+global.TransformStream = TransformStream;
+global.WritableStream = WritableStream;
+global.TextDecoderStream = TextDecoderStream;
+
// https://github.com/jsdom/jsdom/issues/3002
Range.prototype.getBoundingClientRect = () => ({
bottom: 0,
diff --git a/package.json b/package.json
index 02e926123da..23640793940 100644
--- a/package.json
+++ b/package.json
@@ -107,6 +107,7 @@
"dompurify": "^3.4.11",
"echarts": "6.1.0",
"events": "^3.3.0",
+ "eventsource-parser": "^3.0.6",
"fast-text-encoding": "^1.0.6",
"formik": "^2.4.5",
"frimousse": "^0.3.0",
@@ -213,6 +214,7 @@
"@rspack/dev-server": "^2.0.1",
"@rspack/plugin-react-refresh": "^2.0.0",
"@sinonjs/fake-timers": "^9.1.2",
+ "@stardazed/streams-text-encoding": "^1.0.2",
"@storybook/addon-a11y": "^8.5.0",
"@storybook/addon-actions": "^8.5.0",
"@storybook/addon-essentials": "^8.5.0",
diff --git a/resources/migrations/064/20260610_metabot_message_data_version.yaml b/resources/migrations/064/20260610_metabot_message_data_version.yaml
new file mode 100644
index 00000000000..9fcbc3c4201
--- /dev/null
+++ b/resources/migrations/064/20260610_metabot_message_data_version.yaml
@@ -0,0 +1,38 @@
+# Add data_version to metabot_message so the at-rest format of `data` is explicit.
+
+databaseChangeLog:
+ - changeSet:
+ id: v64.2026-06-10T00:00:00
+ author: sloansparger
+ comment: Add data_version to metabot_message for explicit format tracking
+ changes:
+ - addColumn:
+ tableName: metabot_message
+ columns:
+ - column:
+ name: data_version
+ remarks: Version of the data format stored in this message
+ type: integer
+ defaultValueNumeric: 1
+ constraints:
+ nullable: false
+ rollback:
+ - dropColumn:
+ tableName: metabot_message
+ columnName: data_version
+
+ - changeSet:
+ id: v64.2026-06-10T00:00:01
+ author: sloansparger
+ comment: Drop default for metabot_message.data_version so new rows must set it explicitly
+ changes:
+ - dropDefaultValue:
+ tableName: metabot_message
+ columnName: data_version
+ columnDataType: integer
+ rollback:
+ - addDefaultValue:
+ tableName: metabot_message
+ columnName: data_version
+ columnDataType: integer
+ defaultValueNumeric: 1
diff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.clj
index 10ca65eb44f..a01903e7f74 100644
--- a/src/metabase/metabot/agent/core.clj
+++ b/src/metabase/metabot/agent/core.clj
@@ -509,7 +509,7 @@
xf (comp (accumulate-usage-xf usage-atom (:model profile))
(u/tee-xf parts-atom))
;; We use `reduce` instead of `transduce` because rf is the outer reducing
- ;; function (e.g. aisdk-line-xf wrapping streaming-writer-rf) whose completion
+ ;; function (e.g. parts->aisdk-sse-xf wrapping streaming-writer-rf) whose completion
;; arity emits a finish message — that must only fire once, at the end of the
;; entire agent loop, not after every iteration.
result' (reduce (xf rf) result llm-call)
diff --git a/src/metabase/metabot/agent/streaming.clj b/src/metabase/metabot/agent/streaming.clj
index 07a50c3119a..46d73e86cf7 100644
--- a/src/metabase/metabot/agent/streaming.clj
+++ b/src/metabase/metabot/agent/streaming.clj
@@ -85,7 +85,6 @@
[todos]
{:type :data
:data-type todo-list-type
- :version 1
:data todos})
(defn code-edit-part
@@ -97,7 +96,6 @@
[edit-data]
{:type :data
:data-type code-edit-type
- :version 1
:data edit-data})
(defn transform-suggestion-part
@@ -109,7 +107,6 @@
[suggestion]
{:type :data
:data-type transform-suggestion-type
- :version 1
:data suggestion})
(defn adhoc-viz-part
@@ -122,7 +119,6 @@
[value]
{:type :data
:data-type adhoc-viz-type
- :version 1
:data value})
(defn static-viz-part
@@ -134,7 +130,6 @@
[value]
{:type :data
:data-type static-viz-type
- :version 1
:data value})
(defn generated-entity-part
@@ -144,7 +139,6 @@
[entity]
{:type :data
:data-type generated-entity-type
- :version 1
:data entity})
(defn viz-part
diff --git a/src/metabase/metabot/api.clj b/src/metabase/metabot/api.clj
index a6b52129d4c..be77bce8e2b 100644
--- a/src/metabase/metabot/api.clj
+++ b/src/metabase/metabot/api.clj
@@ -85,9 +85,10 @@
(defn- native-agent-streaming-request
"Handle streaming request using native Clojure agent.
- Streams AI SDK v4 line protocol to the client in real-time while simultaneously
- collecting parts for database storage. Text parts are combined before storage
- to consolidate streaming chunks into single text parts.
+ Streams AI SDK SSE `data: {UIMessageChunk}` events
+ (see [[self.core/parts->aisdk-sse-xf]]) to the client in real-time while
+ simultaneously collecting parts for database storage. Text parts are combined
+ before storage to consolidate streaming chunks into single text parts.
Monitors `canceled-chan` for client disconnection — when the client closes the
connection, the pipeline stops via `reduced` and collected parts are still persisted.
@@ -97,8 +98,9 @@
`:assistant-msg-id` is the PK of the placeholder assistant row created by
[[metabot.persistence/start-turn!]]; the finally block UPDATEs that row.
- `:external-id` is the assistant row's `external_id`, threaded into the AI-SDK
- line protocol so the client can correlate streamed messages with feedback."
+ `:external-id` is the assistant row's `external_id`, emitted as the SSE
+ `start` event's `messageId` so the client can correlate streamed messages
+ with feedback."
[{:keys [metabot-id profile-id message context history conversation-id state debug?
assistant-msg-id external-id]}]
(let [enriched-context (metabot.context/create-context context {:metabot-id metabot-id
@@ -113,10 +115,8 @@
;; this, such turns finalize as `:finished true :error nil` — indistinguishable
;; from a clean success.
thrown (volatile! nil)
- ;; In dev mode, emit usage parts in the SSE stream for debugging/benchmarking.
xf (comp (u/tee-xf parts-atom)
- (self.core/aisdk-line-xf {:emit-usage? config/is-dev?
- :external-id external-id}))]
+ (self.core/parts->aisdk-sse-xf {:message-id external-id}))]
(try
(transduce xf
(streaming-writer-rf os canceled-chan canceled?)
@@ -142,16 +142,17 @@
{:conversation-id conversation-id
:assistant-msg-id assistant-msg-id
:external-id external-id})
- ;; Stream a well-formed AI SDK error part so the client surfaces the failure
+ ;; Stream a well-formed AI SDK error tail so the client surfaces the failure
;; instead of treating the truncated stream as a silent success. Unlike binary
;; downloads (which abort the connection), an event stream carries its own error
- ;; framing, so we emit the error event and then let the body fn return to close
- ;; the socket cleanly — aborting here would deny the client this very event.
+ ;; framing, so we emit the error event, a closing `finish`, and `[DONE]`, then let
+ ;; the body fn return to close the socket cleanly — aborting here would deny the
+ ;; client this very event.
(try
- (let [error-line (self.core/format-error-line
- {:error (metabot.persistence/throwable->error-payload t)})]
- (.write os (.getBytes (str error-line "\n") "UTF-8"))
- (.flush os))
+ (.write os (.getBytes ^String (self.core/format-error-frames
+ {:error (metabot.persistence/throwable->error-payload t)})
+ "UTF-8"))
+ (.flush os)
(catch org.eclipse.jetty.io.EofException _
(vreset! canceled? true))))
(finally
diff --git a/src/metabase/metabot/models/metabot_message.clj b/src/metabase/metabot/models/metabot_message.clj
index b8d2084a127..73cde532392 100644
--- a/src/metabase/metabot/models/metabot_message.clj
+++ b/src/metabase/metabot/models/metabot_message.clj
@@ -1,6 +1,10 @@
(ns metabase.metabot.models.metabot-message
(:require
+ [metabase.config.core :as config]
+ [metabase.metabot.schema.migrate-v1-to-v2 :as migrate]
+ [metabase.metabot.schema.v2 :as schema.v2]
[metabase.models.interface :as mi]
+ [metabase.util.log :as log]
[methodical.core :as methodical]
[toucan2.core :as t2]))
@@ -15,3 +19,32 @@
{:usage mi/transform-json
:data mi/transform-json
:role mi/transform-keyword})
+
+(defn- migrate-v1->v2-on-read
+ "Convert a v1 row's `:data` to the v2 format and bump `:data_version`. On failure,
+ log and return the row unchanged so a malformed legacy row can't break reads."
+ [message]
+ (try
+ (-> message
+ (update :data #(->> (migrate/migrate-v1->v2 %)
+ (schema.v2/check-message-data "migrated metabot_message.data")))
+ (assoc :data_version schema.v2/current-data-version))
+ (catch Throwable e
+ (log/warn e "Failed to migrate metabot_message data v1->v2 on read" {:id (:id message)})
+ message)))
+
+(t2/define-after-select :model/MetabotMessage
+ [{:keys [data data_version] :as message}]
+ (cond
+ (not data)
+ message
+
+ (= 1 data_version)
+ (migrate-v1->v2-on-read message)
+
+ (and config/is-dev? (= 2 data_version))
+ (do (schema.v2/check-message-data "metabot_message.data (read)" data)
+ message)
+
+ :else
+ message))
diff --git a/src/metabase/metabot/persistence.clj b/src/metabase/metabot/persistence.clj
index 06a30c73e25..d3d83b7567d 100644
--- a/src/metabase/metabot/persistence.clj
+++ b/src/metabase/metabot/persistence.clj
@@ -1,11 +1,14 @@
(ns metabase.metabot.persistence
"Persistence for Metabot conversations and messages."
(:require
+ [clojure.string :as str]
[metabase.analytics-interface.core :as analytics]
[metabase.api.common :as api]
[metabase.app-db.core :as app-db]
[metabase.metabot.agent.streaming :as streaming]
[metabase.metabot.provider-util :as provider-util]
+ [metabase.metabot.schema.migrate-v1-to-v2 :as migrate]
+ [metabase.metabot.schema.v2 :as schema.v2]
[metabase.metabot.settings :as metabot.settings]
[metabase.metabot.used-tables :as used-tables]
[metabase.util :as u]
@@ -27,24 +30,72 @@
(when (map? structured)
(not-empty (select-keys structured persisted-structured-output-keys))))
-(defn strip-tool-output-bloat
- "For :tool-output parts, keep `:output` and a trimmed `:structured-output` in
- the result map. Both LLM adapters only read `(get-in part [:result :output])`
- when replaying history, so `:output` is all they need. The analytics extractor,
- however, reads a small subset of `:structured-output` off persisted messages
- (see `persisted-structured-output-keys`), so we keep those four keys and drop
- everything else (`:resources`, `:data-parts`, `:reactions`, …) — that's where
- the bulk of the bloat lives."
- [{:keys [type] :as part}]
- (if (= :tool-output type)
- (update part :result
- (fn [r]
- (cond-> (select-keys r [:output])
- (trim-structured-output (:structured-output r))
- (assoc :structured-output (trim-structured-output (:structured-output r)))
- (trim-structured-output (:structured_output r))
- (assoc :structured_output (trim-structured-output (:structured_output r))))))
- part))
+(defn- tool-result->storable-output
+ "The stored `:output` value for a v2 tool part. A map result is trimmed to
+ `:output` (the LLM-facing text adapters read on history replay) plus the
+ `persisted-structured-output-keys` subset of structured output, canonicalized
+ to `:structured_output`; everything else (`:resources`, `:data-parts`,
+ `:reactions`, …) is dropped — that's where the bulk of the bloat lives. A
+ non-map result (a tool that returned a bare string/scalar) is stored as-is —
+ the v2 part schema types `:output` as any JSON value — so readers must
+ tolerate a non-map `:output` (see [[metabase.slackbot.persistence]])."
+ [result]
+ (if (map? result)
+ (let [structured (trim-structured-output (or (:structured-output result)
+ (:structured_output result)))]
+ (cond-> (select-keys result [:output])
+ structured (assoc :structured_output structured)))
+ result))
+
+(defn- internal-parts->storable
+ "Convert internal agent-loop parts to v2 at-rest parts
+ (`:metabase.metabot.schema.v2/message-data`). `:tool-input`/`:tool-output`
+ pairs merge into a single `tool-<name>` part whose `:state` reflects the
+ outcome; the stored `:output` is the trimmed result map
+ (see [[tool-result->storable-output]])."
+ [parts]
+ (let [outputs (into {}
+ (comp (filter #(= :tool-output (:type %)))
+ (map (juxt :id identity)))
+ parts)]
+ (->> parts
+ (remove #(= :tool-output (:type %)))
+ (into []
+ (keep (fn [part]
+ (case (:type part)
+ :start {:type "step-start"}
+ :text {:type "text" :text (:text part) :state "done"}
+ :tool-input
+ (let [output (get outputs (:id part))
+ error (:error output)]
+ (cond-> {:type (str "tool-" (:function part))
+ :toolCallId (:id part)
+ :state (cond
+ (some? error) "output-error"
+ (some? output) "output-available"
+ :else "input-available")
+ :input (:arguments part)}
+ (and (some? output) (nil? error))
+ (assoc :output (tool-result->storable-output (:result output)))
+
+ (some? error)
+ (assoc :errorText (migrate/error->text error))))
+ :data {:type (str "data-" (or (:data-type part) "data"))
+ :data (:data part)}
+ (do (log/warn "Dropping internal part with no v2 storage representation"
+ {:type (:type part)})
+ nil))))))))
+
+(defn parts->storable-content
+ "Drop transient/lifecycle parts and convert what remains to the v2 at-rest
+ format. Stream metadata (`:usage`/`:finish`/`:error`) and `state` data parts
+ (salvaged separately into `MetabotConversation.state`) carry no history value."
+ [parts]
+ (->> parts
+ (remove #(#{:usage :finish :error} (:type %)))
+ (filter streaming/persistable-data-part?)
+ internal-parts->storable
+ (schema.v2/check-message-data "metabot_message.data")))
(defn extract-usage
"Extract usage from parts, taking the last `:usage` per model.
@@ -119,7 +170,7 @@
earlier turn's assistant reply when reads sort by `created_at`.
Positional args: `conversation-id`, `profile-id`, `user-message` (a single message
- map matching the existing `:data` block shape, e.g. `{:role \"user\" :content ...}`).
+ map shaped `{:role \"user\" :content ...}`; stored as a v2 text part).
Keyword args:
- `:hostname` — always-on `embedding_hostname`; recorded on conversation insert only.
@@ -182,7 +233,9 @@
(assoc :slack_thread_ts slack-thread-ts))))
(t2/insert! :model/MetabotMessage
(cond-> {:conversation_id conversation-id
- :data [user-message]
+ :data (schema.v2/check-message-data "metabot_message.data"
+ [{:type "text" :text (:content user-message)}])
+ :data_version schema.v2/current-data-version
:role :user
:profile_id profile-id
:external_id (str (random-uuid))
@@ -197,6 +250,7 @@
:model/MetabotMessage
(cond-> {:conversation_id conversation-id
:data []
+ :data_version schema.v2/current-data-version
:role :assistant
:profile_id profile-id
:external_id assistant-external-id
@@ -214,7 +268,8 @@
a state data part is present in `parts`.
`parts` should be the post-[[combine-text-parts-xf]] parts vector. Stream metadata
- (`:start`/`:usage`/`:finish`/`:error`) is filtered out before storage; usage is
+ (`:start`/`:usage`/`:finish`/`:error`) is filtered out and the rest is converted to
+ the v2 at-rest format (see [[parts->storable-content]]) before storage; usage is
accumulated separately into the `usage` column; the error part body, if any,
is captured into the `error` column via the `:error` kwarg.
@@ -235,12 +290,12 @@
(= "state" (:data-type %)))
parts)
usage (extract-usage parts)
- ;; used-table extraction needs the value before `strip-tool-output-bloat` so it can see keys that strip
- ;; discards, e.g. `:transform`
- pre-strip (->> parts
+ ;; used-table extraction needs the raw internal parts before conversion trims
+ ;; tool outputs, so it can see keys the stored format discards, e.g. `:transform`
+ kept-parts (->> parts
(remove #(#{:start :usage :finish :error} (:type %)))
(filter streaming/persistable-data-part?))
- content (mapv strip-tool-output-bloat pre-strip)]
+ content (parts->storable-content parts)]
(analytics/observe! :metabase-metabot/message-persist-bytes
{:profile-id (or profile-id "unknown")}
(u/string-byte-count (json/encode content)))
@@ -250,6 +305,7 @@
{:state (:data state-part)}))
(t2/update! :model/MetabotMessage assistant-msg-id
(cond-> {:data content
+ :data_version schema.v2/current-data-version
:usage usage
:total_tokens (->> (vals usage)
(map #(+ (:prompt %) (:completion %)))
@@ -261,7 +317,7 @@
;; Hand the (potentially slow) used-table extraction + insert off to a background worker *after* the message
;; UPDATE transaction commits, so it neither blocks nor fails the turn. The assistant row already exists, so its
;; `message_id` FK is valid even before the UPDATE completes.
- (used-tables/record-used-tables! assistant-msg-id pre-strip)))
+ (used-tables/record-used-tables! assistant-msg-id kept-parts)))
(defn set-response-slack-msg-id!
"Backfill slack_msg_id on a MetabotMessage by primary key."
@@ -272,73 +328,51 @@
;;; ---------------------------------------- Chat message conversion ----------------------------------------
(defn- convert-content-block
- "Convert a single raw content block from `:data` into a frontend `MetabotChatMessage` map.
- Returns nil for blocks that should be skipped (tool-output, unknown types).
-
- `external-id` (the parent row's `metabot_message.external_id`) is attached to
- agent text and data part chat messages as `:externalId` — the stable key for
- feedback; the per-block `:id` stays unique."
- [external-id block]
- (let [block-type (:type block)
- block-role (:role block)]
+ "Convert a single v2 part from `:data` into a frontend `MetabotChatMessage` map.
+ Returns nil for parts that should be skipped (unknown types).
+
+ `row-role` decides whether text parts render as user or agent messages — v2
+ text parts carry no role of their own. `external-id` (the parent row's
+ `metabot_message.external_id`) is attached to agent text and data part chat
+ messages as `:externalId` — the stable key for feedback; the per-block `:id`
+ stays unique."
+ [row-role external-id part]
+ (let [part-type (:type part)]
(cond
- ;; User text: {:role "user" :content "..."}
- (= "user" block-role)
- {:id (str (random-uuid)) :role "user" :type "text" :message (:content block)}
-
- ;; Assistant text (standard): {:type "text" :text "..."}
- (= "text" block-type)
- (cond-> {:id (or (:id block) (str (random-uuid)))
- :role "agent"
- :type "text"
- :message (:text block)}
- external-id (assoc :externalId external-id))
-
- ;; Assistant text (slack format): {:role "assistant" :_type "TEXT" :content "..."}
- (and (= "assistant" block-role) (= "TEXT" (:_type block)))
- (cond-> {:id (str (random-uuid))
- :role "agent"
- :type "text"
- :message (:content block)}
- external-id (assoc :externalId external-id))
-
- ;; Tool input: {:type "tool-input" :id "..." :function "..." :arguments {...}}
- (= "tool-input" block-type)
- {:id (:id block)
- :role "agent"
- :type "tool_call"
- :name (:function block)
- :args (when-let [a (:arguments block)] (json/encode a))
- :status "ended"}
-
- ;; Data part: {:type "data" :data-type "navigate_to" :version 1 :data ...}
- (= "data" block-type)
+ (= "text" part-type)
+ (if (= :user row-role)
+ {:id (str (random-uuid)) :role "user" :type "text" :message (:text part)}
+ (cond-> {:id (str (random-uuid))
+ :role "agent"
+ :type "text"
+ :message (:text part)}
+ external-id (assoc :externalId external-id)))
+
+ (schema.v2/tool-part? part)
+ (cond-> {:id (:toolCallId part)
+ :role "agent"
+ :type "tool_call"
+ :name (schema.v2/tool-part-name part)
+ :args (when-let [i (:input part)] (if (string? i) i (json/encode i)))
+ :status "ended"}
+ (= "output-available" (:state part))
+ (assoc :result (when (some? (:output part)) (json/encode (:output part))) :is_error false)
+
+ ;; errored calls store no output — :result stays nil, matching the
+ ;; pre-v2 chat shape the FE renders
+ (= "output-error" (:state part))
+ (assoc :result nil :is_error true))
+
+ (and (string? part-type) (str/starts-with? part-type "data-"))
(cond-> {:id (str (random-uuid))
:role "agent"
:type "data_part"
- :part {:type (:data-type block)
- :version (or (:version block) 1)
- :value (:data block)}}
+ :part {:type part-type
+ :data (:data part)}}
external-id (assoc :externalId external-id))
- ;; Tool output — skip here, merged via merge-tool-results
:else nil)))
-(defn- merge-tool-results
- "Merge tool-output blocks into their matching tool_call chat messages."
- [chat-messages blocks]
- (let [result-map (->> blocks
- (filter #(= "tool-output" (:type %)))
- (into {} (map (fn [o]
- [(:id o)
- {:result (when-let [r (:result o)] (json/encode r))
- :is_error (boolean (:error o))}]))))]
- (mapv (fn [msg]
- (if-let [r (and (= "tool_call" (:type msg)) (get result-map (:id msg)))]
- (merge msg r)
- msg))
- chat-messages)))
-
(defn- decode-error
"JSON-decode a row's `:error` column value (a string written by
`finalize-assistant-turn!`). Falls back to the raw value if it's not a JSON object."
@@ -395,17 +429,16 @@
[message]
(let [blocks (or (:data message) [])
external-id (:external_id message)
- chat-msgs (into [] (keep #(convert-content-block external-id %)) blocks)
- merged (merge-tool-results chat-msgs blocks)
+ chat-msgs (into [] (keep #(convert-content-block (:role message) external-id %)) blocks)
;; Absent :finished is treated as true (success); only explicit nil
;; (stale placeholder) or false (aborted) should drive the stub branch.
not-finished (and (contains? message :finished)
(not (true? (:finished message))))
with-stub (if (and (= :assistant (:role message))
- (empty? merged)
+ (empty? chat-msgs)
(or (some? (:error message)) not-finished))
[(empty-agent-placeholder message)]
- merged)]
+ chat-msgs)]
(annotate-agent-messages with-stub message)))
(defn- errored-agent-row?
diff --git a/src/metabase/metabot/schema/migrate_v1_to_v2.clj b/src/metabase/metabot/schema/migrate_v1_to_v2.clj
index 85c76895706..f5bb2e120e4 100644
--- a/src/metabase/metabot/schema/migrate_v1_to_v2.clj
+++ b/src/metabase/metabot/schema/migrate_v1_to_v2.clj
@@ -94,9 +94,6 @@
(some? output) "output-available"
:else "input-available")
:input (:arguments entry)}
- ;; errored tool-outputs do co-occur with `:result`, but only because
- ;; `persistence/strip-tool-output-bloat` rewrites the absent result of an
- ;; errored call to `{}` — there is never a real result to preserve
(and (some? output) (nil? error))
;; the result map passes through verbatim: its keys (`:output`,
;; `:structured-output`/`:structured_output`) keep their v1 spelling
diff --git a/src/metabase/metabot/schema/v2.clj b/src/metabase/metabot/schema/v2.clj
index 43efb282fba..07528db91fe 100644
--- a/src/metabase/metabot/schema/v2.clj
+++ b/src/metabase/metabot/schema/v2.clj
@@ -24,13 +24,32 @@
This is what keeps the tool-state variants mutually exclusive"
(:require
[clojure.string :as str]
+ [malli.error :as me]
+ [metabase.config.core :as config]
+ [metabase.util.log :as log]
[metabase.util.malli.registry :as mr]))
(set! *warn-on-reflection* true)
+(def current-data-version
+ "The `metabot_message.data_version` written by all current code paths: the at-rest
+ format is [[::message-data]] (v2). Bump in lockstep when a v3 format lands."
+ 2)
+
(defn- data-type? [t] (and (string? t) (str/starts-with? t "data-")))
(defn- tool-type? [t] (and (string? t) (str/starts-with? t "tool-")))
+(defn tool-part?
+ "True when `part` is a tool part — its `:type` is `\"tool-<name>\"`."
+ [part]
+ (tool-type? (:type part)))
+
+(defn tool-part-name
+ "The tool name encoded in a tool part's `:type`:
+ `{:type \"tool-create_sql_query\" ...}` -> `\"create_sql_query\"`."
+ [part]
+ (subs (:type part) (count "tool-")))
+
(mr/def ::provider-metadata
[:map-of :keyword [:map-of :keyword :any]])
@@ -407,3 +426,25 @@
"A whole `metabot_message.data` value in the v2 format: the at-rest projection of a
message's `UIMessagePart`s. Assistant placeholder rows are `[]`."
[:sequential ::ui-message-part])
+
+(defn- check
+ [schema context value]
+ (when-let [error (some-> (mr/explain schema value) me/humanize)]
+ (if (or config/is-dev? config/is-test?)
+ (throw (ex-info (str "Invalid " context) {:context context :error error :value value}))
+ (log/warn "Invalid metabot v2 payload" {:context context :error error})))
+ value)
+
+(defn check-message-data
+ "Validate an at-rest `metabot_message.data` value against `::message-data`.
+ On mismatch, throw in dev/test and log a warning in prod. Returns `value`
+ either way, so prod callers proceed with the original value."
+ [context value]
+ (check ::message-data context value))
+
+(defn check-ui-message-chunk
+ "Validate a wire-format stream event against `::ui-message-chunk`. On
+ mismatch, throw in dev/test and log a warning in prod. Returns `value`
+ either way, so prod callers proceed with the original value."
+ [context value]
+ (check ::ui-message-chunk context value))
diff --git a/src/metabase/metabot/self/claude.clj b/src/metabase/metabot/self/claude.clj
index b3a4cd8ac63..554e739ddfb 100644
--- a/src/metabase/metabot/self/claude.clj
+++ b/src/metabase/metabot/self/claude.clj
@@ -110,10 +110,10 @@
:id @message-id
:model @model-name})
true (rf)))
- ([result {t :type :keys [message content_block delta error] :as chunk}]
+ ([result {t :type :keys [message content_block delta error index] :as chunk}]
(let [block-type (when content_block
(keyword (:type content_block)))
- chunk-id (or (:id content_block) @current-id (core/mkid))]
+ chunk-id (or (:id content_block) @current-id (some-> index str) (core/mkid))]
(cond-> result
;; start of message
(= t "message_start") (-> (rf {:type :start :messageId (:id message)})
diff --git a/src/metabase/metabot/self/core.clj b/src/metabase/metabot/self/core.clj
index 8333e940dec..8a263fc00e1 100644
--- a/src/metabase/metabot/self/core.clj
+++ b/src/metabase/metabot/self/core.clj
@@ -5,6 +5,7 @@
[clojure.string :as str]
[clojure.walk :as walk]
[metabase.llm.settings :as llm]
+ [metabase.metabot.schema.v2 :as schema.v2]
[metabase.premium-features.core :as premium-features]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
@@ -216,135 +217,222 @@
[]
(aisdk-xf {:stream-text? true}))
-;;; AI SDK v4 Line Protocol Output
+;;; AI SDK SSE Output
;;
-;; Converts internal parts to the AI SDK v4 line protocol format used by the Python ai-service.
-;; Format examples:
-;; 0:"text content" - Text (just the string, JSON encoded)
-;; 2:{"type":"state",...} - Data parts
-;; 3:"error message" - Errors
-;; 9:{"toolCallId":...} - Tool calls
-;; a:{"toolCallId":...} - Tool results
-;; d:{"finishReason":...} - Finish message
-;; e:{...} - Finish step
-;; f:{"messageId":...} - Start step
-
-(defn format-text-line
- "Format text part as AI SDK line: 0:\"content\""
- [{:keys [text]}]
- (str "0:" (json/encode text)))
-
-(defn format-data-line
- "Format data part as AI SDK line: 2:{\"type\":...,\"version\":1,\"value\":...}"
- [{:keys [data-type data] :as part}]
- ;; Support both old format (data-type + data) and new format (data map with type inside)
- (let [type-str (or data-type
- (get data :type)
- "data")
- value (or data (dissoc part :type :id))]
- (str "2:" (json/encode {:type (if (keyword? type-str) (name type-str) type-str)
- :version 1
- :value value}))))
+;; Converts internal parts to the AI SDK v5+ SSE protocol: typed `UIMessageChunk`
+;; events (see `:metabase.metabot.schema.v2/ui-message-chunk`), one per
+;; `data: {json}` line, terminated by `data: [DONE]`.
+
+(defn format-sse-event
+ "Format a payload map as an SSE event line: data: {JSON}\\n. The streaming
+ writer appends another newline, forming the blank-line event boundary."
+ [payload]
+ (str "data: "
+ (json/encode (schema.v2/check-ui-message-chunk "SSE event" payload))
+ "\n"))
(defn format-error-line
- "Format error part as AI SDK line.
- Emits a JSON object when the error carries an :error-code, so clients can
- distinguish typed errors from generic ones:
- 3:{\"message\":\"...\",\"error-code\":\"...\"}
- Otherwise emits a plain string for backwards compatibility:
- 3:\"error message\""
+ "An `:error` part's AI SDK `error` SSE event line."
[{:keys [error]}]
- (let [msg (or (:message error) (str error))
- error-code (some-> (:error-code error) name)]
- (str "3:" (json/encode (if error-code
- {"message" msg "error-code" error-code}
- msg)))))
-
-(defn format-tool-call-line
- "Format tool-input part as AI SDK line: 9:{\"toolCallId\":...,\"toolName\":...,\"args\":...}"
- [{:keys [id function arguments]}]
- (str "9:" (json/encode {:toolCallId id
- :toolName function
- :args (if (string? arguments)
- arguments
- (json/encode arguments))})))
-
-(defn format-tool-result-line
- "Format tool-output part as AI SDK line: a:{\"toolCallId\":...,\"result\":...}
- Always includes :result key (AI SDK protocol requirement). When there is only
- an error and no result, :result is set to the error message string."
- [{:keys [id result error]}]
- (let [output (cond
- (string? result) result
- (map? result) (or (:output result) (json/encode result))
- ;; When result is nil but error is present, use the error message as result
- ;; so the `result` key is always present in the output.
- (nil? result) (some-> error :message str)
- :else (str result))]
- (str "a:" (json/encode (cond-> {:toolCallId id
- :result (or output "")}
- error (assoc :error (json/encode error)))))))
-
-(defn format-finish-line
- "Format finish part as AI SDK line: d:{\"finishReason\":\"stop\",\"usage\":{...}}"
- [error? usage]
- (str "d:" (json/encode {:finishReason (if error? "error" "stop") :usage (or usage {})})))
-
-(defn format-start-line
- "Format start part as AI SDK line: f:{\"messageId\":...}"
- [{:keys [id messageId]}]
- (str "f:" (json/encode {:messageId (or messageId id)})))
-
-(defn aisdk-line-xf
- "Transducer that converts internal parts to AI SDK v4 line protocol format.
- Returns strings ready to be written to the output stream (without newlines).
+ (format-sse-event {:type "error" :errorText (or (:message error) (str error))}))
+
+(defn format-error-frames
+ "SSE frames that close a stream which failed mid-flight: the `error` event, a
+ `finish` (finishReason \"error\"), and the `[DONE]` terminator — so the client
+ consumes a well-formed end instead of a truncated close."
+ [error-part]
+ (str (format-error-line error-part) "\n"
+ (format-sse-event {:type "finish" :finishReason "error"}) "\n"
+ "data: [DONE]\n\n"))
+
+(defn- ->message-metadata
+ "Translate accumulated per-model usage into the `finish` event's message
+ metadata.
+
+ Input: `{\"provider/model\" {:promptTokens N :completionTokens N}}`.
+
+ Output: `{:usage {:inputTokens N :outputTokens N :totalTokens N
+ :cacheCreationTokens N :cacheReadTokens N :cachedInputTokens N}
+ :usageByModel {\"provider/model\" {…}}}`
+
+ Returns nil if no usage was observed. The cache counts are a subset of
+ :inputTokens (`:cachedInputTokens` mirrors cache-read), 0 without provider caching."
+ [usage-by-model]
+ (when (seq usage-by-model)
+ (let [by-model (update-vals
+ usage-by-model
+ (fn [{:keys [promptTokens completionTokens
+ cacheCreationTokens cacheReadTokens]
+ :or {promptTokens 0 completionTokens 0
+ cacheCreationTokens 0 cacheReadTokens 0}}]
+ {:inputTokens promptTokens
+ :outputTokens completionTokens
+ :totalTokens (+ promptTokens completionTokens)
+ :cacheCreationTokens cacheCreationTokens
+ :cacheReadTokens cacheReadTokens
+ :cachedInputTokens cacheReadTokens}))
+ totals (reduce (partial merge-with +)
+ {:inputTokens 0 :outputTokens 0 :totalTokens 0
+ :cacheCreationTokens 0 :cacheReadTokens 0
+ :cachedInputTokens 0}
+ (vals by-model))]
+ {:usage totals
+ :usageByModel by-model})))
+
+(defn- tool-output->wire-output
+ "The `tool-output-available` event's `:output` value: the LLM-facing output
+ string. The full result map (`:resources`, `:data-parts`, …) stays off the
+ wire — anything the client renders arrives as its own `:data` part."
+ [result]
+ (cond
+ (string? result) result
+ (map? result) (or (:output result) "")
+ (nil? result) ""
+ :else (str result)))
+
+(defn parts->aisdk-sse-xf
+ "Transducer that converts internal parts to AI SDK SSE events: strings ready
+ to be written to the output stream, one `data: {json}\\n` per event, ending
+ with `data: [DONE]\\n`.
+
+ The agent loop emits `:start` per LLM call (iteration), but the protocol
+ expects a single `start` for the whole message, so subsequent `:start` parts
+ become step boundaries (`finish-step` + `start-step`).
+
+ Consecutive `:text` parts sharing an `:id` coalesce into one text block —
+ one `text-start`, many `text-delta`s, one `text-end`. Any intervening
+ non-text part (or end of stream) closes the open block first.
Options:
- :emit-usage? - When true, emit `:usage` parts as data lines (2:) in the SSE
- stream. Useful for dev/benchmarking. Default false.
- :external-id - When set, force this id into the emitted `f:` (start) line
- so the client sees the same id we persist as
- `metabot_message.external_id`.
-
- Input types and their output:
- :text -> 0:\"content\"
- :data -> 2:{\"type\":...,\"version\":1,\"value\":...}
- :error -> 3:\"message\"
- :tool-input -> 9:{\"toolCallId\":...,\"toolName\":...,\"args\":...}
- :tool-output -> a:{\"toolCallId\":...,\"result\":...}
- :start -> f:{\"messageId\":...}
- :finish -> d:{\"finishReason\":\"stop\",\"usage\":{...}}
- :usage -> 2:{\"type\":\"usage\",...} (when :emit-usage? true, else skipped)"
- ([] (aisdk-line-xf nil))
- ([{:keys [emit-usage? external-id]}]
+ :message-id - When set, force this id into the `start` event so the client
+ sees the same id we persist as `metabot_message.external_id`.
+
+ Input types and their SSE events:
+ :start (1st) -> start + start-step
+ :start (Nth) -> finish-step + start-step
+ :text -> [text-end]? [text-start]? text-delta
+ :tool-input-start -> tool-input-start
+ :tool-input -> tool-input-available
+ :tool-output -> tool-output-available | tool-output-error
+ :data -> data-<data-type>
+ :error -> [start + start-step]? error
+ :usage -> (accumulated; emitted as finish.message_metadata)
+ :finish -> (ignored — the completion arity emits the finish)
+ completion -> [text-end]? finish-step + finish + [DONE]"
+ ([] (parts->aisdk-sse-xf nil))
+ ([{:keys [message-id]}]
(fn [rf]
- (let [error? (volatile! false)
- usage (volatile! nil)]
+ (let [error? (volatile! false)
+ finish-error-code (volatile! nil)
+ started? (volatile! false)
+ usage-by-model (volatile! {})
+ ;; non-nil while a text block is open; holds the block id so we can
+ ;; emit a matching text-end when the block closes
+ current-text-id (volatile! nil)
+ close-text-block (fn [result]
+ (if-let [id @current-text-id]
+ (do (vreset! current-text-id nil)
+ (rf result (format-sse-event {:type "text-end" :id id})))
+ result))
+ ensure-started (fn [result]
+ (if @started?
+ result
+ (do (vreset! started? true)
+ (-> result
+ (rf (format-sse-event {:type "start"
+ :messageId (or message-id (mkid))}))
+ (rf (format-sse-event {:type "start-step"}))))))]
(fn
([] (rf))
([result]
- (-> result
- ;; Emit finish message with accumulated usage at the end
- (rf (format-finish-line @error? (when emit-usage? @usage)))
- (rf)))
+ (let [metadata (merge (->message-metadata @usage-by-model)
+ (when @finish-error-code {:errorCode @finish-error-code}))]
+ (-> result
+ close-text-block
+ (cond-> @started? (rf (format-sse-event {:type "finish-step"})))
+ (rf (format-sse-event
+ (cond-> {:type "finish"
+ :finishReason (if @error? "error" "stop")}
+ (seq metadata) (assoc :messageMetadata metadata))))
+ (rf "data: [DONE]\n")
+ (rf))))
([result part]
- (case (:type part)
- :text (rf result (format-text-line part))
- :data (rf result (format-data-line part))
- :error (do
- (vreset! error? true)
- (rf result (format-error-line part)))
- :tool-input (rf result (format-tool-call-line part))
- :tool-output (rf result (format-tool-result-line part))
- :start (rf result (format-start-line
- (cond-> part
- external-id (assoc :messageId external-id))))
- :finish result ;; Don't emit here, we emit in completion arity
- :usage (do
- (vreset! usage (:usage part))
- result)
- ;; Pass through unknown types as data
- (rf result (format-data-line part)))))))))
+ ;; Any non-text part implicitly closes the current text block before
+ ;; its own events are emitted; the :text branch handles its own
+ ;; closing semantics (only closes when the id changes).
+ (let [result (if (= :text (:type part))
+ result
+ (close-text-block result))]
+ (case (:type part)
+ :start
+ (if @started?
+ (-> result
+ (rf (format-sse-event {:type "finish-step"}))
+ (rf (format-sse-event {:type "start-step"})))
+ (do
+ (vreset! started? true)
+ (-> result
+ (rf (format-sse-event {:type "start"
+ :messageId (or message-id (:id part) (mkid))}))
+ (rf (format-sse-event {:type "start-step"})))))
+
+ :text
+ (let [id (or (:id part) (mkid))]
+ (if (= id @current-text-id)
+ (rf result (format-sse-event {:type "text-delta" :id id :delta (:text part)}))
+ (let [result (close-text-block result)]
+ (vreset! current-text-id id)
+ (-> result
+ (rf (format-sse-event {:type "text-start" :id id}))
+ (rf (format-sse-event {:type "text-delta" :id id :delta (:text part)}))))))
+
+ :tool-input-start
+ (rf result (format-sse-event {:type "tool-input-start"
+ :toolCallId (:id part)
+ :toolName (:function part)}))
+
+ :tool-input
+ (rf result (format-sse-event {:type "tool-input-available"
+ :toolCallId (:id part)
+ :toolName (:function part)
+ :input (:arguments part)}))
+
+ :tool-output
+ (rf result
+ (format-sse-event
+ (if-let [error (:error part)]
+ {:type "tool-output-error"
+ :toolCallId (:id part)
+ :errorText (or (:message error) (str error))}
+ {:type "tool-output-available"
+ :toolCallId (:id part)
+ :output (tool-output->wire-output (:result part))})))
+
+ :data
+ (rf result (format-sse-event {:type (str "data-" (or (:data-type part) "data"))
+ :id (or (:id part) (mkid))
+ :data (:data part)}))
+
+ :error
+ (do
+ (vreset! error? true)
+ (when-let [code (some-> (:error-code (:error part)) name)]
+ (vreset! finish-error-code code))
+ (rf (ensure-started result) (format-error-line part)))
+
+ :finish
+ result
+
+ :usage
+ ;; cumulative per-model snapshot; last-wins, emitted on finish
+ (do
+ (vswap! usage-by-model assoc (or (:model part) "unknown") (:usage part))
+ result)
+
+ ;; Unknown types: emit as data parts
+ (rf result (format-sse-event {:type (str "data-" (name (:type part)))
+ :id (or (:id part) (mkid))
+ :data part}))))))))))
;;; Tool executor
diff --git a/src/metabase/metabot/util.clj b/src/metabase/metabot/util.clj
index bb366beb49c..97f97dab092 100644
--- a/src/metabase/metabot/util.clj
+++ b/src/metabase/metabot/util.clj
@@ -1,11 +1,9 @@
(ns metabase.metabot.util
(:require
[clojure.data.xml :as xml]
- [clojure.set :as set]
[clojure.string :as str]
[clojure.walk :as walk]
- [metabase.util :as u]
- [metabase.util.json :as json]))
+ [metabase.util :as u]))
(set! *warn-on-reflection* true)
@@ -28,67 +26,6 @@
#(cond-> % (map? %) (update-keys f))
form))
-;;; AI SDK support
-
-(def type-prefix
- "AI SDK type to prefix, same names as in FE or ai-service"
- ;; NOTE: if we ever get prefix longer than 2 chars, you need to fix parsing to be more generic
- {:TEXT "0:"
- :DATA "2:"
- :ERROR "3:"
- :FINISH_MESSAGE "d:"
- :FINISH_STEP "e:"
- :START_STEP "f:"
- :TOOL_CALL "9:"
- :TOOL_RESULT "a:"})
-
-(def prefix-type "AI SDK prefix to type" (set/map-invert type-prefix))
-
-(defn parse-aisdk-line
- "Parse a single AI SDK stream line into [type content].
- Each line has a 2-char type prefix followed by a JSON payload."
- [line]
- [(get prefix-type (subs line 0 2)) (json/decode+kw (subs line 2))])
-
-(defn aisdk->messages
- "Convert AI SDK line format into an array of parsed messages."
- [role lines]
- (into [] (comp
- (map parse-aisdk-line)
- (partition-by first)
- (mapcat (fn [block]
- (let [type (ffirst block)]
- (case type
- (:TEXT
- :ERROR) [{:role role
- :_type type
- :content (transduce (map second) str block)}]
- :DATA (map #(-> (second %) (assoc :_type type)) block)
- :TOOL_CALL [{:role role
- :_type type
- :tool_calls (map (fn [[_ v]]
- {:id (:toolCallId v)
- :name (:toolName v)
- :arguments (:args v)})
- block)}]
- :TOOL_RESULT (map (fn [[_ v]]
- {:role "tool"
- :_type type
- :tool_call_id (:toolCallId v)
- :content (:result v)})
- block)
- :FINISH_MESSAGE (map (fn [[_ v]]
- {:role role
- :_type type
- :finish_reason (:finishReason v)
- :usage (-> (:usage v)
- (dissoc :promptTokens :completionTokens))})
- block)
- ;; START_STEP and FINISH_STEP are metadata, not stored as messages
- (:START_STEP
- :FINISH_STEP) [])))))
- lines))
-
(defn xml
"Format hiccup-like data structure to an XML string"
[& bits]
diff --git a/src/metabase/slackbot/persistence.clj b/src/metabase/slackbot/persistence.clj
index 050ed45b7a3..85d96cdeff0 100644
--- a/src/metabase/slackbot/persistence.clj
+++ b/src/metabase/slackbot/persistence.clj
@@ -1,54 +1,50 @@
(ns metabase.slackbot.persistence
"Slack-specific persistence: reconstruct conversation history from stored messages."
(:require
+ [metabase.metabot.schema.v2 :as schema.v2]
+ [metabase.util.json :as json]
[toucan2.core :as t2]))
(set! *warn-on-reflection* true)
-(def ^:private legacy-history-block-types
- "Legacy AI-SDK-message `:_type` values that carry tool history."
- #{"TOOL_CALL" "TOOL_RESULT"})
-
-(defn- legacy-block->history-message
- "Legacy AI-SDK-message block: keep history-relevant fields, keywordize role."
- [block]
- (-> (select-keys block [:role :content :tool_calls :tool_call_id])
- (update :role keyword)))
-
-(defn- native-tool-input->history-message
- "Native `tool-input` block → AI-SDK-message with a single `:tool_calls` entry."
- [block]
- {:role :assistant
- :tool_calls [{:id (:id block)
- :name (:function block)
- :arguments (:arguments block)}]})
-
-(defn- native-tool-output->history-message
- "Native `tool-output` block → AI-SDK `tool` message. Only the `:output`
- string is needed for history replay."
- [block]
- {:role :tool
- :tool_call_id (:id block)
- :content (get-in block [:result :output])})
-
-(defn- block->history-message
- "Dispatch a single stored `:data` block to an AI-SDK-message map, or nil to
- skip. Handles both legacy slackbot blocks (`:_type \"TOOL_CALL\"` /
- `\"TOOL_RESULT\"`) and native agent-loop blocks (`:type \"tool-input\"` /
- `\"tool-output\"`). Text blocks are skipped — assistant text still comes
- from Slack's copy of the thread."
- [block]
- (cond
- (legacy-history-block-types (:_type block)) (legacy-block->history-message block)
- (= "tool-input" (:type block)) (native-tool-input->history-message block)
- (= "tool-output" (:type block)) (native-tool-output->history-message block)
- :else nil))
+(defn- tool-part->history-messages
+ "A stored v2 tool part (`:type \"tool-<name>\"`, merged input/output) →
+ AI-SDK-message pair: an assistant message with a single `:tool_calls` entry,
+ plus a `tool` message when the call resolved. Parts still in
+ `input-available` state (tool never finished) are skipped — there is no
+ result to replay. Text and data parts are skipped — assistant text still
+ comes from Slack's copy of the thread."
+ [part]
+ (when (and (schema.v2/tool-part? part)
+ (not= "input-available" (:state part)))
+ (let [input (:input part)
+ state (:state part)
+ tool-call {:role :assistant
+ :tool_calls [{:id (:toolCallId part)
+ :name (schema.v2/tool-part-name part)
+ :arguments (if (string? input)
+ input
+ (json/encode (or input {})))}]}
+ tool-result (when (#{"output-available" "output-error"} state)
+ {:role :tool
+ :tool_call_id (:toolCallId part)
+ :content (if (= "output-error" state)
+ (or (:errorText part) "Tool execution failed")
+ ;; a map result stores the LLM text under
+ ;; `[:output :output]`; a non-map result (a
+ ;; bare string/scalar) is stored flat under `:output`
+ (let [output (:output part)]
+ (or (if (map? output) (:output output) output) "")))})]
+ (cond-> [tool-call]
+ tool-result (conj tool-result)))))
(defn- extract-history-messages
"Walk `(:data message)` in insertion order and emit AI-SDK-message maps for
history replay. Preserves adjacency of tool calls and their tool results."
[message]
- (into [] (keep block->history-message) (:data message)))
+ (->> (or (:data message) [])
+ (schema.v2/check-message-data "slack history replay metabot_message.data")
+ (into [] (mapcat tool-part->history-messages))))
(defn message-history
"Tool call history for Slack messages. Returns {slack-msg-id -> [messages...]}."
diff --git a/src/metabase/testing_api/api.clj b/src/metabase/testing_api/api.clj
index 7a04901d902..15ce59b5f71 100644
--- a/src/metabase/testing_api/api.clj
+++ b/src/metabase/testing_api/api.clj
@@ -350,6 +350,7 @@
:role role
:profile_id profile-id
:data []
+ :data_version 2
:total_tokens 0
:created_at created-at}))
(t2/insert! :model/AiUsageLog
diff --git a/test/metabase/metabot/agent/streaming_test.clj b/test/metabase/metabot/agent/streaming_test.clj
index d954155b676..24ca4b6931e 100644
--- a/test/metabase/metabot/agent/streaming_test.clj
+++ b/test/metabase/metabot/agent/streaming_test.clj
@@ -77,7 +77,6 @@
part (streaming/todo-list-part todos)]
(is (= :data (:type part)))
(is (= "todo_list" (:data-type part)))
- (is (= 1 (:version part)))
(is (= todos (:data part)))))
(testing "handles empty todo list"
(let [part (streaming/todo-list-part [])]
@@ -93,7 +92,6 @@
part (streaming/code-edit-part edit-data)]
(is (= :data (:type part)))
(is (= "code_edit" (:data-type part)))
- (is (= 1 (:version part)))
(is (= edit-data (:data part)))))
(testing "handles complex edit data"
(let [edit-data {:buffer_id "buf-1"
@@ -112,7 +110,6 @@
part (streaming/transform-suggestion-part suggestion)]
(is (= :data (:type part)))
(is (= "transform_suggestion" (:data-type part)))
- (is (= 1 (:version part)))
(is (= suggestion (:data part)))))
(testing "handles Python transform suggestion"
(let [suggestion {:id 2
@@ -131,7 +128,6 @@
part (streaming/adhoc-viz-part value)]
(is (= :data (:type part)))
(is (= "adhoc_viz" (:data-type part)))
- (is (= 1 (:version part)))
(is (= value (:data part)))))
(testing "handles minimal value without title/display"
(let [value {:query {:database 1} :link "/question#xyz"}
@@ -146,7 +142,6 @@
part (streaming/static-viz-part value)]
(is (= :data (:type part)))
(is (= "static_viz" (:data-type part)))
- (is (= 1 (:version part)))
(is (= value (:data part))))))
(deftest viz-part-test
@@ -160,7 +155,6 @@
:link "/question#abc"})]
(is (= :data (:type part)))
(is (= "generated_entity" (:data-type part)))
- (is (= 1 (:version part)))
(is (= {:type "card"
:id "c-1"
:query {:id "q-1" :query {:database 1}}
diff --git a/test/metabase/metabot/aisdkstream1.txt b/test/metabase/metabot/aisdkstream1.txt
deleted file mode 100644
index 112357c2bd6..00000000000
--- a/test/metabase/metabot/aisdkstream1.txt
+++ /dev/null
@@ -1,58 +0,0 @@
-9:{"toolCallId":"call_hZ6Q9AwsxqXaL0lbtpoAEdBi","toolName":"construct_notebook_query","args":"{\"query_plan\":{\"analysis\":\"The user wants to know the number of orders each user has made. The best source for this data is the 'Orders + People' model, which contains the 'User ID' field and the unique 'ID' for each order. We can count the number of orders grouped by 'User ID'.\",\"approach\":\"Use the 'Orders + People' model. Group by 'User ID' and count the distinct 'ID' (order ID) to get the number of orders per user.\"},\"source\":{\"model_id\":1},\"instructions\":\"Show the count of orders (using the unique order ID) grouped by each user ID from the 'Orders + People' model.\"}"}
-a:{"toolCallId":"call_hZ6Q9AwsxqXaL0lbtpoAEdBi","result":"\n<result>\nThe query has been successfully constructed.\n\n<query type=\"notebook\" id=\"ajjvXmzxvwACMrT7t9pHK\" database_id=\"1\">\n{\"aggregation\": [[\"count\"]], \"breakout\": [[\"field\", \"USER_ID\", {\"base-type\": \"type/Integer\"}]], \"source-table\": \"card__1\"}\n\n\n<query_results>\n\n### Result Columns\n| Field Name | Type | Description |\n| ---------- | ---- | ----------- |\n| User ID | number | The id of the user who made this order. Note that in some cases where an order was created on behalf of a customer who phoned the order in, this might be the employee who handled the request. |\n| Count | number | None |\n\n\n</query_results>\n\n</query>\n</result>\n<instructions>\n\nNote that you cannot view the results directly yourself.\nConsider how best to present the query results to the user:\n- If they want to see raw tabular data, navigate them to the query results using query ID: ajjvXmzxvwACMrT7t9pHK\n- If they want a visualization, create a chart or graph using the query data\n- You can reference the query naturally in your response using this markdown format: `[Link text](metabase://query/ajjvXmzxvwACMrT7t9pHK)`\n\nPresent the results in a way that matches the user's intent and context.\n</instructions>"}
-0:"I"
-0:" created"
-0:" a"
-0:" query"
-0:" that"
-0:" shows"
-0:" how"
-0:" many"
-0:" orders"
-0:" each"
-0:" user"
-0:" has"
-0:" made"
-0:","
-0:" grouped"
-0:" by"
-0:" their"
-0:" User"
-0:" ID"
-0:"."
-0:" You"
-0:" can"
-0:" check"
-0:" the"
-0:" detailed"
-0:" results"
-0:" here"
-0:":"
-0:" "
-0:"[Orders Count by User](/question#eyJkYXRhc2V0X3F1ZXJ5IjogeyJkYXRhYmFzZSI6IDEsICJ0eXBlIjogInF1ZXJ5IiwgInF1ZXJ5IjogeyJhZ2dyZWdhdGlvbiI6IFtbImNvdW50Il1dLCAiYnJlYWtvdXQiOiBbWyJmaWVsZCIsICJVU0VSX0lEIiwgeyJiYXNlLXR5cGUiOiAidHlwZS9JbnRlZ2VyIn1dXSwgInNvdXJjZS10YWJsZSI6ICJjYXJkX18xIn19LCAiZGlzcGxheSI6ICJ0YWJsZSIsICJkaXNwbGF5SXNMb2NrZWQiOiB0cnVlLCAidmlzdWFsaXphdGlvbl9zZXR0aW5ncyI6IHt9fQ==).\n\n"
-0:"If"
-0:" you'd"
-0:" like"
-0:","
-0:" I"
-0:" can"
-0:" also"
-0:" help"
-0:" you"
-0:" visualize"
-0:" this"
-0:" data"
-0:" or"
-0:" provide"
-0:" it"
-0:" in"
-0:" another"
-0:" format"
-0:"."
-0:" Just"
-0:" let"
-0:" me"
-0:" know"
-0:"!"
-2:{"type":"state","version":1,"value":{"queries":{"e897317e-4da0-46cb-827b-59ca52ee2ef2":{"database":1,"type":"query","query":{"source_table":"card__1"}},"ajjvXmzxvwACMrT7t9pHK":{"database":1,"type":"query","query":{"aggregation":[["count"]],"breakout":[["field","USER_ID",{"base-type":"type/Integer"}]],"source-table":"card__1"}}},"charts":{"99994d2e-e38f-4ec3-a582-1068b796e807":{"chart_id":"99994d2e-e38f-4ec3-a582-1068b796e807","queries":[{"query_id":"e897317e-4da0-46cb-827b-59ca52ee2ef2","query":{"database":1,"type":"query","query":{"source_table":"card__1"}},"result":null}],"visualization_settings":{"chart_type":"table"},"image_base_64":null,"timeline_events":[]}}}}
-d:{"finishReason":"stop","usage":{"openai/gpt-4.1-mini-2025-04-14":{"prompt":22829,"completion":231},"openai/gpt-4o-mini-2024-07-18":{"prompt":7975,"completion":142},"promptTokens":30804,"completionTokens":373}}
diff --git a/test/metabase/metabot/aisdkstream2.txt b/test/metabase/metabot/aisdkstream2.txt
deleted file mode 100644
index b63ace8828e..00000000000
--- a/test/metabase/metabot/aisdkstream2.txt
+++ /dev/null
@@ -1,49 +0,0 @@
-9:{"toolCallId":"call_sJpEjRXSqi5vSjAOJXFdJhjt","toolName":"create_chart","args":"{\"data_source\":{\"query_id\":\"ajjvXmzxvwACMrT7t9pHK\"},\"viz_settings\":{\"chart_type\":\"bar\"}}"}
-a:{"toolCallId":"call_sJpEjRXSqi5vSjAOJXFdJhjt","result":"\n<result>\nA new chart has been created with the ID 64e67fbd-7858-4765-b58f-8d8378732600\n</result>\n<instructions>\nThe chart is not visible to you or the user yet.\nTo show the chart to the user, use your navigation tool (chart ID: 64e67fbd-7858-4765-b58f-8d8378732600).\nReference it in your response using: `[Link text](metabase://chart/64e67fbd-7858-4765-b58f-8d8378732600)`\n</instructions>"}
-9:{"toolCallId":"call_oPCH01Dt3jlFAwRuemtq9fTt","toolName":"navigate_user","args":"{\"destination\":{\"chart_id\":\"64e67fbd-7858-4765-b58f-8d8378732600\"}}"}
-2:{"type":"navigate_to","version":1,"value":"/question#eyJkYXRhc2V0X3F1ZXJ5IjogeyJkYXRhYmFzZSI6IDEsICJ0eXBlIjogInF1ZXJ5IiwgInF1ZXJ5IjogeyJhZ2dyZWdhdGlvbiI6IFtbImNvdW50Il1dLCAiYnJlYWtvdXQiOiBbWyJmaWVsZCIsICJVU0VSX0lEIiwgeyJiYXNlX3R5cGUiOiAidHlwZS9JbnRlZ2VyIn1dXSwgInNvdXJjZV90YWJsZSI6ICJjYXJkX18xIn19LCAiZGlzcGxheSI6ICJiYXIiLCAiZGlzcGxheUlzTG9ja2VkIjogdHJ1ZSwgInZpc3VhbGl6YXRpb25fc2V0dGluZ3MiOiB7fX0="}
-a:{"toolCallId":"call_oPCH01Dt3jlFAwRuemtq9fTt","result":"The user now can see the result of the chart with ID 64e67fbd-7858-4765-b58f-8d8378732600"}
-0:"I"
-0:" created"
-0:" a"
-0:" bar"
-0:" chart"
-0:" that"
-0:" shows"
-0:" how"
-0:" many"
-0:" orders"
-0:" each"
-0:" user"
-0:" has"
-0:" made"
-0:"."
-0:" You"
-0:" can"
-0:" view"
-0:" it"
-0:" here"
-0:":"
-0:" "
-0:"[Orders per User Bar Chart](/question#eyJkYXRhc2V0X3F1ZXJ5IjogeyJkYXRhYmFzZSI6IDEsICJ0eXBlIjogInF1ZXJ5IiwgInF1ZXJ5IjogeyJhZ2dyZWdhdGlvbiI6IFtbImNvdW50Il1dLCAiYnJlYWtvdXQiOiBbWyJmaWVsZCIsICJVU0VSX0lEIiwgeyJiYXNlX3R5cGUiOiAidHlwZS9JbnRlZ2VyIn1dXSwgInNvdXJjZV90YWJsZSI6ICJjYXJkX18xIn19LCAiZGlzcGxheSI6ICJiYXIiLCAiZGlzcGxheUlzTG9ja2VkIjogdHJ1ZSwgInZpc3VhbGl6YXRpb25fc2V0dGluZ3MiOiB7fX0=)."
-0:" Let"
-0:" me"
-0:" know"
-0:" if"
-0:" you'd"
-0:" like"
-0:" it"
-0:" in"
-0:" another"
-0:" format"
-0:" or"
-0:" if"
-0:" you"
-0:" want"
-0:" to"
-0:" explore"
-0:" more"
-0:" details"
-0:"!"
-2:{"type":"state","version":1,"value":{"queries":{"e897317e_4da0_46cb_827b_59ca52ee2ef2":{"database":1,"type":"query","query":{"source_table":"card__1"}},"ajjvXmzxvwACMrT7t9pHK":{"database":1,"type":"query","query":{"aggregation":[["count"]],"breakout":[["field","USER_ID",{"base_type":"type/Integer"}]],"source_table":"card__1"}},"e897317e-4da0-46cb-827b-59ca52ee2ef2":{"database":1,"type":"query","query":{"source_table":"card__1"}},"16098fb6-3a51-4ac6-bf72-062b386d871b":{"database":1,"type":"query","query":{"source_table":"card__1"}}},"charts":{"99994d2e-e38f-4ec3-a582-1068b796e807":{"chart_id":"99994d2e-e38f-4ec3-a582-1068b796e807","queries":[{"query_id":"e897317e-4da0-46cb-827b-59ca52ee2ef2","query":{"database":1,"type":"query","query":{"source_table":"card__1"}},"result":null}],"visualization_settings":{"chart_type":"table"},"image_base_64":null,"timeline_events":[]},"24172f7e-c5f8-433d-850b-f6be68c0c3ac":{"chart_id":"24172f7e-c5f8-433d-850b-f6be68c0c3ac","queries":[{"query_id":"16098fb6-3a51-4ac6-bf72-062b386d871b","query":{"database":1,"type":"query","query":{"source_table":"card__1"}},"result":null}],"visualization_settings":{"chart_type":"table"},"image_base_64":null,"timeline_events":[]},"64e67fbd-7858-4765-b58f-8d8378732600":{"chart_id":"64e67fbd-7858-4765-b58f-8d8378732600","queries":[{"query_id":"ajjvXmzxvwACMrT7t9pHK","query":{"database":1,"type":"query","query":{"aggregation":[["count"]],"breakout":[["field","USER_ID",{"base_type":"type/Integer"}]],"source_table":"card__1"}},"result":null}],"visualization_settings":{"chart_type":"bar"},"image_base_64":null,"timeline_events":null}}}}
-d:{"finishReason":"stop","usage":{"openai/gpt-4.1-mini-2025-04-14":{"prompt":36300,"completion":151},"promptTokens":36300,"completionTokens":151}}
diff --git a/test/metabase/metabot/aisdkstream3.txt b/test/metabase/metabot/aisdkstream3.txt
deleted file mode 100644
index ce34a12d97e..00000000000
--- a/test/metabase/metabot/aisdkstream3.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-3:"litellm.ServiceUnavailableError: litellm.MidStreamFallbackError: litellm.APIConnectionError: APIConnectionError: OpenrouterException - Connection closed. Original exception: APIConnectionError: litellm.APIConnectionError: APIConnectionError: OpenrouterException - Connection closed."
-d:{"finishReason":"error","usage":{"promptTokens":0,"completionTokens":0}}
diff --git a/test/metabase/metabot/aisdkstream4.txt b/test/metabase/metabot/aisdkstream4.txt
deleted file mode 100644
index 0ccd3af818b..00000000000
--- a/test/metabase/metabot/aisdkstream4.txt
+++ /dev/null
@@ -1,14 +0,0 @@
-0:"I'll help you show"
-0:" trends in user"
-0:" monthly spending by user ID. Let me"
-0:" first"
-0:" explore"
-0:" the available data to fin"
-0:"d the"
-0:" right"
-0:" source"
-0:"."
-9:{"toolCallId":"toolu_bdrk_01GPPfFCzqWpZhsDdGMi9mYi","toolName":"search","args":"{\"semantic_queries\": [\"user spending trends over time\",\"monthly spending by user\"], \"keyword_queries\": [\"spending\",\"user\",\"monthly\",\"trend\"], \"entity_types\": [\"metric\",\"model\",\"table\"]}"}
-a:{"toolCallId":"toolu_bdrk_01GPPfFCzqWpZhsDdGMi9mYi","result":"\n<result>\nHere are the search results:\n<search-results>\n<metric id=\"77\", name=\"User monthly spending trend\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nMonthly spending patterns by user showing total spend, transaction count, and spending tier. Enables identification of high spenders and spending behavior changes.\n\n</metric>\n \n\n<metric id=\"78\", name=\"Category spending concentration\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nSpending distribution across expense categories with transaction volumes and unique merchant counts. Identifies major cost drivers and category trends.\n\n</metric>\n \n\n<metric id=\"82\", name=\"User activity level distribution\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nDistribution of users by activity level with lifetime spend metrics. Identifies active vs inactive cardholders and engagement patterns.\n\n</metric>\n \n\n<metric id=\"79\", name=\"Department budget performance\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nDepartment-level spending aggregated monthly with approved and pending expense breakdowns. Critical for budget tracking and departmental cost management.\n\n</metric>\n \n\n<metric id=\"81\", name=\"Transaction size distribution\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nDistribution of transactions by size category (Small/Medium/Large) with merchant and category breakdowns. Helps understand spending patterns.\n\n</metric>\n \n\n<metric id=\"87\", name=\"User cards per transaction ratio\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nAverage number of different cards used per user per month. Indicates card usage diversity and potential card consolidation opportunities.\n\n</metric>\n \n\n<metric id=\"89\", name=\"Department user spending intensity\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nAverage spending per active user within each department. Identifies departments with high per-capita spending.\n\n</metric>\n \n\n<metric id=\"85\", name=\"Average transaction value by category\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nAverage transaction amount by spending category using pre-aggregated monthly data. Identifies typical spending levels per category.\n\n</metric>\n \n\n<metric id=\"97\", name=\"Weekly scheduling capacity trend\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nTrend analysis of weekly scheduling capacity utilization. Shows how users are ramping up or down their availability.\n\n</metric>\n \n\n<table id=\"151\", name=\"int_brex_spending_by_department_facts\" database_id=\"2\" database_engine=\"unknown\" fully_qualified_name=\"brex_enriched.int_brex_spending_by_department_facts\">\n\n### Description\nNone\n\n\n\nBefore using any field in comparisons, filters, or conditions, you should check their metadata and sample values first, using the uri `metabase://table/{table.id}/fields/{field_id}`.\nThis will help avoid errors due to unexpected data types or formats.\n</table>\n\n<metric id=\"93\", name=\"User expense reporting rate\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nNumber of expense reports submitted per user per month. Measures expense reporting activity and compliance.\n\n</metric>\n \n\n<metric id=\"95\", name=\"Category user adoption rate\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nNumber of unique users making purchases in each spending category. Indicates category usage breadth across the organization.\n\n</metric>\n \n\n<metric id=\"88\", name=\"Category merchant diversity\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nNumber of unique merchants per spending category. Measures vendor diversity and potential for vendor consolidation.\n\n</metric>\n \n\n<metric id=\"99\", name=\"Multi event user percentage\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nPercentage of users hosting multiple event types. Shows product stickiness and feature adoption.\n\n</metric>\n \n\n<metric id=\"86\", name=\"Quarterly transaction volume\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nTotal number of transactions per quarter. Tracks overall company transaction activity trends.\n\n</metric>\n \n\n<metric id=\"76\", name=\"Card Utilization Rate\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nPercentage of credit limit being utilized across active cards. Measures credit usage efficiency and potential need for limit adjustments.\n\n</metric>\n \n\n<metric id=\"80\", name=\"Expense approval cycle time\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nAverage days from expense submission to approval by department. Measures approval workflow efficiency and identifies bottlenecks.\n\n</metric>\n \n\n<metric id=\"113\", name=\"Click count\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nNumber of email link clicks. Tracks engagement trends and identifies high-activity periods for email campaigns.\n\n</metric>\n \n\n<metric id=\"92\", name=\"Card transaction volume per card\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nAverage number of transactions per card using pre-aggregated card data. Identifies heavily used vs underutilized cards.\n\n</metric>\n \n\n<metric id=\"116\", name=\"Open to click conversion rate\" is_verified=\"False\">\n\nThe metric is stored in the following collection:\n<collection name=\"Metrics\">\n</collection>\n\n\n### Metric Description\nPercentage of opened emails that resulted in at least one click. Measures content relevance for engaged users.\n\n</metric>\n </search-results>\n</result>\n<instructions>\nSearch results ranked by relevance (highest first).\n\nWhen using results:\n- Prioritize verified dashboards, questions, and metrics over raw data exploration when they match the user's request\n- Check available fields or metadata of promising results before recommending them to ensure they contain needed data\n- **Be proactive with clear matches**: When the top result(s) have names or descriptions that clearly match the user's request, use them to achieve the users goal first, then list alternatives at the end. Don't ask for confirmation when there's an obvious best choice.\n- **Collection metadata**: Results may include collection information (name, description, authority level) that can help you determine whether the result is relevant to the user's query. Use this context to assess relevance.\n- **Present options for ambiguous results**: When search returns multiple plausible options without a clear best match, present them and ask the user to choose\n- Reference results using the metabase protocol link format: [display name](metabase://type/id)\n\nExamples: [Customer Metrics](metabase://metric/42), [Sales Dashboard](metabase://dashboard/158)\n\n</instructions>"}
-3:"litellm.Timeout: Timeout Error: OpenrouterException - litellm.Timeout: Connection timed out. Timeout passed=600.0, time taken=600.018 seconds"
-d:{"finishReason":"error","usage":{"openrouter/anthropic/claude-haiku-4.5":{"prompt":10458,"completion":117,"costs":0.011043},"promptTokens":10458,"completionTokens":117}}
diff --git a/test/metabase/metabot/api/conversations_test.clj b/test/metabase/metabot/api/conversations_test.clj
index 6f96670e4dc..21949f15f5d 100644
--- a/test/metabase/metabot/api/conversations_test.clj
+++ b/test/metabase/metabot/api/conversations_test.clj
@@ -33,7 +33,7 @@
(mt/with-temp [:model/MetabotConversation {convo-id :id} {:user_id user-id :summary "legacy"}
;; No :user_id on the message — simulates a legacy row from before user_id stamping.
:model/MetabotMessage _ {:conversation_id convo-id
- :data [{:role "assistant" :content "hello from before user_id stamping"}]}]
+ :data [{:type "text" :text "hello from before user_id stamping"}]}]
(let [ids (set (map :conversation_id
(:data (mt/user-http-request :rasta :get 200 "metabot/conversations"))))]
(is (contains? ids convo-id)))))))
@@ -72,7 +72,7 @@
:model/MetabotMessage _ {:conversation_id convo-id
:user_id user-id
:role "user"
- :data [{:role "user" :content "hello"}]}]
+ :data [{:type "text" :text "hello"}]}]
(let [response (mt/user-http-request :rasta :get 200
(str "metabot/conversations/" convo-id))]
(is (= convo-id (:conversation_id response)))
diff --git a/test/metabase/metabot/api_test.clj b/test/metabase/metabot/api_test.clj
index 85dcdd5ba75..410743f1264 100644
--- a/test/metabase/metabot/api_test.clj
+++ b/test/metabase/metabot/api_test.clj
@@ -5,7 +5,6 @@
[clojure.string :as str]
[clojure.test :refer :all]
[compojure.response]
- [medley.core :as m]
[metabase.api.common :as mb.api]
[metabase.config.core :as config]
[metabase.lib.convert :as lib.convert]
@@ -58,32 +57,35 @@
:conversation_id conversation-id
:history [historical-message]
:state {}})
- lines (str/split-lines response)
+ lines (->> (str/split-lines response)
+ (filter #(str/starts-with? % "data: ")))
+ events (->> lines
+ (remove #(= "data: [DONE]" %))
+ (mapv #(json/decode+kw (subs % 6))))
conv (t2/select-one :model/MetabotConversation :id conversation-id)
messages (t2/select :model/MetabotMessage :conversation_id conversation-id)]
- ;; Native agent emits AI SDK v4 line protocol directly
- (testing "response contains expected line types"
- ;; f:{start}, 0:"text" chunks, 2:{state data}, d:{finish with usage}
- (is (=? [#"f:.*"
- #"0:.*"
- #"2:.*"
- #"d:.*"]
- (m/distinct-by #(subs % 0 2) lines)))
- ;; Text chunks reassemble to full message
- (let [text-lines (filter #(str/starts-with? % "0:") lines)]
+ (testing "response is an SSE stream of typed events ending with [DONE]"
+ (is (= "data: [DONE]" (last lines)))
+ (is (= ["start" "start-step"] (mapv :type (take 2 events))))
+ (is (= ["finish-step" "finish"] (mapv :type (take-last 2 events))))
+ (let [text-deltas (filter #(= "text-delta" (:type %)) events)]
(is (= "Hello from native agent!"
- (apply str (map #(json/decode (subs % 2)) text-lines)))))
- ;; Finish line includes usage
- (is (str/includes? (last lines) "promptTokens")))
+ (apply str (map :delta text-deltas)))))
+ (is (=? {:messageMetadata {:usage {:inputTokens 10 :outputTokens 5 :totalTokens 15}}}
+ (last events))
+ "finish event carries accumulated usage"))
(is (=? {:user_id (mt/user->id :rasta)}
conv))
- ;; Native agent stores parts in raw format
+ ;; Native agent stores parts in the v2 at-rest format
(is (=? [{:total_tokens 0
:role :user
- :data [{:role "user" :content (:content question)}]}
+ :data [{:type "text" :text (:content question)}]
+ :data_version 2}
{:total_tokens pos-int?
:role :assistant
- :data [{:type "text" :text "Hello from native agent!"}]}]
+ :data [{:type "step-start"}
+ {:type "text" :text "Hello from native agent!" :state "done"}]
+ :data_version 2}]
messages)))))))))))
(defn ^:private sse-event
@@ -98,7 +100,7 @@
;; streams text-delta events. The Metabase server connects to it via the
;; full native-agent pipeline:
;; openrouter-raw → sse-reducible → openrouter->aisdk-chunks-xf → tool-executor-xf
- ;; → lite-aisdk-xf → agent loop → aisdk-line-xf → streaming-writer-rf → client
+ ;; → lite-aisdk-xf → agent loop → parts->aisdk-sse-xf → streaming-writer-rf → client
;; The test client reads one byte and closes. The streaming-writer-rf's poll
;; of `canceled-chan` flips the `canceled?` volatile and returns `reduced`,
;; the agent loop unwinds, and `finalize-assistant-turn!` is called from the
@@ -253,9 +255,14 @@
:data {:status 503 :provider :test}}
(:error @stored-kwargs))
"the throwable becomes a structured error payload")
- (testing "the failure is streamed to the client as an AI SDK error part (3:...) rather than a silent close"
- (is (some #(str/starts-with? % "3:") (str/split-lines response)))
- (is (re-find #"(?i)agent setup exploded" response)))))))))))
+ (testing "the failure is streamed to the client as a well-formed AI SDK error tail rather than a silent close"
+ (is (some #(str/includes? % "\"type\":\"error\"")
+ (str/split-lines response)))
+ (is (re-find #"(?i)agent setup exploded" response))
+ (is (str/includes? response "\"finishReason\":\"error\"")
+ "the errored stream is closed with a finish event")
+ (is (str/includes? response "data: [DONE]")
+ "the stream terminates with [DONE]"))))))))))
(deftest settings-get-returns-live-models-test
(mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider "anthropic/claude-haiku-4-5"
@@ -737,7 +744,8 @@
:profile_id "gpt-x"
:external_id external-id
:total_tokens 5
- :data [{:type "text" :text "hi"}]}))]
+ :data [{:type "text" :text "hi"}]
+ :data_version 2}))]
(is (nil? (mt/user-http-request :rasta :post 204 "metabot/source-feedback"
{:metabot_id 1
:message_id external-id
@@ -923,8 +931,10 @@
{:type :finish}])
(let [msg (t2/select-one :model/MetabotMessage assistant-msg-id)
conv (t2/select-one :model/MetabotConversation :id conv-id)
- data-types (into #{} (keep :data-type) (:data msg))
- part-types (into #{} (map :type) (:data msg))]
+ part-types (into #{} (map :type) (:data msg))
+ data-types (into #{}
+ (keep #(when (str/starts-with? % "data-") (subs % 5)))
+ part-types)]
(is (= #{"navigate_to" "todo_list" "code_edit" "transform_suggestion" "adhoc_viz" "static_viz"}
data-types)
"all persistable data parts (not state) should be in :data")
@@ -938,57 +948,76 @@
(t2/delete! :model/MetabotMessage :conversation_id conv-id)
(t2/delete! :model/MetabotConversation :id conv-id)))))))
-(deftest strip-tool-output-bloat-test
- (testing "drops transient keys and structured-output fields outside the persisted subset"
- (is (= {:type :tool-output :id "call-1" :result {:output "<result>XML</result>"}}
- (metabot.persistence/strip-tool-output-bloat
- {:type :tool-output
- :id "call-1"
- :result {:output "<result>XML</result>"
- :resources [{:id 1 :name "Orders" :columns [{:field_values [1 2 3]}]}]
- :structured-output {:result-type :search :data [{:id 1}]}
- :data-parts [{:type :data :data-type "navigate_to"}]}}))))
- (testing "keeps the query-related subset of :structured-output for analytics extraction"
+(deftest parts->storable-content-tool-output-trimming-test
+ (testing "drops transient result keys and structured-output fields outside the persisted subset"
+ (is (= [{:type "tool-search"
+ :toolCallId "call-1"
+ :state "output-available"
+ :input {:q "x"}
+ :output {:output "<result>XML</result>"}}]
+ (metabot.persistence/parts->storable-content
+ [{:type :tool-input :id "call-1" :function "search" :arguments {:q "x"}}
+ {:type :tool-output
+ :id "call-1"
+ :result {:output "<result>XML</result>"
+ :resources [{:id 1 :name "Orders" :columns [{:field_values [1 2 3]}]}]
+ :structured-output {:result-type :search :data [{:id 1}]}
+ :data-parts [{:type :data :data-type "navigate_to"}]}}])))))
+
+(deftest parts->storable-content-structured-output-subset-test
+ (testing "keeps the query-related subset of structured output, canonicalized to :structured_output"
(let [query-map {:database 1 :type :native :native {:query "SELECT 1"}}]
- (is (= {:type :tool-output
- :id "call-sql"
+ (is (= [{:type "tool-create_sql_query"
+ :toolCallId "call-sql"
+ :state "output-available"
+ :input {}
+ :output {:output "<result>...</result>"
+ :structured_output {:query-id "qid-1"
+ :query-content "SELECT 1"
+ :query query-map
+ :database 1}}}]
+ (metabot.persistence/parts->storable-content
+ [{:type :tool-input :id "call-sql" :function "create_sql_query" :arguments {}}
+ {:type :tool-output
+ :id "call-sql"
+ :result {:output "<result>...</result>"
+ :structured-output {:query-id "qid-1"
+ :query-content "SELECT 1"
+ :query query-map
+ :database 1
+ :resources [{:field_values [1 2 3]}]
+ :reactions [:noop]}
+ :data-parts [{:type :data}]}}]))))))
+
+(deftest parts->storable-content-snake-alias-test
+ (testing "reads the snake-case :structured_output alias when present"
+ (is (= [{:type "tool-search"
+ :toolCallId "call-snake"
+ :state "output-available"
+ :input {}
+ :output {:output "<result>...</result>"
+ :structured_output {:query-id "qid-2" :query-content "SELECT 2"}}}]
+ (metabot.persistence/parts->storable-content
+ [{:type :tool-input :id "call-snake" :function "search" :arguments {}}
+ {:type :tool-output
+ :id "call-snake"
:result {:output "<result>...</result>"
- :structured-output {:query-id "qid-1"
- :query-content "SELECT 1"
- :query query-map
- :database 1}}}
- (metabot.persistence/strip-tool-output-bloat
- {:type :tool-output
- :id "call-sql"
- :result {:output "<result>...</result>"
- :structured-output {:query-id "qid-1"
- :query-content "SELECT 1"
- :query query-map
- :database 1
- :resources [{:field_values [1 2 3]}]
- :reactions [:noop]}
- :data-parts [{:type :data}]}})))))
- (testing "preserves the snake-case :structured_output alias when present"
- (is (= {:type :tool-output
- :id "call-snake"
- :result {:output "<result>...</result>"
- :structured_output {:query-id "qid-2" :query-content "SELECT 2"}}}
- (metabot.persistence/strip-tool-output-bloat
- {:type :tool-output
- :id "call-snake"
- :result {:output "<result>...</result>"
- :structured_output {:query-id "qid-2"
- :query-content "SELECT 2"
- :extra-bloat [1 2 3]}}}))))
- (testing "leaves non-tool-output parts untouched"
- (let [text-part {:type :text :text "hello"}]
- (is (= text-part (metabot.persistence/strip-tool-output-bloat text-part)))))
- (testing "handles result with no :output key and no query-related structured-output"
- (is (= {:type :tool-output :id "call-2" :result {}}
- (metabot.persistence/strip-tool-output-bloat
- {:type :tool-output
- :id "call-2"
- :result {:structured-output {:some "data"}}})))))
+ :structured_output {:query-id "qid-2"
+ :query-content "SELECT 2"
+ :extra-bloat [1 2 3]}}}])))))
+
+(deftest parts->storable-content-empty-result-test
+ (testing "result with no :output key and no query-related structured output stores an empty map"
+ (is (= [{:type "tool-search"
+ :toolCallId "call-2"
+ :state "output-available"
+ :input {}
+ :output {}}]
+ (metabot.persistence/parts->storable-content
+ [{:type :tool-input :id "call-2" :function "search" :arguments {}}
+ {:type :tool-output
+ :id "call-2"
+ :result {:structured-output {:some "data"}}}])))))
(defn- legacy-query
"A legacy inner-query-style map suitable for [[#'api/upgrade-viewing-queries]]."
diff --git a/test/metabase/metabot/feedback_test.clj b/test/metabase/metabot/feedback_test.clj
index be702df4353..d67a64fe4cd 100644
--- a/test/metabase/metabot/feedback_test.clj
+++ b/test/metabase/metabot/feedback_test.clj
@@ -41,7 +41,8 @@
:profile_id "gpt-x"
:external_id external-id
:total_tokens 5
- :data [{:type "text" :text "hi"}]}
+ :data [{:type "text" :text "hi"}]
+ :data_version 2}
user-id (assoc :user_id user-id))))))
(deftest persist-feedback-resolves-by-external-id-test
diff --git a/test/metabase/metabot/models/metabot_message_test.clj b/test/metabase/metabot/models/metabot_message_test.clj
new file mode 100644
index 00000000000..1a41cac51f2
--- /dev/null
+++ b/test/metabase/metabot/models/metabot_message_test.clj
@@ -0,0 +1,64 @@
+(ns metabase.metabot.models.metabot-message-test
+ (:require
+ [clojure.test :refer [deftest is testing]]
+ [metabase.config.core :as config]
+ [metabase.test :as mt]
+ [toucan2.core :as t2]))
+
+(deftest after-select-migrates-v1-data-test
+ (mt/with-temp [:model/MetabotConversation conv {}
+ :model/MetabotMessage msg {:conversation_id (:id conv)
+ :role "user"
+ :data [{:role "user" :content "hi"}]
+ :data_version 1}]
+ (let [row (t2/select-one :model/MetabotMessage :id (:id msg))]
+ (testing "v1 data is converted to v2 parts on read"
+ (is (= [{:type "text" :text "hi"}] (:data row))))
+ (testing "the in-memory data_version reflects the converted format"
+ (is (= 2 (:data_version row))))
+ (testing "the stored row is untouched"
+ (is (= 1 (:data_version (t2/query-one {:select [:data_version]
+ :from [:metabot_message]
+ :where [:= :id (:id msg)]}))))))))
+
+(deftest after-select-passes-v2-data-through-test
+ (mt/with-temp [:model/MetabotConversation conv {}
+ :model/MetabotMessage msg {:conversation_id (:id conv)
+ :data [{:type "text" :text "already v2"}]
+ :data_version 2}]
+ (let [row (t2/select-one :model/MetabotMessage :id (:id msg))]
+ (is (= [{:type "text" :text "already v2"}] (:data row)))
+ (is (= 2 (:data_version row))))))
+
+(deftest after-select-survives-unmigratable-data-test
+ (mt/with-temp [:model/MetabotConversation conv {}
+ :model/MetabotMessage msg {:conversation_id (:id conv)
+ :data [{:role "assistant" :garbage true}]
+ :data_version 1}]
+ (let [row (t2/select-one :model/MetabotMessage :id (:id msg))]
+ (testing "unrecognized v1 shapes log and pass through unchanged"
+ (is (= [{:role "assistant" :garbage true}] (:data row)))
+ (is (= 1 (:data_version row)))))))
+
+(deftest after-select-tolerates-narrow-column-lists-test
+ (mt/with-temp [:model/MetabotConversation conv {}
+ :model/MetabotMessage msg {:conversation_id (:id conv)
+ :data [{:role "user" :content "hi"}]
+ :data_version 1}]
+ (testing "selecting without data_version leaves data unconverted rather than throwing"
+ (let [row (t2/select-one [:model/MetabotMessage :id :data] :id (:id msg))]
+ (is (= [{:role "user" :content "hi"}] (:data row)))))))
+
+(deftest after-select-revalidates-v2-data-in-dev-test
+ (mt/with-temp [:model/MetabotConversation conv {}
+ :model/MetabotMessage msg {:conversation_id (:id conv)
+ ;; invalid v2: a text part missing :text
+ :data [{:type "text"}]
+ :data_version 2}]
+ (testing "outside dev, malformed v2 rows are re-served without validation"
+ (is (= [{:type "text"}] (:data (t2/select-one :model/MetabotMessage :id (:id msg))))))
+ (testing "in dev, a malformed v2 row throws on read"
+ (with-redefs [config/is-dev? true]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"Invalid metabot_message.data \(read\)"
+ (t2/select-one :model/MetabotMessage :id (:id msg))))))))
diff --git a/test/metabase/metabot/persistence_test.clj b/test/metabase/metabot/persistence_test.clj
index 45ee665ba84..dc6c39f50b0 100644
--- a/test/metabase/metabot/persistence_test.clj
+++ b/test/metabase/metabot/persistence_test.clj
@@ -20,48 +20,30 @@
(use-fixtures :once (fixtures/initialize :test-users))
(deftest ^:parallel message->chat-messages-test
- (testing "user text block"
+ (testing "text part on a user row renders as a user message"
(let [result (metabot-persistence/message->chat-messages
{:role :user
- :data [{:role "user" :content "hello"}]})]
+ :data [{:type "text" :text "hello"}]})]
(is (= 1 (count result)))
(is (= {:role "user" :type "text" :message "hello"}
(select-keys (first result) [:role :type :message])))
(is (string? (:id (first result)))))))
(deftest ^:parallel message->chat-messages-test-2
- (testing "assistant standard text block preserves block id"
+ (testing "text part on an assistant row renders as an agent message with a generated id"
(let [result (metabot-persistence/message->chat-messages
{:role :assistant
- :data [{:type "text" :text "hi there" :id "block-1"}]})]
- (is (= [{:id "block-1" :role "agent" :type "text" :message "hi there"}]
- (mapv #(select-keys % [:id :role :type :message]) result))))))
-
-(deftest ^:parallel message->chat-messages-test-3
- (testing "assistant standard text block without id gets a generated one"
- (let [result (metabot-persistence/message->chat-messages
- {:role :assistant
- :data [{:type "text" :text "no id"}]})]
- (is (= 1 (count result)))
- (is (string? (:id (first result))))
- (is (= "no id" (:message (first result)))))))
-
-(deftest ^:parallel message->chat-messages-test-4
- (testing "assistant slack-format text block"
- (let [result (metabot-persistence/message->chat-messages
- {:role :assistant
- :data [{:role "assistant" :_type "TEXT" :content "from slack"}]})]
- (is (= 1 (count result)))
- (is (= {:role "agent" :type "text" :message "from slack"}
- (select-keys (first result) [:role :type :message]))))))
+ :data [{:type "text" :text "hi there"}]})]
+ (is (= [{:role "agent" :type "text" :message "hi there"}]
+ (mapv #(select-keys % [:role :type :message]) result)))
+ (is (string? (:id (first result)))))))
(deftest ^:parallel message->chat-messages-test-5
- (testing "tool-input merged with matching tool-output"
+ (testing "resolved tool part carries args and result"
(let [result (metabot-persistence/message->chat-messages
{:role :assistant
- :data [{:type "tool-input" :id "call-1" :function "search"
- :arguments {:query "foo"}}
- {:type "tool-output" :id "call-1" :result {:rows [1 2 3]}}]})]
+ :data [{:type "tool-search" :toolCallId "call-1" :state "output-available"
+ :input {:query "foo"} :output {:output "rows!" :structured_output {:query-id "q"}}}]})]
(is (= 1 (count result)))
(is (= {:id "call-1"
:role "agent"
@@ -71,43 +53,52 @@
:is_error false}
(select-keys (first result) [:id :role :type :name :status :is_error])))
(is (= {:query "foo"} (json/decode+kw (:args (first result)))))
- (is (= {:rows [1 2 3]} (json/decode+kw (:result (first result))))))))
+ (is (= {:output "rows!" :structured_output {:query-id "q"}}
+ (json/decode+kw (:result (first result))))))))
(deftest ^:parallel message->chat-messages-test-6
- (testing "tool-output flagged as error"
+ (testing "errored tool part is flagged and carries no result"
(let [result (metabot-persistence/message->chat-messages
{:role :assistant
- :data [{:type "tool-input" :id "call-2" :function "boom" :arguments {}}
- {:type "tool-output" :id "call-2" :error "exploded"}]})]
+ :data [{:type "tool-boom" :toolCallId "call-2" :state "output-error"
+ :input {} :errorText "exploded"}]})]
(is (true? (:is_error (first result))))
(is (nil? (:result (first result)))))))
(deftest ^:parallel message->chat-messages-test-7
- (testing "tool-input without matching output is left as-is"
+ (testing "unresolved tool part renders without result/error fields"
(let [result (metabot-persistence/message->chat-messages
{:role :assistant
- :data [{:type "tool-input" :id "call-3" :function "search" :arguments {}}]})]
+ :data [{:type "tool-search" :toolCallId "call-3" :state "input-available" :input {}}]})]
(is (= 1 (count result)))
(is (= "tool_call" (:type (first result))))
(is (not (contains? (first result) :result)))
(is (not (contains? (first result) :is_error))))))
+(deftest ^:parallel message->chat-messages-test-11
+ (testing "resolved tool part with a nil :output yields a nil :result, not the string \"null\""
+ (let [result (metabot-persistence/message->chat-messages
+ {:role :assistant
+ :data [{:type "tool-search" :toolCallId "call-4" :state "output-available"
+ :input {} :output nil}]})]
+ (is (false? (:is_error (first result))))
+ (is (nil? (:result (first result)))))))
+
(deftest ^:parallel message->chat-messages-test-8
- (testing "unknown block types are dropped"
+ (testing "unknown part types are dropped"
(is (= []
(metabot-persistence/message->chat-messages
{:role :assistant
- :data [{:type "data-foo" :payload {}}
- {:type "mystery"}]})))))
+ :data [{:type "mystery"}]})))))
(deftest ^:parallel message->chat-messages-test-9
(testing "data parts are converted to data_part chat messages"
- (let [blocks [{:type "data" :data-type "navigate_to" :data "/question/1"}
- {:type "data" :data-type "todo_list" :version 1 :data [{:id "t1"}]}
- {:type "data" :data-type "code_edit" :version 1 :data {:buffer_id "b" :value "v"}}]]
- (is (=? [{:role "agent" :type "data_part" :part {:type "navigate_to" :version 1 :value "/question/1"}}
- {:role "agent" :type "data_part" :part {:type "todo_list" :version 1 :value [{:id "t1"}]}}
- {:role "agent" :type "data_part" :part {:type "code_edit" :version 1 :value {:buffer_id "b" :value "v"}}}]
+ (let [blocks [{:type "data-navigate_to" :data "/question/1"}
+ {:type "data-todo_list" :data [{:id "t1"}]}
+ {:type "data-code_edit" :data {:buffer_id "b" :value "v"}}]]
+ (is (=? [{:role "agent" :type "data_part" :part {:type "data-navigate_to" :data "/question/1"}}
+ {:role "agent" :type "data_part" :part {:type "data-todo_list" :data [{:id "t1"}]}}
+ {:role "agent" :type "data_part" :part {:type "data-code_edit" :data {:buffer_id "b" :value "v"}}}]
(metabot-persistence/message->chat-messages {:role :assistant :data blocks}))))))
(deftest ^:parallel message->chat-messages-test-10
@@ -116,16 +107,16 @@
(deftest ^:parallel messages->chat-messages-flattens-across-messages-test
(let [result (metabot-persistence/messages->chat-messages
- [{:role :user :data [{:role "user" :content "hi"}]}
- {:role :assistant :data [{:type "text" :text "hello!" :id "b1"}
- {:type "tool-input" :id "t1" :function "f" :arguments {:x 1}}
- {:type "tool-output" :id "t1" :result {:ok true}}]}])]
+ [{:role :user :data [{:type "text" :text "hi"}]}
+ {:role :assistant :data [{:type "text" :text "hello!"}
+ {:type "tool-f" :toolCallId "t1" :state "output-available"
+ :input {:x 1} :output {:output "ok"}}]}])]
(is (= 3 (count result)))
(is (= ["user" "agent" "agent"] (map :role result)))
(is (= ["text" "text" "tool_call"] (map :type result)))
(is (= "hi" (:message (nth result 0))))
(is (= "hello!" (:message (nth result 1))))
- (is (= {:ok true} (json/decode+kw (:result (nth result 2)))))))
+ (is (= {:output "ok"} (json/decode+kw (:result (nth result 2)))))))
(deftest start-turn-persists-slack-conversation-metadata-test
(t2/with-transaction [_conn nil {:rollback-only true}]
@@ -167,6 +158,49 @@
(testing "empty input"
(is (= [] (into [] (metabot-persistence/combine-text-parts-xf) [])))))
+(deftest ^:parallel parts->storable-content-preserves-non-map-tool-result-test
+ ;; Regression: a non-map tool result used to throw in `select-keys`, failing the turn's persist.
+ (testing "a bare string/scalar tool result is stored as-is under :output"
+ (is (= [{:type "tool-search" :toolCallId "c1" :state "output-available"
+ :input {:q "x"} :output "just a string"}]
+ (metabot-persistence/parts->storable-content
+ [{:type :tool-input :id "c1" :function "search" :arguments {:q "x"}}
+ {:type :tool-output :id "c1" :result "just a string"}]))))
+ (testing "a map result still trims to {:output … :structured_output …}"
+ (is (= [{:type "tool-search" :toolCallId "c1" :state "output-available"
+ :input {} :output {:output "rows" :structured_output {:query-id "q"}}}]
+ (metabot-persistence/parts->storable-content
+ [{:type :tool-input :id "c1" :function "search" :arguments {}}
+ {:type :tool-output :id "c1"
+ :result {:output "rows" :structured-output {:query-id "q" :resources [:drop-me]}}}]))))
+ (testing "a nil result passes through as nil :output"
+ (is (= [{:type "tool-search" :toolCallId "c1" :state "output-available"
+ :input {} :output nil}]
+ (metabot-persistence/parts->storable-content
+ [{:type :tool-input :id "c1" :function "search" :arguments {}}
+ {:type :tool-output :id "c1" :result nil}])))))
+
+(deftest ^:parallel parts->storable-content-emits-step-start-boundaries-test
+ (testing "each :start becomes a step-start boundary, in stream order"
+ (is (= [{:type "step-start"}
+ {:type "tool-search" :toolCallId "c1" :state "output-available"
+ :input {:q "x"} :output "rows"}
+ {:type "step-start"}
+ {:type "text" :text "done" :state "done"}]
+ (metabot-persistence/parts->storable-content
+ [{:type :start :id "m1"}
+ {:type :tool-input :id "c1" :function "search" :arguments {:q "x"}}
+ {:type :tool-output :id "c1" :result "rows"}
+ {:type :start :id "m2"}
+ {:type :text :text "done"}]))))
+ (testing "step-start renders no chat message on read"
+ (is (= [{:role "agent" :type "text" :message "Hello"}]
+ (mapv #(select-keys % [:role :type :message])
+ (metabot-persistence/message->chat-messages
+ {:role :assistant
+ :data [{:type "step-start"}
+ {:type "text" :text "Hello"}]}))))))
+
(deftest start-turn-persists-slack-metadata-on-rows-test
(testing "start-turn! lands slack-team-id / channel-id / slack-thread-ts on the conversation row,
and slack-msg-id / channel-id / user-id on the user message row, plus user-id on the
@@ -275,7 +309,7 @@
(is (= created-at-before (:created_at row)) "created_at is not changed on UPDATE")
(is (true? (:finished row)) "default :finished? is true")
(is (nil? (:error row)))
- (is (= [{:type "text" :text "Hello"}] (:data row)))
+ (is (= [{:type "text" :text "Hello" :state "done"}] (:data row)))
(is (= 15 (:total_tokens row)))))))))
(deftest finalize-assistant-turn-passes-through-aborted-and-errored-test
@@ -333,13 +367,8 @@
(let [rows (t2/select :model/MetabotMessage :conversation_id conversation-id
{:order-by [[:created_at :asc] [:id :asc]]})]
(is (= [:user :assistant :user :assistant] (mapv :role rows)))
- (is (= [["user" "A"] "partial-A" ["user" "B"] "reply-B"]
- (mapv (fn [r]
- (let [d (first (:data r))]
- (if (= "user" (:role d))
- [(:role d) (:content d)]
- (:text d))))
- rows)))))))))))
+ (is (= ["A" "partial-A" "B" "reply-B"]
+ (mapv #(-> % :data first :text) rows)))))))))))
(deftest start-turn-user-and-placeholder-share-created-at-test
(testing "user-message and assistant-placeholder rows inserted by start-turn! share an instant;
@@ -419,7 +448,8 @@
:profile_id "metabot-1"
:external_id (or external-id (str (random-uuid)))
:total_tokens 0
- :data [{:type "text" :text text :id (str (random-uuid))}]
+ :data [{:type "text" :text text}]
+ :data_version 2
:created_at created-at
:finished true}
deleted-at (assoc :deleted_at deleted-at))))]
@@ -454,36 +484,36 @@
{:role :assistant :finished true :data []}))))
(testing "agent message gets :finished true and no :error by default"
(let [[msg] (metabot-persistence/message->chat-messages
- {:role :assistant :data [{:type "text" :text "ok" :id "b1"}]})]
+ {:role :assistant :data [{:type "text" :text "ok"}]})]
(is (=? {:finished true} msg))
(is (not (contains? msg :error)))))
(testing "agent message inherits :finished false from parent row"
(is (=? [{:finished false}]
(metabot-persistence/message->chat-messages
{:role :assistant :finished false
- :data [{:type "text" :text "interrupted" :id "b1"}]}))))
+ :data [{:type "text" :text "interrupted"}]}))))
(testing "agent message inherits JSON-decoded :error from parent row"
(is (=? [{:error {:message "boom" :type "RuntimeException"}}]
(metabot-persistence/message->chat-messages
{:role :assistant :finished true
:error (json/encode {:message "boom" :type "RuntimeException"})
- :data [{:type "text" :text "partial" :id "b1"}]}))))
+ :data [{:type "text" :text "partial"}]}))))
(testing "non-JSON :error column values fall through unchanged"
(is (=? [{:error "raw legacy text"}]
(metabot-persistence/message->chat-messages
{:role :assistant :finished true :error "raw legacy text"
- :data [{:type "text" :text "partial" :id "b1"}]}))))
+ :data [{:type "text" :text "partial"}]}))))
(testing "user messages do not receive agent-only status fields"
(let [[msg] (metabot-persistence/message->chat-messages
- {:role :user :data [{:role "user" :content "hi"}]})]
+ {:role :user :data [{:type "text" :text "hi"}]})]
(is (not-any? #(contains? msg %) [:finished :error]))))
(testing "multi-block assistant row: only the last agent message is annotated"
(let [result (metabot-persistence/message->chat-messages
{:role :assistant :finished false
:error (json/encode {:message "boom"})
- :data [{:type "text" :text "first" :id "b1"}
- {:type "tool-input" :id "call-1" :function "search"}
- {:type "text" :text "last" :id "b2"}]})
+ :data [{:type "text" :text "first"}
+ {:type "tool-search" :toolCallId "call-1" :state "input-available" :input nil}
+ {:type "text" :text "last"}]})
annotated? #(or (contains? % :finished) (contains? % :error))]
(is (=? [{:message "first"}
{:type "tool_call" :name "search"}
@@ -496,19 +526,19 @@
(is (= ["first" "first reply" "third" "third reply"]
(mapv :message
(metabot-persistence/messages->chat-messages
- [{:role :user :data [{:role "user" :content "first"}]}
- {:role :assistant :data [{:type "text" :text "first reply" :id "a1"}]}
- {:role :user :data [{:role "user" :content "broken"}]}
+ [{:role :user :data [{:type "text" :text "first"}]}
+ {:role :assistant :data [{:type "text" :text "first reply"}]}
+ {:role :user :data [{:type "text" :text "broken"}]}
{:role :assistant :error (json/encode {:message "boom"}) :data []}
- {:role :user :data [{:role "user" :content "third"}]}
- {:role :assistant :data [{:type "text" :text "third reply" :id "a3"}]}])))))
+ {:role :user :data [{:type "text" :text "third"}]}
+ {:role :assistant :data [{:type "text" :text "third reply"}]}])))))
(testing "with :include-errored? true, errored pairs stay and :error surfaces on the agent message"
(let [result (metabot-persistence/messages->chat-messages
- [{:role :user :data [{:role "user" :content "broken"}]}
+ [{:role :user :data [{:type "text" :text "broken"}]}
{:role :assistant :error (json/encode {:message "boom"}) :finished true
- :data [{:type "text" :text "partial" :id "a1"}]}
- {:role :user :data [{:role "user" :content "ok"}]}
- {:role :assistant :data [{:type "text" :text "fine" :id "a2"}]}]
+ :data [{:type "text" :text "partial"}]}
+ {:role :user :data [{:type "text" :text "ok"}]}
+ {:role :assistant :data [{:type "text" :text "fine"}]}]
{:include-errored? true})]
(is (= ["broken" "partial" "ok" "fine"] (mapv :message result)))
(is (= [nil {:message "boom"} nil nil] (mapv :error result))))))
@@ -643,8 +673,8 @@
stale (.minusHours recent 2)
placeholder {:role :assistant :data [] :finished nil :error nil :created_at recent}
stale-stub {:role :assistant :data [] :finished nil :error nil :created_at stale}
- user-msg {:role :user :data [{:role "user" :content "hi"}]}
- done-asst {:role :assistant :data [{:type "text" :text "done" :id "b1"}] :finished true}]
+ user-msg {:role :user :data [{:type "text" :text "hi"}]}
+ done-asst {:role :assistant :data [{:type "text" :text "done"}] :finished true}]
(testing "in-flight placeholder is skipped; surrounding messages still render"
(is (= ["hi" "done"]
(mapv :message (metabot-persistence/messages->chat-messages [user-msg done-asst placeholder])))))
@@ -749,8 +779,8 @@
(defn- ->transform-sql-parts
"Build a `write_transform_sql` tool-input/output pair whose suggested transform's `[:source :query]` is a native query.
- The structured-output's `:transform` key is dropped by `strip-tool-output-bloat`, so this pair exercises finalize's
- pre-strip extraction path."
+ The structured-output's `:transform` key is dropped by the storable conversion, so this pair exercises finalize's
+ raw-parts extraction path."
[call-id db-id sql]
(let [query (lib/native-query (mt/metadata-provider) sql)]
[{:type :tool-input
diff --git a/test/metabase/metabot/schema/migrate_v1_to_v2_test.clj b/test/metabase/metabot/schema/migrate_v1_to_v2_test.clj
index 111288a1be0..ea6cb56f024 100644
--- a/test/metabase/metabot/schema/migrate_v1_to_v2_test.clj
+++ b/test/metabase/metabot/schema/migrate_v1_to_v2_test.clj
@@ -72,6 +72,14 @@
(is (= "output-available" (:state part)))
(is (contains? part :output))
(is (= {} (:output part)))))
+ (testing "the result map passes through verbatim, not trimmed like new writes"
+ (let [[part] (migrate/migrate-v1-native->v2
+ [tool-input {:type "tool-output" :id "tc1"
+ :result {:output "rows"
+ :structured-output {:query-id 7}
+ :resources [{:blob "big"}]}}])]
+ (is (= {:output "rows" :structured-output {:query-id 7} :resources [{:blob "big"}]}
+ (:output part)))))
(testing "tool-output with a string :error becomes output-error with :errorText and no :output"
(let [[part] (migrate/migrate-v1-native->v2
[tool-input {:type "tool-output" :id "tc1" :result {} :error "boom"}])]
diff --git a/test/metabase/metabot/schema/v1_test.clj b/test/metabase/metabot/schema/v1_test.clj
index 0c362981fee..c2b20044076 100644
--- a/test/metabase/metabot/schema/v1_test.clj
+++ b/test/metabase/metabot/schema/v1_test.clj
@@ -1,10 +1,7 @@
(ns metabase.metabot.schema.v1-test
(:require
- [clojure.java.io :as io]
[clojure.test :refer [deftest is testing]]
[metabase.metabot.schema.v1 :as schema.v1]
- [metabase.metabot.util :as metabot.u]
- [metabase.util.json :as json]
[metabase.util.malli.registry :as mr]))
(def ^:private ai-service-entries
@@ -71,16 +68,3 @@
(is (not (mr/validate ::schema.v1/message-data
[{:role "user" :content "hi"}
{:type "text" :id "t1" :text "Hello"}])))))
-
-(deftest ^:parallel fixture-parity-test
- (doseq [fixture ["metabase/metabot/aisdkstream1.txt"
- "metabase/metabot/aisdkstream2.txt"
- "metabase/metabot/aisdkstream3.txt"
- "metabase/metabot/aisdkstream4.txt"]]
- (testing fixture
- (let [messages (with-open [r (io/reader (io/resource fixture))]
- (metabot.u/aisdk->messages "assistant" (line-seq r)))
- ;; the schema describes the at-rest shape, so simulate the `mi/transform-json` write +
- ;; read cycle: keyword values become strings, keys stay keywords
- at-rest (-> messages json/encode json/decode+kw)]
- (is (nil? (mr/explain ::schema.v1/message-data at-rest)))))))
diff --git a/test/metabase/metabot/self/claude_test.clj b/test/metabase/metabot/self/claude_test.clj
index c9da018a615..aa6fef985d6 100644
--- a/test/metabase/metabot/self/claude_test.clj
+++ b/test/metabase/metabot/self/claude_test.clj
@@ -40,6 +40,23 @@
:cacheReadTokens nat-int?}}]
(into [] (comp (claude/claude->aisdk-chunks-xf) (self.core/aisdk-xf)) raw-chunks))))))
+(deftest ^:parallel claude-text-id-from-content-block-index-test
+ (testing "text blocks (no provider id) take their id from the content-block index, matching @ai-sdk/anthropic"
+ (let [raw [{:type "message_start" :message {:id "msg-1" :model "claude-haiku-4-5"}}
+ {:type "content_block_start" :index 0 :content_block {:type "text"}}
+ {:type "content_block_delta" :index 0 :delta {:type "text_delta" :text "Hello"}}
+ {:type "content_block_stop" :index 0}
+ {:type "content_block_start" :index 1 :content_block {:type "text"}}
+ {:type "content_block_delta" :index 1 :delta {:type "text_delta" :text "world"}}
+ {:type "content_block_stop" :index 1}
+ {:type "message_stop"}]
+ chunks (into [] (claude/claude->aisdk-chunks-xf) raw)]
+ (is (= [["text-start" "0"] ["text-delta" "0"] ["text-end" "0"]
+ ["text-start" "1"] ["text-delta" "1"] ["text-end" "1"]]
+ (->> chunks
+ (filter #(#{:text-start :text-delta :text-end} (:type %)))
+ (mapv (juxt (comp name :type) :id))))))))
+
(deftest ^:parallel claude-error-event-uses-canonical-error-shape-test
(testing "an `error` SSE event becomes an :error part keyed by :error (read by the wire serializer + persistence)"
(let [raw [{:type "message_start" :message {:id "msg_1" :model "claude-haiku-4-5"}}
@@ -47,7 +64,8 @@
parts (into [] (comp (claude/claude->aisdk-chunks-xf) (self.core/aisdk-xf)) raw)
err (m/find-first #(= :error (:type %)) parts)]
(is (=? {:message "Overloaded"} (:error err)))
- (is (= "3:\"Overloaded\"" (self.core/format-error-line err))))))
+ (is (= (self.core/format-sse-event {:type "error" :errorText "Overloaded"})
+ (self.core/format-error-line err))))))
(deftest ^:parallel claude-tool-input-conv-test
(let [raw-chunks (fixture "claude-tool-input"
diff --git a/test/metabase/metabot/self/openai_test.clj b/test/metabase/metabot/self/openai_test.clj
index dc9f22edcea..7af991f30e7 100644
--- a/test/metabase/metabot/self/openai_test.clj
+++ b/test/metabase/metabot/self/openai_test.clj
@@ -154,7 +154,8 @@
(testing "persistence picks up a non-nil error payload"
(is (=? {:message "boom"} (:error err))))
(testing "wire serializer emits the message (not an empty string)"
- (is (= "3:\"boom\"" (self.core/format-error-line err)))))))
+ (is (= (self.core/format-sse-event {:type "error" :errorText "boom"})
+ (self.core/format-error-line err)))))))
(deftest ^:parallel openai-response-incomplete-keeps-partial-text-and-usage-test
(testing "a terminal response.incomplete event keeps the partial text and still emits usage (not an error)"
diff --git a/test/metabase/metabot/self_test.clj b/test/metabase/metabot/self_test.clj
index 60589953301..9ca0ba52234 100644
--- a/test/metabase/metabot/self_test.clj
+++ b/test/metabase/metabot/self_test.clj
@@ -6,6 +6,7 @@
[clojure.test :refer :all]
[metabase.analytics-interface.core :as analytics]
[metabase.analytics.snowplow-test :as snowplow-test]
+ [metabase.metabot.schema.v2 :as schema.v2]
[metabase.metabot.self :as self]
[metabase.metabot.self.core :as self.core]
[metabase.metabot.self.openrouter :as openrouter]
@@ -14,6 +15,7 @@
[metabase.test.fixtures :as fixtures]
[metabase.util.json :as json]
[metabase.util.log.capture :as log.capture]
+ [metabase.util.malli.registry :as mr]
[ring.adapter.jetty :as jetty]))
(set! *warn-on-reflection* true)
@@ -399,132 +401,168 @@
(is (=? {:type :tool-output-available :toolCallId "call-d5"}
(last result))))))
-;;; AI SDK v4 Line Protocol tests
+;;; AI SDK SSE output tests
-(deftest format-text-line-test
- (testing "formats text as JSON-encoded string with 0: prefix"
- (is (= "0:\"Hello world\"" (self.core/format-text-line {:text "Hello world"})))
- (is (= "0:\"Line with \\\"quotes\\\"\"" (self.core/format-text-line {:text "Line with \"quotes\""})))))
+(defn- sse-events
+ "Run `parts` through [[self.core/parts->aisdk-sse-xf]] and decode each
+ `data: {json}` line back to a map, asserting every event validates against
+ the wire schema. The `[DONE]` terminator is checked then excluded."
+ ([parts] (sse-events nil parts))
+ ([opts parts]
+ (let [lines (into [] (self.core/parts->aisdk-sse-xf opts) parts)]
+ (is (every? #(str/starts-with? % "data: ") lines))
+ (is (= "data: [DONE]\n" (last lines)))
+ (let [events (mapv #(json/decode+kw (subs (str/trimr %) 6)) (butlast lines))]
+ (doseq [event events]
+ (is (nil? (mr/explain ::schema.v2/ui-message-chunk event))
+ (str "event does not match the wire schema: " (pr-str event))))
+ events))))
-(deftest format-data-line-test
- (testing "formats data with type, version, and value"
- (is (= "2:{\"type\":\"state\",\"version\":1,\"value\":{\"queries\":{}}}"
- (self.core/format-data-line {:data-type "state" :data {:queries {}}})))
- (is (= "2:{\"type\":\"navigate_to\",\"version\":1,\"value\":{\"url\":\"/question/123\"}}"
- (self.core/format-data-line {:data-type "navigate_to" :data {:url "/question/123"}})))))
+(deftest parts->aisdk-sse-xf-lifecycle-test
+ (testing "first :start opens the message and a step; later :start is a step boundary; completion closes"
+ (is (= [["start" "msg-1"] ["start-step" nil]
+ ["finish-step" nil] ["start-step" nil]
+ ["finish-step" nil] ["finish" nil]]
+ (mapv (juxt :type :messageId)
+ (sse-events [{:type :start :id "msg-1"}
+ {:type :start :id "msg-1"}])))))
+ (testing "a stream that never started still emits finish"
+ (is (= ["finish"] (mapv :type (sse-events [])))))
+ (testing ":message-id overrides the start event's message_id"
+ (is (= "override-id"
+ (-> (sse-events {:message-id "override-id"} [{:type :start :id "provider-id"}])
+ first
+ :messageId)))))
-(deftest format-error-line-test
- (testing "formats plain error message as a JSON string with 3: prefix"
- (is (= "3:\"Something went wrong\"" (self.core/format-error-line {:error {:message "Something went wrong"}})))
- (is (= "3:\"Unknown error\"" (self.core/format-error-line {:error "Unknown error"}))))
- (testing "formats structured error with error-code as a JSON object"
- (let [line (self.core/format-error-line {:error {:message "You've used all of your included AI service tokens."
- :error-code "metabase_ai_managed_locked"}})
- parsed (json/decode+kw (subs line 2))]
- (is (str/starts-with? line "3:"))
- (is (= "You've used all of your included AI service tokens." (:message parsed)))
- (is (= "metabase_ai_managed_locked" (:error-code parsed)))))
- (testing "formats ai_usage_limit_reached error-code as a JSON object"
- (let [line (self.core/format-error-line {:error {:message "You have reached your AI usage limit."
- :error-code "ai_usage_limit_reached"}})
- parsed (json/decode+kw (subs line 2))]
- (is (str/starts-with? line "3:"))
- (is (= "You have reached your AI usage limit." (:message parsed)))
- (is (= "ai_usage_limit_reached" (:error-code parsed)))))
- (testing "coerces keyword error-code to string"
- (let [line (self.core/format-error-line {:error {:message "Usage limit reached"
- :error-code :metabase_ai_managed_locked}})
- parsed (json/decode+kw (subs line 2))]
- (is (= "metabase_ai_managed_locked" (:error-code parsed))))))
+(deftest parts->aisdk-sse-xf-text-coalescing-test
+ (testing "consecutive same-id :text parts share one block; an id change closes it"
+ (is (= [["text-start" "t1" nil]
+ ["text-delta" "t1" "Hel"]
+ ["text-delta" "t1" "lo"]
+ ["text-end" "t1" nil]
+ ["text-start" "t2" nil]
+ ["text-delta" "t2" "next"]
+ ["text-end" "t2" nil]
+ ["finish" nil nil]]
+ (mapv (juxt :type :id :delta)
+ (sse-events [{:type :text :id "t1" :text "Hel"}
+ {:type :text :id "t1" :text "lo"}
+ {:type :text :id "t2" :text "next"}])))))
+ (testing "a non-text part closes the open text block before its own events"
+ (is (= ["text-start" "text-delta" "text-end" "data-state" "finish"]
+ (mapv :type
+ (sse-events [{:type :text :id "t1" :text "hi"}
+ {:type :data :data-type "state" :data {:queries {}}}]))))))
-(deftest format-tool-call-line-test
- (testing "formats tool call with toolCallId, toolName, and args"
- (let [line (self.core/format-tool-call-line {:id "call-123"
- :function "search"
- :arguments {:query "revenue"}})]
- (is (str/starts-with? line "9:"))
- (let [parsed (json/decode+kw (subs line 2))]
- (is (= "call-123" (:toolCallId parsed)))
- (is (= "search" (:toolName parsed)))
- ;; args should be JSON string, not object
- (is (string? (:args parsed)))))))
+(deftest parts->aisdk-sse-xf-tool-test
+ (testing "tool input and successful output"
+ (let [[input-event output-event] (sse-events [{:type :tool-input :id "call-1" :function "search"
+ :arguments {:q "test"}}
+ {:type :tool-output :id "call-1" :function "search"
+ :result {:output "rows" :resources [1 2]}
+ :duration-ms 12}])]
+ (is (= {:type "tool-input-available" :toolCallId "call-1" :toolName "search" :input {:q "test"}}
+ input-event))
+ (is (= {:type "tool-output-available" :toolCallId "call-1" :output "rows"}
+ output-event)
+ "only the LLM-facing output string goes on the wire")))
+ (testing "a result map without :output keeps internal keys off the wire"
+ (let [[output-event] (sse-events [{:type :tool-output :id "call-i" :function "clarify"
+ :result {:instructions "internal LLM steering — never show the client"
+ :data-parts [{:type "data-foo"}]}}])]
+ (is (= {:type "tool-output-available" :toolCallId "call-i" :output ""}
+ output-event)
+ "no :output key -> empty wire output; internal :instructions/:data-parts never leak")))
+ (testing "tool error becomes tool-output-error"
+ (is (= {:type "tool-output-error" :toolCallId "call-2" :errorText "Tool failed"}
+ (first (sse-events [{:type :tool-output :id "call-2" :error {:message "Tool failed"}}])))))
+ (testing ":tool-input-start maps to tool-input-start"
+ (is (= {:type "tool-input-start" :toolCallId "call-3" :toolName "search"}
+ (first (sse-events [{:type :tool-input-start :id "call-3" :function "search"}]))))))
-(deftest format-tool-result-line-test
- (testing "formats tool result with toolCallId and result"
- (let [line (self.core/format-tool-result-line {:id "call-123"
- :result {:data [{:id 1}]}})]
- (is (str/starts-with? line "a:"))
- (let [parsed (json/decode+kw (subs line 2))]
- (is (= "call-123" (:toolCallId parsed)))
- ;; result should be JSON string
- (is (string? (:result parsed))))))
- (testing "formats tool error"
- (let [line (self.core/format-tool-result-line {:id "call-456"
- :error {:message "Tool failed"}})]
- (is (str/starts-with? line "a:"))
- (let [parsed (json/decode+kw (subs line 2))]
- (is (= "call-456" (:toolCallId parsed)))
- (is (string? (:error parsed))))))
- (testing ":duration-ms is ignored"
- (let [line (self.core/format-tool-result-line {:id "call-789"
- :result {:data [{:id 1}]}
- :duration-ms 1234})]
- (is (str/starts-with? line "a:"))
- (let [parsed (json/decode+kw (subs line 2))]
- (is (= "call-789" (:toolCallId parsed)))
- (is (not (contains? parsed :duration-ms)))))))
+(deftest parts->aisdk-sse-xf-data-test
+ (testing "data parts become typed data events with a generated id"
+ (let [[event] (sse-events [{:type :data :data-type "navigate_to" :data {:url "/question/123"}}])]
+ (is (= "data-navigate_to" (:type event)))
+ (is (string? (:id event)))
+ (is (= {:url "/question/123"} (:data event)))))
+ (testing "unknown part types pass through as data events"
+ (is (= "data-mystery"
+ (-> (sse-events [{:type :mystery :id "x"}]) first :type)))))
-(deftest format-finish-line-test
- (testing "formats finish message with usage"
- (let [line (self.core/format-finish-line false {"claude-sonnet-4-5-20250929" {:prompt 100 :completion 50}})]
- (is (str/starts-with? line "d:"))
- (let [parsed (json/decode+kw (subs line 2))]
- (is (= "stop" (:finishReason parsed)))
- ;; Keys are keywordized when parsing JSON
- (is (= 100 (get-in parsed [:usage :claude-sonnet-4-5-20250929 :prompt])))))))
+(deftest parts->aisdk-sse-xf-error-test
+ (testing "an error before any :start synthesizes start + start-step, and flips finish_reason"
+ (let [events (sse-events [{:type :error :error {:message "Something went wrong"}}])]
+ (is (=? [{:type "start" :messageId string?}
+ {:type "start-step"}
+ {:type "error" :errorText "Something went wrong"}
+ {:type "finish-step"}
+ {:type "finish" :finishReason "error"}]
+ events))))
+ (testing "a typed error's code rides finish.messageMetadata"
+ (let [events (sse-events [{:type :error :error {:message "You have reached your AI usage limit."
+ :error-code :ai_usage_limit_reached}}])]
+ (is (=? [{:type "start" :messageId string?}
+ {:type "start-step"}
+ {:type "error" :errorText "You have reached your AI usage limit."}
+ {:type "finish-step"}
+ {:type "finish" :finishReason "error"
+ :messageMetadata {:errorCode "ai_usage_limit_reached"}}]
+ events))))
+ (testing "accumulated usage and a typed error code share one finish.messageMetadata"
+ (let [parts [{:type :start :id "m"}
+ {:type :usage :model "claude-sonnet-4-6" :usage {:promptTokens 10 :completionTokens 5}}
+ {:type :error :error {:message "You have reached your AI usage limit."
+ :error-code :ai_usage_limit_reached}}]
+ finish (last (sse-events parts))]
+ (is (=? {:type "finish"
+ :finishReason "error"
+ :messageMetadata {:usage {:inputTokens 10 :outputTokens 5 :totalTokens 15}
+ :usageByModel {:claude-sonnet-4-6 {:inputTokens 10 :outputTokens 5 :totalTokens 15}}
+ :errorCode "ai_usage_limit_reached"}}
+ finish)))))
-(deftest format-start-line-test
- (testing "formats start message with messageId"
- (let [line (self.core/format-start-line {:id "msg-123"})]
- (is (str/starts-with? line "f:"))
- (let [parsed (json/decode+kw (subs line 2))]
- (is (= "msg-123" (:messageId parsed)))))))
+(deftest parts->aisdk-sse-xf-usage-test
+ (testing "accumulated usage lands on finish.messageMetadata, last snapshot per model"
+ (let [parts [{:type :start :id "m"}
+ {:type :usage :model "claude-sonnet-4-6" :usage {:promptTokens 5 :completionTokens 1}}
+ {:type :usage :model "claude-sonnet-4-6" :usage {:promptTokens 10 :completionTokens 5}}
+ {:type :usage :model "gpt-5" :usage {:promptTokens 2 :completionTokens 3}}
+ {:type :finish}]
+ finish (last (sse-events parts))]
+ (is (= {:type "finish"
+ :finishReason "stop"
+ :messageMetadata {:usage {:inputTokens 12 :outputTokens 8 :totalTokens 20
+ :cacheCreationTokens 0 :cacheReadTokens 0 :cachedInputTokens 0}
+ :usageByModel {:claude-sonnet-4-6 {:inputTokens 10 :outputTokens 5 :totalTokens 15
+ :cacheCreationTokens 0 :cacheReadTokens 0 :cachedInputTokens 0}
+ :gpt-5 {:inputTokens 2 :outputTokens 3 :totalTokens 5
+ :cacheCreationTokens 0 :cacheReadTokens 0 :cachedInputTokens 0}}}}
+ finish))))
+ (testing "finish omits messageMetadata when no usage was observed"
+ (is (not (contains? (last (sse-events [{:type :text :id "t" :text "x"}]))
+ :messageMetadata)))))
-(deftest aisdk-line-xf-test
- (testing "converts internal parts to AI SDK v4 line protocol, skipping usage by default"
- (let [parts [{:type :start :id "msg-1"}
- {:type :text :text "Hello"}
- {:type :tool-input :id "call-1" :function "search" :arguments {:q "test"}}
- {:type :tool-output :id "call-1" :result {:data []}}
- {:type :usage :id "msg-1" :model "claude-sonnet-4-5-20250929" :usage {:promptTokens 10 :completionTokens 5}}
- {:type :finish}]
- lines (into [] (self.core/aisdk-line-xf) parts)]
- ;; Should have: start, text, tool-call, tool-result, finish (usage skipped)
- (is (= 5 (count lines)))
- (is (str/starts-with? (nth lines 0) "f:")) ;; start
- (is (str/starts-with? (nth lines 1) "0:")) ;; text
- (is (str/starts-with? (nth lines 2) "9:")) ;; tool-call
- (is (str/starts-with? (nth lines 3) "a:")) ;; tool-result
- (is (str/starts-with? (nth lines 4) "d:")))) ;; finish
-
- (testing "emits usage as data line when :emit-usage? is true"
- (let [parts [{:type :start :id "msg-1"}
- {:type :text :text "Hello"}
- {:type :usage :id "msg-1" :model "claude-sonnet-4-5-20250929"
- :usage {:promptTokens 10 :completionTokens 5}}]
- lines (into [] (self.core/aisdk-line-xf {:emit-usage? true}) parts)]
- (is (=? [#"f:.*"
- #"0:.*"
- #"d:.*"]
- lines))
- (is (=? {:usage {:promptTokens 10 :completionTokens 5}}
- (-> (last lines) (subs 2) (json/decode+kw))))))
- (testing ":external-id overrides the messageId on the start line"
- (let [parts [{:type :start :id "provider-id" :messageId "provider-msg-id"}
- {:type :text :text "hi"}]
- lines (into [] (self.core/aisdk-line-xf {:external-id "override-id"}) parts)]
- (is (= "override-id"
- (-> (first lines) (subs 2) (json/decode+kw) :messageId))))))
+(deftest parts->aisdk-sse-xf-usage-cache-test
+ (testing "Anthropic cache breakdown flows onto finish.messageMetadata; non-cache models contribute 0"
+ (let [parts [{:type :start :id "m"}
+ {:type :usage :model "claude-sonnet-4-6"
+ :usage {:promptTokens 1540 :completionTokens 10
+ :cacheCreationTokens 300 :cacheReadTokens 1200}}
+ {:type :usage :model "gpt-5" :usage {:promptTokens 2 :completionTokens 3}}
+ {:type :finish}]
+ finish (last (sse-events parts))]
+ (is (= {:type "finish"
+ :finishReason "stop"
+ :messageMetadata
+ {:usage {:inputTokens 1542 :outputTokens 13 :totalTokens 1555
+ ;; cachedInputTokens mirrors cacheReadTokens; gpt-5 adds 0 to each
+ :cacheCreationTokens 300 :cacheReadTokens 1200 :cachedInputTokens 1200}
+ :usageByModel {:claude-sonnet-4-6 {:inputTokens 1540 :outputTokens 10 :totalTokens 1550
+ :cacheCreationTokens 300 :cacheReadTokens 1200 :cachedInputTokens 1200}
+ :gpt-5 {:inputTokens 2 :outputTokens 3 :totalTokens 5
+ :cacheCreationTokens 0 :cacheReadTokens 0 :cachedInputTokens 0}}}}
+ finish)))))
;;; ===================== Retry Logic Tests =====================
diff --git a/test/metabase/metabot/tools/todo_test.clj b/test/metabase/metabot/tools/todo_test.clj
index 0b8c91c8d6e..b8d8807a0da 100644
--- a/test/metabase/metabot/tools/todo_test.clj
+++ b/test/metabase/metabot/tools/todo_test.clj
@@ -24,7 +24,6 @@
data-part (first (:data-parts result))]
(is (= :data (:type data-part)))
(is (= "todo_list" (:data-type data-part)))
- (is (= 1 (:version data-part)))
(is (= todos (:data data-part))))))
(deftest ^:parallel todo-write-test-3
diff --git a/test/metabase/metabot/tools/transforms/write_test.clj b/test/metabase/metabot/tools/transforms/write_test.clj
index 5d9b5582a29..20d117782f4 100644
--- a/test/metabase/metabot/tools/transforms/write_test.clj
+++ b/test/metabase/metabot/tools/transforms/write_test.clj
@@ -138,8 +138,7 @@
(is (= 1 (count (:data-parts result))))
(let [data-part (first (:data-parts result))]
(is (= :data (:type data-part)))
- (is (= "transform_suggestion" (:data-type data-part)))
- (is (= 1 (:version data-part)))))))
+ (is (= "transform_suggestion" (:data-type data-part)))))))
;;; Memory Storage Tests
diff --git a/test/metabase/metabot/util_test.clj b/test/metabase/metabot/util_test.clj
deleted file mode 100644
index eb2b6583916..00000000000
--- a/test/metabase/metabot/util_test.clj
+++ /dev/null
@@ -1,108 +0,0 @@
-(ns metabase.metabot.util-test
- (:require
- [clojure.java.io :as io]
- [clojure.test :refer :all]
- [metabase.metabot.util :as metabot.u]))
-
-(deftest ^:parallel aisdk-line-parse-test
- (testing "We should be able to parse AI SDK streaming format to ai-service format 1"
- (is (=? [{:role "assistant"
- :_type :TOOL_CALL
- :tool_calls [{:id "call_hZ6Q9AwsxqXaL0lbtpoAEdBi"
- :name "construct_notebook_query"
- :arguments #"\{\"query_plan\":.*\}"}]}
- {:role "tool"
- :_type :TOOL_RESULT
- :tool_call_id "call_hZ6Q9AwsxqXaL0lbtpoAEdBi"
- :content #"(?s)\n<result>.*</instructions>"}
- {:role "assistant"
- :_type :TEXT
- :content #"(?s)I created.*let me know!"}
- {:type "state"
- :_type :DATA
- :version 1
- :value {}}
- {:finish_reason "stop"
- :_type :FINISH_MESSAGE
- :usage {}}]
- (metabot.u/aisdk->messages "assistant"
- (-> (io/resource "metabase/metabot/aisdkstream1.txt")
- io/reader
- line-seq))))))
-
-(deftest ^:parallel aisdk-line-parse-test-2
- (testing "We should be able to parse AI SDK streaming format to ai-service format 2"
- (is (=? [{:role "assistant"
- :_type :TOOL_CALL
- :tool_calls [{:id "call_sJpEjRXSqi5vSjAOJXFdJhjt"
- :name "create_chart"
- :arguments #"\{\"data_source\":.*\}"}]}
- {:role "tool"
- :_type :TOOL_RESULT
- :tool_call_id "call_sJpEjRXSqi5vSjAOJXFdJhjt"
- :content #"(?s)\n<result>.*</instructions>"}
- {:role "assistant"
- :_type :TOOL_CALL
- :tool_calls [{:id "call_oPCH01Dt3jlFAwRuemtq9fTt"
- :name "navigate_user"
- :arguments #"\{\"destination\":.*\}"}]}
- {:type "navigate_to"
- :_type :DATA
- :version 1
- :value #"/question#eyJ.*"}
- {:role "tool"
- :_type :TOOL_RESULT
- :tool_call_id "call_oPCH01Dt3jlFAwRuemtq9fTt"
- :content #"(?s)The user now can see the result of the chart.*"}
- {:role "assistant"
- :_type :TEXT
- :content #"(?s)I created.*more details!"}
- {:type "state"
- :_type :DATA
- :version 1
- :value {}}
- {:finish_reason "stop"
- :_type :FINISH_MESSAGE
- :usage {}}]
- (metabot.u/aisdk->messages "assistant"
- (-> (io/resource "metabase/metabot/aisdkstream2.txt")
- io/reader
- line-seq))))))
-
-(deftest ^:parallel aisdk-line-parse-test-3
- (testing "We should be able to parse AI SDK streaming format with error messages 3"
- (is (=? [{:role "assistant"
- :_type :ERROR
- :content #"litellm.ServiceUnavailableError: litellm.MidStreamFallbackError:.*Connection closed."}
- {:finish_reason "error"
- :_type :FINISH_MESSAGE
- :usage {}}]
- (metabot.u/aisdk->messages "assistant"
- (-> (io/resource "metabase/metabot/aisdkstream3.txt")
- io/reader
- line-seq))))))
-
-(deftest ^:parallel aisdk-line-parse-test-4
- (testing "We should be able to parse AI SDK streaming format with error messages 4"
- (is (=? [{:role "assistant"
- :_type :TEXT
- :content #"(?s)I'll help.*find the right source."}
- {:role "assistant"
- :_type :TOOL_CALL
- :tool_calls [{:id "toolu_bdrk_01GPPfFCzqWpZhsDdGMi9mYi"
- :name "search"
- :arguments #"\{\"semantic_queries\":.*\}"}]}
- {:role "tool"
- :_type :TOOL_RESULT
- :tool_call_id "toolu_bdrk_01GPPfFCzqWpZhsDdGMi9mYi"
- :content #"(?s)\n<result>.*</instructions>"}
- {:role "assistant"
- :_type :ERROR
- :content #"litellm.Timeout: Timeout Error: OpenrouterException.*seconds"}
- {:finish_reason "error"
- :_type :FINISH_MESSAGE
- :usage {}}]
- (metabot.u/aisdk->messages "assistant"
- (-> (io/resource "metabase/metabot/aisdkstream4.txt")
- io/reader
- line-seq))))))
diff --git a/test/metabase/slackbot/api_test.clj b/test/metabase/slackbot/api_test.clj
index 7fcb48a5d40..152455b5c71 100644
--- a/test/metabase/slackbot/api_test.clj
+++ b/test/metabase/slackbot/api_test.clj
@@ -820,7 +820,8 @@
:profile_id "slackbot"
:external_id external-id
:total_tokens 5
- :data [{:_type "TEXT" :role "assistant" :content "hi"}]}))]
+ :data [{:type "text" :text "hi"}]
+ :data_version 2}))]
{:conv-id conv-id :external-id external-id :message-id msg-id})))
(defn- tear-down-slackbot-feedback!
@@ -921,7 +922,8 @@
:profile_id "slackbot"
:user_id lucky-id
:total_tokens 0
- :data [{:_type "TEXT" :role "user" :content "+1"}]})
+ :data [{:type "text" :text "+1"}]
+ :data_version 2})
(let [rasta-result (#'slackbot/handle-feedback-modal-submission
(modal-submission-payload {:conv-id conv-id
:external-id external-id
@@ -1002,7 +1004,8 @@
:channel_id channel-id
:slack_msg_id message-ts
:total_tokens 5
- :data [{:_type "TEXT" :role "assistant" :content "hi"}]}))
+ :data [{:type "text" :text "hi"}]
+ :data_version 2}))
;; external-id intentionally omitted from the button payload — only channel + ts are present
payload (modal-submission-payload {:conv-id conv-id
:external-id nil
diff --git a/test/metabase/slackbot/persistence_test.clj b/test/metabase/slackbot/persistence_test.clj
index 6c8dd990081..c22e690de6e 100644
--- a/test/metabase/slackbot/persistence_test.clj
+++ b/test/metabase/slackbot/persistence_test.clj
@@ -10,6 +10,24 @@
(use-fixtures :once (fixtures/initialize :test-users))
+(deftest message-history-validates-stored-parts-test
+ (testing "malformed stored v2 parts are rejected before slack history replay"
+ (let [conv-id (str (random-uuid))]
+ (mt/with-model-cleanup [:model/MetabotMessage [:model/MetabotConversation :created_at]]
+ (t2/insert! :model/MetabotConversation {:id conv-id :user_id (mt/user->id :rasta)})
+ (t2/insert! :model/MetabotMessage
+ {:conversation_id conv-id
+ :slack_msg_id "1709567890.000099"
+ :role "assistant"
+ :profile_id "test"
+ :total_tokens 0
+ ;; invalid v2: a tool part with no :state to dispatch on
+ :data [{:type "tool-search" :toolCallId "z"}]
+ :data_version 2})
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"Invalid slack history replay metabot_message.data"
+ (slackbot.persistence/message-history conv-id #{"1709567890.000099"})))))))
+
(deftest message-history-test
(let [conv-id (str (random-uuid))]
(mt/with-model-cleanup [:model/MetabotMessage [:model/MetabotConversation :created_at]]
@@ -21,18 +39,20 @@
:role "user"
:profile_id "test"
:total_tokens 0
- :data [{:_type "TEXT" :role "user" :content "what is 2+2?"}]})
- ;; Assistant message with tool calls
+ :data [{:type "text" :text "what is 2+2?"}]
+ :data_version 2})
+ ;; Assistant message with a resolved tool call
(t2/insert! :model/MetabotMessage
{:conversation_id conv-id
:slack_msg_id "1709567890.000002"
:role "assistant"
:profile_id "test"
:total_tokens 10
- :data [{:_type "TEXT" :role "assistant" :content "hi"}
- {:_type "TOOL_CALL" :role "assistant" :tool_calls [{:id "x"}]}
- {:_type "TOOL_RESULT" :role "tool" :tool_call_id "x" :content "y"}]})
- (testing "only TOOL_CALL and TOOL_RESULT are included, TEXT is filtered out"
+ :data [{:type "text" :text "hi"}
+ {:type "tool-search" :toolCallId "x" :state "output-available"
+ :input {} :output {:output "y"}}]
+ :data_version 2})
+ (testing "only tool parts are included, text is filtered out"
(let [result (slackbot.persistence/message-history conv-id #{"1709567890.000002"})]
(is (= 2 (count (get result "1709567890.000002"))))
(is (every? #(#{:assistant :tool} (:role %)) (get result "1709567890.000002")))))
@@ -50,19 +70,23 @@
:role "assistant"
:profile_id "test"
:total_tokens 10
- :data [{:_type "TOOL_CALL" :role "assistant" :tool_calls [{:id "y"}]}]
+ :data [{:type "tool-search" :toolCallId "y" :state "output-available"
+ :input {} :output {:output "z"}}]
+ :data_version 2
:deleted_at (java.time.OffsetDateTime/now)
:deleted_by_user_id (mt/user->id :rasta)})
(is (empty? (slackbot.persistence/message-history conv-id #{deleted-ts})))
(is (= #{deleted-ts}
(slackbot.persistence/deleted-message-ids conv-id #{deleted-ts}))))))))
-(deftest message-history-native-shape-test
- (testing "assistant messages persisted in native-parts shape are translated to AI-SDK messages"
+(deftest message-history-v2-parts-test
+ (testing "stored v2 tool parts are translated to AI-SDK message pairs"
(let [conv-id (str (random-uuid))
slack-ts "1712000000.000001"
search-id "call-search"
- query-id "call-query"]
+ query-id "call-query"
+ orphan-id "call-orphan"
+ failed-id "call-failed"]
(mt/with-model-cleanup [:model/MetabotMessage [:model/MetabotConversation :created_at]]
(t2/insert! :model/MetabotConversation {:id conv-id :user_id (mt/user->id :rasta)})
(t2/insert! :model/MetabotMessage
@@ -71,49 +95,109 @@
:role "assistant"
:profile_id "slackbot"
:total_tokens 10
- :data [{:type :text :text "Let me check."}
- {:type :tool-input
- :id search-id
- :function "search"
- :arguments {:query "orders"}}
- {:type :tool-output
- :id search-id
- :result {:output "<result>orders</result>"
- :structured-output {:query-id "qid-1"}}}
- {:type :tool-input
- :id query-id
- :function "construct_notebook_query"
- :arguments {:data_source_ids ["table-1"]}}
- {:type :tool-output
- :id query-id
- :result {:output "<result>query</result>"}}]})
+ :data [{:type "text" :text "Let me check."}
+ {:type "tool-search"
+ :toolCallId search-id
+ :state "output-available"
+ :input {:query "orders"}
+ :output {:output "<result>orders</result>"
+ :structured_output {:query-id "qid-1"}}}
+ {:type "tool-construct_notebook_query"
+ :toolCallId query-id
+ :state "output-available"
+ :input {:data_source_ids ["table-1"]}
+ :output {:output "<result>query</result>"}}
+ {:type "tool-search"
+ :toolCallId orphan-id
+ :state "input-available"
+ :input {:query "never finished"}}
+ {:type "tool-search"
+ :toolCallId failed-id
+ :state "output-error"
+ :input {:query "boom"}
+ :errorText "it broke"}]
+ :data_version 2})
(let [result (slackbot.persistence/message-history conv-id #{slack-ts})
msgs (get result slack-ts)]
- (testing "text blocks are skipped — only tool blocks surface"
- (is (= 4 (count msgs))))
- (testing "tool-input → assistant message with :tool_calls"
+ (testing "text parts and unresolved tool parts are skipped"
+ (is (= 6 (count msgs))))
+ (testing "tool part → assistant message with :tool_calls, input JSON-encoded"
(is (= {:role :assistant
:tool_calls [{:id search-id
:name "search"
- :arguments {:query "orders"}}]}
+ :arguments "{\"query\":\"orders\"}"}]}
(first msgs))))
- (testing "tool-output → tool message with :content and :tool_call_id"
+ (testing "tool part → tool message with :content from the inner output"
(is (= {:role :tool
:tool_call_id search-id
:content "<result>orders</result>"}
(second msgs))))
- (testing "order is preserved — matching tool-call/tool-result pairs stay adjacent"
- (is (= [search-id search-id query-id query-id]
+ (testing "errored tool part replays its error text"
+ (is (= {:role :tool
+ :tool_call_id failed-id
+ :content "it broke"}
+ (last msgs))))
+ (testing "order is preserved — call/result pairs stay adjacent"
+ (is (= [search-id search-id query-id query-id failed-id failed-id]
(mapv #(or (:tool_call_id %)
(-> % :tool_calls first :id))
- msgs)))
- (is (= ["search" "construct_notebook_query"]
- (->> msgs
- (filter #(= :assistant (:role %)))
- (map #(-> % :tool_calls first :name)))))))))))
+ msgs)))))))))
+
+(deftest message-history-degenerate-parts-test
+ (testing "nil input and outputs without an inner :output string replay safely"
+ (let [conv-id (str (random-uuid))
+ slack-ts "1712000000.000002"
+ call-id "call-degenerate"]
+ (mt/with-model-cleanup [:model/MetabotMessage [:model/MetabotConversation :created_at]]
+ (t2/insert! :model/MetabotConversation {:id conv-id :user_id (mt/user->id :rasta)})
+ (t2/insert! :model/MetabotMessage
+ {:conversation_id conv-id
+ :slack_msg_id slack-ts
+ :role "assistant"
+ :profile_id "slackbot"
+ :total_tokens 10
+ :data [{:type "tool-search"
+ :toolCallId call-id
+ :state "output-available"
+ :input nil
+ :output {}}]
+ :data_version 2})
+ (let [msgs (get (slackbot.persistence/message-history conv-id #{slack-ts}) slack-ts)]
+ (testing "nil input encodes as an empty JSON object, not \"null\""
+ (is (= "{}" (-> msgs first :tool_calls first :arguments))))
+ (testing "a successful call with no LLM-facing output replays as empty content, not a failure"
+ (is (= {:role :tool
+ :tool_call_id call-id
+ :content ""}
+ (second msgs)))))))))
+
+(deftest message-history-flat-string-output-test
+ (testing "a tool part whose :output is a bare string (not a map) replays that string as the tool content"
+ (let [conv-id (str (random-uuid))
+ slack-ts "1712000000.000003"
+ call-id "call-flat"]
+ (mt/with-model-cleanup [:model/MetabotMessage [:model/MetabotConversation :created_at]]
+ (t2/insert! :model/MetabotConversation {:id conv-id :user_id (mt/user->id :rasta)})
+ (t2/insert! :model/MetabotMessage
+ {:conversation_id conv-id
+ :slack_msg_id slack-ts
+ :role "assistant"
+ :profile_id "slackbot"
+ :total_tokens 3
+ :data [{:type "tool-search"
+ :toolCallId call-id
+ :state "output-available"
+ :input {:q "x"}
+ :output "just a string"}]
+ :data_version 2})
+ (let [msgs (get (slackbot.persistence/message-history conv-id #{slack-ts}) slack-ts)]
+ (is (= {:role :tool
+ :tool_call_id call-id
+ :content "just a string"}
+ (second msgs))))))))
-(deftest message-history-mixed-shape-test
- (testing "a single message row with both legacy and native blocks round-trips both shapes in order"
+(deftest message-history-migrates-v1-rows-test
+ (testing "data_version 1 rows are upgraded on read and replay through the v2 history reader"
(let [conv-id (str (random-uuid))
slack-ts "1712000000.000002"]
(mt/with-model-cleanup [:model/MetabotMessage [:model/MetabotConversation :created_at]]
@@ -130,25 +214,15 @@
{:_type "TOOL_RESULT"
:role "tool"
:tool_call_id "legacy-1"
- :content "legacy output"}
- {:type :tool-input
- :id "native-1"
- :function "construct_notebook_query"
- :arguments {:data_source_ids ["t1"]}}
- {:type :tool-output
- :id "native-1"
- :result {:output "native output"}}]})
+ :content "legacy output"}]
+ :data_version 1})
(let [msgs (get (slackbot.persistence/message-history conv-id #{slack-ts}) slack-ts)]
- (is (= 4 (count msgs)))
- (testing "legacy blocks still normalize correctly"
- (is (= :assistant (:role (nth msgs 0))))
- (is (= "legacy-1" (-> (nth msgs 0) :tool_calls first :id)))
- (is (= {:role :tool :tool_call_id "legacy-1" :content "legacy output"}
- (nth msgs 1))))
- (testing "native blocks translate alongside legacy blocks"
- (is (= "native-1" (-> (nth msgs 2) :tool_calls first :id)))
- (is (= {:role :tool :tool_call_id "native-1" :content "native output"}
- (nth msgs 3)))))))))
+ (is (= [{:role :assistant
+ :tool_calls [{:id "legacy-1" :name "search" :arguments "{}"}]}
+ {:role :tool
+ :tool_call_id "legacy-1"
+ :content "legacy output"}]
+ msgs)))))))
(deftest soft-delete-response-test
(testing "soft-delete-response! marks the assistant response as deleted"
@@ -165,7 +239,8 @@
:role "assistant"
:profile_id "test"
:total_tokens 5
- :data []})
+ :data []
+ :data_version 2})
(testing "returns true when a message is soft-deleted"
(is (true? (slackbot.persistence/soft-delete-response! channel-id slack-ts user-id))))
(let [msg (t2/select-one :model/MetabotMessage
@@ -199,7 +274,8 @@
:user_id requester-id
:profile_id "test"
:total_tokens 5
- :data []})
+ :data []
+ :data_version 2})
(testing "returns the requester, not the (potentially overwritten) conversation owner"
(is (= requester-id (slackbot.persistence/response-owner-user-id channel-id slack-ts))))
(testing "returns nil for an untracked message ts"
@@ -216,6 +292,7 @@
:user_id later-user-id
:profile_id "test"
:total_tokens 5
- :data []})
+ :data []
+ :data_version 2})
(is (= requester-id (slackbot.persistence/response-owner-user-id channel-id slack-ts)))
(is (= later-user-id (slackbot.persistence/response-owner-user-id channel-id second-slack-ts)))))))))
diff --git a/test/metabase/test/util.clj b/test/metabase/test/util.clj
index 4fd5d47a833..7fdc584c5dd 100644
--- a/test/metabase/test/util.clj
+++ b/test/metabase/test/util.clj
@@ -227,7 +227,8 @@
(fn [_] {:role "assistant"
:profile_id "gpt-5"
:total_tokens 0
- :data []})
+ :data []
+ :data_version 2})
:model/AiUsageLog
(fn [_] {:source "test"