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
fix: double new lines are not pasted correctly into metabot prompt input (#69493)
Maksym Yakubych7bf8c80d4257
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/AIMarkdown/AIMarkdown.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/AIMarkdown/AIMarkdown.tsxindex 1c3ddb51738..8e51305ffe4 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/AIMarkdown/AIMarkdown.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/AIMarkdown/AIMarkdown.tsx@@ -17,6 +17,9 @@ type AIMarkdownProps = MarkdownProps & {onInternalLinkClick?: (link: string) => void;};+const splitMessageLinesAsParagraphs = (message: string) =>+ message.replaceAll(/\r?\n|\r/g, "\n\n");+const getComponents = ({onInternalLinkClick,}: Pick<AIMarkdownProps, "onInternalLinkClick">) => ({@@ -60,18 +63,25 @@ const getComponents = ({});export const AIMarkdown = memo(- ({ className, onInternalLinkClick, ...props }: AIMarkdownProps) => {+ ({ className, onInternalLinkClick, children, ...props }: AIMarkdownProps) => {const components = useMemo(() => getComponents({ onInternalLinkClick }),[onInternalLinkClick],);+ const normalizedChildren = useMemo(+ () => splitMessageLinesAsParagraphs(children),+ [children],+ );+return (<MarkdownclassName={cx(S.aiMarkdown, className)}components={components}{...props}- />+ >+ {normalizedChildren}+ </Markdown>);},);diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsxindex 80e3819e42c..300d108854c 100644--- a/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/ui.unit.spec.tsx@@ -7,6 +7,7 @@ import { logout } from "metabase/auth/actions";import * as domModule from "metabase/lib/dom";import { METABOT_ERR_MSG } from "metabase-enterprise/metabot/constants";import { useMetabotAgent } from "metabase-enterprise/metabot/hooks";+import { metabotActions } from "metabase-enterprise/metabot/state";import { getMetabotInitialState } from "metabase-enterprise/metabot/state/reducer-utils";import { Metabot } from "../components/Metabot";@@ -144,6 +145,56 @@ describe("metabot > ui", () => {});});+ it("should render single newlines in user input as separate paragraphs", async () => {+ const { store } = setup();++ store.dispatch(+ metabotActions.addUserMessage({+ agentId: "omnibot",+ id: "user-1",+ type: "text",+ message: "first line\nsecond line",+ }),+ );++ const messages = await screen.findAllByTestId("metabot-chat-message");+ const userMessage = messages[0];+ const firstParagraph = within(userMessage).getByText("first line", {+ selector: "p",+ });+ const secondParagraph = within(userMessage).getByText("second line", {+ selector: "p",+ });++ expect(firstParagraph).toBeInTheDocument();+ expect(secondParagraph).toBeInTheDocument();+ });++ it("should preserve double newlines from user input", async () => {+ const { store } = setup();++ store.dispatch(+ metabotActions.addUserMessage({+ agentId: "omnibot",+ id: "user-2",+ type: "text",+ message: "first line\n\nsecond line",+ }),+ );++ const messages = await screen.findAllByTestId("metabot-chat-message");+ const userMessage = messages[0];+ const firstParagraph = within(userMessage).getByText("first line", {+ selector: "p",+ });+ const secondParagraph = within(userMessage).getByText("second line", {+ selector: "p",+ });++ expect(firstParagraph).toBeInTheDocument();+ expect(secondParagraph).toBeInTheDocument();+ });+it("should present the user an option to retry a response", async () => {setup();mockAgentEndpoint({ textChunks: whoIsYourFavoriteResponse });diff --git a/frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx b/frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.tsxindex 74787d838a6..20934f2401f 100644--- a/frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx+++ b/frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx@@ -25,6 +25,7 @@ import { getSetting } from "metabase/selectors/settings";import S from "./MetabotPromptInput.module.css";import {+ parseClipboardTextAsParagraphs,parseMetabotMessageToTiptapDoc,serializeTiptapToMetabotMessage,} from "./utils";@@ -165,6 +166,7 @@ export const MetabotPromptInput = forwardRef<clipboardTextSerializer: (content) => {return serializeTiptapToMetabotMessage(content.toJSON());},+ clipboardTextParser: parseClipboardTextAsParagraphs,},});diff --git a/frontend/src/metabase/metabot/components/MetabotPromptInput/utils.ts b/frontend/src/metabase/metabot/components/MetabotPromptInput/utils.tsindex a6e2a57640b..793e352f50d 100644--- a/frontend/src/metabase/metabot/components/MetabotPromptInput/utils.ts+++ b/frontend/src/metabase/metabot/components/MetabotPromptInput/utils.ts@@ -1,4 +1,10 @@import type { JSONContent } from "@tiptap/core";+import {+ Fragment,+ Node,+ type Node as ProseMirrorNode,+ Slice,+} from "@tiptap/pm/model";import { match } from "ts-pattern";import {@@ -53,6 +59,23 @@ export function serializeTiptapToMetabotMessage(content: JSONContent): string {return serializeNodes(content?.content || []).trim();}+export const parseClipboardTextAsParagraphs = (+ text: string,+ context: { doc: ProseMirrorNode },+): Slice => {+ const blocks = text.split(/(?:\r\n?|\n)/);++ const nodes = blocks.map((line) => {+ return Node.fromJSON(context.doc.type.schema, {+ type: "paragraph",+ ...(line.length > 0 ? { content: [{ type: "text", text: line }] } : {}),+ });+ });++ const fragment = Fragment.fromArray(nodes);+ return Slice.maxOpen(fragment);+};+const MENTION_REGEX = new RegExp(METABSE_PROTOCOL_MD_LINK, "g");export function parseMetabotMessageToTiptapDoc(text: string): JSONContent {