The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
[Metabot] Add metabot message validation schemas (#75546)
Sloan Sparger94dc98834efe
diff --git a/src/metabase/metabot/schema/v1.clj b/src/metabase/metabot/schema/v1.cljnew file mode 100644index 00000000000..dcb5fa6d86a--- /dev/null+++ b/src/metabase/metabot/schema/v1.clj@@ -0,0 +1,142 @@+(ns metabase.metabot.schema.v1+ "Schemas for the v1 `metabot_message.data` storage formats, which are two sub-formats:+ one written by the deprecated external AI service and another for our native Clojure+ implementation."+ (:require+ [metabase.util.malli.registry :as mr]))++(set! *warn-on-reflection* true)++(mr/def ::ai-service-entry+ "An entry written by the external ai-service, derived from an AI SDK v4 `DataStreamPart`+ (See https://github.com/vercel/ai, ai@4, packages/ui-utils/src/data-stream-parts.ts).+ The `:_type` tags are the v4 part names, uppercased, but the shapes diverge from v4's:++ - only a subset of the v4 part types were ever used+ - key names often use snake_case instead of camelCase+ - some fields are renamed:+ - `tool_call`'s `{toolCallId, toolName, args}` is stored as `:tool_calls [{:id :name :arguments}]`+ - `tool_result`'s `{toolCallId, result}` as `{tool_call_id, content}`"+ [:multi {:dispatch :_type}+ ["TEXT" [:map {:closed true}+ [:role [:= "assistant"]]+ [:_type [:= "TEXT"]]+ [:content :string]]]+ ["ERROR" [:map {:closed true}+ [:role [:= "assistant"]]+ [:_type [:= "ERROR"]]+ [:content :string]]]+ ["DATA" [:map {:closed true}+ [:_type [:= "DATA"]]+ [:type :string]+ [:version [:= 1]]+ [:value :any]]]+ ["TOOL_CALL" [:map {:closed true}+ [:role [:= "assistant"]]+ [:_type [:= "TOOL_CALL"]]+ [:tool_calls [:sequential [:map {:closed true}+ [:id :string]+ [:name :string]+ [:arguments :string]]]]]]+ ["TOOL_RESULT" [:map {:closed true}+ [:role [:= "tool"]]+ [:_type [:= "TOOL_RESULT"]]+ [:tool_call_id :string]+ [:content :string]]]+ ["FINISH_MESSAGE" [:map {:closed true}+ [:role [:= "assistant"]]+ [:_type [:= "FINISH_MESSAGE"]]+ [:finish_reason :string]+ [:usage :map]]]])++(mr/def ::native-entry+ "An entry written by the native clojure agent."+ [:multi {:dispatch :type}+ ["text" [:map {:closed true}+ [:type [:= "text"]]+ [:id {:optional true} [:maybe :string]]+ [:text :string]]]+ ["tool-input" [:map {:closed true}+ [:type [:= "tool-input"]]+ [:id :string]+ [:function :string]+ [:arguments [:maybe :map]]]]+ ["tool-output" [:map {:closed true}+ [:type [:= "tool-output"]]+ [:id :string]+ [:function {:optional true} [:maybe :string]]+ [:result [:maybe [:map+ [:output {:optional true} :any]+ [:structured-output {:optional true} :map]+ [:structured_output {:optional true} :map]]]]+ [:error {:optional true} [:maybe [:or :map :string]]]+ [:duration-ms {:optional true} [:maybe number?]]]]+ ["data" [:map {:closed true}+ [:type [:= "data"]]+ [:data-type :string]+ [:version {:optional true} [:= 1]]+ [:data :any]]]+ ["error" [:or+ [:map {:closed true}+ [:type [:= "error"]]+ [:error :map]]+ [:map {:closed true}+ [:type [:= "error"]]+ [:errorText :string]]]]])++(mr/def ::user-message+ "A user message."+ [:map {:closed true}+ [:role [:= "user"]]+ [:content :string]])++(mr/def ::empty-data+ "An empty `metabot_message.data` value: assistant placeholder rows and aborted turns."+ [:sequential {:max 0} :any])++(mr/def ::user-message-data+ "A `metabot_message.data` value holding a user message."+ [:sequential {:min 1} ::user-message])++(mr/def ::ai-service-data+ "A `metabot_message.data` value written by the deprecated external ai-service."+ [:sequential {:min 1} ::ai-service-entry])++(mr/def ::native-data+ "A `metabot_message.data` value written by the native clojure agent."+ [:sequential {:min 1} ::native-entry])++(mr/def ::message-data+ "`metabot_message.data` v1 column format. Entries within a row are homogeneous.+ Assistant placeholder rows are empty `[]`."+ [:or ::empty-data ::user-message-data ::ai-service-data ::native-data])++(comment+ ;; validate a CSV dump of the metabot_message table against the v1 schemas+ (require '[clojure.data.csv :as csv]+ '[clojure.java.io :as io]+ '[malli.error :as me]+ '[metabase.util.json :as json])++ (defn validate-csv [path]+ (with-open [reader (io/reader path)]+ (let [[header & rows] (csv/read-csv reader)+ column-index (fn [column] (first (keep-indexed #(when (= column %2) %1) header)))+ id-idx (column-index "id")+ data-idx (column-index "data")+ explain (mr/explainer ::message-data)]+ (reduce (fn [acc row]+ (let [raw (nth row data-idx)+ parsed (try (json/decode+kw raw) (catch Exception e e))+ error (if (instance? Exception parsed)+ {:json-parse (ex-message parsed)}+ (some-> (explain parsed) me/humanize))]+ (cond-> (update acc :row-count inc)+ error (-> (update :failure-count inc)+ (update :failures conj {:message-id (when id-idx (nth row id-idx))+ :message (if (instance? Exception parsed) raw parsed)+ :error error})))))+ {:row-count 0 :failure-count 0 :failures []}+ rows))))++ (validate-csv "./metabot_message_dump.csv"))diff --git a/src/metabase/metabot/schema/v2.clj b/src/metabase/metabot/schema/v2.cljnew file mode 100644index 00000000000..d0bf74cd2ed--- /dev/null+++ b/src/metabase/metabot/schema/v2.clj@@ -0,0 +1,404 @@+(ns metabase.metabot.schema.v2+ "Schemas for the v2 `metabot_message.data` storage and over the wire message format.++ These schemas strictly reimplement Vercel AI SDK v6's message formats:+ - UIMessageChunk (wire): packages/ai/src/ui-message-stream/ui-message-chunks.ts+ - UIMessage/part (at-rest): packages/ai/src/ui/ui-messages.ts and+ packages/ai/src/ui/validate-ui-messages.ts++ Apache License 2.0, © 2023 Vercel, Inc.+ Upstream: https://github.com/vercel/ai (pinned to [email protected])+ License: https://github.com/vercel/ai/blob/ai%406.0.37/LICENSE++ The goal is exact equivalence with upstream's runtime validation behavior (verified+ empirically against `validateUIMessages`/`uiMessageChunkSchema` under the zod version the+ AI SDK pins). The zod -> malli transcription conventions:++ - `z.strictObject` (all wire chunks) -> closed maps+ - `z.object` (all at-rest parts) -> open maps: upstream strips undeclared keys from its+ parse result rather than rejecting them, so undeclared keys must not fail validation+ - `z.unknown()` fields (`:input`, `:output`, `:data`, `:messageMetadata`) -> optional `:any`+ keys: zod does not enforce key presence for `unknown` fields, whether or not `.optional()`+ is chained (https://github.com/colinhacks/zod/issues/1628)+ - `z.never().optional()` fields -> optional `::never` keys, rejecting the key when present.+ This is what keeps the tool-state variants mutually exclusive"+ (:require+ [clojure.string :as str]+ [metabase.util.malli.registry :as mr]))++(set! *warn-on-reflection* true)++(defn- data-type? [t] (and (string? t) (str/starts-with? t "data-")))+(defn- tool-type? [t] (and (string? t) (str/starts-with? t "tool-")))++(mr/def ::provider-metadata+ [:map-of :keyword [:map-of :keyword :any]])++(mr/def ::never+ "`z.never().optional()`: the key must be absent. (zod also tolerates a present `undefined`+ value, which JSON cannot represent.)"+ [:not :any])++(mr/def ::ui-message-chunk+ [:multi {:dispatch (fn [chunk]+ (let [t (:type chunk)]+ (if (data-type? t) ::data t)))}+ ["text-start" [:map {:closed true}+ [:type [:= "text-start"]]+ [:id :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["text-delta" [:map {:closed true}+ [:type [:= "text-delta"]]+ [:id :string]+ [:delta :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["text-end" [:map {:closed true}+ [:type [:= "text-end"]]+ [:id :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["error" [:map {:closed true}+ [:type [:= "error"]]+ [:errorText :string]]]+ ["tool-input-start" [:map {:closed true}+ [:type [:= "tool-input-start"]]+ [:toolCallId :string]+ [:toolName :string]+ [:providerExecuted {:optional true} :boolean]+ [:dynamic {:optional true} :boolean]+ [:title {:optional true} :string]]]+ ["tool-input-delta" [:map {:closed true}+ [:type [:= "tool-input-delta"]]+ [:toolCallId :string]+ [:inputTextDelta :string]]]+ ["tool-input-available" [:map {:closed true}+ [:type [:= "tool-input-available"]]+ [:toolCallId :string]+ [:toolName :string]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:providerMetadata {:optional true} ::provider-metadata]+ [:dynamic {:optional true} :boolean]+ [:title {:optional true} :string]]]+ ["tool-input-error" [:map {:closed true}+ [:type [:= "tool-input-error"]]+ [:toolCallId :string]+ [:toolName :string]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:providerMetadata {:optional true} ::provider-metadata]+ [:dynamic {:optional true} :boolean]+ [:errorText :string]+ [:title {:optional true} :string]]]+ ["tool-approval-request" [:map {:closed true}+ [:type [:= "tool-approval-request"]]+ [:approvalId :string]+ [:toolCallId :string]]]+ ["tool-output-available" [:map {:closed true}+ [:type [:= "tool-output-available"]]+ [:toolCallId :string]+ [:output {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:dynamic {:optional true} :boolean]+ [:preliminary {:optional true} :boolean]]]+ ["tool-output-error" [:map {:closed true}+ [:type [:= "tool-output-error"]]+ [:toolCallId :string]+ [:errorText :string]+ [:providerExecuted {:optional true} :boolean]+ [:dynamic {:optional true} :boolean]]]+ ["tool-output-denied" [:map {:closed true}+ [:type [:= "tool-output-denied"]]+ [:toolCallId :string]]]+ ["reasoning-start" [:map {:closed true}+ [:type [:= "reasoning-start"]]+ [:id :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["reasoning-delta" [:map {:closed true}+ [:type [:= "reasoning-delta"]]+ [:id :string]+ [:delta :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["reasoning-end" [:map {:closed true}+ [:type [:= "reasoning-end"]]+ [:id :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["source-url" [:map {:closed true}+ [:type [:= "source-url"]]+ [:sourceId :string]+ [:url :string]+ [:title {:optional true} :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["source-document" [:map {:closed true}+ [:type [:= "source-document"]]+ [:sourceId :string]+ [:mediaType :string]+ [:title :string]+ [:filename {:optional true} :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["file" [:map {:closed true}+ [:type [:= "file"]]+ [:url :string]+ [:mediaType :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ [::data [:map {:closed true}+ [:type [:fn data-type?]]+ [:id {:optional true} :string]+ [:data {:optional true} :any]+ [:transient {:optional true} :boolean]]]+ ["start-step" [:map {:closed true}+ [:type [:= "start-step"]]]]+ ["finish-step" [:map {:closed true}+ [:type [:= "finish-step"]]]]+ ["start" [:map {:closed true}+ [:type [:= "start"]]+ [:messageId {:optional true} :string]+ [:messageMetadata {:optional true} :any]]]+ ["finish" [:map {:closed true}+ [:type [:= "finish"]]+ [:finishReason {:optional true}+ [:enum "stop" "length" "content-filter" "tool-calls" "error" "other"]]+ [:messageMetadata {:optional true} :any]]]+ ["abort" [:map {:closed true}+ [:type [:= "abort"]]+ [:reason {:optional true} :string]]]+ ["message-metadata" [:map {:closed true}+ [:type [:= "message-metadata"]]+ [:messageMetadata {:optional true} :any]]]])++(mr/def ::tool-ui-part+ [:multi {:dispatch :state}+ ["input-streaming" [:map+ [:type [:fn tool-type?]]+ [:toolCallId :string]+ [:state [:= "input-streaming"]]+ [:providerExecuted {:optional true} :boolean]+ [:input {:optional true} :any]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:approval {:optional true} ::never]]]+ ["input-available" [:map+ [:type [:fn tool-type?]]+ [:toolCallId :string]+ [:state [:= "input-available"]]+ [:providerExecuted {:optional true} :boolean]+ [:input {:optional true} :any]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval {:optional true} ::never]]]+ ["approval-requested" [:map+ [:type [:fn tool-type?]]+ [:toolCallId :string]+ [:state [:= "approval-requested"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval [:map+ [:id :string]+ [:approved {:optional true} ::never]+ [:reason {:optional true} ::never]]]]]+ ["approval-responded" [:map+ [:type [:fn tool-type?]]+ [:toolCallId :string]+ [:state [:= "approval-responded"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval [:map+ [:id :string]+ [:approved :boolean]+ [:reason {:optional true} :string]]]]]+ ["output-available" [:map+ [:type [:fn tool-type?]]+ [:toolCallId :string]+ [:state [:= "output-available"]]+ [:providerExecuted {:optional true} :boolean]+ [:input {:optional true} :any]+ [:output {:optional true} :any]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:preliminary {:optional true} :boolean]+ [:approval {:optional true} [:map+ [:id :string]+ [:approved [:= true]]+ [:reason {:optional true} :string]]]]]+ ["output-error" [:map+ [:type [:fn tool-type?]]+ [:toolCallId :string]+ [:state [:= "output-error"]]+ [:providerExecuted {:optional true} :boolean]+ [:input {:optional true} :any]+ [:rawInput {:optional true} :any]+ [:output {:optional true} ::never]+ [:errorText :string]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval {:optional true} [:map+ [:id :string]+ [:approved [:= true]]+ [:reason {:optional true} :string]]]]]+ ["output-denied" [:map+ [:type [:fn tool-type?]]+ [:toolCallId :string]+ [:state [:= "output-denied"]]+ [:providerExecuted {:optional true} :boolean]+ [:input {:optional true} :any]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval [:map+ [:id :string]+ [:approved [:= false]]+ [:reason {:optional true} :string]]]]]])++(mr/def ::dynamic-tool-ui-part+ [:multi {:dispatch :state}+ ["input-streaming" [:map+ [:type [:= "dynamic-tool"]]+ [:toolName :string]+ [:toolCallId :string]+ [:state [:= "input-streaming"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:approval {:optional true} ::never]]]+ ["input-available" [:map+ [:type [:= "dynamic-tool"]]+ [:toolName :string]+ [:toolCallId :string]+ [:state [:= "input-available"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval {:optional true} ::never]]]+ ["approval-requested" [:map+ [:type [:= "dynamic-tool"]]+ [:toolName :string]+ [:toolCallId :string]+ [:state [:= "approval-requested"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval [:map+ [:id :string]+ [:approved {:optional true} ::never]+ [:reason {:optional true} ::never]]]]]+ ["approval-responded" [:map+ [:type [:= "dynamic-tool"]]+ [:toolName :string]+ [:toolCallId :string]+ [:state [:= "approval-responded"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval [:map+ [:id :string]+ [:approved :boolean]+ [:reason {:optional true} :string]]]]]+ ["output-available" [:map+ [:type [:= "dynamic-tool"]]+ [:toolName :string]+ [:toolCallId :string]+ [:state [:= "output-available"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} :any]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:preliminary {:optional true} :boolean]+ [:approval {:optional true} [:map+ [:id :string]+ [:approved [:= true]]+ [:reason {:optional true} :string]]]]]+ ["output-error" [:map+ [:type [:= "dynamic-tool"]]+ [:toolName :string]+ [:toolCallId :string]+ [:state [:= "output-error"]]+ [:input {:optional true} :any]+ [:rawInput {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText :string]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval {:optional true} [:map+ [:id :string]+ [:approved [:= true]]+ [:reason {:optional true} :string]]]]]+ ["output-denied" [:map+ [:type [:= "dynamic-tool"]]+ [:toolName :string]+ [:toolCallId :string]+ [:state [:= "output-denied"]]+ [:input {:optional true} :any]+ [:providerExecuted {:optional true} :boolean]+ [:output {:optional true} ::never]+ [:errorText {:optional true} ::never]+ [:callProviderMetadata {:optional true} ::provider-metadata]+ [:approval [:map+ [:id :string]+ [:approved [:= false]]+ [:reason {:optional true} :string]]]]]])++(mr/def ::ui-message-part+ [:multi {:dispatch (fn [part]+ (let [t (:type part)]+ (cond+ (= "dynamic-tool" t) t+ (tool-type? t) ::tool+ (data-type? t) ::data+ :else t)))}+ ["text" [:map+ [:type [:= "text"]]+ [:text :string]+ [:state {:optional true} [:enum "streaming" "done"]]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["reasoning" [:map+ [:type [:= "reasoning"]]+ [:text :string]+ [:state {:optional true} [:enum "streaming" "done"]]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["source-url" [:map+ [:type [:= "source-url"]]+ [:sourceId :string]+ [:url :string]+ [:title {:optional true} :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["source-document" [:map+ [:type [:= "source-document"]]+ [:sourceId :string]+ [:mediaType :string]+ [:title :string]+ [:filename {:optional true} :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["file" [:map+ [:type [:= "file"]]+ [:mediaType :string]+ [:filename {:optional true} :string]+ [:url :string]+ [:providerMetadata {:optional true} ::provider-metadata]]]+ ["step-start" [:map+ [:type [:= "step-start"]]]]+ ["dynamic-tool" ::dynamic-tool-ui-part]+ [::tool ::tool-ui-part]+ [::data [:map+ [:type [:fn data-type?]]+ [:id {:optional true} :string]+ [:data {:optional true} :any]]]])++(mr/def ::ui-message+ [:map+ [:id :string]+ [:role [:enum "system" "user" "assistant"]]+ [:metadata {:optional true} :any]+ [:parts [:sequential {:min 1} ::ui-message-part]]])diff --git a/test/metabase/metabot/schema/v1_test.clj b/test/metabase/metabot/schema/v1_test.cljnew file mode 100644index 00000000000..0c362981fee--- /dev/null+++ b/test/metabase/metabot/schema/v1_test.clj@@ -0,0 +1,86 @@+(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+ "A representative entry for each ai-service `:_type`."+ [{:role "assistant" :_type "TEXT" :content "Hi there"}+ {:role "assistant" :_type "ERROR" :content "Something went wrong"}+ {:_type "DATA" :type "navigate_to" :version 1 :value "/question/1"}+ {:role "assistant" :_type "TOOL_CALL" :tool_calls [{:id "tc1" :name "search" :arguments "{\"q\":\"x\"}"}]}+ {:role "tool" :_type "TOOL_RESULT" :tool_call_id "tc1" :content "<result/>"}+ {:role "assistant" :_type "FINISH_MESSAGE" :finish_reason "stop" :usage {}}])++(deftest ^:parallel ai-service-entries-validate-test+ (is (nil? (mr/explain [:sequential ::schema.v1/ai-service-entry] ai-service-entries))))++(def ^:private native-entries+ "A representative entry for each native `:type`."+ [{:type "text" :id "t1" :text "Hello"}+ {:type "tool-input" :id "tc1" :function "search" :arguments {:q "x"}}+ {:type "tool-output" :id "tc1" :function "search"+ :result {:output "rows" :structured-output {:type "table"}}+ :error nil :duration-ms 12.5}+ {:type "tool-output" :id "tc1"+ :result {:output "rows" :instructions "..." :resources [] :data-parts []}}+ {:type "data" :data-type "navigate_to" :version 1 :data "/question/1"}+ {:type "data" :data-type "debug_log" :version 1 :data [{:iteration 0 :request {} :response []}]}+ {:type "error" :error {:message "boom"}}+ {:type "error" :errorText "Overloaded"}])++(deftest ^:parallel native-entries-validate-test+ (is (nil? (mr/explain [:sequential ::schema.v1/native-entry] native-entries))))++(deftest ^:parallel user-message-validates-test+ (is (nil? (mr/explain ::schema.v1/user-message {:role "user" :content "Do we have orders data?"}))))++(deftest ^:parallel unknown-entry-type-test+ (testing "unknown ai-service :_type fails"+ (is (not (mr/validate ::schema.v1/ai-service-entry+ {:role "assistant" :_type "NO_SUCH" :content "hi"}))))+ (testing "unknown native :type fails"+ (is (not (mr/validate ::schema.v1/native-entry+ {:type "no-such" :text "hi"}))))+ (testing "entries with a stray key fail the closed maps"+ (is (not (mr/validate ::schema.v1/ai-service-entry+ {:role "assistant" :_type "TEXT" :content "hi" :stray "x"})))+ (is (not (mr/validate ::schema.v1/native-entry+ {:type "text" :id "t1" :text "hi" :stray "x"})))+ (is (not (mr/validate ::schema.v1/user-message+ {:role "user" :content "hi" :stray "x"})))))++(deftest ^:parallel message-data-test+ (testing "assistant placeholder rows are empty"+ (is (mr/validate ::schema.v1/message-data [])))+ (testing "homogeneous rows validate"+ (is (mr/validate ::schema.v1/message-data+ [{:role "user" :content "hi"}]))+ (is (mr/validate ::schema.v1/message-data+ [{:role "assistant" :_type "TEXT" :content "Hello"}+ {:role "assistant" :_type "FINISH_MESSAGE" :finish_reason "stop" :usage {}}]))+ (is (mr/validate ::schema.v1/message-data+ [{:type "tool-input" :id "tc1" :function "search" :arguments {:q "x"}}+ {:type "tool-output" :id "tc1" :result {:output "rows"} :error nil :duration-ms 3}+ {:type "text" :id "t1" :text "Found it"}])))+ (testing "rows mixing sub-variants fail"+ (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/schema/v2_test.clj b/test/metabase/metabot/schema/v2_test.cljnew file mode 100644index 00000000000..1a80e69dc92--- /dev/null+++ b/test/metabase/metabot/schema/v2_test.clj@@ -0,0 +1,115 @@+(ns metabase.metabot.schema.v2-test+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase.metabot.schema.v2 :as schema.v2]+ [metabase.util.malli.registry :as mr]))++(def ^:private chunks+ "A representative chunk for each uiMessageChunkSchema union member."+ [{:type "text-start" :id "t1"}+ {:type "text-delta" :id "t1" :delta "Hi" :providerMetadata {:openai {:x 1}}}+ {:type "text-end" :id "t1"}+ {:type "error" :errorText "boom"}+ {:type "tool-input-start" :toolCallId "tc1" :toolName "search" :dynamic false}+ {:type "tool-input-delta" :toolCallId "tc1" :inputTextDelta "{\"q\":"}+ {:type "tool-input-available" :toolCallId "tc1" :toolName "search" :input {:q "x"}}+ {:type "tool-input-error" :toolCallId "tc1" :toolName "search" :input nil :errorText "bad input"}+ {:type "tool-approval-request" :approvalId "ap1" :toolCallId "tc1"}+ {:type "tool-output-available" :toolCallId "tc1" :output {:rows []} :preliminary true}+ {:type "tool-output-error" :toolCallId "tc1" :errorText "failed"}+ {:type "tool-output-denied" :toolCallId "tc1"}+ {:type "reasoning-start" :id "r1"}+ {:type "reasoning-delta" :id "r1" :delta "thinking"}+ {:type "reasoning-end" :id "r1"}+ {:type "source-url" :sourceId "s1" :url "https://example.com" :title "Example"}+ {:type "source-document" :sourceId "s1" :mediaType "application/pdf" :title "Doc"}+ {:type "file" :url "data:text/plain;base64,SGk=" :mediaType "text/plain"}+ {:type "data-navigate_to" :data "/question/1" :transient true}+ {:type "start-step"}+ {:type "finish-step"}+ {:type "start" :messageId "m1"}+ {:type "finish" :finishReason "stop"}+ {:type "abort" :reason "user cancelled"}+ {:type "message-metadata" :messageMetadata {:model "gpt-4o"}}])++(def ^:private ui-message-parts+ "A representative part for each uiMessagePartSchema union member, including every tool state."+ [{:type "text" :text "Hello" :state "done"}+ {:type "reasoning" :text "let me think" :state "streaming"}+ {:type "source-url" :sourceId "s1" :url "https://example.com"}+ {:type "source-document" :sourceId "s1" :mediaType "application/pdf" :title "Doc"}+ {:type "file" :mediaType "text/plain" :filename "a.txt" :url "https://example.com/a"}+ {:type "step-start"}+ {:type "tool-search" :toolCallId "tc1" :state "input-streaming"}+ {:type "tool-search" :toolCallId "tc1" :state "input-available" :input {:q "x"}}+ {:type "tool-search" :toolCallId "tc1" :state "approval-requested" :input {:q "x"}+ :approval {:id "ap1"}}+ {:type "tool-search" :toolCallId "tc1" :state "approval-responded" :input {:q "x"}+ :approval {:id "ap1" :approved true :reason "ok"}}+ {:type "tool-search" :toolCallId "tc1" :state "output-available" :input {:q "x"}+ :output {:rows []} :preliminary false}+ {:type "tool-search" :toolCallId "tc1" :state "output-error" :input {:q "x"}+ :rawInput "{\"q\"" :errorText "failed"}+ {:type "tool-search" :toolCallId "tc1" :state "output-denied" :input {:q "x"}+ :approval {:id "ap1" :approved false}}+ {:type "dynamic-tool" :toolName "mcp_tool" :toolCallId "tc1" :state "output-available"+ :input {:q "x"} :output "result"}+ {:type "data-navigate_to" :id "d1" :data "/question/1"}])++(deftest ^:parallel chunks-validate-test+ (is (nil? (mr/explain [:sequential ::schema.v2/ui-message-chunk] chunks))))++(deftest ^:parallel unknown-chunk-type-test+ (is (not (mr/validate ::schema.v2/ui-message-chunk {:type "no-such-chunk"})))+ (is (not (mr/validate ::schema.v2/ui-message-chunk {:id "t1"}))))++(deftest ^:parallel parts-validate-test+ (is (nil? (mr/explain [:sequential ::schema.v2/ui-message-part] ui-message-parts))))++(deftest ^:parallel aisdk-runtime-validator-equivalence-test+ (testing "at-rest parts are open maps: the upstream validator strips undeclared keys rather than rejecting"+ (is (mr/validate ::schema.v2/ui-message-part+ {:type "text" :text "hi" :somethingExtra 1})))+ (testing "wire chunks are closed maps: the upstream validator rejects undeclared keys"+ (is (not (mr/validate ::schema.v2/ui-message-chunk+ {:type "text-start" :id "t1" :somethingExtra 1}))))+ (testing "untyped fields like :input and :output may be absent entirely"+ (is (mr/validate ::schema.v2/ui-message-part+ {:type "tool-search" :toolCallId "tc1" :state "output-available"})))+ (testing "forbidden fields reject any present value, keeping tool states mutually exclusive"+ (is (not (mr/validate ::schema.v2/ui-message-part+ {:type "tool-search" :toolCallId "tc1" :state "output-available"+ :errorText "boom"})))+ (is (not (mr/validate ::schema.v2/ui-message-part+ {:type "tool-search" :toolCallId "tc1" :state "input-available"+ :input {} :approval {:id "a1"}})))))++(deftest ^:parallel tool-approval-shape-test+ (testing "approval-requested approvals carry only an id"+ (let [part {:type "tool-search" :toolCallId "tc1" :state "approval-requested"+ :input {:q "x"} :approval {:id "ap1"}}]+ (is (mr/validate ::schema.v2/ui-message-part part))+ (is (not (mr/validate ::schema.v2/ui-message-part (assoc-in part [:approval :approved] true))))))+ (testing "approval-responded approvals require the approved flag"+ (let [part {:type "tool-search" :toolCallId "tc1" :state "approval-responded"+ :input {:q "x"} :approval {:id "ap1" :approved false :reason "no"}}]+ (is (mr/validate ::schema.v2/ui-message-part part))+ (is (not (mr/validate ::schema.v2/ui-message-part (update part :approval dissoc :approved))))))+ (testing "output-available approvals must be approved"+ (let [part {:type "tool-search" :toolCallId "tc1" :state "output-available"+ :input {:q "x"} :output {:rows []} :approval {:id "ap1" :approved true}}]+ (is (mr/validate ::schema.v2/ui-message-part part))+ (is (not (mr/validate ::schema.v2/ui-message-part (assoc-in part [:approval :approved] false))))))+ (testing "output-denied approvals must be denied"+ (let [part {:type "dynamic-tool" :toolName "mcp_tool" :toolCallId "tc1" :state "output-denied"+ :input {:q "x"} :approval {:id "ap1" :approved false}}]+ (is (mr/validate ::schema.v2/ui-message-part part))+ (is (not (mr/validate ::schema.v2/ui-message-part (assoc-in part [:approval :approved] true)))))))++(deftest ^:parallel ui-message-validates-test+ (is (nil? (mr/explain ::schema.v2/ui-message+ {:id "msg1"+ :role "assistant"+ :parts [{:type "text" :text "Hello"}+ {:type "tool-search" :toolCallId "tc1" :state "output-available"+ :input {:q "x"} :output {:rows []}}]}))))