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
Report Metabot feedback directly to Harbormaster (#64040)
Nemanja Glumac788bf01ea9a9
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex efe5daad047..d320ccc7291 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1834,6 +1834,7 @@searchserversettings+ store-apisyncsystemtaskdiff --git a/enterprise/backend/src/metabase_enterprise/metabot_v3/api.clj b/enterprise/backend/src/metabase_enterprise/metabot_v3/api.cljindex cd1703a3504..01dc346b4c4 100644--- a/enterprise/backend/src/metabase_enterprise/metabot_v3/api.clj+++ b/enterprise/backend/src/metabase_enterprise/metabot_v3/api.clj@@ -1,6 +1,8 @@(ns metabase-enterprise.metabot-v3.api"`/api/ee/metabot-v3/` routes"(:require+ [clj-http.client :as http]+ [clojure.string :as str][metabase-enterprise.metabot-v3.api.document][metabase-enterprise.metabot-v3.api.metabot][metabase-enterprise.metabot-v3.client :as metabot-v3.client]@@ -14,7 +16,11 @@[metabase.api.routes.common :refer [+auth]][metabase.api.util.handlers :as handlers][metabase.app-db.core :as app-db]+ [metabase.premium-features.core :as premium-features]+ [metabase.store-api.core :as store-api][metabase.util :as u]+ [metabase.util.json :as json]+ [metabase.util.log :as log][metabase.util.malli.schema :as ms][toucan2.core :as t2]))@@ -81,6 +87,24 @@(metabot-v3.context/log body :llm.log/fe->be)(streaming-request body))+(api.macros/defendpoint :post "/feedback"+ "Proxy Metabot feedback to Harbormaster, adding the premium embedding token."+ [_route-params+ _query-params+ feedback :- :map]+ (let [token (premium-features/premium-embedding-token)+ base-url (store-api/store-api-url)]+ (when (or (str/blank? token) (str/blank? base-url))+ (throw (ex-info "Cannot build a request. The license token and/or Store api url are missing!" {})))+ (try+ (http/post (str base-url "/api/v2/metabot/feedback/" token)+ {:content-type :json+ :body (json/encode feedback)})+ api/generic-204-no-content+ (catch Exception e+ (log/error e "Failed to submit feedback to Harbormaster")+ (throw e)))))+(def ^{:arglists '([request respond raise])} routes"`/api/ee/metabot-v3` routes."(handlers/routesdiff --git a/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.clj b/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.cljindex b42167e9c56..87c8a082441 100644--- a/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot_v3/api_test.clj@@ -6,6 +6,8 @@[medley.core :as m][metabase-enterprise.metabot-v3.client-test :as client-test][metabase-enterprise.metabot-v3.util :as metabot.u]+ [metabase.premium-features.token-check :as token-check]+ [metabase.premium-features.token-check-test :as token-check-test][metabase.test :as mt][metabase.util.json :as json][toucan2.core :as t2]))@@ -58,3 +60,46 @@:role :assistant:data [{:role "assistant" :content "Hello from streaming!"}]}]messages))))))))))++(deftest feedback-endpoint-test+ (mt/with-premium-features #{:metabot-v3}+ (with-redefs [token-check/fetch-token-status (fn [_x]+ {:valid true+ :status "fake"+ :features ["test" "fixture"]+ :trial false})]+ (let [premium-token (token-check-test/random-token)+ store-url "http://hm.example"]+ (testing "Submits feedback to Harbormaster with token and base URL"+ (mt/with-temporary-setting-values [premium-embedding-token premium-token+ store-api-url store-url]+ (let [captured (atom nil)+ feedback {:metabot_id 1+ :feedback {:positive true+ :message_id "m-1"+ :freeform_feedback "ok"}+ :conversation_data {}+ :version "v0.0.0"+ :submission_time "2025-01-01T00:00:00Z"+ :is_admin false}+ expected-url (str store-url "/api/v2/metabot/feedback/" premium-token)]+ (mt/with-dynamic-fn-redefs+ [http/post (fn [url opts]+ (reset! captured {:url url+ :body (json/decode+kw (:body opts))}))]+ (let [_resp (mt/user-http-request :rasta :post 204 "ee/metabot-v3/feedback" feedback)]+ (is (= {:url expected-url :body feedback}+ @captured)))))))++ (testing "Returns 500 when http post fails"+ (mt/with-temporary-setting-values [premium-embedding-token premium-token]+ (mt/with-dynamic-fn-redefs+ [http/post (fn [_url _opts]+ (throw (ex-info "boom" {})))]+ (mt/user-http-request :rasta :post 500 "ee/metabot-v3/feedback" {:any "payload"}))))++ ;; We're not testing the branch where the store-api-url is missing because that defsetting+ ;; has the default value. It doesn't work well with `with-temporary-setting-values` helper.+ (testing "Throws when premium token is missing"+ (mt/with-temporary-setting-values [premium-embedding-token nil]+ (mt/user-http-request :rasta :post 500 "ee/metabot-v3/feedback" {:foo "bar"})))))))diff --git a/enterprise/frontend/src/metabase-enterprise/api/metabot.ts b/enterprise/frontend/src/metabase-enterprise/api/metabot.tsindex ed586c3105c..82cf5c90e4e 100644--- a/enterprise/frontend/src/metabase-enterprise/api/metabot.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/metabot.ts@@ -1,5 +1,6 @@import type {DeleteSuggestedMetabotPromptRequest,+ MetabotFeedback,MetabotGenerateContentRequest,MetabotGenerateContentResponse,MetabotId,@@ -87,6 +88,13 @@ export const metabotApi = EnterpriseApi.injectEndpoints({body: params,}),}),+ submitMetabotFeedback: builder.mutation<void, MetabotFeedback>({+ query: (params) => ({+ method: "POST",+ url: "/api/ee/metabot-v3/feedback",+ body: params,+ }),+ }),}),});@@ -97,4 +105,5 @@ export const {useDeleteSuggestedMetabotPromptMutation,useRegenerateSuggestedMetabotPromptsMutation,useLazyMetabotGenerateContentQuery,+ useSubmitMetabotFeedbackMutation,} = metabotApi;diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatMessage.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatMessage.tsxindex 08e9cbe309d..8ec0edf6b74 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatMessage.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatMessage.tsx@@ -1,9 +1,9 @@import { useClipboard } from "@mantine/hooks";import cx from "classnames";import { useCallback, useState } from "react";+import { t } from "ttag";import { useToast } from "metabase/common/hooks";-import { downloadObjectAsJson } from "metabase/lib/download";import {ActionIcon,Flex,@@ -12,6 +12,7 @@ import {type IconName,Text,} from "metabase/ui";+import { useSubmitMetabotFeedbackMutation } from "metabase-enterprise/api/metabot";import type {MetabotChatMessage,MetabotErrorMessage,@@ -260,19 +261,25 @@ export const Messages = ({modal: undefined,});+ const [submitMetabotFeedback] = useSubmitMetabotFeedbackMutation();+const submitFeedback = async (metabotFeedback: MetabotFeedback) => {const { message_id, positive } = metabotFeedback.feedback;- downloadObjectAsJson(metabotFeedback, `metabot-feedback-${message_id}`);- sendToast({ icon: "check", message: "Feedback downloaded successfully" });+ try {+ await submitMetabotFeedback(metabotFeedback).unwrap();+ sendToast({ icon: "check", message: t`Feedback submitted` });- setFeedbackState((prevState) => ({- submitted: {- ...prevState.submitted,- [message_id]: positive ? "positive" : "negative",- },- modal: undefined,- }));+ setFeedbackState((prevState) => ({+ submitted: {+ ...prevState.submitted,+ [message_id]: positive ? "positive" : "negative",+ },+ modal: undefined,+ }));+ } catch (error) {+ sendToast({ icon: "warning", message: t`Failed to submit feedback` });+ }};const onAgentMessageCopy = useCallback(diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsxindex c8d723e4b31..7268808034a 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotFeedbackModal.tsx@@ -126,7 +126,7 @@ export const MetabotFeedbackModal = ({</Button><Button variant="filled" type="submit">{c("This is a verb, not a noun",- ).t`Download`}</Button>+ ).t`Submit`}</Button></Group></Stack></Form>diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsxindex 4b5f9c5eba5..95ef811d13f 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/metabot.unit.spec.tsx@@ -16,7 +16,6 @@ import {} from "__support__/ui";import { logout } from "metabase/auth/actions";import * as domModule from "metabase/lib/dom";-import { downloadObjectAsJson } from "metabase/lib/download";import { useRegisterMetabotContextProvider } from "metabase/metabot";import {type MockStreamedEndpointParams,@@ -52,10 +51,6 @@ import {setVisible,} from "./state";-jest.mock("metabase/lib/download", () => ({- downloadObjectAsJson: jest.fn(),-}));-const mockAgentEndpoint = (params: MockStreamedEndpointParams) =>mockStreamedEndpoint("/api/ee/metabot-v3/agent-streaming", params);@@ -282,7 +277,10 @@ describe("metabot-streaming", () => {});it("should present the user an option to provide feedback", async () => {+ const feedbackPath = "path:/api/ee/metabot-v3/feedback";+setup();+ fetchMock.post(feedbackPath, 204);mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });await enterChatMessage("Who is your favorite?");@@ -294,10 +292,6 @@ describe("metabot-streaming", () => {within(lastMessage!).findByTestId("metabot-chat-message-thumbs-up");const thumbsDown = () =>within(lastMessage!).findByTestId("metabot-chat-message-thumbs-down");- const mockDownloadObjectAsJson =- downloadObjectAsJson as jest.MockedFunction<- typeof downloadObjectAsJson- >;expect(await thumbsUp()).toBeInTheDocument();expect(await thumbsDown()).toBeInTheDocument();@@ -306,11 +300,11 @@ describe("metabot-streaming", () => {expect(await feedbackModal()).toBeInTheDocument();await userEvent.click(await within(await feedbackModal()).findByRole("button", {- name: /Download/,+ name: /Submit/,}),);- expect(mockDownloadObjectAsJson).toHaveBeenCalledTimes(1);+ expect(fetchMock.callHistory.calls(feedbackPath)).toHaveLength(1);expect(await thumbsUp()).toBeDisabled();expect(await thumbsDown()).toBeDisabled();