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
feat: inline charts (#75550)
Maksym Yakubych91628d34bf46
diff --git a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts b/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.tsindex 2353599e155..3c83deeba9e 100644--- a/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts+++ b/e2e/test/scenarios/metabot/metabot-query-builder.cy.spec.ts@@ -75,6 +75,22 @@ describe("Metabot Query Builder", () => {cy.url().should("include", "/question/ask");});+ it("should render a generated chart inline without leaving the page", () => {+ cy.visit("/question/ask");+ H.metabotChatInput().should("be.visible");++ H.mockMetabotResponse({+ body: mockGeneratedEntityResponse(allOrdersQuestion.dataset_query),+ });+ H.sendMetabotMessage("Show me all orders");++ cy.wait("@metabotAgent");+ // the chart renders inline and we stay on the /ask page+ cy.findByTestId("metabot-inline-chart").should("be.visible");+ cy.findByTestId("qb-header").should("not.exist");+ cy.url().should("include", "/question/ask");+ });+it("should navigate to a question when the agent returns a navigate_to", () => {cy.visit("/");@@ -143,5 +159,17 @@ const mockTextOnlyResponse = (text: string) =>`0:"${text}"d:{"finishReason":"stop","usage":{"promptTokens":100,"completionTokens":10}}`;+const mockGeneratedEntityResponse = (datasetQuery: unknown) => {+ const value = {+ type: "card",+ id: "card-1",+ title: "All orders",+ 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}}`;+};+const mockErrorResponse = `3:"Anthropic API key expired or invalid"d:{"finishReason":"error","usage":{}}`;diff --git a/frontend/src/metabase/api/ai-streaming/schemas.ts b/frontend/src/metabase/api/ai-streaming/schemas.tsindex 7f381b678b4..e921e7d87ff 100644--- a/frontend/src/metabase/api/ai-streaming/schemas.ts+++ b/frontend/src/metabase/api/ai-streaming/schemas.ts@@ -1,6 +1,8 @@import * as Yup from "yup";import type {+ CardDisplayType,+ DatasetQuery,MetabotCodeEdit,MetabotTodoItem,SuggestedTransform,@@ -18,6 +20,7 @@ export const knownDataPartTypes = ["todo_list","code_edit","transform_suggestion",+ "generated_entity","adhoc_viz","static_viz",];@@ -33,12 +36,28 @@ export type StaticVizValue = {entity_id: number;};+export type GeneratedQuery = {+ id: string;+ query: DatasetQuery;+};++export type GeneratedCard = {+ type: "card";+ id: string;+ title: string;+ query: GeneratedQuery;+ display?: CardDisplayType;+};++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 };diff --git a/frontend/src/metabase/dashboard/utils.ts b/frontend/src/metabase/dashboard/utils.tsindex 09e0f5a19e9..11d60e6d178 100644--- a/frontend/src/metabase/dashboard/utils.ts+++ b/frontend/src/metabase/dashboard/utils.ts@@ -8,12 +8,11 @@ import {isQuestionDashCard,isVirtualDashCard,} from "metabase/utils/dashboard";-import { SERVER_ERROR_TYPES } from "metabase/utils/errors";import { isStaticEmbeddingEntityLoadingError } from "metabase/utils/errors/is-static-embedding-entity-loading-error";import type { StaticEmbeddingEntityError } from "metabase/utils/errors/types";import {+ getDatasetPermissionError,getGenericErrorMessage,- getPermissionErrorMessage,} from "metabase/visualizations/lib/errors";import { hasNoResults } from "metabase/visualizations/lib/no-results";import { isVisualizerDashboardCard } from "metabase/visualizer/utils";@@ -263,22 +262,18 @@ export function isDashcardLoading(export function isDashcardAccessRestricted(datasets: ReadonlyArray<Pick<Dataset, "error" | "error_type">>,) {- return datasets.some(- (s) =>- s.error_type === SERVER_ERROR_TYPES.missingPermissions ||- (typeof s.error === "object" && s.error?.status === 403),- );+ return datasets.some((dataset) => getDatasetPermissionError(dataset) != null);}export function getDashcardResultsError(datasets: Dataset[],isGuestEmbed: boolean,) {- if (isDashcardAccessRestricted(datasets)) {- return {- message: getPermissionErrorMessage(),- icon: "key" as const,- };+ const permissionError = datasets+ .map(getDatasetPermissionError)+ .find((error) => error != null);+ if (permissionError) {+ return permissionError;}const staticEntityLoadingError = datasets.find((dataset) =>diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsxindex 8fb73d5682d..46fcba4b245 100644--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsx+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotAgentDataPartMessage.tsx@@ -17,6 +17,7 @@ import {import { AgentSuggestionMessage } from "./MetabotAgentSuggestionMessage";import { AgentTodoListMessage } from "./MetabotAgentTodoMessage";import Styles from "./MetabotChat.module.css";+import { MetabotInlineChart } from "./MetabotInlineChart";type AgentDataPartMessageProps = {message: MetabotAgentDataPartMessage;@@ -67,6 +68,15 @@ export const AgentDataPartMessage = ({</Stack>);})+ .with(+ { part: { type: "generated_entity", value: { type: "card" } } },+ ({ part }) => (+ <Stack gap="md">+ {debug && <DataPartJsonCard type={part.type} value={part.value} />}+ <MetabotInlineChart value={part.value} />+ </Stack>+ ),+ ).with({ part: { type: "adhoc_viz" } }, ({ part }) =>debug ? <DataPartJsonCard type={part.type} value={part.value} /> : null,)diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.module.css b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.module.cssindex a3afd300921..35609bb8f54 100644--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.module.css+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChat.module.css@@ -68,7 +68,7 @@.messageContainerUser {align-items: flex-end;align-self: flex-end;- max-width: 90%;+ max-width: min(100%, 46.5rem);min-width: 6rem;width: fit-content;}@@ -88,7 +88,7 @@.messageUser {padding: 0.5rem 1rem;border-radius: 1rem;- background: var(--mb-color-background-tertiary);+ background: var(--mb-color-background-secondary);max-width: 100%;overflow: hidden;}diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsxindex 84cfb973555..9fc70914a3a 100644--- a/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotChatMessage.tsx@@ -45,6 +45,7 @@ const isUserVisibleDataPart = (part: MetabotDataPart): boolean =>.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).exhaustive();diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.module.css b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.module.cssnew file mode 100644index 00000000000..5e289f2598b--- /dev/null+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.module.css@@ -0,0 +1,25 @@+.container {+ border: 1px solid var(--mb-color-border);+ border-radius: 0.5rem;+ overflow: hidden;+ background: var(--mb-color-background);+}++.header {+ padding: 0.5rem 0.5rem 0.5rem 0.75rem;+}++.title {+ color: var(--mb-color-text-primary);+ text-decoration: none;++ &:hover {+ color: var(--mb-color-brand);+ text-decoration: none;+ }+}++.viz {+ height: 18rem;+ position: relative;+}diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.tsxnew file mode 100644index 00000000000..b803526e67a--- /dev/null+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.tsx@@ -0,0 +1,94 @@+import { useMemo } from "react";+import { noop } from "underscore";++import { useGetAdhocQueryQuery } from "metabase/api";+import type { GeneratedCard } from "metabase/api/ai-streaming/schemas";+import { ForwardRefLink } from "metabase/common/components/Link";+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";+import { serializeCardForUrl } from "metabase/common/utils/card";+import { Anchor, Box, Center, Flex } from "metabase/ui";+import Visualization from "metabase/visualizations/components/Visualization";+import { ErrorView } from "metabase/visualizations/components/Visualization/ErrorView";+import {+ getDatasetError,+ getGenericErrorMessage,+} from "metabase/visualizations/lib/errors";+import Question from "metabase-lib/v1/Question";+import type { Card } from "metabase-types/api";++import S from "./MetabotInlineChart.module.css";++/**+ * Renders a Metabot-generated `card` entity as a live, read-only chart inline in+ * the conversation: it runs the card's embedded query ad-hoc and renders the+ * result; the title bar links out to the full question.+ */+export function MetabotInlineChart({+ value: { title, display, query },+}: {+ value: GeneratedCard;+}) {+ const datasetQuery = query.query;++ const card: Card = useMemo(+ () =>+ new Question({+ dataset_query: datasetQuery,+ display: display ?? "table",+ displayIsLocked: display != null,+ visualization_settings: {},+ }).card(),+ [datasetQuery, display],+ );++ const link = useMemo(+ () =>+ `/question#${serializeCardForUrl(card, { includeDisplayIsLocked: true })}`,+ [card],+ );++ const { data: dataset, error } = useGetAdhocQueryQuery(datasetQuery);++ const rawSeries = useMemo(+ () => (dataset ? [{ card, data: dataset.data }] : null),+ [card, dataset],+ );++ const datasetError = dataset ? getDatasetError(dataset) : undefined;+ const requestError = error+ ? { message: getGenericErrorMessage(), icon: "warning" as const }+ : undefined;+ const chartError = datasetError ?? requestError;++ return (+ <Box className={S.container} data-testid="metabot-inline-chart">+ <Flex className={S.header} align="center" gap="sm">+ <Anchor+ className={S.title}+ component={ForwardRefLink}+ to={link}+ target="_blank"+ fw="bold"+ truncate+ >+ {title}+ </Anchor>+ </Flex>+ <Box className={S.viz}>+ {chartError ? (+ <Center h="100%" p="md">+ <ErrorView error={chartError.message} icon={chartError.icon} />+ </Center>+ ) : !rawSeries ? (+ <LoadingAndErrorWrapper loading />+ ) : (+ <Visualization+ rawSeries={rawSeries}+ isQueryBuilder={false}+ onChangeCardAndRun={noop}+ />+ )}+ </Box>+ </Box>+ );+}diff --git a/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.unit.spec.tsxnew file mode 100644index 00000000000..e45ee44ae7b--- /dev/null+++ b/frontend/src/metabase/metabot/components/MetabotChat/MetabotInlineChart.unit.spec.tsx@@ -0,0 +1,103 @@+import fetchMock from "fetch-mock";++import { setupCardDataset } from "__support__/server-mocks";+import { renderWithProviders, screen } from "__support__/ui";+import type { GeneratedCard } from "metabase/api/ai-streaming/schemas";+import { createMockStructuredDatasetQuery } from "metabase-types/api/mocks/query";++import { MetabotInlineChart } from "./MetabotInlineChart";++// Visualization pulls in the whole charting stack; stub it to a sentinel so we+// can unit test MetabotInlineChart's run / render-states logic.+jest.mock("metabase/visualizations/components/Visualization", () => ({+ __esModule: true,+ default: () => <div data-testid="visualization" />,+}));++const datasetQuery = createMockStructuredDatasetQuery();++const value: GeneratedCard = {+ type: "card",+ id: "card-1",+ title: "Orders by month",+ query: { id: "q-1", query: datasetQuery },+ display: "bar",+};++function setup(+ args: Parameters<typeof setupCardDataset>[0] = {},+ valueOverrides: Partial<GeneratedCard> = {},+) {+ setupCardDataset(args);+ return renderWithProviders(+ <MetabotInlineChart value={{ ...value, ...valueOverrides }} />,+ );+}++describe("MetabotInlineChart", () => {+ beforeEach(() => {+ fetchMock.clearHistory();+ });++ it("runs the embedded query and renders the visualization", async () => {+ setup();+ expect(screen.getByTestId("metabot-inline-chart")).toBeInTheDocument();+ expect(await screen.findByTestId("visualization")).toBeInTheDocument();+ expect(fetchMock.callHistory.called("path:/api/dataset")).toBe(true);+ });++ it("shows the title as a link", () => {+ setup();+ expect(screen.getByText("Orders by month")).toHaveAttribute(+ "target",+ "_blank",+ );+ });++ it("does not render the visualization while results are loading", () => {+ setup();+ expect(screen.queryByTestId("visualization")).not.toBeInTheDocument();+ });++ it("shows an error message when the request fails", async () => {+ setup({ status: 500 });+ expect(screen.queryByTestId("visualization")).not.toBeInTheDocument();+ expect(+ await screen.findByText("There was a problem displaying this chart."),+ ).toBeInTheDocument();+ });++ it("shows an error message when the dataset comes back with an error", async () => {+ setup({ dataset: { error: "Something went wrong" } });+ expect(screen.queryByTestId("visualization")).not.toBeInTheDocument();+ expect(+ await screen.findByText("There was a problem displaying this chart."),+ ).toBeInTheDocument();+ });++ it("surfaces a permission error with the permission message", async () => {+ setup({+ dataset: {+ error: "no access",+ error_type: "missing-required-permissions",+ },+ });+ expect(+ await screen.findByText(+ "Sorry, you don't have permission to see this card.",+ ),+ ).toBeInTheDocument();+ });++ it("shows the curated error text when present", async () => {+ setup({+ dataset: {+ error: "Column FOO does not exist",+ error_is_curated: true,+ },+ });+ expect(+ await screen.findByText("Column FOO does not exist"),+ ).toBeInTheDocument();+ });+});diff --git a/frontend/src/metabase/metabot/state/actions.ts b/frontend/src/metabase/metabot/state/actions.tsindex 3e90e208f04..c11788b1223 100644--- a/frontend/src/metabase/metabot/state/actions.ts+++ b/frontend/src/metabase/metabot/state/actions.ts@@ -446,12 +446,14 @@ export const sendAgentRequest = createAsyncThunk<metadata: { editorTransform, suggestionId },});})- .with({ type: "adhoc_viz" }, (part) => {- pushDataPart({ type: "data_part", part });- })- .with({ type: "static_viz" }, (part) => {- pushDataPart({ type: "data_part", part });- })+ .with(+ { type: "generated_entity" },+ { type: "adhoc_viz" },+ { type: "static_viz" },+ (part) => {+ pushDataPart({ type: "data_part", part });+ },+ ).exhaustive();},onStartMessagePart: function handleStartMessagePart(part) {diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsx b/frontend/src/metabase/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsxindex 817915c5e74..a546e7f7835 100644--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsx+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/CardEmbed/CardEmbedNode.tsx@@ -60,9 +60,9 @@ import { DocumentMode } from "metabase/visualizations/click-actions/modes/Documeimport Visualization from "metabase/visualizations/components/Visualization";import { ErrorView } from "metabase/visualizations/components/Visualization/ErrorView/ErrorView";import ChartSkeleton from "metabase/visualizations/components/skeletons/ChartSkeleton";-import { getGenericErrorMessage } from "metabase/visualizations/lib/errors";+import { getDatasetError } from "metabase/visualizations/lib/errors";import Question from "metabase-lib/v1/Question";-import type { CardDisplayType, Dataset } from "metabase-types/api";+import type { CardDisplayType } from "metabase-types/api";import { CommentsButton } from "../../components/CommentsButton";import {@@ -90,15 +90,6 @@ function formatCardEmbed(attrs: CardEmbedAttributes): string {}}-const getDatasetError = (dataset: Dataset) => {- if (dataset.error) {- return {- message: getGenericErrorMessage(),- icon: "warning" as const,- };- }-};-export interface CardEmbedAttributes {id?: number;name?: string;diff --git a/frontend/src/metabase/visualizations/lib/errors.ts b/frontend/src/metabase/visualizations/lib/errors.tsindex 397463bb2f1..da36405a693 100644--- a/frontend/src/metabase/visualizations/lib/errors.ts+++ b/frontend/src/metabase/visualizations/lib/errors.ts@@ -1,5 +1,8 @@import { msgid, ngettext, t } from "ttag";+import { SERVER_ERROR_TYPES } from "metabase/utils/errors";+import type { Dataset, IconName } from "metabase-types/api";+export class MinColumnsError extends Error {constructor(minColumns: number) {super(@@ -63,3 +66,39 @@ export function getGenericErrorMessage() {export function getPermissionErrorMessage() {return t`Sorry, you don't have permission to see this card.`;}++export function getDatasetPermissionError(+ dataset: Pick<Dataset, "error" | "error_type">,+): { message: string; icon: "key" } | undefined {+ const isPermissionError =+ dataset.error_type === SERVER_ERROR_TYPES.missingPermissions ||+ (dataset.error != null &&+ typeof dataset.error === "object" &&+ dataset.error.status === 403);++ return isPermissionError+ ? { message: getPermissionErrorMessage(), icon: "key" }+ : undefined;+}++export function getDatasetError(+ dataset: Pick<Dataset, "error" | "error_type" | "error_is_curated">,+): { message: string; icon: IconName } | undefined {+ const { error } = dataset;+ if (error == null) {+ return undefined;+ }++ const permissionError = getDatasetPermissionError(dataset);+ if (permissionError) {+ return permissionError;+ }++ return {+ message:+ dataset.error_is_curated && typeof error === "string"+ ? error+ : getGenericErrorMessage(),+ icon: "warning",+ };+}diff --git a/frontend/src/metabase/visualizations/lib/errors.unit.spec.ts b/frontend/src/metabase/visualizations/lib/errors.unit.spec.tsindex 3cadd16b6f2..cefbe1d1a27 100644--- a/frontend/src/metabase/visualizations/lib/errors.unit.spec.ts+++ b/frontend/src/metabase/visualizations/lib/errors.unit.spec.ts@@ -1,4 +1,9 @@-import { MinRowsError } from "metabase/visualizations/lib/errors";+import {+ MinRowsError,+ getDatasetError,+ getGenericErrorMessage,+ getPermissionErrorMessage,+} from "metabase/visualizations/lib/errors";describe("MinRowsError", () => {it("should be an instanceof Error", () => {@@ -9,3 +14,41 @@ describe("MinRowsError", () => {expect(new MinRowsError(0) instanceof MinRowsError).toBe(true);});});++describe("getDatasetError", () => {+ it("returns undefined when there is no error", () => {+ expect(getDatasetError({ error: undefined })).toBeUndefined();+ });++ it("returns a permission error for missing-required-permissions", () => {+ expect(+ getDatasetError({+ error: "nope",+ error_type: "missing-required-permissions",+ }),+ ).toEqual({ message: getPermissionErrorMessage(), icon: "key" });+ });++ it("returns a permission error for a 403 status", () => {+ expect(getDatasetError({ error: { status: 403 } })).toEqual({+ message: getPermissionErrorMessage(),+ icon: "key",+ });+ });++ it("surfaces a curated error string verbatim", () => {+ expect(+ getDatasetError({+ error: "Column FOO does not exist",+ error_is_curated: true,+ }),+ ).toEqual({ message: "Column FOO does not exist", icon: "warning" });+ });++ it("falls back to the generic message for non-curated errors", () => {+ expect(getDatasetError({ error: "boom" })).toEqual({+ message: getGenericErrorMessage(),+ icon: "warning",+ });+ });+});diff --git a/resources/metabot/prompts/tools/construct_notebook_query.md b/resources/metabot/prompts/tools/construct_notebook_query.mdindex 9ee4e07d5c5..9e4d792e6fd 100644--- a/resources/metabot/prompts/tools/construct_notebook_query.md+++ b/resources/metabot/prompts/tools/construct_notebook_query.md@@ -4,6 +4,7 @@ Construct a Metabase MBQL 5 query as a JSON object describing the query shape. MReturn:- `query`: a JSON **object** (never a quoted string). The target database is inferred from the first stage's `source-table` (or `source-card`) — use the **exact** database name reported by `entity_details` / metadata tools.+- `title`: required short, human-friendly title for the resulting chart, shown above it (e.g. `"Orders by month"`).- `visualization`: optional `{"chart_type": "bar"}` (sibling of `query`, never embedded inside it).## Minimal example — count of orders by monthdiff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.cljindex f27c581a397..ef90d437ed4 100644--- a/src/metabase/metabot/agent/core.clj+++ b/src/metabase/metabot/agent/core.clj@@ -271,10 +271,11 @@(map #(get-structured-output (:result %)))(filter #(and (:chart-id %) (:query-id %))))(completing- (fn [mem {:keys [chart-id chart-type query]}]+ (fn [mem {:keys [chart-id query-id chart-type query]}](memory/store-chart memchart-id{:chart_id chart-id+ :query_id query-id:queries [query]:visualization_settings {:chart_type chart-type}})))memory@@ -414,7 +415,7 @@(memory/load-todos-from-state seeded)(memory/load-link-registry-from-state seeded))memory-atom (atom memory)- tools (tools/wrap-tools-with-state base-tools memory-atom metabot-id)]+ tools (tools/wrap-tools-with-state base-tools memory-atom metabot-id profile-id)](log/info "Starting agent" {:profile profile-id:tools (count tools):max-iter (:max-iterations profile)diff --git a/src/metabase/metabot/agent/links.clj b/src/metabase/metabot/agent/links.cljindex ff437d942b9..b7bd748ee01 100644--- a/src/metabase/metabot/agent/links.clj+++ b/src/metabase/metabot/agent/links.clj@@ -28,30 +28,37 @@;;; Query/Chart URL Generation+(defn ->legacy-mbql+ "Normalize a pMBQL query (has `:lib/type`) to legacy MBQL. Frontend /question#+ URLs require legacy MBQL format; non-pMBQL values pass through unchanged."+ [query]+ #_{:clj-kondo/ignore [:discouraged-var]}+ (if (and (map? query) (:lib/type query))+ (lib/->legacy-MBQL query)+ query))+(defn- query->url-hash"Convert an MBQL query to a base64-encoded URL hash for /question# URLs."[query]- ;; Frontend /question# URLs require legacy MBQL format- #_{:clj-kondo/ignore [:discouraged-var]}- (let [dataset-query (if (and (map? query) (:lib/type query))- (lib/->legacy-MBQL query)- query)]- (-> {:dataset_query dataset-query- :type "question"- :visualization_settings {}}- json/encode- (.getBytes "UTF-8")- codecs/bytes->b64-str)))+ (-> {:dataset_query (->legacy-mbql query)+ :type "question"+ :visualization_settings {}}+ json/encode+ (.getBytes "UTF-8")+ codecs/bytes->b64-str))(defn pseudo-card->link"Convert map with relevant card keys into a link. Relevant keys are e.g. dataset_query, display, displayIsLocked.- `:visualization_settings` defaults to `{}` so the frontend always gets a populated map to read chart settings from."+ `:visualization_settings` defaults to `{}` so the frontend always gets a populated map to read chart settings from.+ A pMBQL `:dataset_query` is normalized to legacy MBQL (/question# URLs are legacy-only)."[pc]- (str "/question#"- (-> (merge {:visualization_settings {}} pc)- json/encode- (.getBytes "UTF-8")- codecs/bytes->b64-str)))+ (let [pc (cond-> (merge {:visualization_settings {}} pc)+ (:dataset_query pc) (update :dataset_query ->legacy-mbql))]+ (str "/question#"+ (-> pc+ json/encode+ (.getBytes "UTF-8")+ codecs/bytes->b64-str))))(defn query-and-viz-link"Generate a question link for query and chart type. Chart type"diff --git a/src/metabase/metabot/agent/streaming.clj b/src/metabase/metabot/agent/streaming.cljindex ee4d38b4bf7..07a50c3119a 100644--- a/src/metabase/metabot/agent/streaming.clj+++ b/src/metabase/metabot/agent/streaming.clj@@ -18,6 +18,7 @@(def todo-list-type "AI-SDK data type for todo lists." "todo_list")(def code-edit-type "AI-SDK data type for code edits." "code_edit")(def transform-suggestion-type "AI-SDK data type for transform suggestions." "transform_suggestion")+(def generated-entity-type "AI-SDK data type for generated entities." "generated_entity")(def adhoc-viz-type "AI-SDK data type for ad-hoc visualizations." "adhoc_viz")(def static-viz-type "AI-SDK data type for static visualizations." "static_viz")@@ -136,6 +137,36 @@:version 1:data value})+(defn generated-entity-part+ "Create a GENERATED_ENTITY data part for streaming. `entity` is a map describing+ a generated card (see the FE `GeneratedEntity` type): a titled visualization over+ a referenced query, with that query embedded so the FE can run and render it."+ [entity]+ {:type :data+ :data-type generated-entity-type+ :version 1+ :data entity})++(defn viz-part+ "Return the data part used to surface a query/chart result to the frontend.++ When `inline?` is true (the surface declared it can render visualizations+ inline) returns a `generated_entity` card part that embeds the (legacy)+ dataset_query so the FE runs and renders it in the conversation; otherwise+ returns a `navigate_to` part that sends the user to the question. Pure: the+ caller passes `inline?` (typically `(shared/inline-viz-capable?)`) and a legacy+ `query`. The caller must supply a distinct `entity-id` (card id), `query-id`,+ and `title`; `display` is included when present."+ [{:keys [inline? entity-id query-id query display title link]}]+ (if inline?+ (generated-entity-part+ (cond-> {:type "card"+ :id entity-id+ :title title+ :query {:id query-id :query query}}+ display (assoc :display (some-> display name))))+ (navigate-to-part link)))+;;; Reaction Conversion(defn reactions->data-partsdiff --git a/src/metabase/metabot/tools.clj b/src/metabase/metabot/tools.cljindex e59f4349ed0..f4707e9b0c2 100644--- a/src/metabase/metabot/tools.clj+++ b/src/metabase/metabot/tools.clj@@ -105,7 +105,7 @@#{"analyze_chart""create_chart" "edit_chart" "todo_write" "todo_read" "navigate_user""write_transform_sql" "write_transform_python" "create_autogenerated_dashboard"- "create_sql_query" "edit_sql_query" "replace_sql_query"+ "create_sql_query" "edit_sql_query" "replace_sql_query" "construct_notebook_query""document_schema_collect" "document_construct_sql_chart" "document_construct_model_chart""create_alert" "create_dashboard_subscription" "static_viz"})@@ -134,17 +134,19 @@the skill registry (`metabase.metabot.skills`) and are surfaced as a manifestin the system prompt, with full bodies loaded on demand via the `load_skill`tool."- [tools memory-atom metabot-id]+ [tools memory-atom metabot-id profile-id](reduce-kv(fn [acc tool-name tool-var](let [m (meta tool-var)base-fn (if (contains? state-dependent-tools tool-name)(fn [args](binding [shared/*memory-atom* memory-atom- shared/*metabot-id* metabot-id]+ shared/*metabot-id* metabot-id+ shared/*profile-id* profile-id](tool-var args)))(fn [args]- (binding [shared/*metabot-id* metabot-id]+ (binding [shared/*metabot-id* metabot-id+ shared/*profile-id* profile-id](tool-var args))))tool-scope (:scope m)tool-fn (if tool-scopediff --git a/src/metabase/metabot/tools/charts.clj b/src/metabase/metabot/tools/charts.cljindex f9f4404a158..e23dc2cbc74 100644--- a/src/metabase/metabot/tools/charts.clj+++ b/src/metabase/metabot/tools/charts.clj@@ -1,7 +1,6 @@(ns metabase.metabot.tools.charts"Chart tool wrappers."(:require- [medley.core :as m][metabase.metabot.agent.links :as links][metabase.metabot.agent.streaming :as streaming][metabase.metabot.scope :as scope]@@ -31,25 +30,33 @@[:data_source [:map {:closed true}[:query_id :string]]][:viz_settings [:map {:closed true}- [:chart_type chart-type-enum]]]])+ [:chart_type chart-type-enum]]]+ [:title :string]])(mu/defn ^{:tool-name "create_chart":scope scope/agent-viz-create}create-chart-tool"Create a chart from a query.- Provide a query_id in data_source and a chart_type in viz_settings."- [{:keys [data_source viz_settings]} :- create-chart-schema]+ Provide a query_id in data_source, a chart_type in viz_settings, and a short,+ human-friendly `title` shown above the chart."+ [{:keys [data_source viz_settings title]} :- create-chart-schema](try(let [result (create-chart-tools/create-chart{:query-id (get data_source :query_id):chart-type (keyword (get viz_settings :chart_type)):queries-state (shared/current-queries-state)})- reactions (:reactions result)- structured (assoc (dissoc result :reactions) :result-type :chart)]- (-> {:output (format-chart-output structured)- :structured-output structured}- (m/assoc-some :reactions (not-empty reactions))))+ structured (assoc (dissoc result :results-url) :result-type :chart)]+ {:output (format-chart-output structured)+ :structured-output structured+ :data-parts [(streaming/viz-part+ {:inline? (shared/inline-viz-capable?)+ :entity-id (:chart-id result)+ :query-id (:query-id result)+ :query (links/->legacy-mbql (:query result))+ :display (:chart-type result)+ :title title+ :link (:results-url result)})]})(catch Exception e(log/error e "Error creating chart")(if (:agent-error? (ex-data e))@@ -60,20 +67,22 @@[:map {:closed true}[:chart_id :string][:new_viz_settings [:map {:closed true}- [:chart_type chart-type-enum]]]])+ [:chart_type chart-type-enum]]]+ [:title :string]])(mu/defn ^{:tool-name "edit_chart":scope scope/agent-viz-edit}edit-chart-tool"Edit an existing chart's visualization type.- Provide a new chart_type in new_viz_settings."- [{:keys [chart_id new_viz_settings]} :- edit-chart-schema]+ Provide a new chart_type in new_viz_settings and a short, human-friendly `title`+ shown above the chart."+ [{:keys [chart_id new_viz_settings title]} :- edit-chart-schema](try(let [new-viz (keyword (get new_viz_settings :chart_type))chart (get (shared/current-charts-state) chart_id)- queries (:queries chart)- query (first queries)+ query (or (first (:queries chart))+ (get (shared/current-queries-state) (:query_id chart))){:keys [new-chart-data result]}(edit-chart-tools/edit-chart@@ -86,13 +95,19 @@(when (and (:chart_id new-chart-data) shared/*memory-atom*)(swap! shared/*memory-atom* assoc-in [:state :charts (:chart_id new-chart-data)]new-chart-data))- {:output (format-chart-output structured)+ {:output (format-chart-output structured):structured-output structured- :data-parts [(streaming/navigate-to-part- (links/pseudo-card->link- {:dataset_query query- :display new-viz- :displayIsLocked true}))]})+ :data-parts [(streaming/viz-part+ {:inline? (shared/inline-viz-capable?)+ :entity-id (or (:chart_id new-chart-data) chart_id)+ :query-id (or (:query_id chart) (str (random-uuid)))+ :query (links/->legacy-mbql query)+ :display new-viz+ :title title+ :link (links/pseudo-card->link+ {:dataset_query query+ :display new-viz+ :displayIsLocked true})})]})(catch Exception e(log/error e "Error editing chart")(if (:agent-error? (ex-data e))diff --git a/src/metabase/metabot/tools/charts/create.clj b/src/metabase/metabot/tools/charts/create.cljindex ade8b3ed138..bf7f9e4b2e0 100644--- a/src/metabase/metabot/tools/charts/create.clj+++ b/src/metabase/metabot/tools/charts/create.clj@@ -77,7 +77,8 @@:chart-link (format-chart-link chart-id):chart-type chart-type:query-id query-id+ :query query+ :results-url results-url:instructions (str "Chart created successfully. The user is now viewing the chart.\n""Reference the chart using: [Chart](" (format-chart-link chart-id) ") "- "where 'Chart' is a meaningful description.")- :reactions [{:type :metabot.reaction/redirect :url results-url}]})))+ "where 'Chart' is a meaningful description.")})))diff --git a/src/metabase/metabot/tools/construct.clj b/src/metabase/metabot/tools/construct.cljindex 48561882128..6ef0346a209 100644--- a/src/metabase/metabot/tools/construct.clj+++ b/src/metabase/metabot/tools/construct.clj@@ -7,10 +7,12 @@[metabase.api.common :as api][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib]+ [metabase.metabot.agent.links :as links][metabase.metabot.agent.streaming :as streaming][metabase.metabot.scope :as scope][metabase.metabot.tmpl :as te][metabase.metabot.tools.charts.create :as create-chart-tools]+ [metabase.metabot.tools.shared :as shared][metabase.metabot.tools.shared.content-store :as shared.content-store][metabase.metabot.tools.shared.instructions :as instructions][metabase.metabot.tools.shared.llm-shape :as llm-shape]@@ -49,7 +51,8 @@[:map {:closed true}[:reasoning {:optional true} :string][:query :map]- [:visualization {:optional true} construct-visualization-schema]])+ [:visualization {:optional true} construct-visualization-schema]+ [:title :string]]);;; ---------------------------------------- Source resolution ----------------------------------------@@ -355,9 +358,10 @@construct-notebook-query-tool"Construct and visualize a notebook query from a metric, model, or table.- Accepts an MBQL 5 query as a JSON object matching `::lib.schema/external-query`. See+ Accepts an MBQL 5 query as a JSON object matching `::lib.schema/external-query`, plus a+ short, human-friendly `title` shown above the resulting chart. See`resources/metabot/prompts/tools/construct_notebook_query.md` for the prompt contract."- [{:keys [_reasoning query visualization]} :- construct-notebook-query-args-schema]+ [{:keys [_reasoning query visualization title]} :- construct-notebook-query-args-schema](try(let [normalized-visualization (some-> visualization (update-keys (comp keyword u/->kebab-case-en name)))chart-type (or (chart-type->keyword (:chart-type normalized-visualization))@@ -369,7 +373,7 @@{:query-id (:query-id structured):chart-type chart-type:queries-state {(:query-id structured) (:query structured)}})- navigate-url (get-in chart-result [:reactions 0 :url])+ results-url (:results-url chart-result)full-structured (assoc structured:result-type :query:chart-id (:chart-id chart-result)@@ -387,8 +391,15 @@chart-xml (structured->chart-xml structured (:chart-id chart-result) chart-type)]{:output (str "<result>\n" chart-xml "\n</result>\n""<instructions>\n" instruction-text "\n</instructions>")- :data-parts (when navigate-url- [(streaming/navigate-to-part navigate-url)])+ :data-parts (when results-url+ [(streaming/viz-part+ {:inline? (shared/inline-viz-capable?)+ :entity-id (:chart-id chart-result)+ :query-id (:query-id structured)+ :query (links/->legacy-mbql (:query structured))+ :display chart-type+ :title title+ :link results-url})]):structured-output full-structured:instructions instruction-text});; query-result may already have :output (error) or only :structured-outputdiff --git a/src/metabase/metabot/tools/shared.clj b/src/metabase/metabot/tools/shared.cljindex 518e285b9b8..15e39021ea2 100644--- a/src/metabase/metabot/tools/shared.clj+++ b/src/metabase/metabot/tools/shared.clj@@ -14,6 +14,11 @@so that tools can scope queries to the correct metabot instance's collection."nil)+(def ^:dynamic *profile-id*+ "The profile keyword for the current agent session, e.g. `:nlq`. Bound during the+ agent loop so that tools can adapt their output to the active profile."+ nil)+(defn current-memory"Returns the current agent memory map, or nil if not in an agent context."[]@@ -41,6 +46,11 @@[](get (current-memory) :context))+(defn inline-viz-capable?+ "True when the current profile should render visualizations inline."+ []+ (= *profile-id* :nlq))+(defn with-memory"Helper for debugging memory-bound tools when needed."[f]diff --git a/src/metabase/metabot/tools/sql.clj b/src/metabase/metabot/tools/sql.cljindex 3e6ab33c2a6..28942578b13 100644--- a/src/metabase/metabot/tools/sql.clj+++ b/src/metabase/metabot/tools/sql.clj@@ -1,6 +1,7 @@(ns metabase.metabot.tools.sql"SQL tool wrappers."(:require+ [metabase.metabot.agent.links :as links][metabase.metabot.agent.streaming :as streaming][metabase.metabot.scope :as scope][metabase.metabot.tmpl :as te]@@ -77,14 +78,15 @@(def ^:private create-sql-schema[:map {:closed true}[:database_id :int]- [:sql_query :string]])+ [:sql_query :string]+ [:title :string]])(mu/defn ^{:tool-name "create_sql_query":scope scope/agent-sql-create:capabilities #{:permission-write-sql-queries}}create-sql-query-tool- "Create a new SQL query."- [{:keys [database_id sql_query]} :- create-sql-schema]+ "Create a new SQL query. Provide a short, human-friendly `title` shown above the results."+ [{:keys [database_id sql_query title]} :- create-sql-schema](try(let [{:keys [validation-result action-result]}(create-sql-query-tools/create-sql-query@@ -99,7 +101,12 @@{:output (format-query-output structured instr {:preamble? true}):structured-output structured:instructions instr- :data-parts [(streaming/navigate-to-part results-url)]})+ :data-parts [(streaming/viz-part {:inline? (shared/inline-viz-capable?)+ :entity-id (str (random-uuid))+ :query-id query-id+ :query (links/->legacy-mbql query)+ :title title+ :link results-url})]})(let [instr (instructions/sql-validation-error-instructions dialect error-message)]{:output (format-validation-error-output instr):instructions instr})))@@ -146,14 +153,16 @@[:edits [:sequential [:map {:closed true}[:old_string :string][:new_string :string]- [:replace_all {:optional true} [:maybe :boolean]]]]]])+ [:replace_all {:optional true} [:maybe :boolean]]]]]+ [:title :string]])(mu/defn ^{:tool-name "edit_sql_query":scope scope/agent-sql-edit:capabilities #{:permission-write-sql-queries}}edit-sql-query-tool- "Edit an existing SQL query using structured edits."- [{:keys [query_id edits checklist]} :- edit-sql-schema]+ "Edit an existing SQL query using structured edits. Provide a short,+ human-friendly `title` shown above the results."+ [{:keys [query_id edits checklist title]} :- edit-sql-schema](try(let [{:keys [validation-result action-result]}(edit-sql-query-tools/edit-sql-query@@ -171,7 +180,14 @@{:output (format-query-output structured instr):structured-output structured:instructions instr- :data-parts [(if buffer-id (code-edit-part buffer-id query-content) (streaming/navigate-to-part results-url))]})+ :data-parts [(if buffer-id+ (code-edit-part buffer-id query-content)+ (streaming/viz-part {:inline? (shared/inline-viz-capable?)+ :entity-id (str (random-uuid))+ :query-id query-id+ :query (links/->legacy-mbql query)+ :title title+ :link results-url}))]})(let [instr (instructions/sql-validation-error-instructions dialect error-message)]{:output (format-validation-error-output instr):instructions instr})))@@ -189,14 +205,16 @@[:map {:closed true}[:query_id [:or :string :int]][:checklist :string]- [:new_query :string]])+ [:new_query :string]+ [:title :string]])(mu/defn ^{:tool-name "replace_sql_query":scope scope/agent-sql-edit:capabilities #{:permission-write-sql-queries}}replace-sql-query-tool- "Replace the SQL content of an existing query entirely."- [{:keys [query_id new_query checklist]} :- replace-sql-schema]+ "Replace the SQL content of an existing query entirely. Provide a short,+ human-friendly `title` shown above the results."+ [{:keys [query_id new_query checklist title]} :- replace-sql-schema](try(let [{:keys [validation-result action-result]}(replace-sql-query-tools/replace-sql-query@@ -214,7 +232,14 @@{:output (format-query-output structured instr):structured-output structured:instructions instr- :data-parts [(if buffer-id (code-edit-part buffer-id query-content) (streaming/navigate-to-part results-url))]})+ :data-parts [(if buffer-id+ (code-edit-part buffer-id query-content)+ (streaming/viz-part {:inline? (shared/inline-viz-capable?)+ :entity-id (str (random-uuid))+ :query-id query-id+ :query (links/->legacy-mbql query)+ :title title+ :link results-url}))]})(let [instr (instructions/sql-validation-error-instructions dialect error-message)]{:output (format-validation-error-output instr):instructions instr})))diff --git a/test/metabase/metabot/agent/core_test.clj b/test/metabase/metabot/agent/core_test.cljindex 550e2531694..d97ffeb8584 100644--- a/test/metabase/metabot/agent/core_test.clj+++ b/test/metabase/metabot/agent/core_test.clj@@ -206,6 +206,7 @@memory {:state {:queries {} :charts {}}}updated (#'agent/extract-charts memory parts)](is (= {:chart_id "c-456"+ :query_id "q-123":queries [query]:visualization_settings {:chart_type :bar}} (get-in (memory/get-state updated) [:charts "c-456"])))))(testing "ignores parts without chart-id"@@ -286,6 +287,7 @@:function "construct_notebook_query":arguments {:reasoning "User wants to see orders":query external-query+ :title "First 10 orders":visualization {:chart_type "table"}}}{:type :usage :usage {:promptTokens 200 :completionTokens 30} :model "test" :id "msg-2"}];; Iteration 3: Final text responsediff --git a/test/metabase/metabot/agent/scope_enforcement_test.clj b/test/metabase/metabot/agent/scope_enforcement_test.cljindex a036d26a431..ed67286b246 100644--- a/test/metabase/metabot/agent/scope_enforcement_test.clj+++ b/test/metabase/metabot/agent/scope_enforcement_test.clj@@ -39,7 +39,7 @@:scope "agent:sql:create"})tools-map {"test_tool" tool-var}memory-atom (atom {})- wrapped (tools/wrap-tools-with-state tools-map memory-atom nil)+ wrapped (tools/wrap-tools-with-state tools-map memory-atom nil :sql)wrapped-fn (get-in wrapped ["test_tool" :fn])](testing "tool executes when scope is satisfied"(binding [scope/*current-user-scope* api-scope/unrestricted]@@ -68,7 +68,7 @@:schema [:=> [:cat :map] :map]})tools-map {"legacy_tool" tool-var}memory-atom (atom {})- wrapped (tools/wrap-tools-with-state tools-map memory-atom nil)+ wrapped (tools/wrap-tools-with-state tools-map memory-atom nil :sql)wrapped-fn (get-in wrapped ["legacy_tool" :fn])](testing "tool without :scope always executes regardless of current scope"(binding [scope/*current-user-scope* #{}]diff --git a/test/metabase/metabot/agent/streaming_test.clj b/test/metabase/metabot/agent/streaming_test.cljindex 4e7fc9a8cbf..d954155b676 100644--- a/test/metabase/metabot/agent/streaming_test.clj+++ b/test/metabase/metabot/agent/streaming_test.clj@@ -149,6 +149,38 @@(is (= 1 (:version part)))(is (= value (:data part))))))+(deftest viz-part-test+ (testing "inline -> generated_entity card embedding the query"+ (let [part (streaming/viz-part {:inline? true+ :entity-id "c-1"+ :query-id "q-1"+ :query {:database 1}+ :display :bar+ :title "Orders by month"+ :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}}+ :title "Orders by month"+ :display "bar"}+ (:data part)))))+ (testing "omits display when absent"+ (let [part (streaming/viz-part {:inline? true+ :entity-id "c-1"+ :query-id "q-1"+ :query {:database 1}+ :title "Orders by month"})]+ (is (= "Orders by month" (:title (:data part))))+ (is (not (contains? (:data part) :display)))))+ (testing "non-inline -> navigate_to link"+ (let [part (streaming/viz-part {:inline? false+ :link "/question#abc"})]+ (is (= "navigate_to" (:data-type part)))+ (is (= "/question#abc" (:data part))))))+(deftest data-type-constants-test(testing "data type constants are defined correctly"(is (= "navigate_to" streaming/navigate-to-type))@@ -156,6 +188,7 @@(is (= "todo_list" streaming/todo-list-type))(is (= "code_edit" streaming/code-edit-type))(is (= "transform_suggestion" streaming/transform-suggestion-type))+ (is (= "generated_entity" streaming/generated-entity-type))(is (= "adhoc_viz" streaming/adhoc-viz-type))(is (= "static_viz" streaming/static-viz-type))))diff --git a/test/metabase/metabot/tools/charts/create_test.clj b/test/metabase/metabot/tools/charts/create_test.cljindex faed6322568..c888d0dcf6b 100644--- a/test/metabase/metabot/tools/charts/create_test.clj+++ b/test/metabase/metabot/tools/charts/create_test.clj@@ -20,7 +20,12 @@(is (str/includes? (:chart-content result) "<chart"))(is (str/includes? (:chart-content result) "bar"))(is (str/starts-with? (:chart-link result) "metabase://chart/"))- (is (contains? result :instructions))))+ (is (contains? result :instructions))+ (testing "exposes the question link + query for the viz data part (no redirect reaction)"+ (is (string? (:results-url result)))+ (is (str/starts-with? (:results-url result) "/question#"))+ (is (contains? result :query))+ (is (not (contains? result :reactions))))))(testing "creates chart with different types"(let [queries-state {"q-456" {:query-id "q-456":sql "SELECT COUNT(*) FROM users"diff --git a/test/metabase/metabot/tools/charts/edit_test.clj b/test/metabase/metabot/tools/charts/edit_test.cljindex 66cad6fa360..9a544dd9c5d 100644--- a/test/metabase/metabot/tools/charts/edit_test.clj+++ b/test/metabase/metabot/tools/charts/edit_test.clj@@ -91,6 +91,7 @@external-query (walk/keywordize-keys query-data)construct-result (tools.construct/construct-notebook-query-tool{:query external-query+ :title "Counts by category":visualization {:chart_type "bar"}})query-id (get-in construct-result [:structured-output :query-id])query (get-in construct-result [:structured-output :query])@@ -104,7 +105,8 @@(testing "Edit chart can handle charts created using construct-notebook-query-tool";; (3) call the edit-chart-tool which uses the shared memory(let [edit-result (tools.charts/edit-chart-tool {:chart_id chart-id- :new_viz_settings {:chart_type "pie"}})+ :new_viz_settings {:chart_type "pie"}+ :title "Counts by category"})new-chart-id (get-in edit-result [:structured-output :chart-id])new-chart-in-memory (get (tools.shared/current-charts-state) new-chart-id)](is (= :pie@@ -138,6 +140,7 @@external-query (walk/keywordize-keys query-data)result (tools.construct/construct-notebook-query-tool{:query external-query+ :title "Test chart":visualization {:chart_type "bar"}})query (get-in result [:structured-output :query])](testing "order-by inner clause is now an aggregation reference, not :sum"@@ -181,6 +184,7 @@external-query (walk/keywordize-keys query-data)result (tools.construct/construct-notebook-query-tool{:query external-query+ :title "Test chart":visualization {:chart_type "bar"}})](testing "tool returns a clear, agent-targeted error rather than producing a chart";; The outer `construct-notebook-query-tool` catches the ex-info and returns@@ -213,6 +217,7 @@external-query (walk/keywordize-keys query-data)result (tools.construct/construct-notebook-query-tool{:query external-query+ :title "Test chart":visualization {:chart_type "bar"}})query (get-in result [:structured-output :query])breakout-field (get-in query [:stages 0 :breakout 0])]@@ -267,6 +272,7 @@construct-result (tools.construct/construct-notebook-query-tool{:query external-query+ :title "Test chart":visualization {:chart_type "bar"}})query (get-in construct-result [:structured-output :query])breakout-field (get-in query [:stages 0 :breakout 0])diff --git a/test/metabase/metabot/tools/charts_test.clj b/test/metabase/metabot/tools/charts_test.cljnew file mode 100644index 00000000000..c13dd42a725--- /dev/null+++ b/test/metabase/metabot/tools/charts_test.clj@@ -0,0 +1,66 @@+(ns metabase.metabot.tools.charts-test+ "Tests that chart tools emit inline results in the NLQ profile and `navigate_to` otherwise."+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase.metabot.tools.charts :as charts]+ [metabase.metabot.tools.shared :as shared]))++;; create-chart only needs the query present in queries-state; the link builder+;; json-encodes it and `->legacy-mbql` passes a non-pMBQL value through unchanged,+;; so a stub query is enough to exercise the emission branch without a database.+(def ^:private stub-query+ {:database 1 :type "query" :query {:source-table 1}})++(defn- run-create-chart [profile-id]+ (let [memory (atom {:state {:queries {"q-1" stub-query}}+ :context {}})]+ (binding [shared/*memory-atom* memory+ shared/*profile-id* profile-id]+ (charts/create-chart-tool {:data_source {:query_id "q-1"}+ :viz_settings {:chart_type "bar"}+ :title "Orders by month"}))))++(deftest create-chart-inline-viz-test+ (testing "emits a single generated_entity card part in the NLQ profile"+ (let [result (run-create-chart :nlq)+ parts (:data-parts result)+ entity (:data (first parts))]+ (is (= 1 (count parts)))+ (is (= "generated_entity" (:data-type (first parts))))+ (is (= "card" (:type entity)))+ (is (string? (:id entity)))+ (is (= "Orders by month" (:title entity)))+ (is (= "bar" (:display entity)))+ (testing "embeds the query so the FE can run it"+ (is (= "q-1" (get-in entity [:query :id])))+ (is (some? (get-in entity [:query :query]))))+ (testing "keeps the query in structured-output so chart memory stores it for later edits"+ (is (= stub-query (get-in result [:structured-output :query]))))+ (is (not-any? #(= "navigate_to" (:data-type %)) parts))))+ (testing "emits a single navigate_to data part outside the NLQ profile"+ (let [parts (:data-parts (run-create-chart :sql))]+ (is (= 1 (count parts)))+ (is (= "navigate_to" (:data-type (first parts))))+ (is (str/starts-with? (:data (first parts)) "/question#"))+ (is (not-any? #(= "generated_entity" (:data-type %)) parts)))))++(deftest edit-chart-query-fallback-test+ (testing "edit_chart resolves the query from queries-state when chart memory has no query"+ (let [memory (atom {:state {:queries {"q-1" stub-query}+ :charts {"c-1" {:chart_id "c-1"+ :query_id "q-1"+ :queries [nil]+ :visualization_settings {:chart_type :bar}}}}+ :context {}})+ result (binding [shared/*memory-atom* memory+ shared/*profile-id* :nlq]+ (charts/edit-chart-tool {:chart_id "c-1"+ :new_viz_settings {:chart_type "pie"}+ :title "Orders by month"}))+ parts (:data-parts result)+ entity (:data (first parts))]+ (is (= 1 (count parts)))+ (is (= "generated_entity" (:data-type (first parts))))+ (is (= stub-query (get-in entity [:query :query])))+ (is (= "pie" (:display entity))))))diff --git a/test/metabase/metabot/tools/sql_test.clj b/test/metabase/metabot/tools/sql_test.cljindex cc8a174e631..bce08bc745c 100644--- a/test/metabase/metabot/tools/sql_test.clj+++ b/test/metabase/metabot/tools/sql_test.clj@@ -16,7 +16,8 @@(mt/with-temp [:model/Database {db-id :id} {:engine :h2}](let [result (agent-sql/create-sql-query-tool{:database_id db-id- :sql_query "SELECT 1"})+ :sql_query "SELECT 1"+ :title "Results"})output (:output result)query-id (get-in result [:structured-output :query-id])](is (string? output))@@ -38,7 +39,8 @@(mt/with-temp [:model/Database {db-id :id} {:engine :postgres}](let [result (agent-sql/create-sql-query-tool{:database_id db-id- :sql_query "SELECT ="})+ :sql_query "SELECT ="+ :title "Results"})output (:output result)](is (string? output))(is (str/starts-with? (:instructions result) "The SQL query has a syntax error"))@@ -59,7 +61,8 @@{:query_id query-id:checklist "- [x] checked":edits [{:old_string "SELECT *"- :new_string "SELECT id"}]}))+ :new_string "SELECT id"}]+ :title "Results"}))output (:output result)](is (string? output))(testing "includes query XML with edited content and correct attributes"@@ -87,7 +90,8 @@{:query_id query-id:checklist "- [x] checked":edits [{:old_string "SELECT *"- :new_string "SELECT ="}]}))+ :new_string "SELECT ="}]+ :title "Results"}))output (:output result)](is (string? output))(is (str/starts-with? (:instructions result) "The SQL query has a syntax error"))@@ -107,7 +111,8 @@(agent-sql/replace-sql-query-tool{:query_id query-id:checklist "- [x] checked"- :new_query "SELECT 2"}))+ :new_query "SELECT 2"+ :title "Results"}))output (:output result)](is (string? output))(testing "includes query XML with replaced content and correct attributes"@@ -134,7 +139,45 @@(agent-sql/replace-sql-query-tool{:query_id query-id:checklist "- [x] checked"- :new_query "SELECT ="}))]+ :new_query "SELECT ="+ :title "Results"}))](is (string? output))(is (str/starts-with? instructions "The SQL query has a syntax error"))(is (str/starts-with? output "<result>\nSQL query construction failed.\n</result>\n<instructions>\nThe SQL query has a syntax error")))))))))++(deftest edit-sql-query-inline-viz-test+ (testing "edit_sql_query surfaces results inline in the NLQ profile and navigate otherwise"+ (mt/test-drivers #{:h2}+ (mt/with-current-user (mt/user->id :crowberto)+ (mt/with-temp [:model/Database {:as db} {:engine :h2}]+ (mt/with-db db+ (let [mp (mt/metadata-provider)+ query-id "test-inline-q"+ query (-> (lib/native-query mp "SELECT * FROM t") lib/->legacy-MBQL)+ run (fn [{:keys [context profile-id]}]+ (let [memory (atom {:state {:queries {query-id query}}+ :context context})]+ (binding [shared/*memory-atom* memory+ shared/*profile-id* profile-id]+ (agent-sql/edit-sql-query-tool+ {:query_id query-id+ :checklist "- [x] checked"+ :edits [{:old_string "SELECT *" :new_string "SELECT id"}]+ :title "Results"}))))]+ (testing "NLQ profile -> a single generated_entity (native) part"+ (let [parts (:data-parts (run {:profile-id :nlq}))+ entity (:data (first parts))]+ (is (= 1 (count parts)))+ (is (= "generated_entity" (:data-type (first parts))))+ (is (= "card" (:type entity)))+ (is (= :native (get-in entity [:query :query :type])))))+ (testing "non-NLQ profile -> a single navigate_to part"+ (let [parts (:data-parts (run {:profile-id :sql}))]+ (is (= 1 (count parts)))+ (is (= "navigate_to" (:data-type (first parts))))))+ (testing "an open code-editor buffer wins regardless of profile"+ (let [parts (:data-parts (run {:profile-id :nlq+ :context {:user_is_viewing [{:type "code_editor"+ :buffers [{:id "buf-1"}]}]}}))]+ (is (= 1 (count parts)))+ (is (= "code_edit" (:data-type (first parts)))))))))))))diff --git a/test/metabase/metabot/tools_test.clj b/test/metabase/metabot/tools_test.cljindex 4962090f267..4634ef98c9a 100644--- a/test/metabase/metabot/tools_test.clj+++ b/test/metabase/metabot/tools_test.clj@@ -7,6 +7,7 @@[metabase.metabot.tools :as agent-tools][metabase.metabot.tools.charts.create :as create-chart-tools][metabase.metabot.tools.construct :as construct]+ [metabase.metabot.tools.shared :as shared][metabase.test :as mt]))(deftest all-tools-test@@ -130,8 +131,8 @@:chart-link "metabase://chart/c-1":chart-content "<chart/>":query-id (:query-id args)- :reactions [{:type :metabot.reaction/redirect- :url "/question#hash"}]})]+ :query {:database 1}+ :results-url "/question#hash"})](let [query-input {:lib/type "mbql/query":stages [{:lib/type "mbql.stage/mbql":source-table ["Sample" "PUBLIC" "ORDERS"]@@ -139,6 +140,7 @@result (agent-tools/construct-notebook-query-tool{:reasoning "check seats":query query-input+ :title "Seat check":visualization {:chart_type "table"}})](is (= query-input @query-captured))(is (= "c-1" (get-in result [:structured-output :chart-id])))@@ -153,6 +155,7 @@(is (contains? @#'agent-tools/state-dependent-tools "create_sql_query"))(is (contains? @#'agent-tools/state-dependent-tools "edit_sql_query"))(is (contains? @#'agent-tools/state-dependent-tools "replace_sql_query"))+ (is (contains? @#'agent-tools/state-dependent-tools "construct_notebook_query"))(is (contains? @#'agent-tools/state-dependent-tools "todo_write"))(is (contains? @#'agent-tools/state-dependent-tools "todo_read"))(is (contains? @#'agent-tools/state-dependent-tools "navigate_user"))@@ -172,7 +175,7 @@:charts {"c1" {:query-id "q1"}}}})base-tools {"create_sql_query" #'agent-tools/create-sql-query-tool"search" #'agent-tools/search-tool}- wrapped-tools (agent-tools/wrap-tools-with-state base-tools memory-atom nil)]+ wrapped-tools (agent-tools/wrap-tools-with-state base-tools memory-atom nil :nlq)];; State-dependent tool should be wrapped into a tool-def map(is (map? (get wrapped-tools "create_sql_query")))(is (contains? (get wrapped-tools "create_sql_query") :fn))@@ -183,14 +186,14 @@(testing "wrapped tools preserve original metadata"(let [memory-atom (atom {:state {:queries {} :charts {}}})base-tools {"create_chart" #'agent-tools/create-chart-tool}- wrapped-tools (agent-tools/wrap-tools-with-state base-tools memory-atom nil)+ wrapped-tools (agent-tools/wrap-tools-with-state base-tools memory-atom nil :nlq)wrapped-tool (get wrapped-tools "create_chart")](is (= (:doc (meta #'agent-tools/create-chart-tool)) (:doc wrapped-tool)))(is (= (:schema (meta #'agent-tools/create-chart-tool)) (:schema wrapped-tool)))))(testing "wrapped function receives augmented args with state"(let [memory-atom (atom {:state {:queries {"test-query" {:db 1}}:charts {"test-chart" {:type :bar}}}})- wrapped (agent-tools/wrap-tools-with-state {"create_sql_query" #'agent-tools/create-sql-query-tool} memory-atom nil)+ wrapped (agent-tools/wrap-tools-with-state {"create_sql_query" #'agent-tools/create-sql-query-tool} memory-atom nil :nlq)wrapped-fn (get-in wrapped ["create_sql_query" :fn])];; Just verify the wrapped function is callable(is (fn? wrapped-fn))))@@ -198,13 +201,20 @@(let [memory-atom (atom {:state {:queries {"q1" {:db 1}} :charts {}}})base-tools {"search" #'agent-tools/search-tool"construct_notebook_query" #'agent-tools/construct-notebook-query-tool}- wrapped-tools (agent-tools/wrap-tools-with-state base-tools memory-atom nil)]+ wrapped-tools (agent-tools/wrap-tools-with-state base-tools memory-atom nil :nlq)];; All tools are converted to tool-def maps(is (map? (get wrapped-tools "search")))(is (fn? (:fn (get wrapped-tools "search"))))(is (map? (get wrapped-tools "construct_notebook_query")))(is (fn? (:fn (get wrapped-tools "construct_notebook_query")))))))+(deftest inline-viz-capable-test+ (testing "only the nlq profile emits inline visualizations"+ (binding [shared/*profile-id* :nlq]+ (is (true? (shared/inline-viz-capable?))))+ (binding [shared/*profile-id* :sql]+ (is (false? (shared/inline-viz-capable?))))))+(deftest tool-schemas-exclude-state-keys-test(testing "create_chart schema does not expose state keys"(let [{:keys [schema]} (meta #'agent-tools/create-chart-tool)