Skip to content

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

feat(sdk-bundle): stacked and sidebar layouts for metabot questions (#64142)

Phoomparin Mano6e17e96bf064
diff --git a/docs/embedding/sdk/ai-chat.md b/docs/embedding/sdk/ai-chat.md
index 1dbd056f8fb..42f46334cc6 100644
--- a/docs/embedding/sdk/ai-chat.md
+++ b/docs/embedding/sdk/ai-chat.md
@@ -24,9 +24,11 @@ You can check out a [demo of the AI chat component](https://embedded-analytics-s
```typescript
{% include_file "{{ dirname }}/snippets/questions/ai-question.tsx" %}
```
+
## API reference
- [Component](./api/MetabotQuestion.html)
+- [Props](./api/MetabotQuestionProps.html)
## Setting up AI chat
@@ -40,3 +42,11 @@ To configure your embedded AI chat in your Metabase:
When embedding the Metabot component in your app, you should specify a collection that embedded Metabot is allowed to use for creating queries. Embedded Metabot will only have access to that collection.
For tips and more, see [Metabot settings](../../ai/settings.md).
+
+## Layout
+
+Use the `layout` prop to specify which layout to use for the Metabot component:
+
+- `auto` (default): Metabot uses the `stacked` layout on mobile screens, and a `sidebar` layout on larger screens.
+- `stacked`: the question visualization stacks on top of the chat interface.
+- `sidebar`: the question visualization appears to the left of the chat interface, which is on a sidebar on the right.
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 d6abb4e1710..f541d538521 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
@@ -171,4 +171,101 @@ describe("scenarios > embedding-sdk > metabot-question", () => {
});
});
});
+
+ it("should show suggestion buttons when no messages exist", () => {
+ setup(metabotResponse);
+ mockSuggestedPrompts();
+
+ mountSdkContent(<MetabotQuestion />);
+
+ getSdkRoot().within(() => {
+ // Verify suggestion buttons are visible
+ cy.findAllByTestId("metabot-suggestion-button", { timeout: 10_000 })
+ .should("have.length", 3)
+ .should("be.visible");
+
+ // Verify suggestion texts are shown in order
+ cy.findAllByTestId("metabot-suggestion-button")
+ .eq(0)
+ .should("contain.text", "Show me total sales by product");
+
+ cy.findAllByTestId("metabot-suggestion-button")
+ .eq(1)
+ .should("contain.text", "What are the top performing products?");
+
+ cy.findAllByTestId("metabot-suggestion-button")
+ .eq(2)
+ .should("contain.text", "How many orders were placed last month?");
+
+ // Clicking on a suggestion should remove the suggestion buttons and add the user's message
+ cy.findByText("Show me total sales by product").click();
+ cy.findAllByTestId("metabot-suggestion-button").should("not.exist");
+ cy.findAllByTestId("metabot-chat-message").should("have.length", 2);
+
+ cy.findAllByTestId("metabot-chat-message")
+ .first()
+ .should("contain.text", "Show me total sales by product");
+ });
+ });
+
+ it("should switch to stacked layout on small viewport", () => {
+ setup(metabotResponse);
+ mountSdkContent(<MetabotQuestion />);
+
+ getSdkRoot().within(() => {
+ // On desktop, use the sidebar layout
+ cy.findByTestId("metabot-question-container").should(
+ "have.attr",
+ "data-layout",
+ "sidebar",
+ );
+
+ // On mobile, use the stacked layout
+ cy.viewport(400, 600);
+ cy.findByTestId("metabot-overflow-button").should("be.visible");
+ cy.findByTestId("metabot-question-container").should(
+ "have.attr",
+ "data-layout",
+ "stacked",
+ );
+
+ // On desktop, use the sidebar layout
+ cy.viewport(1200, 800);
+ cy.findByTestId("metabot-overflow-button").should("not.be.visible");
+ cy.findByTestId("metabot-question-container").should(
+ "have.attr",
+ "data-layout",
+ "sidebar",
+ );
+ });
+ });
});
+
+const mockSuggestedPrompts = () => {
+ cy.intercept(
+ "GET",
+ "/api/ee/metabot-v3/metabot/2/prompt-suggestions?limit=3&sample=true",
+ {
+ statusCode: 200,
+ body: {
+ prompts: [
+ {
+ id: 1,
+ prompt: "Show me total sales by product",
+ },
+ {
+ id: 2,
+ prompt: "What are the top performing products?",
+ },
+ {
+ id: 3,
+ prompt: "How many orders were placed last month?",
+ },
+ ],
+ limit: 3,
+ offset: 0,
+ total: 3,
+ },
+ },
+ ).as("suggestedPrompts");
+};
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
index a6718913156..a2145806436 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/private/SdkQuestionDefaultView/SdkQuestionDefaultView.tsx
@@ -246,7 +246,12 @@ export const SdkQuestionDefaultView = ({
)}
</Stack>
- <Box className={InteractiveQuestionS.Main} p="sm" w="100%" h="100%">
+ <Box
+ className={cx(InteractiveQuestionS.Main, "sdk-question-main")}
+ p="sm"
+ w="100%"
+ h="100%"
+ >
<Box className={InteractiveQuestionS.Content}>
{isEditorOpen ? (
<Editor onApply={closeEditor} />
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatEmbedding.module.css b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatEmbedding.module.css
deleted file mode 100644
index 479b82e2d6b..00000000000
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatEmbedding.module.css
+++ /dev/null
@@ -1,146 +0,0 @@
-/* TODO: rtl support */
-
-@keyframes chat-bar-slide-in {
- 0% {
- opacity: 0;
- transform: translate(-50%, 2rem);
- transform-origin: top;
- }
-
- 100% {
- opacity: 1;
- transform: translate(-50%, 0);
- transform-origin: top;
- }
-}
-
-.container {
- position: relative;
- max-width: 41.5rem;
- width: 100%;
- border: 1px solid var(--mb-color-brand);
- border-radius: 22px;
-}
-
-.innerContainer {
- padding: 0 1rem;
- border-radius: 22px;
- align-items: center;
- box-sizing: border-box;
- border: solid 1.5px transparent;
- background: var(--mb-color-bg-white);
- transition: background 150ms ease;
-}
-
-.innerContainer::before {
- content: "";
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: linear-gradient(to right, #2e96f1, #d932f4);
- z-index: -1;
- margin: -1.5px;
- border-radius: 24px;
-}
-
-.innerContainerLoading {
- background: var(--mb-color-bg-light);
-}
-
-.innerContainerExpanded {
- padding: 1rem;
- align-items: flex-start;
- height: 8rem;
-}
-
-.input {
- caret-color: var(--mb-color-brand);
-}
-
-.textarea {
- textarea {
- box-sizing: border-box;
- border: none;
- border-radius: 0;
- background: transparent;
- outline: none;
- width: 100%;
- caret-color: var(--mb-color-brand);
- padding: 0.5rem 0;
- line-height: 1.7;
- color: var(--mb-color-text-dark);
- }
-}
-
-.textareaExpanded {
- textarea {
- padding: 0;
- }
-}
-
-.textareaLoading {
- textarea {
- font-weight: bold;
-
- &:disabled {
- opacity: 1;
- background: transparent;
- }
- }
-}
-
-/* nesting the style above keeps it from getting applied */
-.textareaLoading textarea::placeholder,
-.textareaLoading textarea:disabled::placeholder {
- color: var(--mb-color-brand);
-}
-
-@keyframes responses-slide-in {
- 0% {
- opacity: 0;
- transform: translate(0, -0.5rem);
- transform-origin: top;
- }
-
- 100% {
- opacity: 1;
- transform: translate(0, 0);
- transform-origin: top;
- }
-}
-
-.responses {
- position: absolute;
- left: 0;
- top: 100%;
- margin-top: 0.25rem;
- pointer-events: none; /* allow user to click through transparent area behind repsonses */
- animation: responses-slide-in 150ms ease forwards;
- transition-property: transform, opacity;
-}
-
-.response {
- padding: 0.5rem 1rem;
- display: flex;
- background: var(--mb-color-brand);
- font-size: 0.875rem;
- font-weight: 700;
- border-radius: 1rem;
- color: white;
- pointer-events: auto;
- margin-top: 0.25rem;
- position: relative;
-}
-
-.responseMessage {
- flex-grow: 1;
-}
-
-button.responseDismissBtn {
- color: #cbe2f7;
- height: 1rem;
- cursor: pointer;
- margin-left: 0.5rem;
-}
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatEmbedding.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatEmbedding.tsx
deleted file mode 100644
index 1ee173f2d1c..00000000000
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatEmbedding.tsx
+++ /dev/null
@@ -1,181 +0,0 @@
-import cx from "classnames";
-import { useEffect, useState } from "react";
-import { t } from "ttag";
-
-import { useSdkDispatch } from "embedding-sdk-bundle/store";
-import {
- Box,
- Flex,
- Icon,
- Loader,
- Textarea,
- Tooltip,
- UnstyledButton,
-} from "metabase/ui";
-import {
- useMetabotAgent,
- useMetabotChatHandlers,
-} from "metabase-enterprise/metabot/hooks";
-import {
- cancelInflightAgentRequests,
- resetConversationId,
-} from "metabase-enterprise/metabot/state";
-
-import Styles from "./MetabotChatEmbedding.module.css";
-
-const MIN_INPUT_HEIGHT = 42;
-
-export const MetabotChatEmbedding = () => {
- const metabot = useMetabotAgent();
- const { handleSubmitInput, handleResetInput } = useMetabotChatHandlers();
-
- const resetInput = () => {
- handleResetInput();
- setInputExpanded(false);
- };
-
- const startNewConversation = () => {
- resetInput();
- metabot.resetConversation();
- };
-
- const [inputExpanded, setInputExpanded] = useState(false);
- const handleMaybeExpandInput = () => {
- const textarea = metabot.promptInputRef?.current;
-
- if (!textarea) {
- return;
- }
-
- const isMultiRow = textarea.scrollHeight > MIN_INPUT_HEIGHT;
- if (inputExpanded !== isMultiRow) {
- setInputExpanded(isMultiRow);
- }
- // keep scrolled to bottom
- textarea.scrollTop = Math.max(MIN_INPUT_HEIGHT, textarea.scrollHeight);
- };
-
- const inputPlaceholder = t`Tell me to do something, or ask a question`;
- const placeholder = metabot.isDoingScience
- ? t`Doing science...`
- : inputPlaceholder;
-
- function cancelRequest() {
- dispatch(cancelInflightAgentRequests());
- }
-
- const dispatch = useSdkDispatch();
-
- useEffect(() => {
- dispatch(resetConversationId());
- }, [dispatch]);
-
- const isOngoingConversation = metabot.messages.length > 0;
- const isPromptDefined = metabot.prompt.length > 0;
-
- return (
- <Box className={Styles.container} data-testid="metabot-chat">
- <Flex
- className={cx(
- Styles.innerContainer,
- metabot.isDoingScience && Styles.innerContainerLoading,
- inputExpanded && Styles.innerContainerExpanded,
- )}
- >
- <Flex
- w="16px"
- h="16px"
- style={{
- flexShrink: 0,
- }}
- justify="center"
- align="center"
- >
- {metabot.isDoingScience ? (
- <Loader size="sm" />
- ) : (
- <Icon name="ai" c="var(--mb-color-brand)" size="1rem" />
- )}
- </Flex>
- <Textarea
- id="metabot-chat-input"
- data-testid="metabot-chat-input"
- w="100%"
- autosize
- minRows={1}
- maxRows={4}
- ref={metabot.promptInputRef}
- autoFocus
- value={metabot.prompt}
- disabled={metabot.isDoingScience}
- className={cx(
- Styles.textarea,
- inputExpanded && Styles.textareaExpanded,
- metabot.isDoingScience && Styles.textareaLoading,
- )}
- placeholder={placeholder}
- onChange={(e) => metabot.setPrompt(e.target.value)}
- // @ts-expect-error - undocumented API for mantine Textarea - leverages the prop from react-textarea-autosize's TextareaAutosize component
- onHeightChange={handleMaybeExpandInput}
- onKeyDown={(e) => {
- if (e.nativeEvent.isComposing) {
- return;
- }
-
- if (e.key === "Enter") {
- // prevent event from inserting new line + interacting with other content
- e.preventDefault();
- e.stopPropagation();
- handleSubmitInput(metabot.prompt);
- }
- }}
- />
- {metabot.isDoingScience && (
- <UnstyledButton
- h="1rem"
- data-testid="metabot-cancel-request"
- onClick={cancelRequest}
- >
- <Tooltip label={t`Stop generation`}>
- <Icon name="stop" c="var(--mb-color-text-primary)" size="1rem" />
- </Tooltip>
- </UnstyledButton>
- )}
-
- {!metabot.isDoingScience && (
- <>
- {isOngoingConversation && !isPromptDefined && (
- <UnstyledButton
- h="1rem"
- onClick={startNewConversation}
- data-testid="metabot-new-conversation"
- >
- <Tooltip label={t`Start new chat`}>
- <Icon
- name="edit_document_outlined"
- c="var(--mb-color-text-primary)"
- size="1rem"
- />
- </Tooltip>
- </UnstyledButton>
- )}
-
- {isPromptDefined && (
- <UnstyledButton
- h="1rem"
- onClick={resetInput}
- data-testid="metabot-close-chat"
- >
- <Icon
- name="close"
- c="var(--mb-color-text-primary)"
- size="1rem"
- />
- </UnstyledButton>
- )}
- </>
- )}
- </Flex>
- </Box>
- );
-};
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatHistory.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatHistory.tsx
new file mode 100644
index 00000000000..684f74ee289
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatHistory.tsx
@@ -0,0 +1,50 @@
+import { useEffect, useRef } from "react";
+
+import { Stack } from "metabase/ui";
+import { Messages } from "metabase-enterprise/metabot/components/MetabotChat/MetabotChatMessage";
+import { MetabotResetLongChatButton } from "metabase-enterprise/metabot/components/MetabotChat/MetabotResetLongChatButton";
+import {
+ useMetabotAgent,
+ useMetabotChatHandlers,
+} from "metabase-enterprise/metabot/hooks";
+import { useMetabotReactions } from "metabase-enterprise/metabot/hooks/use-metabot-reactions";
+
+export function MetabotChatHistory() {
+ const metabot = useMetabotAgent();
+ const { messages, errorMessages } = metabot;
+ const { handleRetryMessage } = useMetabotChatHandlers();
+ const { setNavigateToPath } = useMetabotReactions();
+ const scrollContainerRef = useRef<HTMLDivElement>(null);
+
+ const hasMessages = messages.length > 0 || errorMessages.length > 0;
+
+ // Auto-scroll to bottom when new messages are received
+ useEffect(() => {
+ if (scrollContainerRef.current) {
+ scrollContainerRef.current.scrollTop =
+ scrollContainerRef.current.scrollHeight;
+ }
+ }, [messages.length, errorMessages.length, metabot.isDoingScience]);
+
+ return (
+ <Stack
+ ref={scrollContainerRef}
+ flex={1}
+ gap={0}
+ style={{ overflowY: "auto" }}
+ p="md"
+ >
+ {hasMessages ? (
+ <Messages
+ messages={messages}
+ errorMessages={errorMessages}
+ onRetryMessage={handleRetryMessage}
+ isDoingScience={metabot.isDoingScience}
+ showFeedbackButtons={false}
+ onInternalLinkClick={setNavigateToPath}
+ />
+ ) : null}
+ {metabot.isLongConversation && <MetabotResetLongChatButton />}
+ </Stack>
+ );
+}
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatInput.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatInput.tsx
new file mode 100644
index 00000000000..0c081008e64
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatInput.tsx
@@ -0,0 +1,137 @@
+import { t } from "ttag";
+
+import { useSdkDispatch } from "embedding-sdk-bundle/store";
+import {
+ Flex,
+ Icon,
+ Loader,
+ Menu,
+ Textarea,
+ Tooltip,
+ UnstyledButton,
+} from "metabase/ui";
+import {
+ useMetabotAgent,
+ useMetabotChatHandlers,
+} from "metabase-enterprise/metabot/hooks";
+import { cancelInflightAgentRequests } from "metabase-enterprise/metabot/state";
+
+import S from "./MetabotQuestion.module.css";
+
+export function MetabotChatInput() {
+ const metabot = useMetabotAgent();
+ const { handleSubmitInput, handleResetInput } = useMetabotChatHandlers();
+ const dispatch = useSdkDispatch();
+
+ const placeholder = metabot.isDoingScience
+ ? t`Doing science...`
+ : t`Ask AI a question...`;
+
+ const cancelRequest = () => {
+ dispatch(cancelInflightAgentRequests());
+ };
+
+ const resetInput = () => {
+ handleResetInput();
+ };
+
+ const startNewConversation = () => {
+ metabot.resetConversation();
+ };
+
+ return (
+ <Flex
+ gap="xs"
+ px="md"
+ align="center"
+ justify="center"
+ style={{ borderTop: "1px solid var(--mb-color-border)" }}
+ >
+ <Flex justify="center" align="center" style={{ flexShrink: 0 }}>
+ {metabot.isDoingScience ? (
+ <Loader size="sm" />
+ ) : (
+ <Icon name="ai" c="var(--mb-color-brand)" size="1rem" />
+ )}
+ </Flex>
+
+ <Textarea
+ id="metabot-chat-input"
+ data-testid="metabot-chat-input"
+ w="100%"
+ pb="0.2rem"
+ pt="0.6rem"
+ autosize
+ minRows={1}
+ maxRows={4}
+ ref={metabot.promptInputRef}
+ autoFocus
+ value={metabot.prompt}
+ disabled={metabot.isDoingScience}
+ placeholder={placeholder}
+ onChange={(e) => metabot.setPrompt(e.target.value)}
+ classNames={{ input: S.chatInput }}
+ onKeyDown={(e) => {
+ if (e.nativeEvent.isComposing) {
+ return;
+ }
+
+ const isModifiedKeyPress =
+ e.shiftKey || e.ctrlKey || e.metaKey || e.altKey;
+
+ // Prevent event from inserting new line and interacting with other content
+ if (e.key === "Enter" && !isModifiedKeyPress) {
+ e.preventDefault();
+ e.stopPropagation();
+ handleSubmitInput(metabot.prompt);
+ }
+ }}
+ />
+
+ <Flex align="center" justify="center" gap="xs">
+ {metabot.isDoingScience && (
+ <UnstyledButton
+ data-testid="metabot-cancel-request"
+ onClick={cancelRequest}
+ className={S.chatButton}
+ >
+ <Tooltip label={t`Stop generation`}>
+ <Icon name="stop" c="var(--mb-color-text-secondary)" />
+ </Tooltip>
+ </UnstyledButton>
+ )}
+
+ {!metabot.isDoingScience && metabot.prompt.length > 0 && (
+ <UnstyledButton
+ onClick={resetInput}
+ data-testid="metabot-close-chat"
+ className={S.chatButton}
+ >
+ <Icon name="close" c="var(--mb-color-text-secondary)" />
+ </UnstyledButton>
+ )}
+
+ {/* Overflow menu - only shown in stacked layout and auto layout mobile */}
+ <Menu position="top-end" withinPortal>
+ <Menu.Target>
+ <UnstyledButton
+ data-testid="metabot-overflow-button"
+ className={S.stackedOverflowButton}
+ >
+ <Icon name="ellipsis" c="var(--mb-color-text-secondary)" />
+ </UnstyledButton>
+ </Menu.Target>
+
+ <Menu.Dropdown>
+ <Menu.Item
+ leftSection={<Icon name="edit_document_outlined" size="1rem" />}
+ onClick={startNewConversation}
+ >
+ {t`Start new chat`}
+ </Menu.Item>
+ </Menu.Dropdown>
+ </Menu>
+ </Flex>
+ </Flex>
+ );
+}
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatSuggestions.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatSuggestions.tsx
new file mode 100644
index 00000000000..8309d26b0b8
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotChatSuggestions.tsx
@@ -0,0 +1,50 @@
+import { Button, Stack } from "metabase/ui";
+import { useGetSuggestedMetabotPromptsQuery } from "metabase-enterprise/api";
+import {
+ useMetabotAgent,
+ useMetabotChatHandlers,
+} from "metabase-enterprise/metabot/hooks";
+
+import S from "./MetabotQuestion.module.css";
+
+export const MetabotChatSuggestions = () => {
+ const metabot = useMetabotAgent();
+ const { handleSubmitInput } = useMetabotChatHandlers();
+
+ // Keep in sync with [MetabotChat.tsx]
+ const suggestedPromptsQuery = useGetSuggestedMetabotPromptsQuery({
+ metabot_id: metabot.metabotId,
+ limit: 3,
+ sample: true,
+ });
+
+ const suggestedPrompts = suggestedPromptsQuery.currentData?.prompts ?? [];
+
+ const shouldShowSuggestedPrompts =
+ metabot.messages.length === 0 &&
+ metabot.errorMessages.length === 0 &&
+ !metabot.isDoingScience &&
+ suggestedPrompts.length > 0;
+
+ if (!shouldShowSuggestedPrompts) {
+ return null;
+ }
+
+ return (
+ <Stack gap="sm" p="md" className={S.promptSuggestionsContainer}>
+ {suggestedPrompts.map(({ prompt }, index) => (
+ <Button
+ key={index}
+ size="xs"
+ variant="outline"
+ fw={400}
+ onClick={() => handleSubmitInput(prompt)}
+ className={S.promptSuggestionButton}
+ data-testid="metabot-suggestion-button"
+ >
+ {prompt}
+ </Button>
+ ))}
+ </Stack>
+ );
+};
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.module.css b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.module.css
new file mode 100644
index 00000000000..0a3833fb7b6
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.module.css
@@ -0,0 +1,103 @@
+.container {
+ display: flex;
+ width: 100%;
+ height: 100%;
+
+ &[data-layout="sidebar"] {
+ flex-direction: row;
+
+ .question {
+ flex: 1;
+ height: 100%;
+ }
+
+ .chat {
+ width: 100%;
+ height: 100%;
+ max-width: 18.75rem;
+ border-left: 1px solid var(--mb-color-border);
+ }
+
+ .stackedOverflowButton {
+ display: none;
+ }
+ }
+
+ &[data-layout="stacked"] {
+ flex-direction: column;
+
+ .question {
+ flex: 0 0 60%;
+ height: 60%;
+ overflow: hidden;
+ }
+
+ .chat {
+ flex: 0 0 40%;
+ height: 40%;
+ width: 100%;
+ max-width: none;
+ border-top: 1px solid var(--mb-color-border);
+ border-left: none;
+ }
+
+ .sidebarHeader {
+ display: none;
+ }
+
+ /* Overflow menu is only visible in stacked layout. */
+ .stackedOverflowButton,
+ .chatButton {
+ display: flex;
+ padding: 0.25rem;
+ border-radius: 4px;
+ transition: background-color 0.2s ease;
+
+ &:hover {
+ background-color: var(--mb-color-brand-lighter);
+ }
+ }
+ }
+}
+
+/* Prompt suggestions */
+.promptSuggestionsContainer {
+ flex-shrink: 0;
+ max-width: 30rem;
+}
+
+.promptSuggestionButton {
+ display: block;
+ height: auto;
+ text-align: left;
+ line-height: 1.4;
+ padding: 0.5rem 0.75rem;
+ width: 100%;
+ border-radius: 8px;
+ border: 1px solid var(--mb-color-border);
+ background-color: var(--mb-color-bg-white);
+ color: var(--mb-color-text-secondary);
+ font-size: 0.875rem;
+}
+
+.promptSuggestionButton span {
+ text-wrap-mode: wrap;
+ justify-content: flex-start;
+}
+
+/* Chat input */
+.chatInput {
+ border: none;
+ border-radius: 0;
+ background-color: transparent !important;
+
+ &:focus {
+ outline: none;
+ border-color: transparent;
+ }
+}
+
+/* Remove the default padding to align the scrollbars */
+.question :global(.sdk-question-main) {
+ padding: 0 !important;
+}
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.schema.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.schema.ts
index f9258ca056e..8ef186e3a57 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.schema.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.schema.ts
@@ -1,5 +1,17 @@
+import * as Yup from "yup";
+
import type { FunctionSchema } from "embedding-sdk-bundle/types/schema";
-export const metabotQuestionSchema: FunctionSchema = {
- input: [],
-};
+import type { MetabotQuestionProps } from "./types";
+
+const propsSchema: Yup.SchemaOf<MetabotQuestionProps> = Yup.object({
+ height: Yup.mixed().optional(),
+ width: Yup.mixed().optional(),
+ className: Yup.string().optional(),
+ style: Yup.object().optional(),
+ layout: Yup.mixed<"auto" | "sidebar" | "stacked">()
+ .oneOf(["auto", "sidebar", "stacked"])
+ .optional(),
+});
+
+export const metabotQuestionSchema: FunctionSchema = { input: [propsSchema] };
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.stories.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.stories.tsx
index a7e0f4089ef..1ef16d099eb 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.stories.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.stories.tsx
@@ -7,7 +7,7 @@ import {
MOCK_AD_HOC_QUESTION_ID,
mockStreamResponse,
} from "embedding-sdk-shared/test/mocks/mock-metabot-response";
-import { Box } from "metabase/ui";
+import { Flex, Stack } from "metabase/ui";
import { MetabotQuestion } from "./MetabotQuestion";
@@ -23,22 +23,31 @@ export default {
};
const Template: StoryFn<MetabotQuestionProps> = () => {
- return (
- <Box
- bg="var(--mb-color-background)"
- mih="100vh"
- bd="1px solid #000"
- pt="2rem"
- >
- <MetabotQuestion />
- </Box>
- );
+ return <MetabotQuestion height="100vh" />;
};
-export const Default = {
+export const FullWidth = {
render: Template,
};
+export const Centered = {
+ render() {
+ return (
+ <Stack align="center" justify="center">
+ <Flex
+ m="40px"
+ style={{
+ border: "1px solid var(--mb-color-border)",
+ borderRadius: "16px",
+ }}
+ >
+ <MetabotQuestion height="calc(100vh - 90px)" />
+ </Flex>
+ </Stack>
+ );
+ },
+};
+
export const RedirectReaction = {
render: Template,
parameters: {
@@ -77,3 +86,39 @@ export const MultipleInstances = {
</>
),
};
+
+export const SidebarLayout = {
+ render() {
+ return (
+ <Stack align="center" justify="center">
+ <Flex
+ m="40px"
+ style={{
+ border: "1px solid var(--mb-color-border)",
+ borderRadius: "16px",
+ }}
+ >
+ <MetabotQuestion height="calc(100vh - 90px)" layout="sidebar" />
+ </Flex>
+ </Stack>
+ );
+ },
+};
+
+export const StackedLayout = {
+ render() {
+ return (
+ <Stack align="center" justify="center">
+ <Flex
+ m="40px"
+ style={{
+ border: "1px solid var(--mb-color-border)",
+ borderRadius: "16px",
+ }}
+ >
+ <MetabotQuestion height="calc(100vh - 90px)" layout="stacked" />
+ </Flex>
+ </Stack>
+ );
+ },
+};
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.tsx
index 50e215e8a03..a84598c813d 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.tsx
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestion.tsx
@@ -1,100 +1,111 @@
-import { useId } from "react";
-import { t } from "ttag";
+import { useElementSize } from "@mantine/hooks";
+import { useId, useMemo } from "react";
+import { match } from "ts-pattern";
-import {
- SdkLoader,
- withPublicComponentWrapper,
-} from "embedding-sdk-bundle/components/private/PublicComponentWrapper";
+import { FlexibleSizeComponent } from "embedding-sdk-bundle/components/private/FlexibleSizeComponent";
+import { withPublicComponentWrapper } from "embedding-sdk-bundle/components/private/PublicComponentWrapper";
import { SdkAdHocQuestion } from "embedding-sdk-bundle/components/private/SdkAdHocQuestion";
import { SdkQuestionDefaultView } from "embedding-sdk-bundle/components/private/SdkQuestionDefaultView";
import { EnsureSingleInstance } from "embedding-sdk-shared/components/EnsureSingleInstance/EnsureSingleInstance";
import { useLocale } from "metabase/common/hooks/use-locale";
-import { Flex, Paper, Stack, Text } from "metabase/ui";
-import { Messages } from "metabase-enterprise/metabot/components/MetabotChat/MetabotChatMessage";
-import { MetabotResetLongChatButton } from "metabase-enterprise/metabot/components/MetabotChat/MetabotResetLongChatButton";
-import {
- useMetabotAgent,
- useMetabotChatHandlers,
-} from "metabase-enterprise/metabot/hooks";
+import { Stack } from "metabase/ui";
import { useMetabotReactions } from "metabase-enterprise/metabot/hooks/use-metabot-reactions";
-import { MetabotChatEmbedding } from "./MetabotChatEmbedding";
+import { MetabotChatHistory } from "./MetabotChatHistory";
+import { MetabotChatInput } from "./MetabotChatInput";
+import { MetabotChatSuggestions } from "./MetabotChatSuggestions";
+import S from "./MetabotQuestion.module.css";
import { metabotQuestionSchema } from "./MetabotQuestion.schema";
+import { MetabotQuestionEmptyState } from "./MetabotQuestionEmptyState";
import { QuestionDetails } from "./QuestionDetails";
import { QuestionTitle } from "./QuestionTitle";
-
-const MetabotQuestionInner = () => {
+import { SidebarHeader } from "./SidebarHeader";
+import type { MetabotQuestionProps } from "./types";
+
+/**
+ * If the Metabot component's container size is smaller
+ * than this, use the stacked layout.
+ **/
+const MAX_MOBILE_CONTAINER_WIDTH = 700;
+
+const MetabotQuestionInner = ({
+ height,
+ width,
+ className,
+ style,
+ layout = "auto",
+}: MetabotQuestionProps) => {
const { isLocaleLoading } = useLocale();
const { navigateToPath } = useMetabotReactions();
-
- if (isLocaleLoading) {
- return <SdkLoader />;
+ const { ref: containerRef, width: containerWidth } = useElementSize();
+
+ const hasQuestion = !!navigateToPath;
+
+ const derivedLayout = useMemo(() => {
+ return match(layout)
+ .with("auto", () =>
+ containerWidth <= MAX_MOBILE_CONTAINER_WIDTH ? "stacked" : "sidebar",
+ )
+ .otherwise((layout) => layout);
+ }, [layout, containerWidth]);
+
+ function renderQuestion() {
+ if (!hasQuestion || isLocaleLoading) {
+ return <MetabotQuestionEmptyState />;
+ }
+
+ return (
+ <SdkAdHocQuestion
+ questionPath={navigateToPath}
+ title={false}
+ isSaveEnabled={false}
+ >
+ <SdkQuestionDefaultView
+ height="100%"
+ withChartTypeSelector
+ title={
+ <Stack gap="sm" mb="1rem">
+ <QuestionTitle />
+ <QuestionDetails />
+ </Stack>
+ }
+ />
+ </SdkAdHocQuestion>
+ );
}
return (
- <Flex direction="column" align="center" gap="md">
- <MetabotMessages />
-
- <MetabotChatEmbedding />
-
- {!!navigateToPath && (
- <SdkAdHocQuestion
- questionPath={navigateToPath}
- title={false}
- isSaveEnabled={false}
- >
- <SdkQuestionDefaultView
- withChartTypeSelector
- title={
- <Stack gap="sm" mb="1rem">
- <QuestionTitle />
- <QuestionDetails />
- </Stack>
- }
- />
- </SdkAdHocQuestion>
- )}
-
- <Disclaimer />
- </Flex>
+ <FlexibleSizeComponent
+ height={height}
+ width={width}
+ className={className}
+ style={style}
+ >
+ <div
+ ref={containerRef}
+ className={S.container}
+ data-layout={derivedLayout}
+ data-testid="metabot-question-container"
+ >
+ <div className={S.question}>{renderQuestion()}</div>
+
+ <div className={S.chat}>
+ <Stack w="100%" h="100%" pos="relative" gap={0}>
+ <SidebarHeader />
+ <MetabotChatHistory />
+
+ <Stack gap={0}>
+ <MetabotChatSuggestions />
+ <MetabotChatInput />
+ </Stack>
+ </Stack>
+ </div>
+ </div>
+ </FlexibleSizeComponent>
);
};
-function MetabotMessages() {
- const metabot = useMetabotAgent();
- const { messages, errorMessages } = metabot;
- const { handleRetryMessage } = useMetabotChatHandlers();
- const { setNavigateToPath } = useMetabotReactions();
-
- if (!messages.length && !errorMessages.length) {
- return null;
- }
-
- return (
- <Paper shadow="sm" p="lg" w="100%" maw="41.5rem" radius="lg">
- <Flex direction="column">
- <Messages
- messages={messages}
- errorMessages={errorMessages}
- onRetryMessage={handleRetryMessage}
- isDoingScience={metabot.isDoingScience}
- showFeedbackButtons={false}
- onInternalLinkClick={setNavigateToPath}
- />
- </Flex>
-
- {metabot.isLongConversation && <MetabotResetLongChatButton />}
- </Paper>
- );
-}
-
-function Disclaimer() {
- return (
- <Text c="var(--mb-color-text-secondary)">{t`AI can make mistakes. Double-check results.`}</Text>
- );
-}
-
-const MetabotQuestionWrapped = () => {
+const MetabotQuestionWrapped = (props: MetabotQuestionProps) => {
const ensureSingleInstanceId = useId();
return (
@@ -105,7 +116,7 @@ const MetabotQuestionWrapped = () => {
"Multiple instances of MetabotQuestion detected. Ensure only one instance of MetabotQuestion is rendered at a time."
}
>
- <MetabotQuestionInner />
+ <MetabotQuestionInner {...props} />
</EnsureSingleInstance>
);
};
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestionEmptyState.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestionEmptyState.tsx
new file mode 100644
index 00000000000..a67c5a63242
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/MetabotQuestionEmptyState.tsx
@@ -0,0 +1,14 @@
+import { t } from "ttag";
+
+import { Icon, Stack, Text } from "metabase/ui";
+
+export const MetabotQuestionEmptyState = () => (
+ <Stack h="100%" w="100%" gap="lg" align="center" justify="center">
+ <Icon name="ai" c="var(--mb-color-bg-black)" size="5rem" opacity={0.25} />
+
+ <Stack gap="xs" align="center">
+ <Text lh="md">{t`Ask questions to AI.`}</Text>
+ <Text lh="md">{t`Results will appear here.`}</Text>
+ </Stack>
+ </Stack>
+);
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/SidebarHeader.tsx b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/SidebarHeader.tsx
new file mode 100644
index 00000000000..fd211c42d6e
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/SidebarHeader.tsx
@@ -0,0 +1,41 @@
+import { t } from "ttag";
+
+import { Flex, Icon, Text, Tooltip, UnstyledButton } from "metabase/ui";
+import { useMetabotAgent } from "metabase-enterprise/metabot/hooks";
+
+import S from "./MetabotQuestion.module.css";
+
+export function SidebarHeader() {
+ const metabot = useMetabotAgent();
+
+ const startNewConversation = () => {
+ metabot.resetConversation();
+ };
+
+ return (
+ <Flex
+ px="md"
+ py="sm"
+ justify="space-between"
+ align="center"
+ className={S.sidebarHeader}
+ >
+ <Text fz="sm" c="var(--mb-color-text-tertiary)">
+ {t`AI isn't perfect. Double-check results.`}
+ </Text>
+
+ <Tooltip label={t`Start new chat`}>
+ <UnstyledButton
+ onClick={startNewConversation}
+ data-testid="metabot-new-conversation"
+ >
+ <Icon
+ name="edit_document_outlined"
+ size="1rem"
+ c="var(--mb-color-text-tertiary)"
+ />
+ </UnstyledButton>
+ </Tooltip>
+ </Flex>
+ );
+}
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/index.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/index.ts
index 2f4f9ea0e3a..aedb9e4034c 100644
--- a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/index.ts
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/index.ts
@@ -1 +1,2 @@
export { MetabotQuestion } from "./MetabotQuestion";
+export type { MetabotQuestionProps } from "./types";
diff --git a/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/types.ts b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/types.ts
new file mode 100644
index 00000000000..0cd8ff1b97f
--- /dev/null
+++ b/enterprise/frontend/src/embedding-sdk-bundle/components/public/MetabotQuestion/types.ts
@@ -0,0 +1,31 @@
+import type { CSSProperties } from "react";
+
+import type { CommonStylingProps } from "embedding-sdk-bundle/types/props";
+
+/**
+ * Props for the MetabotQuestion component.
+ *
+ * @interface
+ * @expand
+ * @category MetabotQuestion
+ */
+export interface MetabotQuestionProps extends CommonStylingProps {
+ /**
+ * Layout for the MetabotQuestion component.
+ *
+ * - `auto` (default): Metabot uses the `stacked` layout on mobile screens, and a `sidebar` layout on larger screens.
+ * - `stacked`: the question visualization stacks on top of the chat interface.
+ * - `sidebar`: the question visualization appears to the left of the chat interface, which is on a sidebar on the right.
+ */
+ layout?: "auto" | "sidebar" | "stacked";
+
+ /**
+ * A number or string specifying a CSS size value that specifies the height of the component
+ */
+ height?: CSSProperties["height"];
+
+ /**
+ * A number or string specifying a CSS size value that specifies the width of the component
+ */
+ width?: CSSProperties["width"];
+}
diff --git a/enterprise/frontend/src/embedding-sdk-package/index.ts b/enterprise/frontend/src/embedding-sdk-package/index.ts
index 9ebe29dafaa..862e279d860 100644
--- a/enterprise/frontend/src/embedding-sdk-package/index.ts
+++ b/enterprise/frontend/src/embedding-sdk-package/index.ts
@@ -69,6 +69,7 @@ export {
type InteractiveQuestionComponents,
type InteractiveQuestionProps,
} from "embedding-sdk-bundle/components/public/InteractiveQuestion";
+export { type MetabotQuestionProps } from "embedding-sdk-bundle/components/public/MetabotQuestion";
export {
type StaticQuestionProps,
type StaticQuestionComponents,