Skip to content

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

feat: fix this query feature not using inline sql edits (#70172)

Maksym Yakubychbc7ffe094f71
diff --git a/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsx b/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsx
index c0542d73887..c13725389bf 100644
--- a/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.tsx
@@ -1,38 +1,33 @@
import { t } from "ttag";
+import { useDispatch } from "metabase/lib/redux";
import { useMetabotEnabledEmbeddingAware } from "metabase/metabot/hooks";
+import { setIsNativeEditorOpen } from "metabase/query_builder/actions";
import { Button } from "metabase/ui";
import { useMetabotAgent } from "metabase-enterprise/metabot/hooks";
import { trackQueryFixClicked } from "../../analytics";
-type FixSqlQueryButtonProps = {
- rawSql?: string | null;
- errorMessage?: string | null;
-};
-
-export function FixSqlQueryButton({
- rawSql,
- errorMessage,
-}: FixSqlQueryButtonProps) {
+export function FixSqlQueryButton() {
+ const dispatch = useDispatch();
const isMetabotEnabled = useMetabotEnabledEmbeddingAware();
- const { submitInput } = useMetabotAgent("omnibot");
+ const { submitInput, isDoingScience } = useMetabotAgent("sql");
if (!isMetabotEnabled) {
return null;
}
- const handleClick = () => {
+ const handleClick = async () => {
trackQueryFixClicked();
- const promptParts = ["Fix this SQL query"];
- if (errorMessage) {
- promptParts.push(`The database returned this error: ${errorMessage}`);
- }
- if (rawSql) {
- promptParts.push(`SQL:\n${rawSql}`);
- }
- submitInput(promptParts.join("\n\n"));
+ await dispatch(setIsNativeEditorOpen(true));
+ // SQL and error message are included in the context.
+ await submitInput("Fix this SQL query");
};
- return <Button onClick={handleClick}>{t`Have Metabot fix it`}</Button>;
+ return (
+ <Button
+ loading={isDoingScience}
+ onClick={handleClick}
+ >{t`Have Metabot fix it`}</Button>
+ );
}
diff --git a/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.unit.spec.tsx
index b38ac93cc6a..3104cfd70a2 100644
--- a/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton.unit.spec.tsx
@@ -2,22 +2,43 @@ import userEvent from "@testing-library/user-event";
import { mockSettings } from "__support__/settings";
import { renderWithProviders, screen } from "__support__/ui";
+import { useDispatch } from "metabase/lib/redux";
+import { setIsNativeEditorOpen } from "metabase/query_builder/actions";
+import { useMetabotAgent } from "metabase-enterprise/metabot/hooks";
import { createMockTokenFeatures } from "metabase-types/api/mocks";
import { createMockState } from "metabase-types/store/mocks";
+import { trackQueryFixClicked } from "../../analytics";
+
import { FixSqlQueryButton } from "./FixSqlQueryButton";
-// Mock the useMetabotAgent hook
const mockSubmitInput = jest.fn();
+const mockDispatch = jest.fn();
+const mockSetIsNativeEditorOpen = jest.fn();
+
+jest.mock("../../analytics", () => ({
+ trackQueryFixClicked: jest.fn(),
+}));
+
+jest.mock("metabase/lib/redux", () => ({
+ ...jest.requireActual("metabase/lib/redux"),
+ useDispatch: jest.fn(),
+}));
+
jest.mock("metabase-enterprise/metabot/hooks", () => ({
- useMetabotAgent: () => ({
- submitInput: mockSubmitInput,
- }),
+ useMetabotAgent: jest.fn(),
+}));
+
+jest.mock("metabase/query_builder/actions", () => ({
+ ...jest.requireActual("metabase/query_builder/actions"),
+ setIsNativeEditorOpen: jest.fn(),
}));
-function setup({
- isMetabotEnabled = true,
-}: { isMetabotEnabled?: boolean } = {}) {
+function setup(options?: {
+ isMetabotEnabled?: boolean;
+ isDoingScience?: boolean;
+}) {
+ const { isMetabotEnabled = true, isDoingScience = false } = options ?? {};
const settings = mockSettings({
"metabot-enabled?": isMetabotEnabled,
"token-features": createMockTokenFeatures({
@@ -25,6 +46,15 @@ function setup({
}),
});
+ jest.mocked(useMetabotAgent).mockReturnValue({
+ submitInput: mockSubmitInput,
+ isDoingScience,
+ } as any);
+ jest.mocked(useDispatch).mockReturnValue(mockDispatch as any);
+ jest
+ .mocked(setIsNativeEditorOpen)
+ .mockImplementation(mockSetIsNativeEditorOpen as any);
+
renderWithProviders(<FixSqlQueryButton />, {
storeInitialState: createMockState({ settings }),
});
@@ -33,6 +63,12 @@ function setup({
describe("FixSqlQueryButton", () => {
beforeEach(() => {
jest.clearAllMocks();
+ mockSubmitInput.mockResolvedValue(undefined);
+ mockDispatch.mockResolvedValue(undefined);
+ mockSetIsNativeEditorOpen.mockReturnValue({
+ type: "metabase/qb/SET_IS_NATIVE_EDITOR_OPEN",
+ isNativeEditorOpen: true,
+ });
});
it("should render the button with correct text when metabot is enabled", () => {
@@ -40,6 +76,7 @@ describe("FixSqlQueryButton", () => {
expect(
screen.getByRole("button", { name: /Have Metabot fix it/ }),
).toBeInTheDocument();
+ expect(useMetabotAgent).toHaveBeenCalledWith("sql");
});
it("should not render the button when metabot is disabled", () => {
@@ -49,13 +86,29 @@ describe("FixSqlQueryButton", () => {
).not.toBeInTheDocument();
});
- it("should submit a prompt to the metabot agent when clicked", async () => {
- setup({ isMetabotEnabled: true });
+ it("should submit an SQL fix prompt when clicked", async () => {
+ setup();
await userEvent.click(
screen.getByRole("button", { name: /Have Metabot fix it/ }),
);
+ expect(trackQueryFixClicked).toHaveBeenCalled();
+ expect(mockSetIsNativeEditorOpen).toHaveBeenCalledWith(true);
+ expect(mockDispatch).toHaveBeenCalledWith({
+ type: "metabase/qb/SET_IS_NATIVE_EDITOR_OPEN",
+ isNativeEditorOpen: true,
+ });
expect(mockSubmitInput).toHaveBeenCalledWith("Fix this SQL query");
});
+
+ it("should show a loading state while the SQL agent is processing", () => {
+ setup({ isDoingScience: true });
+
+ expect(
+ screen.getByRole("button", {
+ name: /Have Metabot fix it/,
+ }),
+ ).toBeDisabled();
+ });
});
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-sql-suggestion.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-sql-suggestion.tsx
index 7358ff9a8eb..cb348df3f0c 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-sql-suggestion.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-sql-suggestion.tsx
@@ -68,6 +68,7 @@ export function useMetabotSQLSuggestion({
!responseHasCodeEdit(action)
) {
setHasError(true);
+ throw new Error(t`Something went wrong. Please try again.`);
} else {
onGenerated?.();
}
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/tests/context.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/tests/context.unit.spec.tsx
index 67e0a08eb78..49b8418a727 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/tests/context.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/tests/context.unit.spec.tsx
@@ -1,16 +1,21 @@
+import userEvent from "@testing-library/user-event";
import fetchMock from "fetch-mock";
import { P, isMatching } from "ts-pattern";
import _ from "underscore";
import { setupDatabaseListEndpoint } from "__support__/server-mocks";
+import { screen } from "__support__/ui";
import { useRegisterMetabotContextProvider } from "metabase/metabot";
+import { setIsNativeEditorOpen } from "metabase/query_builder/actions";
import {
createMockDatabase,
createMockUser,
createMockUserPermissions,
} from "metabase-types/api/mocks";
+import { FixSqlQueryButton } from "../../ai-sql-fixer/components/FixSqlQueryButton/FixSqlQueryButton";
import { Metabot } from "../components/Metabot";
+import { METABOT_PROFILE_OVERRIDES } from "../constants";
import {
enterChatMessage,
@@ -20,7 +25,28 @@ import {
whoIsYourFavoriteResponse,
} from "./utils";
+jest.mock("metabase/metabot/hooks", () => ({
+ ...jest.requireActual("metabase/metabot/hooks"),
+ useMetabotEnabledEmbeddingAware: () => true,
+}));
+
+jest.mock("metabase/query_builder/actions", () => ({
+ ...jest.requireActual("metabase/query_builder/actions"),
+ setIsNativeEditorOpen: jest.fn(),
+}));
+
describe("metabot > context", () => {
+ beforeEach(() => {
+ jest.clearAllMocks();
+ jest.mocked(setIsNativeEditorOpen).mockImplementation(
+ (isNativeEditorOpen: boolean) =>
+ ({
+ type: "metabase/qb/SET_IS_NATIVE_EDITOR_OPEN",
+ isNativeEditorOpen,
+ }) as any,
+ );
+ });
+
it("should send along default context", async () => {
setup();
const agentSpy = mockAgentEndpoint({
@@ -101,4 +127,82 @@ describe("metabot > context", () => {
),
).toBe(true);
});
+
+ it("should send SQL profile and SQL error context for SQL fixes", async () => {
+ const rawSql = "SELECT * FROM bad_table";
+ const queryError = "bad_table";
+ const editorOpen = { resolve: undefined as (() => void) | undefined };
+
+ jest.mocked(setIsNativeEditorOpen).mockImplementationOnce(
+ () =>
+ (() =>
+ new Promise<void>((resolve) => {
+ editorOpen.resolve = resolve;
+ })) as any,
+ );
+
+ const agentSpy = mockAgentEndpoint({
+ textChunks: whoIsYourFavoriteResponse,
+ });
+
+ const SqlFixContextRegistration = () => {
+ useRegisterMetabotContextProvider(() =>
+ Promise.resolve({
+ user_is_viewing: [
+ {
+ type: "adhoc",
+ query: {
+ type: "native",
+ database: 1,
+ native: {
+ query: rawSql,
+ },
+ },
+ error: queryError,
+ },
+ ],
+ }),
+ );
+
+ return null;
+ };
+
+ setup({
+ ui: (
+ <>
+ <SqlFixContextRegistration />
+ <FixSqlQueryButton />
+ </>
+ ),
+ });
+
+ await userEvent.click(
+ await screen.findByRole("button", { name: /Have Metabot fix it/ }),
+ );
+
+ expect(setIsNativeEditorOpen).toHaveBeenCalledWith(true);
+ expect(agentSpy).not.toHaveBeenCalled();
+
+ expect(editorOpen.resolve).toBeDefined();
+ editorOpen.resolve?.();
+
+ const requestBody = await lastReqBody(agentSpy);
+ expect(requestBody.profile_id).toBe(METABOT_PROFILE_OVERRIDES.SQL);
+ expect(requestBody.context).toEqual(
+ expect.objectContaining({
+ user_is_viewing: expect.arrayContaining([
+ expect.objectContaining({
+ type: "adhoc",
+ error: queryError,
+ query: expect.objectContaining({
+ type: "native",
+ native: expect.objectContaining({
+ query: rawSql,
+ }),
+ }),
+ }),
+ ]),
+ }),
+ );
+ });
});
diff --git a/eslint.config.mjs b/eslint.config.mjs
index 16e42f27500..62b9dd23476 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -215,7 +215,12 @@ const configs = [
// React Hooks rules
...reactHooksPlugin.configs.recommended.rules,
- "react-hooks/exhaustive-deps": "warn",
+ "react-hooks/exhaustive-deps": [
+ "warn",
+ {
+ additionalHooks: "(useRegisterMetabotContextProvider)",
+ },
+ ],
"no-only-tests/no-only-tests": [
"error",
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
index 8b9bcce6751..31773839f1d 100644
--- a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
@@ -19,7 +19,11 @@ import { useLlmSqlGenerationEnabled } from "metabase/metabot/hooks";
import { PLUGIN_METABOT } from "metabase/plugins";
import * as Lib from "metabase-lib";
import type Question from "metabase-lib/v1/Question";
-import type { DatabaseId, DatasetQuery } from "metabase-types/api";
+import type {
+ DatabaseId,
+ DatasetQuery,
+ GenerateSqlResponse,
+} from "metabase-types/api";
import { MetabotInlineSQLPrompt } from "./MetabotInlineSQLPrompt";
import {
@@ -37,20 +41,15 @@ function useRegisterCodeEditorMetabotContext(
bufferId: string,
): void {
useRegisterMetabotContextProvider(
- async () =>
- buffer
- ? {
- user_is_viewing: [
- {
- type: "code_editor",
- buffers: [
- extractMetabotBufferContext(buffer, databaseId, bufferId),
- ],
- },
- ],
- }
- : {},
- [buffer],
+ async () => ({
+ user_is_viewing: [
+ {
+ type: "code_editor",
+ buffers: [extractMetabotBufferContext(buffer, databaseId, bufferId)],
+ },
+ ],
+ }),
+ [buffer, databaseId, bufferId],
);
}
@@ -92,7 +91,7 @@ export function useInlineSQLPrompt(
);
useRegisterCodeEditorMetabotContext(
- portalTarget?.view,
+ portalTarget?.view ?? undefined,
question.databaseId(),
bufferId,
);
@@ -114,6 +113,13 @@ export function useInlineSQLPrompt(
});
}, [sourceSqlTables]);
+ const onGenerated = useCallback((res: GenerateSqlResponse | undefined) => {
+ if (res) {
+ setSelectedTables(res.referenced_entities as unknown as SelectedTable[]);
+ }
+ setPromptValue("");
+ }, []);
+
const {
source: generatedSource,
isLoading,
@@ -127,18 +133,11 @@ export function useInlineSQLPrompt(
} = PLUGIN_METABOT.useMetabotSQLSuggestion({
databaseId,
bufferId,
- onGenerated: (res) => {
- if (res) {
- setSelectedTables(
- res.referenced_entities as unknown as SelectedTable[],
- );
- }
- setPromptValue("");
- },
+ onGenerated,
});
const getSourceSql = useCallback(() => {
- return portalTarget?.view.state.doc.toString() ?? "";
+ return portalTarget?.view?.state.doc.toString() ?? "";
}, [portalTarget?.view]);
const prevDatabaseIdRef = useRef(databaseId);
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/utils.ts b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/utils.ts
index dc6a689cbec..4c9037d3ed8 100644
--- a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/utils.ts
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/utils.ts
@@ -6,13 +6,13 @@ import type {
} from "metabase-types/api";
export function extractMetabotBufferContext(
- view: EditorView,
+ view: EditorView | undefined,
databaseId: DatabaseId | null,
bufferId: string,
): MetabotCodeEditorBufferContext {
- const state = view.state;
- const selection = state.selection.main;
- const cursorLine = state.doc.lineAt(selection.head);
+ const state = view?.state;
+ const selection = state?.selection.main;
+ const cursorLine = state?.doc.lineAt(selection!.head);
const buffer: MetabotCodeEditorBufferContext = {
id: bufferId,
@@ -21,12 +21,12 @@ export function extractMetabotBufferContext(
database_id: databaseId,
},
cursor: {
- line: cursorLine.number,
- column: cursorLine.from,
+ line: cursorLine?.number ?? 0,
+ column: cursorLine?.from ?? 0,
},
};
- if (!selection.empty) {
+ if (selection && !selection.empty) {
const startLine = state.doc.lineAt(selection.from);
const endLine = state.doc.lineAt(selection.to);
diff --git a/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx b/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
index df4732bcc27..8aca3ff4c89 100644
--- a/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
+++ b/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
@@ -35,8 +35,9 @@ export function useMetabotSQLSuggestion({
referencedEntities?: ReferencedEntity[];
}) => {
if (!databaseId) {
- setError(t`No database selected.`);
- return;
+ const errText = t`No database selected.`;
+ setError(errText);
+ throw new Error(errText);
}
setError(undefined);
try {
@@ -54,15 +55,16 @@ export function useMetabotSQLSuggestion({
setSource(result.sql);
onGenerated?.(result);
} else {
- setError(t`Something went wrong. Please try again.`);
+ throw new Error(t`Something went wrong. Please try again.`);
}
} catch (err) {
const error = err as { status?: number; data?: unknown };
+ let errText = t`Something went wrong. Please try again.`;
if (error.status === 400 && typeof error.data === "string") {
- setError(error.data);
- } else {
- setError(t`Something went wrong. Please try again.`);
+ errText = error.data;
}
+ setError(errText);
+ throw new Error(errText);
}
},
[generateSql, databaseId, onGenerated],
diff --git a/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsx b/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsx
index 9721c12229f..123608329bc 100644
--- a/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsx
+++ b/frontend/src/metabase/query_builder/components/VisualizationError/VisualizationError.tsx
@@ -46,7 +46,6 @@ export function VisualizationError({
const isResultDirty = useSelector(getIsResultDirty);
const showMetabaseLinks = useSelector(getShowMetabaseLinks);
const isNative = question && Lib.queryDisplayInfo(query).isNative;
- const rawSql = isNative ? Lib.rawNativeQuery(query) : null;
if (typeof error === "object" && error.status != null) {
// Assume if the request took more than 15 seconds it was due to a timeout
@@ -144,12 +143,7 @@ export function VisualizationError({
{t`Learn how to debug SQL errors`}
</ExternalLink>
)}
- {!isResultDirty && (
- <PLUGIN_AI_SQL_FIXER.FixSqlQueryButton
- rawSql={rawSql}
- errorMessage={processedError}
- />
- )}
+ {!isResultDirty && <PLUGIN_AI_SQL_FIXER.FixSqlQueryButton />}
</Flex>
</Flex>
</Box>