Skip to content

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

Metabot OSS Single Shot Text-2-Sql (#67883)

Tyler Pludee38d87f6161d
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 4e1fb7f4646..e4c6f7341eb 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -776,6 +776,23 @@
settings
util}}
+ llm
+ {:team "Metabot"
+ :api #{metabase.llm.api
+ metabase.llm.init}
+ :uses #{analytics
+ api
+ driver
+ lib
+ lib-be
+ models
+ premium-features
+ request
+ settings
+ sync
+ util
+ warehouse-schema}}
+
logger
{:team "DevEx"
:api #{metabase.logger.api
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index 7e54ce2f899..c3b120ba21a 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -6,6 +6,8 @@ title: Driver interface changelog
## Metabase 0.59.0
+- Added `metabase.driver/llm-sql-dialect-resource` multimethod. Returns the resource path for dialect-specific LLM prompt instructions, or nil if no dialect-specific instructions exist for this driver.
+
- Added `sql-jdbc.execute/db-type-name` multimethod. Override this if something more than the default is needed in your sql-jdbc-based driver. See the `:mysql` implementation as an example.
## Metabase 0.58.0
diff --git a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
index 1893cfc9a59..5c1d9822f71 100644
--- a/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
+++ b/e2e/test/scenarios/metabot/native-sql-generation.cy.spec.ts
@@ -1,9 +1,13 @@
+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+
const { H } = cy;
// Helper functions
const isMac = Cypress.platform === "darwin";
const metaKey = isMac ? "Meta" : "Control";
+const { ORDERS_ID, PEOPLE_ID } = SAMPLE_DATABASE;
+
const toggleInlineSQLPrompt = () => {
H.NativeEditor.get().focus();
cy.wait(250);
@@ -18,6 +22,12 @@ const cancelButton = () => cy.findByTestId("metabot-inline-sql-cancel");
const errorMessage = () => cy.findByTestId("metabot-inline-sql-error");
const acceptButton = () => cy.findByTestId("accept-proposed-changes-button");
const rejectButton = () => cy.findByTestId("reject-proposed-changes-button");
+const generatingLoader = () => cy.findByTestId("metabot-inline-sql-generating");
+
+// Table bar helpers
+const tableBar = () => cy.findByTestId("metabot-table-input");
+const tableBarInput = () => tableBar().find("input");
+const tablePill = (name: string) => tableBar().contains(name);
describe("Native SQL generation", () => {
describe("OSS", { tags: "@OSS" }, () => {
@@ -32,6 +42,119 @@ describe("Native SQL generation", () => {
toggleInlineSQLPrompt();
inlinePrompt().should("not.exist");
});
+
+ it("should show table bar and support full generation workflow", () => {
+ cy.request("PUT", "/api/setting/llm-anthropic-api-key", {
+ value: "sk-ant-api03-test-token",
+ });
+
+ const finalSQL =
+ "SELECT o.*, p.*\nFROM public.orders o\nJOIN public.people p ON o.user_id = p.id";
+ cy.intercept("POST", "/api/llm/generate-sql", {
+ statusCode: 200,
+ delay: 100,
+ body: {
+ sql: finalSQL,
+ referenced_entities: [
+ {
+ id: ORDERS_ID,
+ model: "table",
+ name: "ORDERS",
+ display_name: "Orders",
+ },
+ {
+ id: PEOPLE_ID,
+ model: "table",
+ name: "PEOPLE",
+ display_name: "People",
+ },
+ ],
+ },
+ }).as("generateSql");
+
+ cy.log("feature is available when LLM is configured");
+ H.startNewNativeQuestion({ query: "SELECT * FROM ORDERS" });
+ H.NativeEditor.get().should("be.visible");
+ toggleInlineSQLPrompt();
+ inlinePrompt().should("be.visible");
+ generateButton().should("be.disabled");
+ tableBar().should("be.visible");
+
+ cy.log("extract-tables populates table pills from existing query");
+ tablePill("Orders").should("be.visible");
+
+ cy.log("typing in table bar input renders search results");
+ tableBarInput().click();
+ cy.realType("people", { pressDelay: 10 });
+ cy.findByRole("option", { name: /People/i }).should("be.visible");
+
+ cy.log("selecting People from dropdown adds it to the list");
+ cy.findByRole("option", { name: /People/i }).click();
+ tablePill("People").should("be.visible");
+ tablePill("Orders").should("be.visible");
+
+ cy.log("selecting Reviews from dropdown adds it to the list");
+ tableBarInput().click();
+ cy.realType("reviews", { pressDelay: 10 });
+ cy.findByRole("option", { name: /Reviews/i }).should("be.visible");
+ cy.findByRole("option", { name: /Reviews/i }).click();
+ tablePill("Reviews").should("be.visible");
+ tablePill("People").should("be.visible");
+ tablePill("Orders").should("be.visible");
+
+ cy.log("can remove table pill by clicking and pressing backspace");
+ tablePill("Reviews").click();
+ cy.realPress("Backspace");
+ tablePill("Reviews").should("not.exist");
+ tablePill("People").should("be.visible");
+ tablePill("Orders").should("be.visible");
+
+ cy.log("generate button enables after typing prompt");
+ inlinePromptInput().click();
+ cy.realType("show me orders", { pressDelay: 10 });
+ generateButton().should("be.enabled");
+
+ cy.log("clicking generate shows loading state and sends correct request");
+ generateButton().click();
+ generatingLoader().should("exist");
+
+ cy.wait("@generateSql").then(({ request }) => {
+ expect(request.body.prompt).to.eq("show me orders");
+ expect(request.body.database_id).to.eq(1);
+ expect(request.body.referenced_entities).to.have.length(2);
+ const tableIds = request.body.referenced_entities.map(
+ (e: { id: number }) => e.id,
+ );
+ expect(tableIds).to.include(ORDERS_ID);
+ expect(tableIds).to.include(PEOPLE_ID);
+ });
+
+ cy.log("generation completes and shows accept/reject buttons");
+ inlinePrompt().should("not.exist");
+ acceptButton().should("be.visible");
+ rejectButton().should("be.visible");
+ H.NativeEditor.get().should("contain", "SELECT * FROM ORDERS");
+
+ cy.log("accepting changes updates the editor");
+ acceptButton().click();
+ acceptButton().should("not.exist");
+ rejectButton().should("not.exist");
+ finalSQL.split("\n").forEach((part) => {
+ H.NativeEditor.get().should("contain", part);
+ });
+
+ cy.log("manually editing query to add reviews extracts reviews table");
+ H.NativeEditor.get().click();
+ H.NativeEditor.get().realPress([metaKey, "a"]);
+ H.NativeEditor.get().realPress("Backspace");
+ H.NativeEditor.type(
+ "SELECT * FROM ORDERS JOIN REVIEWS ON ORDERS.PRODUCT_ID = REVIEWS.PRODUCT_ID",
+ { delay: 10 },
+ );
+ toggleInlineSQLPrompt();
+ tablePill("Orders").should("be.visible");
+ tablePill("Reviews").should("be.visible");
+ });
});
describe("ee", () => {
@@ -46,6 +169,8 @@ describe("Native SQL generation", () => {
});
it("should be able to successfully generate sql", () => {
+ cy.intercept("POST", "/api/llm/extract-tables").as("extractTables");
+
H.startNewNativeQuestion({ query: "SELECT 1" });
H.NativeEditor.get().should("be.visible");
@@ -53,7 +178,11 @@ describe("Native SQL generation", () => {
inlinePrompt().should("be.visible");
generateButton().should("be.disabled");
- inlinePromptInput().type("select all users");
+ cy.log("table bar should not be shown for EE");
+ tableBar().should("not.exist");
+
+ inlinePromptInput().click();
+ cy.realType("select all users", { pressDelay: 10 });
generateButton().should("be.enabled");
H.mockMetabotResponse({
@@ -61,7 +190,7 @@ describe("Native SQL generation", () => {
delay: 100,
});
generateButton().click();
- generateButton().should("contain", "Generating...");
+ generatingLoader().should("exist");
cy.wait("@metabotAgent").then(({ request }) => {
const codeEditor = request.body.context.user_is_viewing.find(
(ctx: { type: string }) => ctx.type === "code_editor",
@@ -85,6 +214,9 @@ describe("Native SQL generation", () => {
rejectButton().should("not.exist");
H.NativeEditor.get().should("not.contain", "SELECT 1");
H.NativeEditor.get().should("contain", "SELECT * FROM users");
+
+ cy.log("extract-tables should not be called for EE");
+ cy.get("@extractTables.all").should("have.length", 0);
});
it("should be able to correctly control the input", () => {
@@ -106,13 +238,14 @@ describe("Native SQL generation", () => {
// cancel button should cancel inflight request
toggleInlineSQLPrompt();
- inlinePromptInput().type("select all users");
+ inlinePromptInput().click();
+ cy.realType("select all users", { pressDelay: 10 });
H.mockMetabotResponse({
body: mockCodeEditResponse("SELECT * FROM users"),
delay: 1000,
});
generateButton().click();
- generateButton().should("contain", "Generating...");
+ generatingLoader().should("exist");
cancelButton().click();
cy.get("@metabotAgent").its("state").should("eq", "Errored");
inlinePrompt().should("not.exist");
@@ -136,7 +269,8 @@ describe("Native SQL generation", () => {
// open input, send a prompt, get suggestion back
toggleInlineSQLPrompt();
- inlinePromptInput().type("select all users");
+ inlinePromptInput().click();
+ cy.realType("select all users", { pressDelay: 10 });
H.mockMetabotResponse({
body: mockCodeEditResponse("SELECT * FROM users"),
});
@@ -154,7 +288,8 @@ describe("Native SQL generation", () => {
inlinePrompt().should("be.visible");
// send another message, history should contain rejection info
- inlinePromptInput().type("try again");
+ inlinePromptInput().click();
+ cy.realType("try again", { pressDelay: 10 });
H.mockMetabotResponse({
body: mockCodeEditResponse("SELECT id FROM users"),
});
@@ -176,7 +311,8 @@ describe("Native SQL generation", () => {
// open again, send a prompt, req.body.history should be empty
toggleInlineSQLPrompt();
- inlinePromptInput().type("select something");
+ inlinePromptInput().click();
+ cy.realType("select something", { pressDelay: 10 });
H.mockMetabotResponse({
body: mockCodeEditResponse("SELECT 123"),
});
@@ -195,7 +331,8 @@ describe("Native SQL generation", () => {
H.startNewNativeQuestion();
H.NativeEditor.get().should("be.visible");
toggleInlineSQLPrompt();
- inlinePromptInput().type("new prompt");
+ inlinePromptInput().click();
+ cy.realType("new prompt", { pressDelay: 10 });
H.mockMetabotResponse({
body: mockCodeEditResponse("SELECT 456"),
});
@@ -218,7 +355,8 @@ describe("Native SQL generation", () => {
H.NativeEditor.get().should("be.visible");
toggleInlineSQLPrompt();
- inlinePromptInput().type("do something");
+ inlinePromptInput().click();
+ cy.realType("do something", { pressDelay: 10 });
H.mockMetabotResponse({
body: mockTextOnlyResponse("I can help with that!"),
});
diff --git a/e2e/test/scenarios/onboarding/command-palette.cy.spec.js b/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
index 778056410f9..d14bca37444 100644
--- a/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
+++ b/e2e/test/scenarios/onboarding/command-palette.cy.spec.js
@@ -612,9 +612,9 @@ describe("shortcuts", { tags: ["@actions"] }, () => {
cy.findByTestId("site-name-setting").should("exist");
cy.location("pathname").should("contain", "/admin/settings");
- cy.realPress("4");
+ cy.realPress("5");
cy.location("pathname").should("contain", "/admin/datamodel");
- cy.realPress("8");
+ cy.realPress("9");
cy.location("pathname").should("contain", "/admin/tools");
});
diff --git a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
index 993db24b526..c43bfee9abf 100644
--- a/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/cloud_add_ons/api.clj
@@ -127,14 +127,6 @@
(not quantity))
response-no-quantity
- (and (= product-type "metabase-ai")
- (not (premium-features/offer-metabase-ai-trial?)))
- response-not-eligible
-
- (and (= product-type "metabase-ai-tiered")
- (not (premium-features/offer-metabase-ai-paid?)))
- response-not-eligible
-
(and (= product-type "python-execution")
(not (premium-features/enable-python-transforms?)))
response-not-eligible
diff --git a/enterprise/backend/src/metabase_enterprise/llm/settings.clj b/enterprise/backend/src/metabase_enterprise/llm/settings.clj
index abe61b4d516..1108fcd271a 100644
--- a/enterprise/backend/src/metabase_enterprise/llm/settings.clj
+++ b/enterprise/backend/src/metabase_enterprise/llm/settings.clj
@@ -7,7 +7,7 @@
(deferred-tru "The OpenAI Model (e.g. ''gpt-4'', ''gpt-3.5-turbo'')")
:encryption :no
:visibility :settings-manager
- :default "gpt-4-turbo-preview"
+ :default "gpt-4.1-mini"
:export? false
:doc false)
@@ -21,7 +21,14 @@
(defsetting ee-openai-api-key
(deferred-tru "The OpenAI API Key used in Metabase Enterprise.")
- :encryption :no
+ :encryption :when-encryption-key-set
+ :visibility :settings-manager
+ :export? false
+ :doc false)
+
+(defsetting ee-anthropic-api-key
+ (deferred-tru "The Anthropic API Key used in Metabase Enterprise.")
+ :encryption :when-encryption-key-set
:visibility :settings-manager
:export? false
:doc false)
diff --git a/enterprise/backend/test/metabase_enterprise/api/session_test.clj b/enterprise/backend/test/metabase_enterprise/api/session_test.clj
index a086ca9a68c..310c3d5f24f 100644
--- a/enterprise/backend/test/metabase_enterprise/api/session_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/api/session_test.clj
@@ -34,8 +34,6 @@
:ai-sql-fixer
:ai-sql-generation
:no-upsell
- :offer-metabase-ai
- :offer-metabase-ai-tiered
:official-collections
:query-reference-validation
:remote-sync
@@ -83,8 +81,6 @@
:ai_entity_analysis true
:ai_sql_fixer true
:ai_sql_generation true
- :offer_metabase_ai true
- :offer_metabase_ai_tiered true
:official_collections true
:query_reference_validation true
:remote_sync true
diff --git a/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj b/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
index 21ff1a94a48..8c79fe6c50e 100644
--- a/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/cloud_add_ons/api_test.clj
@@ -24,19 +24,14 @@
(is (=? "Can only access Store API for Metabase Cloud instances."
(mt/user-http-request :crowberto :post 400 "ee/cloud-add-ons/metabase-ai"
{:terms_of_service true})))))
- (testing "requires token feature 'offer-metabase-ai'"
- (mt/with-premium-features #{:hosting}
- (is (=? "Can only purchase add-ons for eligible subscriptions."
- (mt/user-http-request :crowberto :post 400 "ee/cloud-add-ons/metabase-ai"
- {:terms_of_service true})))))
(testing "requires current user being a store user"
- (mt/with-premium-features #{:hosting :offer-metabase-ai}
+ (mt/with-premium-features #{:hosting}
(is (=? "Only Metabase Store users can purchase add-ons."
(mt/user-http-request :crowberto :post 403 "ee/cloud-add-ons/metabase-ai"
{:terms_of_service true})))))
(testing "when all conditions are met"
(mt/with-temp [:model/User user {:is_superuser true}]
- (mt/with-premium-features #{:hosting :offer-metabase-ai :audit-app}
+ (mt/with-premium-features #{:hosting :audit-app}
;; FIXME: With `(mt/with-temporary-setting-values [token-status {:store-users [{:email (:email user)}]}])`,
;; `(premium-features/token-status)` still returns `nil`; thus resort to `with-redefs`:
(with-redefs [premium-features/token-status (constantly {:store-users [{:email (:email user)}]})]
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsx
index 202fc0759ab..cb1eb7379b4 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotAdminPage.tsx
@@ -1,5 +1,6 @@
import { useDisclosure } from "@mantine/hooks";
import { useEffect, useMemo } from "react";
+import { IndexRoute, Route } from "react-router";
import { push } from "react-router-redux";
import { P, match } from "ts-pattern";
import { c, jt, t } from "ttag";
@@ -48,6 +49,13 @@ import type {
import { MetabotPromptSuggestionPane } from "./MetabotAdminSuggestedPrompts";
import { useMetabotIdPath } from "./utils";
+export function getAdminRoutes() {
+ return [
+ <IndexRoute key="index" component={MetabotAdminPage} />,
+ <Route key="route" path=":metabotId" component={MetabotAdminPage} />,
+ ];
+}
+
export function MetabotAdminPage() {
const metabotId = useMetabotIdPath() ?? FIXED_METABOT_IDS.DEFAULT;
const { data, isLoading, error } = useListMetabotsQuery();
@@ -129,7 +137,7 @@ function MetabotNavPane() {
}
return (
- <Flex direction="column" w="266px" flex="0 0 auto">
+ <Flex direction="column" flex="0 0 auto">
<AdminNavWrapper>
{metabots?.map((metabot) => (
<AdminNavItem
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotPurchasePage/MetabotPurchasePage.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotPurchasePage/MetabotPurchasePage.tsx
index 12ecb8c61ff..0b055700213 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotPurchasePage/MetabotPurchasePage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotPurchasePage/MetabotPurchasePage.tsx
@@ -1,7 +1,9 @@
import type { ReactElement } from "react";
+import { IndexRoute, Redirect } from "react-router";
import { t } from "ttag";
import { SettingsPageWrapper } from "metabase/admin/components/SettingsSection";
+import { AdminSettingsLayout } from "metabase/common/components/AdminLayout/AdminSettingsLayout";
import { useSelector } from "metabase/lib/redux";
import { getStoreUsers } from "metabase/selectors/store-users";
import { Text } from "metabase/ui";
@@ -9,23 +11,32 @@ import { Text } from "metabase/ui";
import { MetabotPurchasePageForNonStoreUser } from "./MetabotPurchasePageForNonStoreUser";
import { MetabotPurchasePageForStoreUser } from "./MetabotPurchasePageForStoreUser";
+export function getAdminRoutes() {
+ return [
+ <IndexRoute key="index" component={MetabotPurchasePage} />,
+ <Redirect key="redirect" from="*" to="/admin/metabot" />,
+ ];
+}
+
export function MetabotPurchasePage(): ReactElement {
const { isStoreUser, anyStoreUserEmailAddress } = useSelector(getStoreUsers);
return (
- <SettingsPageWrapper title={t`Metabot AI`}>
- <Text>
- {t`Metabot helps you move faster and understand your data better.`}
- <br />
- {t`You can ask it to generate SQL, and build or explain queries.`}
- </Text>
- {isStoreUser ? (
- <MetabotPurchasePageForStoreUser />
- ) : (
- <MetabotPurchasePageForNonStoreUser
- anyStoreUserEmailAddress={anyStoreUserEmailAddress}
- />
- )}
- </SettingsPageWrapper>
+ <AdminSettingsLayout>
+ <SettingsPageWrapper title={t`Metabot AI`}>
+ <Text>
+ {t`Metabot helps you move faster and understand your data better.`}
+ <br />
+ {t`You can ask it to generate SQL, and build or explain queries.`}
+ </Text>
+ {isStoreUser ? (
+ <MetabotPurchasePageForStoreUser />
+ ) : (
+ <MetabotPurchasePageForNonStoreUser
+ anyStoreUserEmailAddress={anyStoreUserEmailAddress}
+ />
+ )}
+ </SettingsPageWrapper>
+ </AdminSettingsLayout>
);
}
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotTrialPage.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotTrialPage.tsx
deleted file mode 100644
index e31f0350093..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotTrialPage.tsx
+++ /dev/null
@@ -1,151 +0,0 @@
-import { useDisclosure } from "@mantine/hooks";
-import { useCallback } from "react";
-import { t } from "ttag";
-import * as Yup from "yup";
-
-import {
- SettingsPageWrapper,
- SettingsSection,
-} from "metabase/admin/components/SettingsSection";
-import { ExternalLink } from "metabase/common/components/ExternalLink";
-import { Markdown } from "metabase/common/components/Markdown";
-import {
- Form,
- FormCheckbox,
- FormProvider,
- FormSubmitButton,
-} from "metabase/forms";
-import { useSelector } from "metabase/lib/redux";
-import { getStoreUsers } from "metabase/selectors/store-users";
-import { Divider, Flex, Group, List, Stack, Text } from "metabase/ui";
-import { usePurchaseCloudAddOnMutation } from "metabase-enterprise/api";
-
-import { MetabotSettingUpModal } from "./MetabotSettingUpModal";
-import { handleFieldError, isFetchBaseQueryError } from "./utils";
-
-interface MetabotTrialFormFields {
- terms_of_service: boolean;
-}
-
-export const MetabotTrialPage = () => {
- const { isStoreUser, anyStoreUserEmailAddress } = useSelector(getStoreUsers);
- const [settingUpModalOpened, settingUpModalHandlers] = useDisclosure(false);
- const [purchaseCloudAddOn] = usePurchaseCloudAddOnMutation();
- const onSubmit = useCallback(
- async ({ terms_of_service }: MetabotTrialFormFields) => {
- settingUpModalHandlers.open();
- await purchaseCloudAddOn({
- product_type: "metabase-ai",
- terms_of_service,
- })
- .unwrap()
- .catch((error: unknown) => {
- settingUpModalHandlers.close();
- if (isFetchBaseQueryError(error)) {
- handleFieldError<MetabotTrialFormFields>(
- error.data,
- "terms_of_service",
- );
- }
- throw error;
- });
- },
- [purchaseCloudAddOn, settingUpModalHandlers],
- );
-
- const validationSchema = Yup.object({
- terms_of_service: Yup.boolean()
- .required()
- .isTrue(t`Terms of Service must be accepted`),
- });
-
- return (
- <SettingsPageWrapper title={t`Metabot AI`}>
- <SettingsSection
- title={
- /* eslint-disable-next-line metabase/no-literal-metabase-strings -- This string only shows for admins. */
- t`Get a free month of Metabot, the new AI assistant for Metabase.`
- }
- >
- <Stack>
- <Text>{t`Metabot helps you move faster and understand your data better. You can ask it to:`}</Text>
- <List>
- <List.Item>
- <Markdown>{
- /* eslint-disable-next-line metabase/no-literal-metabase-strings -- This string only shows for admins. */
- t`**Act as a SQL generation copilot** in the Metabase SQL editor`
- }</Markdown>
- </List.Item>
- <List.Item>
- <Markdown>{t`**Help you build queries** based on models and metrics in a specified collection`}</Markdown>
- </List.Item>
- <List.Item>
- <Markdown>{t`**Explain queries** or existing charts`}</Markdown>
- </List.Item>
- </List>
- <video controls aria-label={t`Demonstration of Metabot AI features`}>
- <source
- src="https://www.metabase.com/images/features/metabot.mp4"
- type="video/mp4"
- />
- {t`Your browser does not support the video tag.`}
- </video>
- <Text>{t`Metabot is a new feature in active development. We're constantly rolling out improvements and appreciate your feedback.`}</Text>
- </Stack>
- <Divider />
-
- {isStoreUser ? (
- <FormProvider
- initialValues={{ terms_of_service: false }}
- onSubmit={onSubmit}
- validationSchema={validationSchema}
- >
- {({ values }) => (
- <Form>
- <Stack>
- <FormCheckbox
- name="terms_of_service"
- label={
- <Text>
- {t`I agree with the Metabot AI add-on`}{" "}
- <ExternalLink href="https://www.metabase.com/license/hosting">{t`Terms of Service`}</ExternalLink>
- </Text>
- }
- />
- <Flex justify="start">
- <Group align="center" gap="sm">
- <FormSubmitButton
- disabled={!values.terms_of_service}
- label={t`Add Metabot AI`}
- failedLabel={t`Failed to add Metabot AI`}
- variant="filled"
- mt="xs"
- />
- </Group>
- </Flex>
- <Markdown>
- {t`Your first month is on us. **You won't be charged automatically.** After the trial, you can decide if you'd like to subscribe.`}
- </Markdown>
- </Stack>
- </Form>
- )}
- </FormProvider>
- ) : (
- <Text fw="bold">
- {
- /* eslint-disable-next-line metabase/no-literal-metabase-strings -- This string only shows for admins. */
- t`Please ask a Metabase Store Admin${anyStoreUserEmailAddress && ` (${anyStoreUserEmailAddress})`} of your organization to enable this for you.`
- }
- </Text>
- )}
- </SettingsSection>
- <MetabotSettingUpModal
- opened={settingUpModalOpened}
- onClose={() => {
- settingUpModalHandlers.close();
- window.location.reload();
- }}
- />
- </SettingsPageWrapper>
- );
-};
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotTrialPage.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotTrialPage.unit.spec.tsx
deleted file mode 100644
index 3961f6644f1..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotAdmin/MetabotTrialPage.unit.spec.tsx
+++ /dev/null
@@ -1,202 +0,0 @@
-import userEvent from "@testing-library/user-event";
-import fetchMock from "fetch-mock";
-import { match } from "ts-pattern";
-
-import {
- findRequests,
- setupCurrentUserEndpoint,
- setupPropertiesEndpoints,
-} from "__support__/server-mocks";
-import { renderWithProviders, screen } from "__support__/ui";
-import {
- createMockSettings,
- createMockTokenFeatures,
- createMockUser,
-} from "metabase-types/api/mocks";
-import { createMockSettingsState } from "metabase-types/store/mocks";
-
-import { MetabotTrialPage } from "./MetabotTrialPage";
-
-const setupRefreshableProperties = ({
- current_user_matches_store_user,
- has_metabot_v3 = false,
-}: {
- current_user_matches_store_user: boolean;
- has_metabot_v3?: boolean;
-}) => {
- const settings = createMockSettings({
- "token-status": {
- status: "",
- valid: true,
- features: has_metabot_v3 ? ["metabot-v3"] : [],
- "store-users": current_user_matches_store_user
- ? [{ email: "[email protected]" }]
- : [],
- },
- "token-features": createMockTokenFeatures({ metabot_v3: has_metabot_v3 }),
- });
- setupPropertiesEndpoints(settings);
- return settings;
-};
-
-const setup = async ({
- current_user_matches_store_user,
- simulate_http_post_error,
-}: {
- current_user_matches_store_user: boolean;
- simulate_http_post_error: false | "error-no-quantity" | "error-no-connection";
-}) => {
- const settings = setupRefreshableProperties({
- current_user_matches_store_user,
- });
-
- const user = createMockUser({ email: "[email protected]" });
- setupCurrentUserEndpoint(user);
-
- fetchMock.post(
- "path:/api/ee/cloud-add-ons/metabase-ai",
- match(simulate_http_post_error)
- .with("error-no-quantity", () => ({
- body: { errors: { quantity: "Purchase of add-on requires quantity." } },
- status: 403,
- }))
- .with("error-no-connection", () => ({
- body: "Could not establish a connection to Metabase Cloud.",
- status: 404,
- }))
- .otherwise(() => ({
- status: 200,
- })),
- );
-
- renderWithProviders(<MetabotTrialPage />, {
- // Calling `setupPropertiesEndpoints` above is not enough; we also need to set `storeInitialState`:
- storeInitialState: {
- settings: createMockSettingsState(settings),
- currentUser: user,
- },
- });
-
- await screen.findByText(/Get a free month of Metabot/);
-};
-
-describe("MetabotTrialPage", () => {
- it("shows empty state alternate text when current user is not a store user", async () => {
- await setup({
- current_user_matches_store_user: false,
- simulate_http_post_error: false,
- });
- expect(screen.getByText(/Get a free month of Metabot/)).toBeInTheDocument();
- expect(
- screen.getByText(/Please ask a Metabase Store Admin/),
- ).toBeInTheDocument();
- });
-
- it("requires Terms of Service to be accepted", async () => {
- await setup({
- current_user_matches_store_user: true,
- simulate_http_post_error: false,
- });
- expect(
- screen.queryByText(/Please ask a Metabase Store Admin/),
- ).not.toBeInTheDocument();
- expect(
- screen.getByRole("checkbox", { name: /Terms of Service/ }),
- ).not.toBeChecked();
- expect(
- screen.getByRole("button", { name: /Add Metabot AI/ }),
- ).toBeDisabled();
- await userEvent.click(
- screen.getByRole("checkbox", { name: /Terms of Service/ }),
- );
- expect(
- screen.getByRole("checkbox", { name: /Terms of Service/ }),
- ).toBeChecked();
- expect(
- screen.getByRole("button", { name: /Add Metabot AI/ }),
- ).toBeEnabled();
- });
-
- it("submits a HTTP request", async () => {
- await setup({
- current_user_matches_store_user: true,
- simulate_http_post_error: false,
- });
- await userEvent.click(
- screen.getByRole("checkbox", { name: /Terms of Service/ }),
- );
-
- await userEvent.click(
- screen.getByRole("button", { name: /Add Metabot AI/ }),
- );
- expect(screen.getByRole("button", { name: /Success/ })).toBeInTheDocument();
-
- expect(
- await screen.findByText(/Setting up Metabot AI, please wait/),
- ).toBeInTheDocument();
- expect(
- screen.queryByRole("button", { name: /Done/ }),
- ).not.toBeInTheDocument();
-
- const postRequests = await findRequests("POST");
- const cloudAddOnsRequest = postRequests.find(({ url }) =>
- /\/api\/ee\/cloud-add-ons\/metabase-ai$/.test(url),
- );
- expect(cloudAddOnsRequest).toBeTruthy();
- expect("terms_of_service" in cloudAddOnsRequest?.body).toBeTruthy();
- expect(cloudAddOnsRequest?.body?.terms_of_service).toEqual(true);
-
- setupRefreshableProperties({
- current_user_matches_store_user: true,
- has_metabot_v3: true,
- });
- expect(await screen.findByText(/Metabot AI is ready/)).toBeInTheDocument();
- expect(screen.getByRole("button", { name: /Done/ })).toBeEnabled();
- });
-
- it("reports validation errors", async () => {
- await setup({
- current_user_matches_store_user: true,
- simulate_http_post_error: "error-no-quantity",
- });
- expect(
- await screen.findByText(/Metabot helps you move faster/),
- ).toBeVisible();
- await userEvent.click(
- screen.getByRole("checkbox", { name: /Terms of Service/ }),
- );
- await userEvent.click(
- screen.getByRole("button", { name: /Add Metabot AI/ }),
- );
- expect(await screen.findByText(/Failed to add Metabot AI/)).toBeVisible();
-
- const postRequests = await findRequests("POST");
- const cloudAddOnsRequest = postRequests.find(({ url }) =>
- /\/api\/ee\/cloud-add-ons\/metabase-ai$/.test(url),
- );
- expect(cloudAddOnsRequest).toBeTruthy();
- });
-
- it("reports other errors", async () => {
- await setup({
- current_user_matches_store_user: true,
- simulate_http_post_error: "error-no-connection",
- });
- expect(
- await screen.findByText(/Metabot helps you move faster/),
- ).toBeVisible();
- await userEvent.click(
- screen.getByRole("checkbox", { name: /Terms of Service/ }),
- );
- await userEvent.click(
- screen.getByRole("button", { name: /Add Metabot AI/ }),
- );
- expect(await screen.findByText(/Failed to add Metabot AI/)).toBeVisible();
-
- const postRequests = await findRequests("POST");
- const cloudAddOnsRequest = postRequests.find(({ url }) =>
- /\/api\/ee\/cloud-add-ons\/metabase-ai$/.test(url),
- );
- expect(cloudAddOnsRequest).toBeTruthy();
- });
-});
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsx
index ffefed24d81..5245a24c377 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotChat/MetabotChatEditor/MetabotChatEditor.tsx
@@ -2,12 +2,11 @@ import cx from "classnames";
import { forwardRef } from "react";
import type { MetabotPromptInputRef } from "metabase/metabot";
-import { Box, Icon, UnstyledButton } from "metabase/ui";
-
import {
MetabotPromptInput,
type MetabotPromptInputProps,
-} from "../../MetabotPromptInput";
+} from "metabase/metabot/components/MetabotPromptInput";
+import { Box, Icon, UnstyledButton } from "metabase/ui";
import S from "./MetabotChatEditor.module.css";
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.tsx
deleted file mode 100644
index 9c68c42d4d1..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.tsx
+++ /dev/null
@@ -1,133 +0,0 @@
-import { isFulfilled, isRejected } from "@reduxjs/toolkit";
-import { useCallback, useEffect, useRef, useState } from "react";
-import { tinykeys } from "tinykeys";
-import { t } from "ttag";
-
-import type { MetabotPromptInputRef } from "metabase/metabot";
-import { Box, Button, Flex, Icon, Loader } from "metabase/ui";
-import { METABOT_PROFILE_OVERRIDES } from "metabase-enterprise/metabot/constants";
-import { useMetabotAgent } from "metabase-enterprise/metabot/hooks";
-import type { DatabaseId } from "metabase-types/api";
-
-import { MetabotPromptInput } from "../MetabotPromptInput";
-
-import S from "./MetabotInlineSQLPrompt.module.css";
-import { responseHasCodeEdit } from "./utils";
-
-interface MetabotInlineSQLPromptProps {
- databaseId: DatabaseId | null;
- onClose: () => void;
-}
-
-export const MetabotInlineSQLPrompt = ({
- databaseId,
- onClose,
-}: MetabotInlineSQLPromptProps) => {
- const inputRef = useRef<MetabotPromptInputRef>(null);
- const [value, setValue] = useState("");
- const [hasError, setHasError] = useState(false);
- const { isDoingScience, submitInput, cancelRequest } = useMetabotAgent("sql");
-
- const disabled = !value.trim() || isDoingScience;
-
- const handleSubmit = useCallback(async () => {
- const value = inputRef.current?.getValue?.().trim() ?? "";
- setHasError(false);
- const action = await submitInput(value, {
- profile: METABOT_PROFILE_OVERRIDES.SQL,
- preventOpenSidebar: true,
- });
- if (
- isRejected(action) ||
- (isFulfilled(action) && !action.payload?.success) ||
- !responseHasCodeEdit(action)
- ) {
- setHasError(true);
- }
- }, [submitInput]);
-
- const handleClose = useCallback(() => {
- cancelRequest();
- onClose();
- }, [cancelRequest, onClose]);
-
- useEffect(() => {
- return tinykeys(
- window,
- {
- "$mod+Enter": (e) => {
- if (!disabled) {
- e.preventDefault();
- handleSubmit();
- }
- },
- "$mod+Shift+i": (e) => {
- e.preventDefault();
- handleClose();
- },
- },
- { capture: true },
- );
- }, [disabled, handleSubmit, handleClose]);
-
- return (
- <Box className={S.container} data-testid="metabot-inline-sql-prompt">
- <Box className={S.inputContainer}>
- <MetabotPromptInput
- ref={inputRef}
- value={value}
- placeholder={t`Describe what SQL you want...`}
- autoFocus
- disabled={isDoingScience}
- onChange={setValue}
- onStop={handleClose}
- suggestionConfig={{
- suggestionModels: [
- "dataset",
- "metric",
- "card",
- "table",
- "database",
- ],
- searchOptions: databaseId ? { table_db_id: databaseId } : undefined,
- }}
- />
- </Box>
- <Flex justify="flex-start" align="center" gap="xs" mt="xs">
- <Button
- data-testid="metabot-inline-sql-generate"
- size="xs"
- px="sm"
- variant="filled"
- onClick={handleSubmit}
- disabled={disabled}
- leftSection={
- isDoingScience ? (
- <Loader size="xs" color="text-tertiary" />
- ) : (
- <Icon name="insight" />
- )
- }
- >
- {isDoingScience ? t`Generating...` : t`Generate`}
- </Button>
- <Button
- data-testid="metabot-inline-sql-cancel"
- size="xs"
- variant="subtle"
- onClick={handleClose}
- >
- {t`Cancel`}
- </Button>
- {hasError && (
- <Box
- data-testid="metabot-inline-sql-error"
- fz="sm"
- c="error"
- ml="sm"
- >{t`Something went wrong. Please try again.`}</Box>
- )}
- </Flex>
- </Box>
- );
-};
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/index.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/index.tsx
deleted file mode 100644
index b880c1b0e34..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/index.tsx
+++ /dev/null
@@ -1,2 +0,0 @@
-export * from "./MetabotInlineSQLPrompt";
-export * from "./useInlineSQLPrompt";
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
deleted file mode 100644
index a3ecd543527..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
+++ /dev/null
@@ -1,210 +0,0 @@
-import type { EditorView } from "@codemirror/view";
-import { EditorView as EV, keymap } from "@codemirror/view";
-import type { Extension } from "@uiw/react-codemirror";
-import {
- useCallback,
- useEffect,
- useLayoutEffect,
- useMemo,
- useRef,
- useState,
-} from "react";
-import { createPortal } from "react-dom";
-
-import { useRegisterMetabotContextProvider } from "metabase/metabot/context";
-import {
- useMetabotDispatch,
- useMetabotSelector,
-} from "metabase-enterprise/metabot/hooks/use-metabot-store";
-import {
- addDeveloperMessage,
- getMetabotSuggestedCodeEdit,
- removeSuggestedCodeEdit,
- resetConversation,
-} from "metabase-enterprise/metabot/state";
-import * as Lib from "metabase-lib";
-import type Question from "metabase-lib/v1/Question";
-import type { DatabaseId, DatasetQuery } from "metabase-types/api";
-
-import { MetabotInlineSQLPrompt } from "./MetabotInlineSQLPrompt";
-import {
- type PortalTarget,
- createPromptInputExtension,
- hideEffect,
- toggleEffect,
-} from "./MetabotInlineSQLPromptWidget";
-import { extractMetabotBufferContext } from "./utils";
-
-function useRegisterCodeEditorMetabotContext(
- buffer: EditorView | undefined,
- databaseId: DatabaseId | null,
- bufferId: string,
-): void {
- useRegisterMetabotContextProvider(
- async () =>
- buffer
- ? {
- user_is_viewing: [
- {
- type: "code_editor",
- buffers: [
- extractMetabotBufferContext(buffer, databaseId, bufferId),
- ],
- },
- ],
- }
- : {},
- [buffer],
- );
-}
-
-export interface UseInlineSqlEditResult {
- portalElement: React.ReactPortal | null;
- extensions: Extension[];
- proposedQuestion: Question | undefined;
- handleAcceptProposed?: (datasetQuery: DatasetQuery) => void;
- handleRejectProposed?: () => void;
-}
-
-export function useInlineSQLPrompt(
- question: Question,
- bufferId: string,
-): UseInlineSqlEditResult {
- const [portalTarget, setPortalTarget] = useState<PortalTarget | null>(null);
-
- useRegisterCodeEditorMetabotContext(
- portalTarget?.view,
- question.databaseId(),
- bufferId,
- );
-
- const dispatch = useMetabotDispatch();
- const generatedSql = useMetabotSelector((state) =>
- getMetabotSuggestedCodeEdit(state, bufferId),
- )?.value;
-
- const hideInput = useCallback(() => {
- portalTarget?.view.dispatch({ effects: hideEffect.of() });
- portalTarget?.view.focus();
- }, [portalTarget?.view]);
-
- const hideInputRef = useRef(hideInput);
- useLayoutEffect(() => {
- hideInputRef.current = hideInput;
- });
-
- useEffect(
- function autoCloseOnResult() {
- if (generatedSql) {
- hideInput();
- }
- },
- [generatedSql, hideInput],
- );
-
- const generatedSqlRef = useRef(generatedSql);
- generatedSqlRef.current = generatedSql;
-
- const clearSuggestion = useCallback(() => {
- dispatch(removeSuggestedCodeEdit(bufferId));
- }, [dispatch, bufferId]);
-
- const clearSuggestionRef = useRef(clearSuggestion);
- useEffect(() => {
- clearSuggestionRef.current = clearSuggestion;
- }, [clearSuggestion]);
-
- const databaseId = question.databaseId();
- useEffect(
- function resetOnDbChangeAndUnmount() {
- return () => {
- hideInputRef.current();
- dispatch(removeSuggestedCodeEdit(bufferId));
- dispatch(resetConversation({ agentId: "sql" }));
- };
- },
- [dispatch, databaseId, bufferId],
- );
-
- const resetInput = useCallback(() => {
- dispatch(removeSuggestedCodeEdit(bufferId));
- hideInput();
- }, [dispatch, hideInput, bufferId]);
-
- // NOTE: ref is needed for the extension to not be recalculated in the useMemo
- // below, while still being able to reset the suggestion state on close
- const resetInputRef = useRef(resetInput);
- useEffect(() => {
- resetInputRef.current = resetInput;
- }, [resetInput]);
-
- const proposedQuestion = useMemo(
- () =>
- generatedSql
- ? question.setQuery(Lib.withNativeQuery(question.query(), generatedSql))
- : undefined,
- [generatedSql, question],
- );
-
- const handleRejectProposed = generatedSql
- ? () => {
- resetInput();
- dispatch(
- addDeveloperMessage({
- agentId: "sql",
- message: `User rejected the following suggestion:\n\n${generatedSql}`,
- }),
- );
- }
- : undefined;
-
- const handleAcceptProposed = generatedSql ? resetInput : undefined;
-
- const extensions = useMemo(
- () => [
- createPromptInputExtension(setPortalTarget),
- keymap.of([
- {
- key: `Mod-Shift-i`,
- run: (view) => {
- resetInputRef.current();
- view.dispatch({ effects: toggleEffect.of({ view }) });
- return true;
- },
- },
- ]),
- EV.updateListener.of((update) => {
- if (!update.docChanged || !generatedSqlRef.current) {
- return;
- }
- // Only clear suggestion if change was from user input, not programmatic
- const isUserEdit = update.transactions.some(
- (tr) =>
- tr.isUserEvent("input") ||
- tr.isUserEvent("delete") ||
- tr.isUserEvent("move"),
- );
- if (isUserEdit) {
- clearSuggestionRef.current();
- }
- }),
- ],
- [],
- );
-
- return {
- extensions,
- portalElement: portalTarget
- ? createPortal(
- <MetabotInlineSQLPrompt
- databaseId={databaseId}
- onClose={hideInput}
- />,
- portalTarget.container,
- )
- : null,
- proposedQuestion,
- handleRejectProposed,
- handleAcceptProposed,
- };
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotQueryBuilder/MetabotQueryBuilder.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotQueryBuilder/MetabotQueryBuilder.tsx
index bb09419f71b..c7ca391bcb9 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotQueryBuilder/MetabotQueryBuilder.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotQueryBuilder/MetabotQueryBuilder.tsx
@@ -8,6 +8,7 @@ import _ from "underscore";
import { MetabotLogo } from "metabase/common/components/MetabotLogo";
import { useDispatch } from "metabase/lib/redux";
+import { MetabotPromptInput } from "metabase/metabot/components/MetabotPromptInput";
import { useRouter } from "metabase/router";
import {
Box,
@@ -22,7 +23,6 @@ import { useGetSuggestedMetabotPromptsQuery } from "metabase-enterprise/api";
import { Urls } from "metabase-enterprise/urls";
import { useMetabotAgent } from "../../hooks";
-import { MetabotPromptInput } from "../MetabotPromptInput";
import S from "./MetabotQueryBuilder.module.css";
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/index.ts b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/index.ts
index 237f62445d1..a59c183270d 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/hooks/index.ts
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/index.ts
@@ -1,2 +1,3 @@
export { useMetabotAgent } from "./use-metabot-agent";
export { useMetabotAgentsManager } from "./use-metabot-agents-manager";
+export { useMetabotSQLSuggestion } from "./use-metabot-sql-suggestion";
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
new file mode 100644
index 00000000000..a20455183fe
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/hooks/use-metabot-sql-suggestion.tsx
@@ -0,0 +1,112 @@
+import { isFulfilled, isRejected } from "@reduxjs/toolkit";
+import { useCallback, useMemo, useState } from "react";
+import { t } from "ttag";
+
+import type { UseMetabotSQLSuggestionOptions } from "metabase/metabot/hooks/use-metabot-sql-suggestion";
+import type { SuggestionModel } from "metabase/rich_text_editing/tiptap/extensions/shared/types";
+import {
+ useMetabotDispatch,
+ useMetabotSelector,
+} from "metabase-enterprise/metabot/hooks/use-metabot-store";
+import {
+ addDeveloperMessage,
+ getMetabotSuggestedCodeEdit,
+ removeSuggestedCodeEdit,
+ resetConversation,
+} from "metabase-enterprise/metabot/state";
+
+import { METABOT_PROFILE_OVERRIDES } from "../constants";
+
+import { useMetabotAgent } from "./use-metabot-agent";
+
+type SubmitInputResult = Awaited<
+ ReturnType<ReturnType<typeof useMetabotAgent>["submitInput"]>
+>;
+
+const responseHasCodeEdit = (action: SubmitInputResult) => {
+ return (
+ isFulfilled(action) &&
+ action.payload.data?.processedResponse.data.some(
+ (dp) =>
+ typeof dp === "object" &&
+ dp !== null &&
+ "type" in dp &&
+ (dp as { type: string }).type === "code_edit",
+ )
+ );
+};
+
+export function useMetabotSQLSuggestion({
+ bufferId,
+ onGenerated,
+}: UseMetabotSQLSuggestionOptions) {
+ const { isDoingScience, submitInput, cancelRequest } = useMetabotAgent("sql");
+
+ const [hasError, setHasError] = useState(false);
+
+ const dispatch = useMetabotDispatch();
+ const source = useMetabotSelector((state) =>
+ getMetabotSuggestedCodeEdit(state, bufferId),
+ )?.value;
+
+ const generate = useCallback(
+ async ({
+ prompt,
+ }: {
+ prompt: string;
+ sourceSql?: string;
+ referencedEntities?: unknown;
+ }) => {
+ setHasError(false);
+ const action = await submitInput(prompt, {
+ profile: METABOT_PROFILE_OVERRIDES.SQL,
+ preventOpenSidebar: true,
+ });
+ if (
+ isRejected(action) ||
+ (isFulfilled(action) && !action.payload?.success) ||
+ !responseHasCodeEdit(action)
+ ) {
+ setHasError(true);
+ } else {
+ onGenerated?.();
+ }
+ },
+ [submitInput, onGenerated],
+ );
+
+ const reject = useCallback(() => {
+ dispatch(
+ addDeveloperMessage({
+ agentId: "sql",
+ message: `User rejected the following suggestion:\n\n${source}`,
+ }),
+ );
+ }, [dispatch, source]);
+
+ const clear = useCallback(() => {
+ dispatch(removeSuggestedCodeEdit(bufferId));
+ }, [dispatch, bufferId]);
+
+ const reset = useCallback(() => {
+ dispatch(removeSuggestedCodeEdit(bufferId));
+ dispatch(resetConversation({ agentId: "sql" }));
+ }, [dispatch, bufferId]);
+
+ const suggestionModels: SuggestionModel[] = useMemo(
+ () => ["dataset", "card", "table"],
+ [],
+ );
+
+ return {
+ source,
+ isLoading: isDoingScience,
+ generate,
+ error: hasError ? t`Something went wrong. Please try again.` : undefined,
+ cancelRequest,
+ clear,
+ reject,
+ reset,
+ suggestionModels,
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/index.tsx b/enterprise/frontend/src/metabase-enterprise/metabot/index.tsx
index 8282b57dcfb..4abd1271a57 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/index.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/metabot/index.tsx
@@ -1,27 +1,22 @@
-import { IndexRoute, Route } from "react-router";
-import { t } from "ttag";
+import { Route } from "react-router";
-import { createAdminRouteGuard } from "metabase/admin/utils";
-import { AdminSettingsLayout } from "metabase/common/components/AdminLayout/AdminSettingsLayout";
import { PLUGIN_METABOT, PLUGIN_REDUCERS } from "metabase/plugins";
import { useLazyMetabotGenerateContentQuery } from "metabase-enterprise/api";
-import { MetabotPurchasePage } from "metabase-enterprise/metabot/components/MetabotAdmin/MetabotPurchasePage";
+import { getAdminRoutes as getAdminUpsellRoutes } from "metabase-enterprise/metabot/components/MetabotAdmin/MetabotPurchasePage";
import { MetabotDataStudioSidebar } from "metabase-enterprise/metabot/components/MetabotDataStudioSidebar";
import { hasPremiumFeature } from "metabase-enterprise/settings";
import { Metabot } from "./components/Metabot";
-import { MetabotAdminPage } from "./components/MetabotAdmin/MetabotAdminPage";
-import { MetabotTrialPage } from "./components/MetabotAdmin/MetabotTrialPage";
+import { getAdminRoutes } from "./components/MetabotAdmin/MetabotAdminPage";
import { MetabotAppBarButton } from "./components/MetabotAppBarButton";
import MetabotThinkingStyles from "./components/MetabotChat/MetabotThinking.module.css";
import { MetabotDataStudioButton } from "./components/MetabotDataStudioButton";
-import { useInlineSQLPrompt } from "./components/MetabotInlineSQLPrompt";
import { MetabotQueryBuilder } from "./components/MetabotQueryBuilder";
import { getMetabotQuickLinks } from "./components/MetabotQuickLinks";
import { getNewMenuItemAIExploration } from "./components/NewMenuItemAIExploration";
import { MetabotContext, MetabotProvider, defaultContext } from "./context";
+import { useMetabotSQLSuggestion as useMetabotSQLSuggestionEE } from "./hooks";
import { getMetabotVisible, metabotReducer } from "./state";
-
/**
* This is for Metabot in embedding
*
@@ -40,74 +35,30 @@ PLUGIN_REDUCERS.metabotPlugin = metabotReducer;
*/
export function initializePlugin() {
if (hasPremiumFeature("metabot_v3")) {
- PLUGIN_METABOT.isEnabled = () => true;
- PLUGIN_METABOT.Metabot = Metabot;
- PLUGIN_METABOT.getMetabotRoutes = getMetabotQuickLinks;
-
- PLUGIN_METABOT.getAdminPaths = () => [
- {
- name: t`AI`,
- path: "/admin/metabot",
- key: "metabot",
- },
- ];
- PLUGIN_METABOT.getAdminRoutes = () => (
- <Route
- key="metabot"
- path="metabot"
- component={createAdminRouteGuard("metabot")}
- >
- <IndexRoute component={MetabotAdminPage} />
- <Route path=":metabotId" component={MetabotAdminPage} />
- </Route>
- );
-
- PLUGIN_METABOT.getMetabotVisible =
- getMetabotVisible as unknown as typeof PLUGIN_METABOT.getMetabotVisible;
-
- PLUGIN_METABOT.MetabotAppBarButton = MetabotAppBarButton;
- PLUGIN_METABOT.MetabotDataStudioButton = MetabotDataStudioButton;
- PLUGIN_METABOT.MetabotDataStudioSidebar = MetabotDataStudioSidebar;
- PLUGIN_METABOT.getMetabotQueryBuilderRoute = () => (
- <Route path="ask" component={MetabotQueryBuilder} />
- );
- PLUGIN_METABOT.getNewMenuItemAIExploration = getNewMenuItemAIExploration;
- PLUGIN_METABOT.useLazyMetabotGenerateContentQuery =
- useLazyMetabotGenerateContentQuery;
- PLUGIN_METABOT.MetabotThinkingStyles = MetabotThinkingStyles;
- } else if (hasPremiumFeature("offer_metabase_ai_tiered")) {
- PLUGIN_METABOT.getAdminPaths = () => [
- {
- name: t`AI`,
- path: "/admin/metabot",
- key: "metabot",
- },
- ];
- PLUGIN_METABOT.getAdminRoutes = () => (
- <Route path="metabot" component={createAdminRouteGuard("metabot")}>
- <Route component={AdminSettingsLayout}>
- <IndexRoute component={MetabotPurchasePage} />
- </Route>
- </Route>
- );
- } else if (hasPremiumFeature("offer_metabase_ai")) {
- PLUGIN_METABOT.getAdminPaths = () => [
- {
- name: t`AI`,
- path: "/admin/metabot",
- key: "metabot",
- },
- ];
- PLUGIN_METABOT.getAdminRoutes = () => (
- <Route path="metabot" component={createAdminRouteGuard("metabot")}>
- <Route component={AdminSettingsLayout}>
- <IndexRoute component={MetabotTrialPage} />
- </Route>
- </Route>
- );
- PLUGIN_METABOT.useLazyMetabotGenerateContentQuery =
- useLazyMetabotGenerateContentQuery;
- PLUGIN_METABOT.MetabotThinkingStyles = MetabotThinkingStyles;
+ Object.assign(PLUGIN_METABOT, {
+ // helpers
+ isEnabled: () => true,
+ getNewMenuItemAIExploration,
+ getMetabotVisible,
+ // routes
+ getAdminRoutes,
+ getMetabotRoutes: getMetabotQuickLinks,
+ getMetabotQueryBuilderRoute: () => (
+ <Route path="ask" component={MetabotQueryBuilder} />
+ ),
+ // components
+ Metabot,
+ MetabotAppBarButton,
+ MetabotDataStudioButton,
+ MetabotDataStudioSidebar,
+ MetabotThinkingStyles,
+ // hooks
+ useMetabotSQLSuggestion: useMetabotSQLSuggestionEE,
+ useLazyMetabotGenerateContentQuery,
+ });
+ } else if (hasPremiumFeature("hosting")) {
+ Object.assign(PLUGIN_METABOT, {
+ getAdminRoutes: getAdminUpsellRoutes,
+ });
}
- PLUGIN_METABOT.useInlineSQLPrompt = useInlineSQLPrompt;
}
diff --git a/frontend/src/metabase-types/api/index.ts b/frontend/src/metabase-types/api/index.ts
index 86b4b0a9dda..9590a2125d8 100644
--- a/frontend/src/metabase-types/api/index.ts
+++ b/frontend/src/metabase-types/api/index.ts
@@ -23,6 +23,7 @@ export * from "./entity-id";
export * from "./field";
export * from "./geojson";
export * from "./group";
+export * from "./llm";
export * from "./logger";
export * from "./measure";
export * from "./metabot";
diff --git a/frontend/src/metabase-types/api/llm.ts b/frontend/src/metabase-types/api/llm.ts
new file mode 100644
index 00000000000..e76f71566dc
--- /dev/null
+++ b/frontend/src/metabase-types/api/llm.ts
@@ -0,0 +1,36 @@
+import type { DatabaseId } from "./database";
+import type { Table } from "./table";
+
+export type ReferencedEntity = { model: "table" } & Table;
+
+export type ReferencedEntityId = Pick<ReferencedEntity, "id" | "model">;
+
+export interface GenerateSqlRequest {
+ prompt: string;
+ database_id: DatabaseId;
+ source_sql?: string;
+ referenced_entities?: ReferencedEntityId[];
+}
+
+export interface GenerateSqlResponse {
+ sql: string;
+ referenced_entities: ReferencedEntity[];
+}
+
+export interface ExtractTablesRequest {
+ database_id: DatabaseId;
+ sql: string;
+}
+
+export interface ExtractTablesResponse {
+ tables: Table[];
+}
+
+export interface LLMModel {
+ id: string;
+ display_name: string;
+}
+
+export interface ListModelsResponse {
+ models: LLMModel[];
+}
diff --git a/frontend/src/metabase-types/api/mocks/settings.ts b/frontend/src/metabase-types/api/mocks/settings.ts
index 6db41f81e2a..b0238276895 100644
--- a/frontend/src/metabase-types/api/mocks/settings.ts
+++ b/frontend/src/metabase-types/api/mocks/settings.ts
@@ -133,8 +133,6 @@ export const createMockTokenFeatures = (
ai_entity_analysis: false,
ai_sql_fixer: false,
ai_sql_generation: false,
- offer_metabase_ai: false,
- offer_metabase_ai_tiered: false,
database_routing: false,
development_mode: false,
table_data_editing: false,
@@ -230,6 +228,7 @@ export const createMockSettings = (
"ee-ai-features-enabled": false,
"ee-openai-model": "",
"ee-openai-api-key": "",
+ "llm-anthropic-model": "",
"email-configured?": false,
"email-smtp-host": null,
"email-smtp-port": null,
@@ -309,6 +308,7 @@ export const createMockSettings = (
"ldap-group-mappings": null,
"ldap-group-membership-filter": "(member={dn})",
"loading-message": "doing-science",
+ "llm-sql-generation-enabled": false,
"map-tile-server-url": "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
"native-query-autocomplete-match-style": "substring",
"openai-api-key": null,
diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.ts
index 99ba58b9d0e..8a4dc1fdff2 100644
--- a/frontend/src/metabase-types/api/settings.ts
+++ b/frontend/src/metabase-types/api/settings.ts
@@ -258,8 +258,6 @@ const tokenStatusFeatures = [
"metabase-store-managed",
"metabot-v3",
"no-upsell",
- "offer-metabase-ai",
- "offer-metabase-ai-tiered",
"official-collections",
"query-reference-validation",
"question-error-logs",
@@ -339,8 +337,6 @@ export const tokenFeatures = [
"collection_cleanup",
"cache_preemptive",
"metabot_v3",
- "offer_metabase_ai",
- "offer_metabase_ai_tiered",
"ai_sql_fixer",
"ai_sql_generation",
"ai_entity_analysis",
@@ -440,6 +436,7 @@ interface InstanceSettings {
"example-dashboard-id": number | null;
"has-sample-database?"?: boolean; // Careful! This can be undefined during setup!
"instance-creation": string;
+ "llm-sql-generation-enabled": boolean;
"read-only-mode": boolean;
"search-typeahead-enabled": boolean;
"show-homepage-data": boolean;
@@ -743,6 +740,8 @@ export interface EnterpriseSettings extends Settings {
"python-storage-s-3-path-style-access"?: boolean | null;
"python-runner-timeout-seconds"?: number | null;
"python-runner-test-run-timeout-seconds"?: number | null;
+ "llm-anthropic-api-key"?: string | null;
+ "llm-anthropic-model": string;
/**
* @deprecated
*/
diff --git a/frontend/src/metabase/admin/app/reducers.ts b/frontend/src/metabase/admin/app/reducers.ts
index e19f51ca689..25f84fa8283 100644
--- a/frontend/src/metabase/admin/app/reducers.ts
+++ b/frontend/src/metabase/admin/app/reducers.ts
@@ -3,10 +3,7 @@ import { t } from "ttag";
import { combineReducers } from "metabase/lib/redux";
import { isNotNull } from "metabase/lib/types";
-import {
- PLUGIN_ADMIN_ALLOWED_PATH_GETTERS,
- PLUGIN_METABOT,
-} from "metabase/plugins";
+import { PLUGIN_ADMIN_ALLOWED_PATH_GETTERS } from "metabase/plugins";
import { refreshCurrentUser } from "metabase/redux/user";
import type { AdminPath, AdminPathKey } from "metabase-types/store";
@@ -27,7 +24,11 @@ export const getAdminPaths: () => AdminPath[] = () => {
path: "/admin/embedding",
key: "embedding",
},
- ...PLUGIN_METABOT.getAdminPaths(),
+ {
+ name: t`AI`,
+ path: "/admin/metabot",
+ key: "metabot",
+ },
{
name: t`Table Metadata`,
path: "/admin/datamodel",
diff --git a/frontend/src/metabase/admin/routes.jsx b/frontend/src/metabase/admin/routes.jsx
index 327bfdb262c..378272d6e70 100644
--- a/frontend/src/metabase/admin/routes.jsx
+++ b/frontend/src/metabase/admin/routes.jsx
@@ -37,6 +37,7 @@ import {
} from "metabase/admin/tools/components/ModelCacheRefreshJobs";
import { EmbeddingHubAdminSettingsPage } from "metabase/embedding/embedding-hub";
import { ModalRoute } from "metabase/hoc/ModalRoute";
+import { getAdminRoutes as getMetabotAdminRoutes } from "metabase/metabot/components/MetabotAdmin/MetabotAdminPage";
import { DataModelV1 } from "metabase/metadata/pages/DataModelV1";
import {
PLUGIN_ADMIN_TOOLS,
@@ -44,7 +45,6 @@ import {
PLUGIN_CACHING,
PLUGIN_DB_ROUTING,
PLUGIN_DEPENDENCIES,
- PLUGIN_METABOT,
PLUGIN_SUPPORT,
PLUGIN_TENANTS,
} from "metabase/plugins";
@@ -262,7 +262,12 @@ export const getRoutes = (store, CanAccessSettings, IsAdmin) => {
/>
</Route>
</Route>
- {PLUGIN_METABOT.getAdminRoutes()}
+
+ {/* Metabot */}
+ <Route path="metabot" component={createAdminRouteGuard("metabot")}>
+ {getMetabotAdminRoutes()}
+ </Route>
+
<Route path="tools" component={createAdminRouteGuard("tools")}>
<Route title={t`Tools`} component={ToolsApp}>
<IndexRedirect to="help" />
diff --git a/frontend/src/metabase/api/index.ts b/frontend/src/metabase/api/index.ts
index 0bd9a75a0db..39bccefe8b8 100644
--- a/frontend/src/metabase/api/index.ts
+++ b/frontend/src/metabase/api/index.ts
@@ -20,6 +20,7 @@ export * from "./entity-id";
export * from "./field";
export * from "./google";
export * from "./ldap";
+export * from "./llm";
export * from "./login-history";
export * from "./measure";
export * from "./model-index";
diff --git a/frontend/src/metabase/api/llm.ts b/frontend/src/metabase/api/llm.ts
new file mode 100644
index 00000000000..ecb453e0909
--- /dev/null
+++ b/frontend/src/metabase/api/llm.ts
@@ -0,0 +1,42 @@
+import type {
+ ExtractTablesRequest,
+ ExtractTablesResponse,
+ GenerateSqlRequest,
+ GenerateSqlResponse,
+ ListModelsResponse,
+} from "metabase-types/api";
+
+import { Api } from "./api";
+import { listTag } from "./tags";
+
+export const llmApi = Api.injectEndpoints({
+ endpoints: (builder) => ({
+ generateSql: builder.mutation<GenerateSqlResponse, GenerateSqlRequest>({
+ query: (body) => ({
+ method: "POST",
+ url: "/api/llm/generate-sql",
+ body,
+ }),
+ }),
+ extractTables: builder.query<ExtractTablesResponse, ExtractTablesRequest>({
+ query: (body) => ({
+ method: "POST",
+ url: "/api/llm/extract-tables",
+ body,
+ }),
+ }),
+ listModels: builder.query<ListModelsResponse, void>({
+ query: () => ({
+ method: "GET",
+ url: "/api/llm/list-models",
+ }),
+ providesTags: () => [listTag("llm-models")],
+ }),
+ }),
+});
+
+export const {
+ useGenerateSqlMutation,
+ useExtractTablesQuery,
+ useListModelsQuery,
+} = llmApi;
diff --git a/frontend/src/metabase/api/settings.ts b/frontend/src/metabase/api/settings.ts
index 3e42134fae5..afe4d14a876 100644
--- a/frontend/src/metabase/api/settings.ts
+++ b/frontend/src/metabase/api/settings.ts
@@ -55,6 +55,7 @@ export const settingsApi = Api.injectEndpoints({
return invalidateTags(error, [
tag("session-properties"),
...(key === "uploads-settings" ? [listTag("database")] : []),
+ ...(key === "llm-anthropic-api-key" ? [listTag("llm-models")] : []),
]);
},
}),
diff --git a/frontend/src/metabase/api/tags/constants.ts b/frontend/src/metabase/api/tags/constants.ts
index 50a1738e682..15bb0568a83 100644
--- a/frontend/src/metabase/api/tags/constants.ts
+++ b/frontend/src/metabase/api/tags/constants.ts
@@ -24,6 +24,7 @@ export const TAG_TYPES = [
"field-values",
"glossary",
"indexed-entity",
+ "llm-models",
"logger-preset",
"measure",
"model-index",
diff --git a/frontend/src/metabase/metabot/components/MetabotAdmin/MetabotAdminPage.tsx b/frontend/src/metabase/metabot/components/MetabotAdmin/MetabotAdminPage.tsx
new file mode 100644
index 00000000000..6c2122350d3
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotAdmin/MetabotAdminPage.tsx
@@ -0,0 +1,49 @@
+import { IndexRoute, Redirect } from "react-router";
+import { t } from "ttag";
+
+import ErrorBoundary from "metabase/ErrorBoundary";
+import {
+ AdminNavItem,
+ AdminNavWrapper,
+} from "metabase/admin/components/AdminNav";
+import { AdminSettingsLayout } from "metabase/common/components/AdminLayout/AdminSettingsLayout";
+import { PLUGIN_METABOT } from "metabase/plugins";
+import { Flex } from "metabase/ui";
+
+import { MetabotSQLGenerationSettingsSection } from "./MetabotSQLGenerationSettingsSection";
+
+export function getAdminRoutes() {
+ // NOTE: weird cypress bundle error happens if this `getAdminRoutes` fn
+ // is imported into our ai plugin file as the default OSS implementation.
+ return (
+ PLUGIN_METABOT.getAdminRoutes?.() ?? [
+ <IndexRoute key="index" component={MetabotAdminPage} />,
+ <Redirect key="redirect" from="*" to="/admin/metabot" />,
+ ]
+ );
+}
+
+function MetabotAdminPage() {
+ return (
+ <AdminSettingsLayout sidebar={<MetabotNavPane />}>
+ <ErrorBoundary>
+ <MetabotSQLGenerationSettingsSection />
+ </ErrorBoundary>
+ </AdminSettingsLayout>
+ );
+}
+
+function MetabotNavPane() {
+ return (
+ <Flex direction="column" flex="0 0 auto">
+ <AdminNavWrapper>
+ <AdminNavItem
+ key="sql"
+ icon="sql"
+ label={t`SQL Generation`}
+ path="/admin/metabot"
+ />
+ </AdminNavWrapper>
+ </Flex>
+ );
+}
diff --git a/frontend/src/metabase/metabot/components/MetabotAdmin/MetabotSQLGenerationSettingsSection.tsx b/frontend/src/metabase/metabot/components/MetabotAdmin/MetabotSQLGenerationSettingsSection.tsx
new file mode 100644
index 00000000000..daa9184dac4
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotAdmin/MetabotSQLGenerationSettingsSection.tsx
@@ -0,0 +1,151 @@
+import { type FocusEvent, useMemo, useState } from "react";
+import { t } from "ttag";
+
+import {
+ SettingsPageWrapper,
+ SettingsSection,
+} from "metabase/admin/components/SettingsSection";
+import { SetByEnvVar } from "metabase/admin/settings/components/widgets/AdminSettingInput";
+import { useListModelsQuery } from "metabase/api/llm";
+import { getErrorMessage } from "metabase/api/utils/errors";
+import { useAdminSetting } from "metabase/api/utils/settings";
+import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
+import { useToast } from "metabase/common/hooks";
+import { Box, PasswordInput, Select, Stack, TextInput } from "metabase/ui";
+
+export function MetabotSQLGenerationSettingsSection() {
+ const apiKey = useAdminSetting("llm-anthropic-api-key");
+ const model = useAdminSetting("llm-anthropic-model");
+ const { updateSetting, settingDetails: apiKeyDetails } = apiKey;
+ const { settingDetails: modelDetails } = model;
+
+ const [localApiKey, setLocalApiKey] = useState<string | null>(null);
+ const [apiKeyError, setApiKeyError] = useState<string | null>(null);
+ const [isApiKeyVisible, setIsApiKeyVisible] = useState(false);
+ const [sendToast] = useToast();
+
+ const isApiKeyEnvVar = !!(
+ apiKeyDetails?.is_env_setting && apiKeyDetails?.env_name
+ );
+ const isModelEnvVar = !!(
+ modelDetails?.is_env_setting && modelDetails?.env_name
+ );
+
+ const savedApiKey = apiKey.value ?? "";
+ const hasApiKey = savedApiKey.trim().length > 0;
+ const apiKeyDisplayValue = localApiKey ?? savedApiKey;
+ const ApiKeyInput = isApiKeyEnvVar ? TextInput : PasswordInput;
+
+ const {
+ data: modelsData,
+ isLoading: isModelsLoading,
+ error: modelsError,
+ } = useListModelsQuery(undefined, { skip: !hasApiKey });
+
+ const modelOptions = useMemo(() => {
+ return (modelsData?.models || []).map((m) => ({
+ value: m.id,
+ label: m.display_name,
+ }));
+ }, [modelsData]);
+
+ const isSettingsLoading = apiKey.isLoading || model.isLoading;
+ const settingsError = apiKey.error || model.error;
+
+ if (isSettingsLoading || settingsError) {
+ return (
+ <LoadingAndErrorWrapper
+ loading={isSettingsLoading}
+ error={settingsError}
+ />
+ );
+ }
+
+ const modelValue = model.value ?? "";
+ const isModelFieldDisabled = !hasApiKey || isModelsLoading || !!modelsError;
+ const isDeprecatedModel =
+ modelOptions.length > 0 &&
+ !modelOptions.some((opt) => opt.value === modelValue);
+
+ const handleApiKeyBlur = async (e: FocusEvent<HTMLInputElement>) => {
+ const value = e.target.value;
+ if (value !== savedApiKey) {
+ setApiKeyError(null);
+ const response = await updateSetting({
+ key: "llm-anthropic-api-key",
+ value,
+ toast: false,
+ });
+ if (response.error) {
+ setApiKeyError(
+ getErrorMessage(response.error, t`Failed to save API key`),
+ );
+ return;
+ }
+ sendToast({ message: t`API key saved successfully!`, icon: "check" });
+ setIsApiKeyVisible(false);
+ }
+ setLocalApiKey(null);
+ };
+
+ const handleModelChange = (value: string | null) => {
+ if (value && value !== model.value) {
+ updateSetting({ key: "llm-anthropic-model", value });
+ }
+ };
+
+ return (
+ <SettingsPageWrapper>
+ <SettingsSection
+ title={t`Connect to a model`}
+ description={t`Use your Anthropic API key and specify which language model to use for AI features.`}
+ >
+ <Stack gap="md">
+ <Box>
+ <ApiKeyInput
+ disabled={isApiKeyEnvVar}
+ label={t`Anthropic API Key`}
+ placeholder={t`Enter your API key`}
+ value={apiKeyDisplayValue}
+ onChange={(e) => {
+ setLocalApiKey(e.target.value);
+ setApiKeyError(null);
+ setIsApiKeyVisible(true);
+ }}
+ onBlur={handleApiKeyBlur}
+ error={apiKeyError}
+ {...(isApiKeyEnvVar
+ ? {}
+ : {
+ visible: isApiKeyVisible,
+ onVisibilityChange: setIsApiKeyVisible,
+ })}
+ />
+ {isApiKeyEnvVar && (
+ <SetByEnvVar varName={apiKeyDetails.env_name!} />
+ )}
+ </Box>
+
+ <Box>
+ <Select
+ disabled={isModelFieldDisabled || !!isModelEnvVar}
+ label={t`Model`}
+ placeholder={isModelsLoading ? t`Loading models...` : undefined}
+ data={modelOptions}
+ value={isDeprecatedModel ? null : modelValue}
+ onChange={handleModelChange}
+ error={
+ modelsError
+ ? t`Failed to load models`
+ : isDeprecatedModel
+ ? t`The model "${modelValue}" is no longer available. Please select a new model.`
+ : undefined
+ }
+ />
+ {isModelEnvVar && <SetByEnvVar varName={modelDetails.env_name!} />}
+ </Box>
+ </Stack>
+ </SettingsSection>
+ </SettingsPageWrapper>
+ );
+}
diff --git a/frontend/src/metabase/metabot/components/MetabotAdmin/tests/MetabotSQLGenerationSettingsSection.setup.tsx b/frontend/src/metabase/metabot/components/MetabotAdmin/tests/MetabotSQLGenerationSettingsSection.setup.tsx
new file mode 100644
index 00000000000..427fc61a295
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotAdmin/tests/MetabotSQLGenerationSettingsSection.setup.tsx
@@ -0,0 +1,67 @@
+import fetchMock from "fetch-mock";
+
+import {
+ setupLlmListModelsEndpoint,
+ setupPropertiesEndpoints,
+ setupSettingsEndpoints,
+ setupUpdateSettingEndpoint,
+} from "__support__/server-mocks";
+import { renderWithProviders } from "__support__/ui";
+import type { LLMModel } from "metabase-types/api";
+import {
+ createMockSettingDefinition,
+ createMockSettings,
+} from "metabase-types/api/mocks";
+
+import { MetabotSQLGenerationSettingsSection } from "../MetabotSQLGenerationSettingsSection";
+
+export interface SetupOptions {
+ apiKey?: string | null;
+ model?: string;
+ isApiKeyEnvVar?: boolean;
+ isModelEnvVar?: boolean;
+ models?: LLMModel[];
+ modelsError?: boolean;
+ settingsError?: boolean;
+}
+
+export async function setup({
+ apiKey = null,
+ model = "",
+ isApiKeyEnvVar = false,
+ isModelEnvVar = false,
+ models = [],
+ modelsError = false,
+ settingsError = false,
+}: SetupOptions = {}) {
+ if (settingsError) {
+ fetchMock.get("path:/api/session/properties", 500);
+ fetchMock.get("path:/api/setting", 500);
+ } else {
+ setupPropertiesEndpoints(
+ createMockSettings({
+ "llm-anthropic-api-key": apiKey,
+ "llm-anthropic-model": model,
+ }),
+ );
+ setupSettingsEndpoints([
+ createMockSettingDefinition({
+ key: "llm-anthropic-api-key",
+ value: apiKey,
+ is_env_setting: isApiKeyEnvVar,
+ env_name: isApiKeyEnvVar ? "MB_LLM_ANTHROPIC_API_KEY" : "",
+ }),
+ createMockSettingDefinition({
+ key: "llm-anthropic-model",
+ value: model,
+ is_env_setting: isModelEnvVar,
+ env_name: isModelEnvVar ? "MB_LLM_ANTHROPIC_MODEL" : "",
+ }),
+ ]);
+ }
+
+ setupUpdateSettingEndpoint();
+ setupLlmListModelsEndpoint(models, modelsError);
+
+ renderWithProviders(<MetabotSQLGenerationSettingsSection />);
+}
diff --git a/frontend/src/metabase/metabot/components/MetabotAdmin/tests/MetabotSQLGenerationSettingsSection.unit.spec.tsx b/frontend/src/metabase/metabot/components/MetabotAdmin/tests/MetabotSQLGenerationSettingsSection.unit.spec.tsx
new file mode 100644
index 00000000000..9fd4ed9c07d
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotAdmin/tests/MetabotSQLGenerationSettingsSection.unit.spec.tsx
@@ -0,0 +1,132 @@
+import userEvent from "@testing-library/user-event";
+import fetchMock from "fetch-mock";
+
+import { screen, waitFor } from "__support__/ui";
+
+import { setup } from "./MetabotSQLGenerationSettingsSection.setup";
+
+const apiKeyInput = () => screen.findByLabelText(/Anthropic API Key/i);
+const modelSelect = () => screen.findByLabelText(/Model/i);
+
+function getSettingPutCalls(settingKey?: string) {
+ return fetchMock.callHistory
+ .calls()
+ .filter(
+ (call) =>
+ call.request?.method === "PUT" &&
+ call.request?.url?.includes(settingKey ?? "/api/setting/"),
+ );
+}
+
+const DEFAULT_MODELS = [
+ { id: "claude-opus-4-5-20251101", display_name: "Claude Opus 4.5" },
+ { id: "claude-sonnet-4-20250514", display_name: "Claude Sonnet 4" },
+ { id: "claude-haiku-4-20250414", display_name: "Claude Haiku 4" },
+];
+
+describe("MetabotSQLGenerationSettingsSection", () => {
+ describe("initial rendering", () => {
+ it("should render API key and model fields", async () => {
+ await setup({ models: DEFAULT_MODELS });
+ expect(await apiKeyInput()).toBeInTheDocument();
+ expect(await modelSelect()).toBeInTheDocument();
+ });
+
+ it("should show error state when settings fail to load", async () => {
+ await setup({ settingsError: true });
+ expect(await screen.findByText(/error/i)).toBeInTheDocument();
+ });
+ });
+
+ describe("API key field", () => {
+ it("should display saved API key value", async () => {
+ await setup({ apiKey: "sk-ant-test-key-12345" });
+ expect(await apiKeyInput()).toHaveValue("sk-ant-test-key-12345");
+ });
+
+ it("should save API key on blur when value has changed", async () => {
+ await setup({ apiKey: "" });
+
+ await userEvent.type(await apiKeyInput(), "sk-ant-new-key");
+ await userEvent.tab();
+
+ await waitFor(() => {
+ expect(getSettingPutCalls().length).toBeGreaterThan(0);
+ });
+
+ expect(getSettingPutCalls().at(-1)?.request?.url).toContain(
+ "llm-anthropic-api-key",
+ );
+ });
+
+ it("should not save API key on blur when value is unchanged", async () => {
+ await setup({ apiKey: "existing-key" });
+
+ await userEvent.click(await apiKeyInput());
+ await userEvent.tab();
+
+ await waitFor(() => {
+ expect(getSettingPutCalls("llm-anthropic-api-key").length).toBe(0);
+ });
+ });
+
+ it("should show env var indicator and disable input when API key is set via environment variable", async () => {
+ await setup({ apiKey: "env-api-key", isApiKeyEnvVar: true });
+
+ expect(
+ await screen.findByText(/MB_LLM_ANTHROPIC_API_KEY/),
+ ).toBeInTheDocument();
+ expect(await apiKeyInput()).toBeDisabled();
+ });
+ });
+
+ describe("model select field", () => {
+ it("should be disabled when no API key is configured", async () => {
+ await setup({ apiKey: null, models: DEFAULT_MODELS });
+ expect(await modelSelect()).toBeDisabled();
+ });
+
+ it("should fetch and display available models when API key exists", async () => {
+ await setup({ apiKey: "sk-ant-key", models: DEFAULT_MODELS });
+
+ await waitFor(async () => expect(await modelSelect()).toBeEnabled());
+ await userEvent.click(await modelSelect());
+
+ expect(await screen.findByText("Claude Opus 4.5")).toBeInTheDocument();
+ expect(await screen.findByText("Claude Sonnet 4")).toBeInTheDocument();
+ expect(await screen.findByText("Claude Haiku 4")).toBeInTheDocument();
+ });
+
+ it("should show error message when model fetch fails", async () => {
+ await setup({ apiKey: "sk-ant-key", modelsError: true });
+ expect(
+ await screen.findByText(/Failed to load models/i),
+ ).toBeInTheDocument();
+ });
+
+ it("should show deprecation warning when saved model is not in available list", async () => {
+ await setup({
+ apiKey: "sk-ant-key",
+ model: "claude-2-deprecated",
+ models: DEFAULT_MODELS,
+ });
+ expect(
+ await screen.findByText(/no longer available/i),
+ ).toBeInTheDocument();
+ });
+
+ it("should show env var indicator and disable select when model is set via environment variable", async () => {
+ await setup({
+ apiKey: "sk-ant-key",
+ model: "claude-sonnet-4-20250514",
+ isModelEnvVar: true,
+ models: DEFAULT_MODELS,
+ });
+
+ expect(
+ await screen.findByText(/MB_LLM_ANTHROPIC_MODEL/),
+ ).toBeInTheDocument();
+ expect(await modelSelect()).toBeDisabled();
+ });
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.module.css b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.module.css
similarity index 61%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.module.css
rename to frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.module.css
index e2eb177c63c..acadb5c3e56 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.module.css
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.module.css
@@ -3,10 +3,11 @@
}
.container {
+ max-width: 55rem;
display: flex;
flex-direction: column;
gap: 0.25rem;
- padding: 0.5rem;
+ padding: 0.5rem 0.75rem 0.75rem;
margin-bottom: 0.125rem;
background: var(--mb-color-background-primary);
border: 1px solid var(--mb-color-border);
@@ -14,6 +15,12 @@
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}
+.tableBar {
+ border-bottom: 1px dashed var(--mb-color-border);
+ margin: 0 -0.75rem;
+ padding: 0 0.75rem 0.5rem;
+}
+
.inputContainer {
min-height: 1.5rem;
}
@@ -24,3 +31,12 @@
min-height: 1.25rem;
padding: 0.25rem 0.125rem;
}
+
+.submitButton:disabled {
+ background-color: var(--mb-color-background);
+ color: var(--mb-color-text-primary);
+}
+
+.cancelButton {
+ order: -1;
+}
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.tsx
new file mode 100644
index 00000000000..4539b661fdf
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPrompt.tsx
@@ -0,0 +1,162 @@
+import { useCallback, useEffect, useRef } from "react";
+import { tinykeys } from "tinykeys";
+import { t } from "ttag";
+
+import type { MetabotPromptInputRef } from "metabase/metabot";
+import { MetabotPromptInput } from "metabase/metabot/components/MetabotPromptInput";
+import type { SuggestionModel } from "metabase/rich_text_editing/tiptap/extensions/shared/types";
+import { Box, Button, Flex, Icon, Loader, Tooltip } from "metabase/ui";
+import type { DatabaseId, ReferencedEntityId } from "metabase-types/api";
+
+import S from "./MetabotInlineSQLPrompt.module.css";
+import { type SelectedTable, TablePillsInput } from "./TablePillsInput";
+
+interface MetabotInlineSQLPromptProps {
+ isTableBarEnabled: boolean;
+ databaseId: DatabaseId | null;
+ onClose: () => void;
+ isLoading: boolean;
+ error: string | undefined;
+ generate: (options: {
+ prompt: string;
+ sourceSql?: string;
+ referencedEntities?: ReferencedEntityId[];
+ }) => Promise<void>;
+ cancelRequest: () => void;
+ suggestionModels: SuggestionModel[];
+ getSourceSql?: () => string;
+ value: string;
+ onValueChange: (value: string) => void;
+ selectedTables: SelectedTable[];
+ onSelectedTablesChange: (tables: SelectedTable[]) => void;
+}
+
+export const MetabotInlineSQLPrompt = ({
+ isTableBarEnabled,
+ databaseId,
+ onClose,
+ isLoading,
+ error,
+ generate,
+ cancelRequest,
+ suggestionModels,
+ getSourceSql,
+ value,
+ onValueChange,
+ selectedTables,
+ onSelectedTablesChange,
+}: MetabotInlineSQLPromptProps) => {
+ const promptInputRef = useRef<MetabotPromptInputRef>(null);
+
+ const isSubmitDisabled = !value.trim() || isLoading;
+
+ const handleSubmit = useCallback(async () => {
+ const prompt = promptInputRef.current?.getValue?.().trim() ?? "";
+ const sourceSql = getSourceSql?.();
+ const referencedEntities =
+ selectedTables.map((table) => ({
+ model: "table" as const,
+ id: table.id,
+ })) ?? [];
+ generate({ prompt, sourceSql, referencedEntities });
+ }, [generate, getSourceSql, selectedTables]);
+
+ const handleClose = useCallback(() => {
+ cancelRequest();
+ onClose();
+ }, [cancelRequest, onClose]);
+
+ useEffect(() => {
+ return tinykeys(
+ window,
+ {
+ "$mod+Enter": (e) => {
+ if (!isSubmitDisabled) {
+ e.preventDefault();
+ handleSubmit();
+ }
+ },
+ "$mod+Shift+i": (e) => {
+ e.preventDefault();
+ handleClose();
+ },
+ },
+ { capture: true },
+ );
+ }, [isSubmitDisabled, handleSubmit, handleClose]);
+
+ return (
+ <Box className={S.container} data-testid="metabot-inline-sql-prompt">
+ {isTableBarEnabled && (
+ <Box className={S.tableBar}>
+ <TablePillsInput
+ disabled={isLoading}
+ databaseId={databaseId}
+ selectedTables={selectedTables}
+ onChange={onSelectedTablesChange}
+ onEnterPress={() => promptInputRef.current?.focus()}
+ autoFocus={isTableBarEnabled}
+ />
+ </Box>
+ )}
+ <Box className={S.inputContainer}>
+ <MetabotPromptInput
+ ref={promptInputRef}
+ value={value}
+ placeholder={
+ isTableBarEnabled
+ ? t`Then, ask for what you'd like to see.`
+ : t`Describe what SQL you want...`
+ }
+ autoFocus={!isTableBarEnabled}
+ disabled={isLoading}
+ onChange={onValueChange}
+ onStop={handleClose}
+ suggestionConfig={{
+ suggestionModels,
+ searchOptions: databaseId ? { table_db_id: databaseId } : undefined,
+ }}
+ />
+ </Box>
+ <Flex justify="space-between" align="center" gap="sm" mt="xs">
+ <Box data-testid="metabot-inline-sql-error" w="100%" fz="sm" c="error">
+ {error}
+ </Box>
+ <Flex gap="xs" flex="1 0 auto">
+ <Tooltip disabled={isLoading} label={t`Send to Metabot`}>
+ <Button
+ className={S.submitButton}
+ data-testid="metabot-inline-sql-generate"
+ size="xs"
+ variant="filled"
+ px="0"
+ w="1.875rem"
+ styles={{ label: { display: "flex" } }}
+ onClick={handleSubmit}
+ disabled={isSubmitDisabled}
+ >
+ {isLoading ? (
+ <Loader
+ size="xs"
+ color="text-tertiary"
+ data-testid="metabot-inline-sql-generating"
+ />
+ ) : (
+ <Icon name="send" />
+ )}
+ </Button>
+ </Tooltip>
+ <Button
+ className={S.cancelButton}
+ data-testid="metabot-inline-sql-cancel"
+ size="xs"
+ variant="subtle"
+ onClick={handleClose}
+ >
+ {t`Cancel`}
+ </Button>
+ </Flex>
+ </Flex>
+ </Box>
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPromptWidget.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPromptWidget.tsx
similarity index 100%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPromptWidget.tsx
rename to frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/MetabotInlineSQLPromptWidget.tsx
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/TablePillsInput.module.css b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/TablePillsInput.module.css
new file mode 100644
index 00000000000..a21fba76c78
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/TablePillsInput.module.css
@@ -0,0 +1,21 @@
+.root :global(.mb-mantine-PillsInput-input) {
+ border: none;
+ min-height: 1.25rem;
+ font-size: 0.75rem;
+ padding: 0;
+}
+
+.root [data-combobox-option] {
+ padding: 0.25rem 0.5rem;
+
+ &:hover {
+ background: var(--mb-color-background-brand);
+ color: var(--mb-color-text-primary);
+ }
+
+ &[data-combobox-selected],
+ &[data-combobox-selected]:hover {
+ background-color: var(--mb-color-background-hover);
+ color: var(--mb-color-text-primary);
+ }
+}
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/TablePillsInput.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/TablePillsInput.tsx
new file mode 100644
index 00000000000..b697c7be063
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/TablePillsInput.tsx
@@ -0,0 +1,252 @@
+import { useCallback, useEffect, useMemo, useState } from "react";
+import { match } from "ts-pattern";
+import { t } from "ttag";
+
+import { skipToken, useSearchQuery } from "metabase/api";
+import { useDebouncedValue } from "metabase/common/hooks/use-debounced-value";
+import { SEARCH_DEBOUNCE_DURATION } from "metabase/lib/constants";
+import {
+ Box,
+ Combobox,
+ Flex,
+ Group,
+ Icon,
+ Pill,
+ PillsInput,
+ useCombobox,
+} from "metabase/ui";
+import type { DatabaseId, TableId } from "metabase-types/api";
+
+import S from "./TablePillsInput.module.css";
+
+export interface SelectedTable {
+ id: TableId;
+ name: string;
+ display_name?: string | null;
+}
+
+interface TablePillsInputProps {
+ databaseId: DatabaseId | null;
+ selectedTables: SelectedTable[];
+ onChange: (tables: SelectedTable[]) => void;
+ onEnterPress: () => void;
+ disabled: boolean;
+ autoFocus?: boolean;
+}
+
+export function TablePillsInput({
+ databaseId,
+ selectedTables,
+ onChange,
+ onEnterPress,
+ disabled,
+ autoFocus = false,
+}: TablePillsInputProps) {
+ const [search, setSearch] = useState("");
+ const [focusedTableId, setFocusedTableId] = useState<TableId | undefined>(
+ undefined,
+ );
+ const debouncedSearch = useDebouncedValue(search, SEARCH_DEBOUNCE_DURATION);
+
+ const handleRemoveTable = useCallback(
+ (tableId: TableId) => {
+ onChange(selectedTables.filter((t) => t.id !== tableId));
+ },
+ [onChange, selectedTables],
+ );
+
+ const {
+ data: searchData,
+ isLoading,
+ error,
+ } = useSearchQuery(
+ databaseId
+ ? {
+ q: debouncedSearch.trim() || undefined,
+ models: ["table"],
+ table_db_id: databaseId,
+ limit: 20,
+ }
+ : skipToken,
+ );
+ const tables = useMemo(() => searchData?.data ?? [], [searchData]);
+ const tableOptions = useMemo(() => {
+ const selectedIds = new Set(selectedTables.map((t) => t.id));
+ return tables.filter(
+ (table) => table.id != null && !selectedIds.has(table.id as TableId),
+ );
+ }, [tables, selectedTables]);
+
+ const combobox = useCombobox({
+ onDropdownClose: () => combobox.resetSelectedOption(),
+ onDropdownOpen: () => {
+ setFocusedTableId(undefined);
+ combobox.selectFirstOption();
+ },
+ });
+
+ useEffect(() => {
+ if (combobox.dropdownOpened && tableOptions.length > 0) {
+ combobox.selectFirstOption();
+ }
+ }, [tableOptions, combobox]);
+
+ useEffect(() => {
+ if (disabled) {
+ combobox.closeDropdown();
+ setFocusedTableId(undefined);
+ }
+ }, [disabled, combobox]);
+
+ const handleValueSelect = (tableIdStr: string) => {
+ const tableId = Number(tableIdStr) as TableId;
+ const table = tables.find((t) => t.id === tableId);
+ if (table) {
+ onChange([...selectedTables, table]);
+ }
+ combobox.closeDropdown();
+ setSearch("");
+ };
+
+ const handleFieldChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+ if (!combobox.dropdownOpened) {
+ combobox.openDropdown();
+ }
+ setFocusedTableId(undefined);
+ setSearch(e.currentTarget.value);
+ };
+
+ const handlePillClick = (tableId: TableId) => {
+ if (!disabled) {
+ setFocusedTableId((prev) => (prev === tableId ? undefined : tableId));
+ combobox.closeDropdown();
+ }
+ };
+
+ const emptyMessage = match({
+ showLoading: isLoading || debouncedSearch !== search,
+ hasError: !!error,
+ hasOptions: tableOptions.length > 0,
+ })
+ .with({ showLoading: true }, () => t`Loading tables...`)
+ .with({ hasError: true }, () => t`Error loading tables`)
+ .with({ hasOptions: false }, () => t`No tables found`)
+ .otherwise(() => undefined);
+
+ return (
+ <Flex className={S.root} align="center" gap="sm">
+ <Icon name="table" c="text-tertiary" size={14} />
+ <Box flex="1">
+ <Combobox
+ store={combobox}
+ onOptionSubmit={handleValueSelect}
+ withinPortal={false}
+ width={300}
+ position="bottom-start"
+ >
+ <PillsInput data-testid="metabot-table-input" fz="0.75rem">
+ <Pill.Group style={{ gap: "0.25rem" }}>
+ {selectedTables.map((table) => (
+ <Pill
+ key={table.id}
+ fz="xs"
+ bg={
+ focusedTableId === table.id
+ ? "background-brand"
+ : "background-secondary"
+ }
+ c="text-primary"
+ bd={
+ focusedTableId === table.id
+ ? "1px solid var(--mb-color-brand)"
+ : "1px solid var(--mb-color-border)"
+ }
+ onClick={() => handlePillClick(table.id)}
+ style={{ cursor: "pointer" }}
+ >
+ {table.display_name || table.name || String(table.id)}
+ </Pill>
+ ))}
+ <Combobox.DropdownTarget>
+ <span style={{ display: "inline-flex", flex: 1, minWidth: 0 }}>
+ <Combobox.EventsTarget>
+ <PillsInput.Field
+ disabled={disabled}
+ value={search}
+ placeholder={t`First, tell Metabot which tables to use`}
+ fz="0.75rem"
+ onClick={() => {
+ setFocusedTableId(undefined);
+ if (search.length > 0) {
+ combobox.openDropdown();
+ }
+ }}
+ onBlur={() => combobox.closeDropdown()}
+ onChange={handleFieldChange}
+ onKeyDown={(e) => {
+ if (e.key === "Enter" && !combobox.dropdownOpened) {
+ e.preventDefault();
+ return onEnterPress();
+ }
+
+ const isDelete = e.key === "Backspace";
+ const isInputEmpty =
+ search.length === 0 && selectedTables.length > 0;
+ if (!isDelete || !isInputEmpty) {
+ return;
+ }
+
+ e.preventDefault();
+
+ const tableId =
+ focusedTableId ?? selectedTables.at(-1)?.id;
+ if (tableId) {
+ handleRemoveTable(tableId);
+ if (focusedTableId) {
+ const currIndex = selectedTables.findIndex(
+ (t) => t.id === tableId,
+ );
+ const nextIndex = currIndex > 0 ? currIndex - 1 : 1;
+ const nextTable = selectedTables[nextIndex];
+ setFocusedTableId(nextTable?.id ?? null);
+ }
+ }
+ }}
+ autoFocus={autoFocus}
+ aria-label={t`Select tables`}
+ />
+ </Combobox.EventsTarget>
+ </span>
+ </Combobox.DropdownTarget>
+ </Pill.Group>
+ </PillsInput>
+
+ <Combobox.Dropdown key={selectedTables.map((t) => t.id).join("-")}>
+ <Combobox.Options>
+ {emptyMessage ? (
+ <Combobox.Empty fz="sm" py="md">
+ {emptyMessage}
+ </Combobox.Empty>
+ ) : (
+ tableOptions.map((table) => (
+ <Combobox.Option value={String(table.id)} key={table.id}>
+ <Group gap="sm">
+ <Box component="span" fz="xs">
+ {table.name}
+ {table.table_schema && (
+ <Box component="span" c="text-tertiary" ml="sm">
+ {table.table_schema}
+ </Box>
+ )}
+ </Box>
+ </Group>
+ </Combobox.Option>
+ ))
+ )}
+ </Combobox.Options>
+ </Combobox.Dropdown>
+ </Combobox>
+ </Box>
+ </Flex>
+ );
+}
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/index.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/index.tsx
new file mode 100644
index 00000000000..41dfa278dc6
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/index.tsx
@@ -0,0 +1 @@
+export * from "./useInlineSQLPrompt";
diff --git a/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
new file mode 100644
index 00000000000..6160da96f89
--- /dev/null
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/useInlineSQLPrompt.tsx
@@ -0,0 +1,300 @@
+import type { EditorView } from "@codemirror/view";
+import { EditorView as EV, keymap } from "@codemirror/view";
+import type { Extension } from "@uiw/react-codemirror";
+import {
+ useCallback,
+ useEffect,
+ useLayoutEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
+import { createPortal } from "react-dom";
+
+import { skipToken, useExtractTablesQuery } from "metabase/api";
+import { useHasTokenFeature, useSetting } from "metabase/common/hooks";
+import { useDebouncedValue } from "metabase/common/hooks/use-debounced-value";
+import { useRegisterMetabotContextProvider } from "metabase/metabot/context";
+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 { MetabotInlineSQLPrompt } from "./MetabotInlineSQLPrompt";
+import {
+ type PortalTarget,
+ createPromptInputExtension,
+ hideEffect,
+ toggleEffect,
+} from "./MetabotInlineSQLPromptWidget";
+import type { SelectedTable } from "./TablePillsInput";
+import { extractMetabotBufferContext } from "./utils";
+
+function useRegisterCodeEditorMetabotContext(
+ buffer: EditorView | undefined,
+ databaseId: DatabaseId | null,
+ bufferId: string,
+): void {
+ useRegisterMetabotContextProvider(
+ async () =>
+ buffer
+ ? {
+ user_is_viewing: [
+ {
+ type: "code_editor",
+ buffers: [
+ extractMetabotBufferContext(buffer, databaseId, bufferId),
+ ],
+ },
+ ],
+ }
+ : {},
+ [buffer],
+ );
+}
+
+export interface UseInlineSqlEditResult {
+ portalElement: React.ReactPortal | null;
+ extensions: Extension[];
+ proposedQuestion: Question | undefined;
+ handleAcceptProposed?: (datasetQuery: DatasetQuery) => void;
+ handleRejectProposed?: () => void;
+}
+
+export function useInlineSQLPrompt(
+ question: Question,
+ bufferId: string,
+): UseInlineSqlEditResult {
+ const isTableBarEnabled = !useHasTokenFeature("metabot_v3");
+ const llmSqlGenerationEnabled = useSetting("llm-sql-generation-enabled");
+
+ const databaseId = question.databaseId();
+
+ const [hasEverBeenOpened, setHasEverBeenOpened] = useState(false);
+
+ const [portalTarget, setPortalTarget] = useState<PortalTarget | null>(null);
+ const [promptValue, setPromptValue] = useState("");
+ const [selectedTables, setSelectedTables] = useState<SelectedTable[]>([]);
+ const [editorSql, setEditorSql] = useState(() => {
+ const query = question.query();
+ return Lib.queryDisplayInfo(query).isNative
+ ? Lib.rawNativeQuery(query)
+ : "";
+ });
+
+ const debouncedEditorSql = useDebouncedValue(editorSql.trim(), 1000);
+ const tableExtractionEnabled = hasEverBeenOpened && isTableBarEnabled;
+ const { data: extractedTablesData } = useExtractTablesQuery(
+ databaseId && debouncedEditorSql && tableExtractionEnabled
+ ? { database_id: databaseId, sql: debouncedEditorSql }
+ : skipToken,
+ );
+
+ useRegisterCodeEditorMetabotContext(
+ portalTarget?.view,
+ question.databaseId(),
+ bufferId,
+ );
+
+ const sourceSqlTables = useMemo(
+ () => extractedTablesData?.tables ?? [],
+ [extractedTablesData],
+ );
+
+ // Merge new tables from extracted data into selected tables
+ useEffect(() => {
+ if (!sourceSqlTables.length) {
+ return;
+ }
+ setSelectedTables((prev) => {
+ const selectedIds = new Set(prev.map((t) => t.id));
+ const newTables = sourceSqlTables.filter((t) => !selectedIds.has(t.id));
+ return newTables.length > 0 ? [...prev, ...newTables] : prev;
+ });
+ }, [sourceSqlTables]);
+
+ const {
+ source: generatedSource,
+ isLoading,
+ error,
+ generate,
+ cancelRequest,
+ clear: clearSuggestion,
+ reset: resetSuggestionState,
+ reject,
+ suggestionModels,
+ } = PLUGIN_METABOT.useMetabotSQLSuggestion({
+ databaseId,
+ bufferId,
+ onGenerated: (res) => {
+ if (res) {
+ setSelectedTables(
+ res.referenced_entities as unknown as SelectedTable[],
+ );
+ }
+ setPromptValue("");
+ },
+ });
+
+ const getSourceSql = useCallback(() => {
+ return portalTarget?.view.state.doc.toString() ?? "";
+ }, [portalTarget?.view]);
+
+ const prevDatabaseIdRef = useRef(databaseId);
+ useEffect(() => {
+ if (prevDatabaseIdRef.current !== databaseId) {
+ setPromptValue("");
+ setSelectedTables([]);
+ prevDatabaseIdRef.current = databaseId;
+ }
+ }, [databaseId]);
+
+ const generatedSqlRef = useRef(generatedSource);
+ generatedSqlRef.current = generatedSource;
+
+ const clearSuggestionRef = useRef(clearSuggestion);
+ useEffect(() => {
+ clearSuggestionRef.current = clearSuggestion;
+ }, [clearSuggestion]);
+
+ const hideInput = useCallback(() => {
+ portalTarget?.view.dispatch({ effects: hideEffect.of() });
+ portalTarget?.view.focus();
+ }, [portalTarget?.view]);
+
+ const hideInputRef = useRef(hideInput);
+ useLayoutEffect(() => {
+ hideInputRef.current = hideInput;
+ });
+
+ useEffect(
+ function autoCloseOnResult() {
+ if (generatedSource) {
+ hideInput();
+ }
+ },
+ [generatedSource, hideInput],
+ );
+
+ useEffect(
+ function resetOnDbChangeAndUnmount() {
+ return () => {
+ hideInputRef.current();
+ resetSuggestionState();
+ };
+ },
+ [resetSuggestionState, databaseId],
+ );
+
+ const resetInput = useCallback(() => {
+ clearSuggestion();
+ hideInput();
+ }, [clearSuggestion, hideInput]);
+
+ const handleClose = () => {
+ hideInput();
+ setSelectedTables(sourceSqlTables);
+ };
+
+ // NOTE: ref is needed for the extension to not be recalculated in the useMemo
+ // below, while still being able to reset the suggestion state on close
+ const resetInputRef = useRef(resetInput);
+ useEffect(() => {
+ resetInputRef.current = resetInput;
+ }, [resetInput]);
+
+ const proposedQuestion = useMemo(
+ () =>
+ generatedSource
+ ? question.setQuery(
+ Lib.withNativeQuery(question.query(), generatedSource),
+ )
+ : undefined,
+ [generatedSource, question],
+ );
+
+ const handleRejectProposed = generatedSource
+ ? () => {
+ resetInput();
+ reject();
+ }
+ : undefined;
+
+ const handleAcceptProposed = generatedSource ? resetInput : undefined;
+
+ const extensions = useMemo(
+ () =>
+ llmSqlGenerationEnabled
+ ? [
+ createPromptInputExtension(setPortalTarget),
+ keymap.of([
+ {
+ key: `Mod-Shift-i`,
+ run: (view) => {
+ if (generatedSqlRef.current) {
+ resetInputRef.current();
+ }
+ setHasEverBeenOpened(true);
+ view.dispatch({ effects: toggleEffect.of({ view }) });
+ return true;
+ },
+ },
+ ]),
+ EV.updateListener.of((update) => {
+ if (!update.docChanged || !generatedSqlRef.current) {
+ return;
+ }
+ // Only clear suggestion if change was from user input, not programmatic
+ const isUserEdit = update.transactions.some(
+ (tr) =>
+ tr.isUserEvent("input") ||
+ tr.isUserEvent("delete") ||
+ tr.isUserEvent("move"),
+ );
+ if (isUserEdit) {
+ clearSuggestionRef.current();
+ }
+ }),
+ // Track SQL changes for prefetching table extraction
+ EV.updateListener.of((update) => {
+ if (update.docChanged) {
+ setEditorSql(update.state.doc.toString());
+ }
+ }),
+ ]
+ : [],
+ [llmSqlGenerationEnabled],
+ );
+
+ return {
+ extensions,
+ portalElement:
+ llmSqlGenerationEnabled && portalTarget
+ ? createPortal(
+ <MetabotInlineSQLPrompt
+ databaseId={databaseId}
+ onClose={handleClose}
+ isLoading={isLoading}
+ error={error}
+ generate={generate}
+ cancelRequest={cancelRequest}
+ suggestionModels={suggestionModels}
+ getSourceSql={getSourceSql}
+ value={promptValue}
+ onValueChange={setPromptValue}
+ isTableBarEnabled={isTableBarEnabled}
+ selectedTables={selectedTables}
+ onSelectedTablesChange={setSelectedTables}
+ />,
+ portalTarget.container,
+ )
+ : null,
+ proposedQuestion: llmSqlGenerationEnabled ? proposedQuestion : undefined,
+ handleRejectProposed: llmSqlGenerationEnabled
+ ? handleRejectProposed
+ : undefined,
+ handleAcceptProposed: llmSqlGenerationEnabled
+ ? handleAcceptProposed
+ : undefined,
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/utils.ts b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/utils.ts
similarity index 66%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/utils.ts
rename to frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/utils.ts
index f2a1d159172..dc6a689cbec 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotInlineSQLPrompt/utils.ts
+++ b/frontend/src/metabase/metabot/components/MetabotInlineSQLPrompt/utils.ts
@@ -1,29 +1,10 @@
import type { EditorView } from "@codemirror/view";
-import { isFulfilled } from "@reduxjs/toolkit";
-import type { useMetabotAgent } from "metabase-enterprise/metabot/hooks";
import type {
DatabaseId,
MetabotCodeEditorBufferContext,
} from "metabase-types/api";
-type SubmitInputResult = Awaited<
- ReturnType<ReturnType<typeof useMetabotAgent>["submitInput"]>
->;
-
-export const responseHasCodeEdit = (action: SubmitInputResult) => {
- return (
- isFulfilled(action) &&
- action.payload.data?.processedResponse.data.some(
- (dp) =>
- typeof dp === "object" &&
- dp !== null &&
- "type" in dp &&
- (dp as { type: string }).type === "code_edit",
- )
- );
-};
-
export function extractMetabotBufferContext(
view: EditorView,
databaseId: DatabaseId | null,
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/MetabotPromptInput.module.css b/frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.module.css
similarity index 100%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/MetabotPromptInput.module.css
rename to frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.module.css
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx b/frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx
similarity index 99%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx
rename to frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx
index 3162b9921c9..8c0df154470 100644
--- a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx
+++ b/frontend/src/metabase/metabot/components/MetabotPromptInput/MetabotPromptInput.tsx
@@ -91,6 +91,7 @@ export const MetabotPromptInput = forwardRef<
extensions,
content: parseMetabotMessageToTiptapDoc(value),
autofocus: autoFocus,
+ injectNonce: window.MetabaseNonce,
onUpdate: ({ editor }) => {
const jsonContent = editor.getJSON();
serializedRef.current = serializeTiptapToMetabotMessage(jsonContent);
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/index.tsx b/frontend/src/metabase/metabot/components/MetabotPromptInput/index.tsx
similarity index 100%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/index.tsx
rename to frontend/src/metabase/metabot/components/MetabotPromptInput/index.tsx
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/utils.ts b/frontend/src/metabase/metabot/components/MetabotPromptInput/utils.ts
similarity index 100%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/utils.ts
rename to frontend/src/metabase/metabot/components/MetabotPromptInput/utils.ts
diff --git a/enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/utils.unit.spec.ts b/frontend/src/metabase/metabot/components/MetabotPromptInput/utils.unit.spec.ts
similarity index 100%
rename from enterprise/frontend/src/metabase-enterprise/metabot/components/MetabotPromptInput/utils.unit.spec.ts
rename to frontend/src/metabase/metabot/components/MetabotPromptInput/utils.unit.spec.ts
diff --git a/frontend/src/metabase/metabot/hooks/index.ts b/frontend/src/metabase/metabot/hooks/index.ts
new file mode 100644
index 00000000000..225eafde175
--- /dev/null
+++ b/frontend/src/metabase/metabot/hooks/index.ts
@@ -0,0 +1 @@
+export { useMetabotSQLSuggestion } from "./use-metabot-sql-suggestion";
diff --git a/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx b/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
new file mode 100644
index 00000000000..df4732bcc27
--- /dev/null
+++ b/frontend/src/metabase/metabot/hooks/use-metabot-sql-suggestion.tsx
@@ -0,0 +1,102 @@
+import { useCallback, useMemo, useRef, useState } from "react";
+import { t } from "ttag";
+
+import { useGenerateSqlMutation } from "metabase/api";
+import type { SuggestionModel } from "metabase/rich_text_editing/tiptap/extensions/shared/types";
+import type {
+ DatabaseId,
+ GenerateSqlResponse,
+ ReferencedEntity,
+} from "metabase-types/api";
+
+export interface UseMetabotSQLSuggestionOptions {
+ databaseId: DatabaseId | null;
+ bufferId: string;
+ onGenerated?: (result?: GenerateSqlResponse) => void;
+}
+
+export function useMetabotSQLSuggestion({
+ databaseId,
+ onGenerated,
+}: UseMetabotSQLSuggestionOptions) {
+ const [source, setSource] = useState<string | undefined>(undefined);
+ const [error, setError] = useState<string | undefined>(undefined);
+ const [generateSql, { isLoading }] = useGenerateSqlMutation();
+ const requestRef = useRef<ReturnType<typeof generateSql> | null>(null);
+
+ const generate = useCallback(
+ async ({
+ prompt,
+ sourceSql,
+ referencedEntities,
+ }: {
+ prompt: string;
+ sourceSql?: string;
+ referencedEntities?: ReferencedEntity[];
+ }) => {
+ if (!databaseId) {
+ setError(t`No database selected.`);
+ return;
+ }
+ setError(undefined);
+ try {
+ const request = generateSql({
+ prompt,
+ database_id: databaseId,
+ source_sql: sourceSql,
+ referenced_entities: referencedEntities,
+ });
+ requestRef.current = request;
+ const result = await request.unwrap();
+ requestRef.current = null;
+
+ if (result.sql) {
+ setSource(result.sql);
+ onGenerated?.(result);
+ } else {
+ setError(t`Something went wrong. Please try again.`);
+ }
+ } catch (err) {
+ const error = err as { status?: number; data?: unknown };
+ if (error.status === 400 && typeof error.data === "string") {
+ setError(error.data);
+ } else {
+ setError(t`Something went wrong. Please try again.`);
+ }
+ }
+ },
+ [generateSql, databaseId, onGenerated],
+ );
+
+ const cancelRequest = useCallback(() => {
+ requestRef.current?.abort();
+ requestRef.current = null;
+ }, []);
+
+ const clear = useCallback(() => {
+ setSource(undefined);
+ }, []);
+
+ const reset = useCallback(() => {
+ clear();
+ setError(undefined);
+ }, [clear]);
+
+ const reject = useCallback(() => {
+ // no-op for OSS - upgrade to enterprise if you want the agent to be smarter after rejecting an edit!
+ }, []);
+
+ const suggestionModels: SuggestionModel[] = useMemo(() => ["table"], []);
+
+ return {
+ source,
+ isLoading,
+ generate,
+ error,
+ cancelRequest,
+ clear,
+ reject,
+ reset,
+ suggestionModels,
+ };
+}
diff --git a/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx b/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx
index 999168f0d73..e3072ac4db4 100644
--- a/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx
+++ b/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.styled.tsx
@@ -40,7 +40,6 @@ export const SearchInputContainer = styled.div<{
}
return css`
background-color: var(--mb-color-background-primary);
-
&:hover {
background-color: var(--mb-color-background-secondary);
}
diff --git a/frontend/src/metabase/plugins/oss/ai.ts b/frontend/src/metabase/plugins/oss/ai.ts
index 1bd1b6c0441..29cc44b898f 100644
--- a/frontend/src/metabase/plugins/oss/ai.ts
+++ b/frontend/src/metabase/plugins/oss/ai.ts
@@ -1,24 +1,26 @@
import type { BaseQueryFn } from "@reduxjs/toolkit/query";
import type { TypedUseLazyQuery } from "@reduxjs/toolkit/src/query/react/buildHooks";
-import type { Extension } from "@uiw/react-codemirror";
import type { ComponentType } from "react";
import React from "react";
import type { MetabotContext } from "metabase/metabot";
import { PluginPlaceholder } from "metabase/plugins/components/PluginPlaceholder";
+import type { SuggestionModel } from "metabase/rich_text_editing/tiptap/extensions/shared/types";
import type Question from "metabase-lib/v1/Question";
import type {
CollectionId,
DashCardId,
- DatasetQuery,
+ DatabaseId,
+ GenerateSqlResponse,
MetabotGenerateContentRequest,
MetabotGenerateContentResponse,
+ ReferencedEntityId,
SearchModel,
Timeline,
TimelineEvent,
VisualizationDisplay,
} from "metabase-types/api";
-import type { AdminPath, State } from "metabase-types/store";
+import type { State } from "metabase-types/store";
export type PluginAiSqlFixer = {
FixSqlQueryButton: ComponentType<Record<string, never>>;
@@ -64,50 +66,44 @@ type PluginMetabotType = {
defaultMetabotContextValue: MetabotContext;
MetabotContext: React.Context<MetabotContext>;
getMetabotProvider: () => ComponentType<{ children: React.ReactNode }>;
- getAdminPaths: () => AdminPath[];
- getAdminRoutes: () => React.ReactElement;
+ getAdminRoutes: (() => JSX.Element[]) | null;
getMetabotRoutes: () => React.ReactElement | null;
- MetabotAdminPage: ComponentType;
getMetabotQueryBuilderRoute: () => React.ReactElement | null;
getNewMenuItemAIExploration: (
hasDataAccess: boolean,
collectionId?: CollectionId,
) => React.ReactElement | undefined;
getMetabotVisible: (state: State, conversation_id: string) => boolean;
- MetabotToggleButton: ComponentType<{ className?: string }>;
MetabotAppBarButton: ComponentType;
- MetabotAdminAppBarButton: ComponentType;
MetabotDataStudioButton: ComponentType;
MetabotDataStudioSidebar: ComponentType;
- useInlineSQLPrompt: (
- question: Question,
- bufferId: string,
- ) => {
- portalElement: React.ReactPortal | null;
- extensions: Extension[];
- proposedQuestion: Question | undefined;
- handleAcceptProposed?: (datasetQuery: DatasetQuery) => void;
- handleRejectProposed?: () => void;
- } | void;
useLazyMetabotGenerateContentQuery: TypedUseLazyQuery<
MetabotGenerateContentResponse,
MetabotGenerateContentRequest,
BaseQueryFn
>;
MetabotThinkingStyles: { [key: string]: string };
+ useMetabotSQLSuggestion: (options: {
+ databaseId: DatabaseId | null;
+ bufferId: string;
+ onGenerated?: (result?: GenerateSqlResponse) => void;
+ }) => {
+ source: string | undefined;
+ isLoading: boolean;
+ generate: (options: {
+ prompt: string;
+ sourceSql?: string;
+ referencedEntities?: ReferencedEntityId[];
+ }) => Promise<void>;
+ error: string | undefined;
+ cancelRequest: () => void;
+ clear: () => void;
+ reject: () => void;
+ reset: () => void;
+ suggestionModels: SuggestionModel[];
+ };
};
-const getDefaultMetabotContextValue = (): MetabotContext => ({
- prompt: "",
- setPrompt: () => {},
- promptInputRef: undefined,
- getChatContext: () => ({}) as any,
- registerChatContextProvider: () => () => {},
-});
-
-const defaultMetabotContextValue: MetabotContext =
- getDefaultMetabotContextValue();
-
const getDefaultPluginAiSqlFixer = (): PluginAiSqlFixer => ({
FixSqlQueryButton: PluginPlaceholder,
});
@@ -124,6 +120,17 @@ const getDefaultPluginAIEntityAnalysis = (): PluginAIEntityAnalysis => ({
export const PLUGIN_AI_ENTITY_ANALYSIS: PluginAIEntityAnalysis =
getDefaultPluginAIEntityAnalysis();
+const getDefaultMetabotContextValue = (): MetabotContext => ({
+ prompt: "",
+ setPrompt: () => {},
+ promptInputRef: undefined,
+ getChatContext: () => ({}) as any,
+ registerChatContextProvider: () => () => {},
+});
+
+const defaultMetabotContextValue: MetabotContext =
+ getDefaultMetabotContextValue();
+
const getDefaultPluginMetabot = (): PluginMetabotType => ({
isEnabled: () => false,
Metabot: (_props: { hide?: boolean; config?: PluginMetabotConfig }) =>
@@ -138,22 +145,26 @@ const getDefaultPluginMetabot = (): PluginMetabotType => ({
children,
);
},
- getAdminPaths: () => [],
- getAdminRoutes: () => PluginPlaceholder as unknown as React.ReactElement,
+ getAdminRoutes: null,
getMetabotRoutes: () => null,
- MetabotAdminPage: () => `placeholder`,
getMetabotQueryBuilderRoute: () => null,
getNewMenuItemAIExploration: () => undefined,
getMetabotVisible: () => false,
- MetabotToggleButton: PluginPlaceholder,
MetabotAppBarButton: PluginPlaceholder,
- MetabotAdminAppBarButton: PluginPlaceholder,
MetabotDataStudioButton: PluginPlaceholder,
MetabotDataStudioSidebar: PluginPlaceholder,
- useInlineSQLPrompt: () => {},
useLazyMetabotGenerateContentQuery:
(() => []) as unknown as PluginMetabotType["useLazyMetabotGenerateContentQuery"],
MetabotThinkingStyles: {},
+ useMetabotSQLSuggestion: (options) => {
+ const {
+ useMetabotSQLSuggestion,
+ // lazy require to avoid loading metabase/api and its cljs dependencies at
+ // module init time. without this the jest unit tests will break.
+ /* eslint-disable @typescript-eslint/no-var-requires */
+ } = require("metabase/metabot/hooks/use-metabot-sql-suggestion");
+ return useMetabotSQLSuggestion(options);
+ },
});
export const PLUGIN_METABOT: PluginMetabotType = getDefaultPluginMetabot();
diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/CodeMirrorEditor.tsx b/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/CodeMirrorEditor.tsx
index 8888af18c39..7576b1b4707 100644
--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/CodeMirrorEditor.tsx
+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/CodeMirrorEditor.tsx
@@ -14,6 +14,7 @@ import {
CodeMirror,
type CodeMirrorRef,
} from "metabase/common/components/CodeMirror";
+import { useSetting } from "metabase/common/hooks";
import { isEventOverElement } from "metabase/lib/dom";
import * as Lib from "metabase-lib";
import type { CardId } from "metabase-types/api";
@@ -57,7 +58,7 @@ export const CodeMirrorEditor = forwardRef<
query,
proposedQuery,
highlightedLineNumbers,
- placeholder = getPlaceholderText(Lib.engine(query)),
+ placeholder: placeholderProp,
readOnly,
extensions: customExtensions,
onChange,
@@ -71,6 +72,10 @@ export const CodeMirrorEditor = forwardRef<
ref,
) {
const editorRef = useRef<CodeMirrorRef>(null);
+ const llmSqlGenerationEnabled = useSetting("llm-sql-generation-enabled");
+ const placeholder =
+ placeholderProp ??
+ getPlaceholderText(Lib.engine(query), llmSqlGenerationEnabled);
const baseExtensions = useExtensions({
query,
diff: !!proposedQuery,
diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.tsx b/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.tsx
index f856ea73b31..f4a6b2cf659 100644
--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.tsx
+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.tsx
@@ -6,7 +6,6 @@ import { t } from "ttag";
import { METAKEY } from "metabase/lib/browser";
import { getEngineNativeType } from "metabase/lib/engine";
import { isNotNull } from "metabase/lib/types";
-import { PLUGIN_METABOT } from "metabase/plugins";
import * as Lib from "metabase-lib";
import type { CardId, CardType } from "metabase-types/api";
@@ -255,7 +254,10 @@ export const getReferencedCardIds = createSelector(
},
);
-export const getPlaceholderText = (engine?: string | null): string => {
+export const getPlaceholderText = (
+ engine?: string | null,
+ llmEnabled?: boolean,
+): string => {
if (!engine) {
return "";
}
@@ -265,7 +267,7 @@ export const getPlaceholderText = (engine?: string | null): string => {
const engineType = getEngineNativeType(engine);
- if (PLUGIN_METABOT.isEnabled() && engineType === "sql") {
+ if (llmEnabled && engineType === "sql") {
return t`Write your SQL here, or press ${METAKEY} + Shift + i to have SQL generated for you.`;
}
diff --git a/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.unit.spec.tsx b/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.unit.spec.tsx
index 02808360fe2..ee2c7284834 100644
--- a/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.unit.spec.tsx
+++ b/frontend/src/metabase/query_builder/components/NativeQueryEditor/CodeMirrorEditor/util.unit.spec.tsx
@@ -186,7 +186,7 @@ describe("getPlaceholderText", () => {
});
it("should return metabot placeholder text for sql", () => {
- expect(getPlaceholderText("sql")).toBe(
+ expect(getPlaceholderText("sql", true)).toBe(
"Write your SQL here, or press Ctrl + Shift + i to have SQL generated for you.",
);
});
diff --git a/frontend/src/metabase/query_builder/components/view/View/ViewNativeQueryEditor/ViewNativeQueryEditor.tsx b/frontend/src/metabase/query_builder/components/view/View/ViewNativeQueryEditor/ViewNativeQueryEditor.tsx
index ef84c06cb83..3f01ce55b78 100644
--- a/frontend/src/metabase/query_builder/components/view/View/ViewNativeQueryEditor/ViewNativeQueryEditor.tsx
+++ b/frontend/src/metabase/query_builder/components/view/View/ViewNativeQueryEditor/ViewNativeQueryEditor.tsx
@@ -1,7 +1,7 @@
import type { ResizableBoxProps } from "react-resizable";
import { useSelector } from "metabase/lib/redux";
-import { PLUGIN_METABOT } from "metabase/plugins";
+import { useInlineSQLPrompt } from "metabase/metabot/components/MetabotInlineSQLPrompt";
import { NativeQueryEditor } from "metabase/query_builder/components/NativeQueryEditor";
import type {
SelectionRange,
@@ -87,7 +87,7 @@ export const ViewNativeQueryEditor = (props: ViewNativeQueryEditorProps) => {
getHighlightedNativeQueryLineNumbers,
);
- const inlineSQLPrompt = PLUGIN_METABOT.useInlineSQLPrompt(question, "qb");
+ const inlineSQLPrompt = useInlineSQLPrompt(question, "qb");
// Normally, when users open native models,
// they open an ad-hoc GUI question using the model as a data source
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/Mention/MentionSuggestion.unit.spec.tsx b/frontend/src/metabase/rich_text_editing/tiptap/extensions/Mention/MentionSuggestion.unit.spec.tsx
index d48efa0f255..bad7d9db976 100644
--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/Mention/MentionSuggestion.unit.spec.tsx
+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/Mention/MentionSuggestion.unit.spec.tsx
@@ -180,6 +180,27 @@ describe("MentionSuggestion", () => {
expectOptionNotToBePresent(/Dashboard/);
});
+ it("skips model selector and shows entities when only one searchModel is provided", async () => {
+ setup({
+ query: "",
+ searchModels: ["dashboard"],
+ searchItems: [
+ createMockSearchResult({
+ name: "Orders Dashboard",
+ model: "dashboard",
+ id: 20,
+ }),
+ ],
+ recentItems: [],
+ canFilterSearchModels: true,
+ canBrowseAll: false,
+ });
+
+ // should show recent entity results directly, not the model selector
+ await expectOptionToBePresent(/Orders Dashboard/);
+ expectOptionNotToBePresent(/^Dashboard$/);
+ });
+
it("searches all models when query doesn't match any model name", async () => {
setup({
query: "Sales", // Doesn't match any model name
diff --git a/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.ts b/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.ts
index f3ea7ef4c73..e6b04399da9 100644
--- a/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.ts
+++ b/frontend/src/metabase/rich_text_editing/tiptap/extensions/shared/useEntitySuggestions.ts
@@ -75,7 +75,22 @@ export function useEntitySuggestions({
const [selectedIndex, setSelectedIndex] = useState(0);
const [modal, setModal] = useState<SuggestionPickerModalType>(null);
const [selectedSearchModel, setSelectedSearchModel] =
- useState<SuggestionModel | null>(null);
+ useState<SuggestionModel | null>(
+ searchModels?.length === 1 ? searchModels[0] : null,
+ );
+
+ // sync selectedSearchModel when searchModels changes
+ useEffect(() => {
+ if (searchModels?.length === 1) {
+ setSelectedSearchModel(searchModels[0]);
+ } else if (
+ selectedSearchModel &&
+ !searchModels?.includes(selectedSearchModel)
+ ) {
+ // Reset if current selection is no longer valid
+ setSelectedSearchModel(null);
+ }
+ }, [searchModels, selectedSearchModel]);
const handleRecentSelect = useCallback(
(item: RecentItem) => {
@@ -162,7 +177,10 @@ export function useEntitySuggestions({
const hasSearchModels = (searchModels?.length ?? 0) > 0;
const hasMatchingFilteredModels = (filteredSearchModels?.length ?? 0) > 0;
const isInModelSelectionMode =
- !selectedSearchModel && hasSearchModels && hasMatchingFilteredModels;
+ !selectedSearchModel &&
+ hasSearchModels &&
+ hasMatchingFilteredModels &&
+ (searchModels?.length ?? 0) > 1;
const {
menuItems: entityMenuItems,
diff --git a/frontend/src/metabase/ui/components/inputs/PasswordInput/PasswordInput.config.tsx b/frontend/src/metabase/ui/components/inputs/PasswordInput/PasswordInput.config.tsx
new file mode 100644
index 00000000000..30c40222b55
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/PasswordInput/PasswordInput.config.tsx
@@ -0,0 +1,20 @@
+import { PasswordInput } from "@mantine/core";
+
+import Styles from "./PasswordInput.module.css";
+
+export const passwordInputOverrides = {
+ PasswordInput: PasswordInput.extend({
+ defaultProps: {
+ size: "md",
+ inputWrapperOrder: ["label", "description", "input", "error"],
+ errorProps: {
+ role: "alert",
+ },
+ },
+ classNames: {
+ input: Styles.input,
+ innerInput: Styles.innerInput,
+ error: Styles.error,
+ },
+ }),
+};
diff --git a/frontend/src/metabase/ui/components/inputs/PasswordInput/PasswordInput.module.css b/frontend/src/metabase/ui/components/inputs/PasswordInput/PasswordInput.module.css
new file mode 100644
index 00000000000..8e6793bf2ee
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/PasswordInput/PasswordInput.module.css
@@ -0,0 +1,17 @@
+.input {
+ color: var(--mb-color-text-primary);
+ background: var(--mb-color-background-primary);
+ border-color: var(--mb-color-border);
+}
+
+.innerInput {
+ &::placeholder {
+ color: var(--mb-color-text-tertiary);
+ }
+}
+
+.error {
+ &::first-letter {
+ text-transform: capitalize;
+ }
+}
diff --git a/frontend/src/metabase/ui/components/inputs/PasswordInput/index.ts b/frontend/src/metabase/ui/components/inputs/PasswordInput/index.ts
new file mode 100644
index 00000000000..a613e4a2ea0
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/PasswordInput/index.ts
@@ -0,0 +1,3 @@
+export { PasswordInput } from "@mantine/core";
+export type { PasswordInputProps } from "@mantine/core";
+export { passwordInputOverrides } from "./PasswordInput.config";
diff --git a/frontend/src/metabase/ui/components/inputs/index.ts b/frontend/src/metabase/ui/components/inputs/index.ts
index fcdd21bdc4f..254bc6305e0 100644
--- a/frontend/src/metabase/ui/components/inputs/index.ts
+++ b/frontend/src/metabase/ui/components/inputs/index.ts
@@ -12,6 +12,7 @@ export * from "./MonthPicker";
export * from "./MultiAutocomplete";
export * from "./MultiSelect";
export * from "./NumberInput";
+export * from "./PasswordInput";
export * from "./Pill";
export * from "./PillsInput";
export * from "./QuarterPicker";
diff --git a/frontend/src/metabase/ui/theme.ts b/frontend/src/metabase/ui/theme.ts
index 5a56bc475fe..dc8e611fb15 100644
--- a/frontend/src/metabase/ui/theme.ts
+++ b/frontend/src/metabase/ui/theme.ts
@@ -38,6 +38,7 @@ import {
numberInputOverrides,
overlayOverrides,
paperOverrides,
+ passwordInputOverrides,
pillOverrides,
pillsInputOverrides,
popoverOverrides,
@@ -178,6 +179,7 @@ export const getThemeOverrides = (
...radioOverrides,
...overlayOverrides,
...paperOverrides,
+ ...passwordInputOverrides,
...pillOverrides,
...pillsInputOverrides,
...popoverOverrides,
diff --git a/frontend/test/__support__/server-mocks/index.ts b/frontend/test/__support__/server-mocks/index.ts
index f01ce3a145b..05b894e21af 100644
--- a/frontend/test/__support__/server-mocks/index.ts
+++ b/frontend/test/__support__/server-mocks/index.ts
@@ -27,6 +27,7 @@ export * from "./google";
export * from "./group";
export * from "./impersonation";
export * from "./library";
+export * from "./llm";
export * from "./logger";
export * from "./measure";
export * from "./model-indexes";
diff --git a/frontend/test/__support__/server-mocks/llm.ts b/frontend/test/__support__/server-mocks/llm.ts
new file mode 100644
index 00000000000..01790a5c9d8
--- /dev/null
+++ b/frontend/test/__support__/server-mocks/llm.ts
@@ -0,0 +1,25 @@
+import fetchMock from "fetch-mock";
+
+import type { LLMModel } from "metabase-types/api";
+
+export function setupLlmListModelsEndpoint(
+ models: LLMModel[] = [],
+ error: boolean = false,
+) {
+ if (error) {
+ fetchMock.get(
+ "path:/api/llm/list-models",
+ {
+ status: 500,
+ body: { message: "Failed to fetch models" },
+ },
+ { name: "llm-list-models" },
+ );
+ } else {
+ fetchMock.get(
+ "path:/api/llm/list-models",
+ { models },
+ { name: "llm-list-models" },
+ );
+ }
+}
diff --git a/modules/drivers/athena/src/metabase/driver/athena.clj b/modules/drivers/athena/src/metabase/driver/athena.clj
index 06cc253faa4..a60be290197 100644
--- a/modules/drivers/athena/src/metabase/driver/athena.clj
+++ b/modules/drivers/athena/src/metabase/driver/athena.clj
@@ -539,3 +539,6 @@
(assert (empty? (get-in query [:native :params]))
"Athena queries should not be parameterized; they should have been compiled with metabase.driver/*compile-with-inline-parameters*")
((get-method driver/execute-reducible-query :sql-jdbc) driver query context respond))
+
+(defmethod driver/llm-sql-dialect-resource :athena [_]
+ "llm/prompts/dialects/athena.md")
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
index 34bce70e965..b24c184fdf3 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
@@ -1052,3 +1052,6 @@
[_driver]
;; https://cloud.google.com/bigquery/docs/tables
1024)
+
+(defmethod driver/llm-sql-dialect-resource :bigquery-cloud-sdk [_]
+ "llm/prompts/dialects/bigquery.md")
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
index d8d5d4a51f2..56f1063ed4a 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse.clj
@@ -359,3 +359,6 @@
;; TODO(rileythomp, 2026-01-29): Remove this once we upgrade past 0.8.4
;; This is to work around 68674 where connections are being poisoned with bad roles
{"preferredTestQuery" "SELECT 1"}))
+
+(defmethod driver/llm-sql-dialect-resource :clickhouse [_]
+ "llm/prompts/dialects/clickhouse.md")
diff --git a/modules/drivers/druid/src/metabase/driver/druid.clj b/modules/drivers/druid/src/metabase/driver/druid.clj
index 3df5e88d0a0..a8b1e0ef8e2 100644
--- a/modules/drivers/druid/src/metabase/driver/druid.clj
+++ b/modules/drivers/druid/src/metabase/driver/druid.clj
@@ -63,3 +63,6 @@
(defmethod driver/db-start-of-week :druid
[_]
:sunday)
+
+(defmethod driver/llm-sql-dialect-resource :druid [_]
+ "llm/prompts/dialects/druid.md")
diff --git a/modules/drivers/oracle/src/metabase/driver/oracle.clj b/modules/drivers/oracle/src/metabase/driver/oracle.clj
index c459bc64a7b..67c2c3e9402 100644
--- a/modules/drivers/oracle/src/metabase/driver/oracle.clj
+++ b/modules/drivers/oracle/src/metabase/driver/oracle.clj
@@ -757,3 +757,6 @@
(defmethod sql-jdbc/impl-table-known-to-not-exist? :oracle
[_ ^SQLException e]
(= (.getErrorCode e) 942))
+
+(defmethod driver/llm-sql-dialect-resource :oracle [_]
+ "llm/prompts/dialects/oracle.md")
diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.clj
index 6175bc835fc..1b95e93f830 100644
--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj
+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj
@@ -694,3 +694,6 @@
;; https://docs.aws.amazon.com/redshift/latest/mgmt/rsql-query-tool-error-codes.html
;; 42P01: undefined_table, 3F000: invalid_schema_name
(contains? #{"42P01" "3F000"} (sql-jdbc/get-sql-state e)))
+
+(defmethod driver/llm-sql-dialect-resource :redshift [_]
+ "llm/prompts/dialects/redshift.md")
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index ff748890de2..e2de108ef28 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -959,3 +959,6 @@
(defmethod sql.qp/->honeysql [:snowflake :ends-with]
[driver [_ field arg options]]
(string-filter driver :endswith field arg options))
+
+(defmethod driver/llm-sql-dialect-resource :snowflake [_]
+ "llm/prompts/dialects/snowflake.md")
diff --git a/modules/drivers/sparksql/src/metabase/driver/sparksql.clj b/modules/drivers/sparksql/src/metabase/driver/sparksql.clj
index 43b5b761335..47365478118 100644
--- a/modules/drivers/sparksql/src/metabase/driver/sparksql.clj
+++ b/modules/drivers/sparksql/src/metabase/driver/sparksql.clj
@@ -265,3 +265,6 @@
[_ ^ResultSet rs _rsmeta ^Integer i]
(fn []
(.getObject rs i)))
+
+(defmethod driver/llm-sql-dialect-resource :sparksql [_]
+ "llm/prompts/dialects/databricks.md")
diff --git a/modules/drivers/sqlite/src/metabase/driver/sqlite.clj b/modules/drivers/sqlite/src/metabase/driver/sqlite.clj
index b1885fe033c..2438f4c5ac7 100644
--- a/modules/drivers/sqlite/src/metabase/driver/sqlite.clj
+++ b/modules/drivers/sqlite/src/metabase/driver/sqlite.clj
@@ -530,3 +530,6 @@
(defmethod sql.qp/->integer :sqlite
[driver value]
(sql.qp/->integer-with-round driver value))
+
+(defmethod driver/llm-sql-dialect-resource :sqlite [_]
+ "llm/prompts/dialects/sqlite.md")
diff --git a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
index 49d94433fc3..d92a9f7d7b0 100644
--- a/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
+++ b/modules/drivers/sqlserver/src/metabase/driver/sqlserver.clj
@@ -1081,3 +1081,6 @@
(if schema
(str (quote-identifier (name schema)) "." (quote-identifier (name table-name)))
(quote-identifier (name table-name))))))
+
+(defmethod driver/llm-sql-dialect-resource :sqlserver [_]
+ "llm/prompts/dialects/sqlserver.md")
diff --git a/modules/drivers/vertica/src/metabase/driver/vertica.clj b/modules/drivers/vertica/src/metabase/driver/vertica.clj
index 23b92246f11..af1eaef13a4 100644
--- a/modules/drivers/vertica/src/metabase/driver/vertica.clj
+++ b/modules/drivers/vertica/src/metabase/driver/vertica.clj
@@ -348,3 +348,6 @@
(defmethod sql-jdbc/impl-table-known-to-not-exist? :vertica
[_ e]
(= (sql-jdbc/get-sql-state e) "42V01"))
+
+(defmethod driver/llm-sql-dialect-resource :vertica [_]
+ "llm/prompts/dialects/vertica.md")
diff --git a/resources/llm/prompts/dialects/athena.md b/resources/llm/prompts/dialects/athena.md
new file mode 100644
index 00000000000..75d87dfc7e1
--- /dev/null
+++ b/resources/llm/prompts/dialects/athena.md
@@ -0,0 +1,116 @@
+# Athena (Trino/Presto) Dialect - Key Differences
+
+Athena uses **Trino** (formerly PrestoSQL) as its query engine.
+
+## Identifier Quoting
+
+Double quotes for identifiers. Case-insensitive (stored as lowercase).
+
+```sql
+SELECT "my-column", "order" FROM my_table
+```
+
+**Schema-qualified names:** Database and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "my-database"."my-table"
+SELECT * FROM my_database.my_table -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "my_database.my_table" -- Looks for table literally named "my_database.my_table"
+```
+
+## DATE_ADD / DATE_DIFF Syntax
+
+**Unit comes first**, unlike most databases:
+
+```sql
+SELECT DATE_ADD('day', 7, order_date) -- Add 7 days
+SELECT DATE_ADD('month', -1, order_date) -- Subtract 1 month
+SELECT DATE_DIFF('day', start_date, end_date) -- Difference in days
+```
+
+## No IFNULL or NVL
+
+```sql
+SELECT COALESCE(nullable, 'default') -- Use COALESCE
+SELECT IF(col IS NULL, 'N/A', col) -- Or IF function
+```
+
+## IF Function for Ternary
+
+```sql
+SELECT IF(status = 'active', 'Yes', 'No')
+SELECT IF(amount > 100, 'high', 'low')
+```
+
+## Arrays (1-Indexed)
+
+```sql
+SELECT my_array[1] AS first_element -- 1-indexed!
+SELECT CARDINALITY(my_array) -- Length
+SELECT CONTAINS(my_array, 'value') -- Membership
+SELECT ARRAY_JOIN(my_array, ', ') -- Join to string
+
+-- Flatten array to rows
+SELECT t.id, u.element
+FROM my_table t
+CROSS JOIN UNNEST(t.array_column) AS u(element)
+
+-- With index
+CROSS JOIN UNNEST(t.array_column) WITH ORDINALITY AS u(element, idx)
+```
+
+## TRY_CAST for Safe Casting
+
+```sql
+SELECT TRY_CAST(potentially_bad AS INTEGER) -- NULL on failure
+SELECT CAST(col AS BIGINT) -- Throws on failure
+```
+
+## JSON Functions
+
+```sql
+SELECT JSON_EXTRACT(json_col, '$.field') -- Returns JSON type
+SELECT JSON_EXTRACT_SCALAR(json_col, '$.field') -- Returns VARCHAR
+SELECT CAST(JSON_EXTRACT(json_col, '$.count') AS INTEGER)
+```
+
+## APPROX_DISTINCT
+
+```sql
+SELECT APPROX_DISTINCT(user_id) -- Fast approximate count
+SELECT APPROX_PERCENTILE(amount, 0.5) -- Approximate median
+```
+
+## Date Spine with SEQUENCE
+
+```sql
+SELECT dt
+FROM UNNEST(SEQUENCE(DATE '2024-01-01', DATE '2024-12-31', INTERVAL '1' DAY)) AS t(dt)
+```
+
+## Maps
+
+```sql
+SELECT MAP(ARRAY['a', 'b'], ARRAY[1, 2])
+SELECT my_map['key']
+SELECT ELEMENT_AT(my_map, 'key') -- NULL-safe access
+SELECT MAP_KEYS(my_map), MAP_VALUES(my_map)
+```
+
+## Read-Only Limitations
+
+- **No UPDATE/DELETE** on standard tables
+- **No transactions** (each query is atomic)
+- 30-minute query timeout, 2GB result limit
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) AS rn
+ FROM sales
+) WHERE rn = 1
+```
diff --git a/resources/llm/prompts/dialects/bigquery.md b/resources/llm/prompts/dialects/bigquery.md
new file mode 100644
index 00000000000..cfacf0a60a3
--- /dev/null
+++ b/resources/llm/prompts/dialects/bigquery.md
@@ -0,0 +1,160 @@
+# BigQuery (GoogleSQL) Dialect - Key Differences
+
+BigQuery uses **GoogleSQL**. This guide covers only BigQuery-specific syntax that differs from standard SQL.
+
+## Identifier Quoting
+
+**CRITICAL: BigQuery uses backticks for identifiers, NOT double quotes.**
+
+| Syntax | Meaning |
+|--------|---------|
+| `` `name` `` | Identifier (column, table) |
+| `"text"` | String literal (same as `'text'`) |
+
+```sql
+-- CORRECT: backticks for column reference
+SELECT `language`, `order` FROM my_table
+
+-- WRONG: double quotes are string literals!
+SELECT "language" FROM my_table -- Returns the STRING "language", not the column
+```
+
+Use backticks for: reserved words, special characters, hyphens.
+
+**Project-qualified names:** Project, dataset, and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM `my-project`.`my_dataset`.`my_table`
+SELECT * FROM my_project.my_dataset.my_table -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM `my_project.my_dataset.my_table` -- Looks for dataset literally named "my_project.my_dataset.my_table"
+```
+
+## Arrays (0-Indexed!)
+
+BigQuery arrays are **0-indexed**, unlike most SQL dialects.
+
+```sql
+-- Array access
+SELECT arr[OFFSET(0)] AS first -- 0-indexed, errors if out of bounds
+SELECT arr[SAFE_OFFSET(0)] AS first -- 0-indexed, NULL if out of bounds
+SELECT arr[ORDINAL(1)] AS first -- 1-indexed alternative
+
+-- SPLIT returns array - must use OFFSET to get elements
+SELECT SPLIT(csv, ',')[OFFSET(0)] AS first_value
+
+-- Flatten array to rows
+SELECT id, element
+FROM my_table, UNNEST(array_column) AS element
+
+-- With index
+SELECT id, element, idx
+FROM my_table, UNNEST(array_column) AS element WITH OFFSET AS idx
+```
+
+## Date/Time Type Strictness
+
+BigQuery strictly separates `DATE`, `DATETIME`, `TIMESTAMP`, and `TIME`. Use the matching function prefix.
+
+```sql
+-- Use correct function for your type
+DATE_TRUNC(date_col, MONTH) -- For DATE
+DATETIME_TRUNC(datetime_col, HOUR) -- For DATETIME
+TIMESTAMP_TRUNC(ts_col, DAY) -- For TIMESTAMP
+
+DATE_ADD(date_col, INTERVAL 7 DAY)
+DATETIME_ADD(dt_col, INTERVAL 2 HOUR)
+TIMESTAMP_ADD(ts_col, INTERVAL 30 MINUTE)
+
+DATE_DIFF(end_date, start_date, DAY)
+TIMESTAMP_DIFF(end_ts, start_ts, SECOND)
+```
+
+**Wrong**: `DATE_TRUNC(timestamp_col, MONTH)` - type mismatch error.
+
+## SAFE Functions
+
+BigQuery provides `SAFE_` variants that return NULL instead of errors:
+
+```sql
+SAFE_CAST(value AS INT64) -- NULL if conversion fails
+SAFE_DIVIDE(a, b) -- NULL if b is 0
+SAFE.JSON_VALUE(json, '$.path') -- NULL if path missing
+arr[SAFE_OFFSET(10)] -- NULL if out of bounds
+```
+
+Prefer `SAFE_CAST` over `CAST` when data quality is uncertain.
+
+## QUALIFY Clause
+
+BigQuery-specific clause to filter on window function results (like HAVING for aggregates):
+
+```sql
+-- Get top 1 per category (concise BigQuery idiom)
+SELECT *
+FROM sales
+QUALIFY ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) = 1
+
+-- Without QUALIFY (verbose alternative)
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) AS rn
+ FROM sales
+) WHERE rn = 1
+```
+
+## SELECT EXCEPT / REPLACE
+
+BigQuery-specific modifiers:
+
+```sql
+SELECT * EXCEPT(internal_id, debug_col) FROM t
+SELECT * REPLACE(ROUND(amount, 2) AS amount) FROM t
+SELECT * EXCEPT(tmp) REPLACE(UPPER(name) AS name) FROM t
+```
+
+## BigQuery-Specific Aggregations
+
+```sql
+COUNTIF(condition) -- Count where true
+ARRAY_AGG(col ORDER BY x) -- Aggregate to ordered array
+ARRAY_AGG(DISTINCT col IGNORE NULLS) -- Distinct, no nulls
+STRING_AGG(col, ', ' ORDER BY x) -- Concatenate strings
+APPROX_COUNT_DISTINCT(col) -- Fast approximate distinct count
+LOGICAL_AND(bool_col) -- TRUE if all TRUE
+LOGICAL_OR(bool_col) -- TRUE if any TRUE
+```
+
+## Structs
+
+```sql
+-- Create struct
+SELECT STRUCT(id, name, email) AS user_info FROM users
+
+-- Access struct fields
+SELECT user_info.name FROM t
+```
+
+## JSON
+
+```sql
+-- Preferred extraction functions
+JSON_VALUE(json_col, '$.field') -- Returns STRING
+JSON_QUERY(json_col, '$.nested') -- Returns JSON
+
+-- Safe extraction
+SAFE.JSON_VALUE(json_col, '$.maybe_missing')
+
+-- Cast result
+CAST(JSON_VALUE(json_col, '$.count') AS INT64)
+```
+
+## Table Wildcards
+
+Query multiple tables matching a pattern:
+
+```sql
+SELECT * FROM `project.dataset.events_*`
+WHERE _TABLE_SUFFIX BETWEEN '20240101' AND '20240131'
+```
diff --git a/resources/llm/prompts/dialects/clickhouse.md b/resources/llm/prompts/dialects/clickhouse.md
new file mode 100644
index 00000000000..51770d9ab43
--- /dev/null
+++ b/resources/llm/prompts/dialects/clickhouse.md
@@ -0,0 +1,172 @@
+# ClickHouse Dialect - Key Differences
+
+ClickHouse is a columnar OLAP database with unique syntax.
+
+## Identifier Quoting
+
+Double quotes or backticks. **Case-sensitive** identifiers.
+
+```sql
+SELECT "Column-Name", `another column` FROM my_table
+```
+
+**Schema-qualified names:** Database and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "my-database"."My-Table"
+SELECT * FROM my_database.my_table -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "my_database.my_table" -- Looks for table literally named "my_database.my_table"
+```
+
+## Date Truncation Functions
+
+ClickHouse uses `toStartOf*` functions (though `date_trunc` also works):
+
+```sql
+SELECT toStartOfMonth(dt) -- Truncate to month
+SELECT toStartOfYear(dt), toStartOfQuarter(dt)
+SELECT toStartOfWeek(dt) -- Monday
+SELECT toStartOfDay(dt), toStartOfHour(dt)
+SELECT toDate(dt) -- Truncate to Date
+SELECT date_trunc('month', dt) -- Standard syntax also works
+```
+
+## Current Date/Time
+
+```sql
+SELECT today() -- Date
+SELECT now() -- DateTime
+```
+
+## -If Combinators (Conditional Aggregation)
+
+**Unique to ClickHouse.** More efficient than CASE WHEN:
+
+```sql
+SELECT
+ sumIf(amount, status = 'active'),
+ countIf(status = 'done'),
+ avgIf(amount, region = 'US')
+FROM t
+```
+
+## uniq() for Approximate Count Distinct
+
+```sql
+SELECT uniq(user_id) -- Fast approximate
+SELECT uniqExact(user_id) -- Exact (slower)
+SELECT count(DISTINCT user_id) -- Also works, but slower
+```
+
+## Safe Casting with OrNull / OrZero
+
+```sql
+SELECT toInt64OrNull(potentially_bad) -- NULL on failure
+SELECT toInt64OrZero(potentially_bad) -- 0 on failure
+SELECT toDateOrNull(string_col)
+```
+
+## Arrays (1-Indexed)
+
+```sql
+SELECT arr[1] AS first_element -- 1-indexed!
+SELECT length(arr)
+SELECT has(arr, value) -- Membership test
+SELECT indexOf(arr, value) -- Position (0 if not found)
+
+-- Lambda functions
+SELECT arrayFilter(x -> x > 5, arr)
+SELECT arrayMap(x -> x * 2, arr)
+```
+
+## ARRAY JOIN (Not UNNEST)
+
+```sql
+-- Expand array to rows
+SELECT id, element FROM t ARRAY JOIN arr AS element
+
+-- With index
+SELECT id, element, idx
+FROM t ARRAY JOIN arr AS element, arrayEnumerate(arr) AS idx
+```
+
+## String Aggregation
+
+```sql
+SELECT arrayStringConcat(groupArray(name), ', ')
+FROM t GROUP BY category
+```
+
+## LIMIT BY (Top N Per Group)
+
+Unique clause for limiting within groups without window functions:
+
+```sql
+SELECT * FROM products
+ORDER BY category, price DESC
+LIMIT 3 BY category
+```
+
+## PREWHERE (Filter Before Reading)
+
+More efficient than WHERE for selective filters:
+
+```sql
+SELECT * FROM events
+PREWHERE event_date = '2024-01-15'
+WHERE event_type = 'click'
+```
+
+## WITH FILL (Fill Gaps in Time Series)
+
+```sql
+SELECT toDate(dt) AS day, count() AS events
+FROM events GROUP BY day
+ORDER BY day WITH FILL
+ FROM '2024-01-01' TO '2024-01-31'
+ STEP INTERVAL 1 DAY
+```
+
+## SAMPLE (Random Sampling)
+
+```sql
+SELECT * FROM events SAMPLE 0.1 -- 10% sample
+SELECT * FROM events SAMPLE 10000 -- ~10000 rows
+```
+
+## Nullable Types
+
+ClickHouse distinguishes `Nullable(T)` from `T`. Use explicitly:
+
+```sql
+SELECT CAST(col AS Nullable(Int64))
+SELECT ifNull(nullable_col, 'default')
+SELECT assumeNotNull(col) -- Removes Nullable wrapper
+```
+
+## Tuples
+
+```sql
+SELECT (1, 'Alice', 3.14) AS person
+SELECT person.1, person.2 -- 1-indexed access
+```
+
+## JSON Extraction
+
+```sql
+SELECT JSONExtractString(json_col, 'key')
+SELECT JSONExtractInt(json_col, 'nested', 'value') -- Path as multiple args
+SELECT JSON_VALUE(json_col, '$.key') -- JSONPath syntax
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT *, row_number() OVER (PARTITION BY category ORDER BY amount DESC) AS rn
+ FROM sales
+) WHERE rn = 1
+```
diff --git a/resources/llm/prompts/dialects/databricks.md b/resources/llm/prompts/dialects/databricks.md
new file mode 100644
index 00000000000..cd7e54c73a6
--- /dev/null
+++ b/resources/llm/prompts/dialects/databricks.md
@@ -0,0 +1,127 @@
+# Databricks (Spark SQL) Dialect - Key Differences
+
+Databricks uses **Spark SQL** with Delta Lake extensions.
+
+## Identifier Quoting
+
+Backticks for identifiers. Case-insensitive.
+
+```sql
+SELECT `Column Name`, `select` FROM `my-table`
+```
+
+**Schema-qualified names:** Catalog, schema, and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM `my-catalog`.`my-schema`.`my-table`
+SELECT * FROM my_catalog.my_schema.my_table -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM `my_catalog.my_schema.my_table` -- Looks for table literally named "my_catalog.my_schema.my_table"
+```
+
+## Arrays (0-Indexed)
+
+```sql
+SELECT my_array[0] AS first_element -- 0-indexed!
+SELECT SIZE(arr) -- Length
+SELECT ARRAY_CONTAINS(arr, value)
+SELECT ELEMENT_AT(arr, 1) -- 1-indexed alternative
+```
+
+## EXPLODE (Flatten Array to Rows)
+
+```sql
+-- Inline syntax
+SELECT id, EXPLODE(arr) FROM t
+
+-- LATERAL VIEW syntax
+SELECT id, val FROM t LATERAL VIEW EXPLODE(arr) AS val
+
+-- With position
+SELECT id, pos, val FROM t LATERAL VIEW POSEXPLODE(arr) AS pos, val
+```
+
+## Array Aggregation
+
+```sql
+SELECT COLLECT_LIST(col) FROM t GROUP BY category -- With duplicates
+SELECT COLLECT_SET(col) FROM t GROUP BY category -- Distinct
+SELECT ARRAY_AGG(col) FROM t GROUP BY category -- Same as COLLECT_LIST
+```
+
+## Higher-Order Functions
+
+```sql
+SELECT TRANSFORM(arr, x -> x * 2) -- Map over array
+SELECT FILTER(arr, x -> x > 0) -- Filter array
+SELECT AGGREGATE(arr, 0, (acc, x) -> acc + x) -- Reduce
+SELECT EXISTS(arr, x -> x > 100) -- Any match?
+SELECT FORALL(arr, x -> x > 0) -- All match?
+```
+
+## TRY_CAST and TRY_DIVIDE
+
+```sql
+SELECT TRY_CAST(bad_data AS INT) -- NULL on failure
+SELECT TRY_DIVIDE(numerator, denominator) -- NULL if denom is 0
+```
+
+## IF for Ternary
+
+```sql
+SELECT IF(status = 'active', 'Yes', 'No')
+SELECT NVL(nullable, 'default') -- Also works
+SELECT NVL2(col, 'not null', 'is null')
+```
+
+## Java Date Format Patterns
+
+```sql
+SELECT DATE_FORMAT(order_date, 'yyyy-MM-dd') -- Java SimpleDateFormat
+SELECT TO_DATE('2024-01-15', 'yyyy-MM-dd')
+SELECT TO_TIMESTAMP('2024-01-15 10:30:00', 'yyyy-MM-dd HH:mm:ss')
+```
+
+## JSON Colon Notation
+
+```sql
+SELECT json_col:field -- Top-level
+SELECT json_col:nested.path -- Nested
+SELECT json_col:array[0] -- Array access
+SELECT GET_JSON_OBJECT(json_str, '$.field') -- Alternative
+```
+
+## QUALIFY (Supported)
+
+```sql
+SELECT * FROM sales
+QUALIFY ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) <= 3
+```
+
+## Date Spine with SEQUENCE
+
+```sql
+SELECT EXPLODE(SEQUENCE(DATE'2024-01-01', DATE'2024-12-31', INTERVAL 1 DAY)) AS dt
+```
+
+## COUNT_IF
+
+```sql
+SELECT COUNT_IF(status = 'active') AS active_count
+```
+
+## Delta Lake Time Travel
+
+```sql
+SELECT * FROM my_table VERSION AS OF 5
+SELECT * FROM my_table TIMESTAMP AS OF '2024-01-15 10:00:00'
+```
+
+## String Aggregation
+
+```sql
+SELECT CONCAT_WS(', ', COLLECT_LIST(name))
+FROM t GROUP BY category
+```
diff --git a/resources/llm/prompts/dialects/druid.md b/resources/llm/prompts/dialects/druid.md
new file mode 100644
index 00000000000..71144a979da
--- /dev/null
+++ b/resources/llm/prompts/dialects/druid.md
@@ -0,0 +1,111 @@
+# Apache Druid Dialect - Key Differences
+
+Druid is optimized for real-time analytics and time-series data.
+
+## Critical Limitations
+
+- **No JOINs** between Druid tables (only lookup/broadcast joins)
+- **No RIGHT/FULL OUTER JOINs**
+- **Subqueries only in FROM clause**
+- **No UPDATE or DELETE**
+- **`__time` column required** for most queries
+
+## Identifier Quoting
+
+Double quotes for identifiers. Case-sensitive.
+
+```sql
+SELECT "Column Name" FROM "my-datasource"
+```
+
+**Schema-qualified names:** Schema and datasource are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "druid"."My-Datasource"
+SELECT * FROM druid.my_datasource -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "druid.my_datasource" -- Looks for datasource literally named "druid.my_datasource"
+```
+
+## Always Filter on __time
+
+**CRITICAL: Always filter on `__time` for query performance.**
+
+```sql
+SELECT * FROM events
+WHERE __time >= TIMESTAMP '2024-01-01'
+ AND __time < TIMESTAMP '2024-02-01'
+ AND status = 'active'
+```
+
+## TIME_FLOOR (Preferred over DATE_TRUNC)
+
+Uses ISO 8601 period notation:
+
+```sql
+SELECT TIME_FLOOR(__time, 'P1D') AS day -- P1D = 1 day
+SELECT TIME_FLOOR(__time, 'PT1H') AS hour -- PT1H = 1 hour
+SELECT TIME_FLOOR(__time, 'P1M') AS month -- P1M = 1 month
+SELECT TIME_FLOOR(__time, 'P1D', NULL, 'America/New_York') -- With timezone
+```
+
+**Period notation:** `P1D` (day), `P1W` (week), `P1M` (month), `P1Y` (year), `PT1H` (hour), `PT1M` (minute).
+
+## TIME_SHIFT (Date Arithmetic)
+
+```sql
+SELECT TIME_SHIFT(__time, 'P1D', 1) AS next_day -- Add 1 day
+SELECT TIME_SHIFT(__time, 'P1D', -1) AS prev_day -- Subtract
+SELECT TIME_SHIFT(__time, 'PT1H', 2) AS plus_2h
+```
+
+## LATEST / EARLIEST (Temporal Aggregation)
+
+Druid-specific functions to get first/last value by time:
+
+```sql
+SELECT
+ user_id,
+ LATEST(status) AS current_status,
+ EARLIEST(status) AS first_status,
+ LATEST_BY(amount, __time) AS last_amount
+FROM events
+WHERE __time >= TIMESTAMP '2024-01-01'
+GROUP BY user_id
+```
+
+## APPROX_COUNT_DISTINCT
+
+`COUNT(DISTINCT)` uses approximation by default:
+
+```sql
+SELECT APPROX_COUNT_DISTINCT(user_id) -- HyperLogLog
+SELECT APPROX_COUNT_DISTINCT_DS_HLL(user_id) -- DataSketches HLL
+```
+
+## String Aggregation
+
+```sql
+SELECT STRING_AGG(name, ', ') FROM t GROUP BY category
+SELECT LISTAGG(name, ', ') FROM t GROUP BY category -- Alias
+```
+
+## LOOKUP Function
+
+For joining with dimension/lookup tables:
+
+```sql
+SELECT LOOKUP(country_code, 'country_names') AS country_name
+FROM events
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (ORDER BY amount DESC) AS rn
+ FROM sales WHERE __time >= TIMESTAMP '2024-01-01'
+) WHERE rn = 1
+```
diff --git a/resources/llm/prompts/dialects/h2.md b/resources/llm/prompts/dialects/h2.md
new file mode 100644
index 00000000000..52defca52ed
--- /dev/null
+++ b/resources/llm/prompts/dialects/h2.md
@@ -0,0 +1,112 @@
+# H2 Database Dialect - Key Differences
+
+H2 is a lightweight Java SQL database, commonly used for testing.
+
+## Identifier Quoting
+
+Double quotes for identifiers. **Uppercases unquoted identifiers** by default.
+
+```sql
+SELECT "CamelCase", "select" FROM "My Table"
+SELECT myColumn FROM t -- Actually selects "MYCOLUMN"
+```
+
+**Schema-qualified names:** Schema and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "PUBLIC"."PRODUCTS"
+SELECT * FROM PUBLIC.PRODUCTS -- unquoted is uppercased
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "PUBLIC.PRODUCTS" -- Looks for table literally named "PUBLIC.PRODUCTS"
+```
+
+## Compatibility Modes
+
+H2 can emulate other databases via connection URL:
+
+```
+jdbc:h2:mem:test;MODE=PostgreSQL
+jdbc:h2:mem:test;MODE=MySQL
+```
+
+Available: REGULAR, PostgreSQL, MySQL, Oracle, MSSQLServer, DB2, Derby, HSQLDB, MariaDB.
+
+## SYSTEM_RANGE (Generate Series)
+
+```sql
+SELECT x FROM SYSTEM_RANGE(1, 10) -- 1 to 10
+
+-- Date range
+SELECT DATEADD('DAY', x, DATE '2024-01-01') AS dt
+FROM SYSTEM_RANGE(0, 364)
+```
+
+## CASEWHEN (H2-Specific Ternary)
+
+```sql
+SELECT CASEWHEN(status = 'active', 'Yes', 'No')
+SELECT CASE WHEN status = 'active' THEN 'Yes' ELSE 'No' END -- Standard also works
+```
+
+## Arrays (1-Indexed)
+
+```sql
+SELECT my_array[1] AS first_element -- 1-indexed!
+SELECT CARDINALITY(arr) -- Length
+SELECT ARRAY_CONTAINS(arr, value)
+
+-- Flatten
+SELECT * FROM UNNEST(ARRAY[1, 2, 3])
+```
+
+## Date Functions
+
+```sql
+SELECT DATE_TRUNC('MONTH', order_date)
+SELECT DATEADD('DAY', 7, order_date)
+SELECT DATEDIFF('DAY', start_date, end_date)
+
+-- Java format patterns
+SELECT FORMATDATETIME(order_date, 'yyyy-MM-dd')
+SELECT PARSEDATETIME('2024-01-15', 'yyyy-MM-dd')
+```
+
+## String Aggregation
+
+```sql
+SELECT LISTAGG(name, ', ') FROM t GROUP BY category
+SELECT GROUP_CONCAT(name SEPARATOR ', ') FROM t GROUP BY category -- MySQL mode
+```
+
+## MERGE for Upsert
+
+```sql
+MERGE INTO target_table t
+USING source_table s ON t.id = s.id
+WHEN MATCHED THEN UPDATE SET t.name = s.name
+WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name)
+```
+
+## VALUES (Inline Table)
+
+```sql
+SELECT * FROM (VALUES (1, 'a'), (2, 'b'), (3, 'c')) AS t(id, name)
+```
+
+## JSON (H2 2.x)
+
+```sql
+SELECT JSON_VALUE(json_col, '$.name')
+SELECT json_col.name -- Dot notation
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) AS rn
+ FROM products
+) WHERE rn = 1
+```
diff --git a/resources/llm/prompts/dialects/mysql.md b/resources/llm/prompts/dialects/mysql.md
new file mode 100644
index 00000000000..af0af7faaf9
--- /dev/null
+++ b/resources/llm/prompts/dialects/mysql.md
@@ -0,0 +1,188 @@
+# MySQL Dialect - Key Differences
+
+MySQL 8.0+ syntax. This guide covers only MySQL-specific syntax that differs from standard SQL.
+
+## Identifier Quoting
+
+**CRITICAL: MySQL uses backticks for identifiers.**
+
+| Syntax | Meaning |
+|--------|---------|
+| `` `name` `` | Identifier (column, table) |
+| `'text'` | String literal |
+
+```sql
+SELECT `select`, `order` FROM `my-table`
+```
+
+Use backticks for: reserved words, hyphens, special characters.
+
+**Schema-qualified names:** Database and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM `my-database`.`my-table`
+SELECT * FROM my_database.my_table -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM `my_database.my_table` -- Looks for table literally named "my_database.my_table"
+```
+
+## String Concatenation
+
+**CRITICAL: The `||` operator does NOT work in MySQL. Use CONCAT().**
+
+```sql
+-- CORRECT
+SELECT CONCAT(first_name, ' ', last_name) AS full_name
+SELECT CONCAT_WS(' ', first_name, middle, last_name) -- With separator, skips NULLs
+
+-- WRONG: This does NOT concatenate in MySQL!
+SELECT first_name || ' ' || last_name
+```
+
+## Case-Insensitive by Default
+
+**MySQL string comparisons are case-insensitive by default** (depends on collation).
+
+```sql
+SELECT * FROM t WHERE name LIKE 'john%' -- Matches 'John', 'JOHN', etc.
+SELECT * FROM t WHERE name LIKE BINARY 'John%' -- Force case-sensitive
+SELECT * FROM t WHERE name REGEXP '^[A-Z]' -- Regex (also case-insensitive by default)
+```
+
+## No DATE_TRUNC
+
+MySQL lacks `DATE_TRUNC`. Use `DATE_FORMAT` or `DATE()`:
+
+```sql
+-- Truncate to date
+SELECT DATE(order_datetime)
+
+-- Truncate to month
+SELECT DATE_FORMAT(order_date, '%Y-%m-01')
+
+-- Truncate to year
+SELECT DATE_FORMAT(order_date, '%Y-01-01')
+
+-- Truncate to week (Monday)
+SELECT DATE(order_date - INTERVAL (WEEKDAY(order_date)) DAY)
+```
+
+## Date Differences
+
+```sql
+SELECT DATEDIFF(end_date, start_date) -- Days only (integer)
+SELECT TIMESTAMPDIFF(MONTH, start_date, end_date) -- YEAR, MONTH, DAY, HOUR, MINUTE, SECOND
+SELECT TIMESTAMPDIFF(HOUR, start_datetime, end_datetime)
+```
+
+## Type Casting
+
+**MySQL uses SIGNED/UNSIGNED instead of INTEGER in CAST.**
+
+```sql
+SELECT CAST(string_col AS SIGNED) -- Integer
+SELECT CAST(string_col AS UNSIGNED) -- Unsigned integer
+SELECT CAST(string_col AS DECIMAL(10,2))
+SELECT CAST(string_col AS CHAR)
+SELECT CAST(string_col AS DATE)
+
+-- Implicit conversion tricks
+SELECT string_col + 0 -- String to number
+SELECT CONCAT('', number_col) -- Number to string
+```
+
+## String Aggregation (GROUP_CONCAT)
+
+MySQL uses `GROUP_CONCAT` instead of `STRING_AGG`:
+
+```sql
+SELECT GROUP_CONCAT(name) FROM t GROUP BY category
+SELECT GROUP_CONCAT(name ORDER BY name) FROM t GROUP BY category
+SELECT GROUP_CONCAT(name SEPARATOR '; ') FROM t GROUP BY category
+SELECT GROUP_CONCAT(DISTINCT name ORDER BY name) FROM t GROUP BY category
+```
+
+**Warning**: Default length limit is 1024 characters (`group_concat_max_len`). May silently truncate.
+
+## SUBSTRING_INDEX (Delimiter Parsing)
+
+MySQL-specific function to extract parts of delimited strings:
+
+```sql
+SELECT SUBSTRING_INDEX(csv, ',', 1) -- First element (before 1st comma)
+SELECT SUBSTRING_INDEX(csv, ',', 2) -- Everything before 2nd comma
+SELECT SUBSTRING_INDEX(csv, ',', -1) -- Last element (after last comma)
+SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(csv, ',', 2), ',', -1) -- 2nd element
+```
+
+## Conditional Aggregation
+
+MySQL booleans are 1/0, enabling arithmetic tricks:
+
+```sql
+SELECT
+ SUM(status = 'active') AS active_count, -- Boolean = 1 or 0
+ SUM(IF(type = 'revenue', amount, 0)) AS revenue,
+ COUNT(IF(region = 'US', 1, NULL)) AS us_count -- COUNT ignores NULL
+FROM t
+```
+
+## Integer Division
+
+```sql
+SELECT 10 DIV 3 -- Returns 3 (integer division)
+SELECT 10 / 3 -- Returns 3.3333 (decimal)
+SELECT 10 MOD 3, 10 % 3 -- Modulo (both work)
+```
+
+## JSON Path Syntax
+
+MySQL uses `$.path` syntax:
+
+```sql
+SELECT json_col -> '$.key' -- Returns JSON
+SELECT json_col ->> '$.key' -- Returns unquoted TEXT
+SELECT JSON_EXTRACT(json_col, '$.key') -- Same as ->
+SELECT JSON_EXTRACT(json_col, '$.items[0]') -- Array access (0-indexed)
+```
+
+## NULL Handling
+
+```sql
+SELECT IFNULL(col, 'default') -- MySQL-specific (2 args only)
+SELECT COALESCE(col, 'default') -- Standard (multiple args)
+SELECT NULLIF(col, '')
+```
+
+## Upsert with ON DUPLICATE KEY
+
+```sql
+INSERT INTO t (id, name, count)
+VALUES (1, 'foo', 1)
+ON DUPLICATE KEY UPDATE count = count + 1, name = VALUES(name)
+```
+
+## LIMIT Syntax Variations
+
+```sql
+SELECT * FROM t LIMIT 10 -- First 10 rows
+SELECT * FROM t LIMIT 10 OFFSET 20 -- Skip 20, return 10
+SELECT * FROM t LIMIT 20, 10 -- Alternative: offset, count
+```
+
+## No DISTINCT ON, No FILTER Clause
+
+MySQL lacks PostgreSQL's `DISTINCT ON` and `FILTER`. Use window functions:
+
+```sql
+-- First row per group (MySQL 8.0+)
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY updated_at DESC) AS rn
+ FROM products
+) t WHERE rn = 1
+
+-- Conditional aggregation (use IF instead of FILTER)
+SELECT SUM(IF(status = 'active', amount, 0)) FROM t
+```
diff --git a/resources/llm/prompts/dialects/oracle.md b/resources/llm/prompts/dialects/oracle.md
new file mode 100644
index 00000000000..03826761401
--- /dev/null
+++ b/resources/llm/prompts/dialects/oracle.md
@@ -0,0 +1,197 @@
+# Oracle Dialect - Key Differences
+
+This guide covers only Oracle-specific syntax that differs from standard SQL.
+
+## Identifier Quoting
+
+**CRITICAL: Oracle UPPERCASES unquoted identifiers.**
+
+| Syntax | Meaning |
+|--------|---------|
+| `"Name"` | Identifier (preserves case) |
+| `'text'` | String literal |
+
+```sql
+SELECT myColumn FROM t -- Actually selects "MYCOLUMN"
+SELECT "myColumn" FROM t -- Preserves case
+```
+
+**Schema-qualified names:** Schema and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "MySchema"."MyTable"
+SELECT * FROM HR.EMPLOYEES -- unquoted is uppercased
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "HR.EMPLOYEES" -- Looks for table literally named "HR.EMPLOYEES"
+```
+
+## DUAL Table Required
+
+Oracle requires `FROM` clause. Use `DUAL` for expressions without a table:
+
+```sql
+SELECT SYSDATE FROM DUAL
+SELECT 1 + 1 FROM DUAL
+```
+
+## CONCAT Only Takes 2 Arguments
+
+```sql
+SELECT first_name || ' ' || last_name -- Use || for multiple values
+SELECT CONCAT(CONCAT(first, ' '), last) -- CONCAT only takes 2 args!
+```
+
+## TRUNC for Date Truncation (Not DATE_TRUNC)
+
+```sql
+SELECT TRUNC(order_date) -- To day (removes time)
+SELECT TRUNC(order_date, 'MM') -- To month
+SELECT TRUNC(order_date, 'Q') -- To quarter
+SELECT TRUNC(order_date, 'YYYY') -- To year
+SELECT TRUNC(order_date, 'IW') -- To ISO week (Monday)
+```
+
+## ROWNUM Gotcha
+
+**CRITICAL: `ROWNUM` is applied BEFORE `ORDER BY`.** Always wrap in subquery:
+
+```sql
+-- WRONG: Gets random 10 rows, then sorts
+SELECT * FROM orders WHERE ROWNUM <= 10 ORDER BY order_date DESC
+
+-- CORRECT: Sort first, then limit
+SELECT * FROM (
+ SELECT * FROM orders ORDER BY order_date DESC
+) WHERE ROWNUM <= 10
+
+-- Better: Use FETCH FIRST (12c+)
+SELECT * FROM orders ORDER BY order_date DESC
+FETCH FIRST 10 ROWS ONLY
+```
+
+## Pagination
+
+```sql
+-- 12c+ syntax
+SELECT * FROM orders ORDER BY order_date DESC
+OFFSET 20 ROWS FETCH NEXT 10 ROWS ONLY
+
+-- Pre-12c: Use ROW_NUMBER
+SELECT * FROM (
+ SELECT t.*, ROW_NUMBER() OVER (ORDER BY order_date DESC) rn FROM orders t
+) WHERE rn BETWEEN 21 AND 30
+```
+
+## NVL, NVL2, DECODE
+
+```sql
+SELECT NVL(nullable, 'default') -- Two-arg null coalescing
+SELECT NVL2(col, 'not null', 'is null') -- If NOT NULL then 2nd, else 3rd
+SELECT COALESCE(a, b, c) -- Standard, multiple args
+
+-- DECODE: Oracle-specific CASE that handles NULL
+SELECT DECODE(status, 'A', 'Active', 'I', 'Inactive', 'Unknown')
+SELECT DECODE(col, NULL, 'is null', 'not null') -- Can compare to NULL!
+```
+
+## Date Functions
+
+```sql
+SELECT SYSDATE FROM DUAL -- Current date+time
+SELECT SYSTIMESTAMP FROM DUAL -- With timezone
+
+SELECT order_date + 7 -- Add days directly
+SELECT ADD_MONTHS(order_date, 3) -- Add months (handles month-end)
+SELECT MONTHS_BETWEEN(end_date, start_date) -- Fractional months
+
+SELECT EXTRACT(YEAR FROM order_date)
+SELECT TO_CHAR(order_date, 'YYYY-MM-DD') -- Format
+SELECT TO_CHAR(order_date, 'FMMonth DD') -- FM removes padding
+SELECT TO_DATE('2024-01-15', 'YYYY-MM-DD') -- Parse
+```
+
+## String Aggregation (LISTAGG)
+
+```sql
+SELECT LISTAGG(name, ', ') WITHIN GROUP (ORDER BY name)
+FROM t GROUP BY category
+
+-- DISTINCT requires 19c+
+SELECT LISTAGG(DISTINCT name, ', ') WITHIN GROUP (ORDER BY name)
+FROM t GROUP BY category
+```
+
+## CONNECT BY (Hierarchies and Sequences)
+
+```sql
+-- Generate sequence
+SELECT LEVEL AS n FROM DUAL CONNECT BY LEVEL <= 100
+
+-- Date range
+SELECT DATE '2024-01-01' + LEVEL - 1 AS dt
+FROM DUAL CONNECT BY LEVEL <= 365
+
+-- Hierarchical query
+SELECT id, name, LEVEL, SYS_CONNECT_BY_PATH(name, '/')
+FROM employees
+START WITH manager_id IS NULL
+CONNECT BY PRIOR id = manager_id
+```
+
+## KEEP FIRST/LAST (First Row Per Group)
+
+```sql
+-- Oracle-specific aggregate
+SELECT category,
+ MAX(name) KEEP (DENSE_RANK FIRST ORDER BY updated_at DESC) AS latest_name
+FROM products GROUP BY category
+
+-- Standard approach
+SELECT * FROM (
+ SELECT t.*, ROW_NUMBER() OVER (PARTITION BY category ORDER BY updated_at DESC) rn
+ FROM products t
+) WHERE rn = 1
+```
+
+## JSON Functions (12c+)
+
+```sql
+SELECT JSON_VALUE(json_col, '$.key') -- Scalar value
+SELECT JSON_QUERY(json_col, '$.object') -- Object/array
+SELECT JSON_VALUE(json_col, '$.items[0].name') -- Array access
+SELECT JSON_EXISTS(json_col, '$.key') -- Boolean check
+
+-- JSON_TABLE: parse to rows
+SELECT jt.* FROM t,
+JSON_TABLE(t.json_col, '$.items[*]'
+ COLUMNS (id NUMBER PATH '$.id', name VARCHAR2(100) PATH '$.name')
+) jt
+```
+
+## MERGE for Upsert
+
+```sql
+MERGE INTO target_table t
+USING source_table s ON (t.id = s.id)
+WHEN MATCHED THEN UPDATE SET t.name = s.name
+WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name);
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT t.*, ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) rn
+ FROM sales t
+) WHERE rn = 1
+```
+
+## Substring Position
+
+```sql
+SELECT INSTR(name, 'sub') -- 1-indexed, 0 if not found
+SELECT INSTR(name, 'sub', 1, 2) -- 2nd occurrence
+SELECT SUBSTR(name, 1, 3) -- Substring (1-indexed)
+```
diff --git a/resources/llm/prompts/dialects/postgresql.md b/resources/llm/prompts/dialects/postgresql.md
new file mode 100644
index 00000000000..d6c38cccac0
--- /dev/null
+++ b/resources/llm/prompts/dialects/postgresql.md
@@ -0,0 +1,179 @@
+# PostgreSQL Dialect - Key Differences
+
+PostgreSQL is highly standards-compliant. This guide covers only PostgreSQL-specific syntax that differs from standard SQL.
+
+## Identifier Quoting
+
+**CRITICAL: PostgreSQL uses double quotes for identifiers and lowercases unquoted identifiers.**
+
+| Syntax | Meaning |
+|--------|---------|
+| `"Name"` | Identifier (preserves case) |
+| `'text'` | String literal |
+
+```sql
+-- Unquoted identifiers are LOWERCASED
+SELECT MyColumn FROM t -- Actually selects "mycolumn"
+
+-- Use double quotes to preserve case or for reserved words
+SELECT "MyColumn", "order" FROM "My Table"
+```
+
+Use double quotes for: mixed-case names, reserved words, special characters, spaces.
+
+**Schema-qualified names:** Schema and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "MySchema"."MyTable"
+SELECT * FROM public.orders -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "public.orders" -- Looks for table literally named "public.orders"
+```
+
+## Type Casting Shorthand
+
+PostgreSQL's `::` operator is concise alternative to `CAST()`:
+
+```sql
+SELECT '123'::INTEGER, '2024-01-15'::DATE, amount::TEXT
+SELECT CAST('123' AS INTEGER) -- Standard syntax also works
+```
+
+## Case-Insensitive Matching (ILIKE)
+
+PostgreSQL-specific operator for case-insensitive pattern matching:
+
+```sql
+SELECT * FROM t WHERE name ILIKE 'john%' -- Case-insensitive
+SELECT * FROM t WHERE name LIKE 'John%' -- Case-sensitive (standard)
+
+-- POSIX regex operators
+SELECT * FROM t WHERE name ~ '^[A-Z]' -- Case-sensitive regex
+SELECT * FROM t WHERE name ~* '^[a-z]' -- Case-insensitive regex
+```
+
+## DISTINCT ON (First Row Per Group)
+
+PostgreSQL-specific clause - cleaner than window functions for "first row per group":
+
+```sql
+-- Get most recent order per customer
+SELECT DISTINCT ON (customer_id) *
+FROM orders
+ORDER BY customer_id, created_at DESC
+```
+
+**Important**: `ORDER BY` must start with the `DISTINCT ON` columns.
+
+## Arrays (1-Indexed)
+
+PostgreSQL arrays are **1-indexed**, unlike BigQuery (0-indexed).
+
+```sql
+-- Array access
+SELECT my_array[1] AS first_element -- 1-indexed!
+
+-- Array operators
+SELECT arr @> ARRAY[1, 2] -- Contains
+SELECT arr && ARRAY[1, 2] -- Overlaps
+SELECT value = ANY(arr) -- Membership test
+
+-- Flatten to rows
+SELECT id, elem FROM t, UNNEST(t.arr) AS elem
+
+-- Aggregation
+SELECT ARRAY_AGG(col ORDER BY sort_col) FROM t
+```
+
+## JSON/JSONB Operators
+
+PostgreSQL uses arrow operators (not function syntax like BigQuery):
+
+```sql
+-- Extraction (-> returns JSON, ->> returns TEXT)
+SELECT json_col -> 'key' -- Returns JSON
+SELECT json_col ->> 'key' -- Returns TEXT
+SELECT json_col -> 0 -- Array access
+SELECT json_col #>> '{nested,path}' -- Path extraction as TEXT
+
+-- JSONB-only operators
+SELECT json_col @> '{"key": "value"}' -- Contains
+SELECT json_col ? 'key' -- Has key
+SELECT json_col ?| ARRAY['a', 'b'] -- Has any key
+SELECT json_col ?& ARRAY['a', 'b'] -- Has all keys
+```
+
+## FILTER Clause (Conditional Aggregation)
+
+PostgreSQL-specific clause - cleaner than CASE WHEN:
+
+```sql
+SELECT
+ COUNT(*) FILTER (WHERE status = 'active') AS active_count,
+ SUM(amount) FILTER (WHERE type = 'revenue') AS revenue
+FROM t
+```
+
+## NULL-Safe Comparison
+
+```sql
+SELECT * FROM t WHERE col IS DISTINCT FROM other_col -- NULL-safe <>
+SELECT * FROM t WHERE col IS NOT DISTINCT FROM other_col -- NULL-safe =
+```
+
+## RETURNING Clause
+
+Get affected rows from write operations:
+
+```sql
+INSERT INTO t (col) VALUES ('val') RETURNING *
+UPDATE t SET col = 'new' WHERE id = 1 RETURNING id, col
+DELETE FROM t WHERE id = 1 RETURNING *
+```
+
+## Upsert with ON CONFLICT
+
+```sql
+INSERT INTO t (id, name, count)
+VALUES (1, 'foo', 1)
+ON CONFLICT (id) DO UPDATE SET count = t.count + 1
+-- Or: ON CONFLICT DO NOTHING
+```
+
+## GENERATE_SERIES
+
+Generate sequences of numbers or dates:
+
+```sql
+SELECT GENERATE_SERIES(1, 10)
+SELECT GENERATE_SERIES(1, 10, 2) -- Step of 2
+SELECT GENERATE_SERIES('2024-01-01'::DATE, '2024-12-31'::DATE, '1 month')
+```
+
+## LATERAL Joins
+
+Correlated subquery in FROM clause:
+
+```sql
+SELECT u.*, recent.*
+FROM users u,
+LATERAL (
+ SELECT * FROM orders WHERE user_id = u.id ORDER BY created_at DESC LIMIT 3
+) AS recent
+```
+
+## String Concatenation
+
+```sql
+SELECT first_name || ' ' || last_name -- Use || operator
+SELECT SPLIT_PART(csv, ',', 1) -- Extract nth element (1-indexed)
+```
+
+## Date Truncation
+
+```sql
+SELECT DATE_TRUNC('month', order_date) -- year, quarter, month, week, day, hour
+SELECT DATE_TRUNC('week', order_date) -- Truncates to Monday
+```
diff --git a/resources/llm/prompts/dialects/redshift.md b/resources/llm/prompts/dialects/redshift.md
new file mode 100644
index 00000000000..3b820bea11c
--- /dev/null
+++ b/resources/llm/prompts/dialects/redshift.md
@@ -0,0 +1,133 @@
+# Redshift Dialect - Key Differences
+
+Redshift is based on **PostgreSQL 8.0.2** with significant modifications. Many modern PostgreSQL features are missing.
+
+## Identifier Quoting
+
+Like PostgreSQL: double quotes for identifiers, unquoted names are lowercased.
+
+```sql
+SELECT "order", "group" FROM my_table -- Reserved words need quotes
+SELECT order_date FROM my_table -- Unquoted: lowercased
+```
+
+**Schema-qualified names:** Schema and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "MySchema"."MyTable"
+SELECT * FROM public.orders -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "public.orders" -- Looks for table literally named "public.orders"
+```
+
+## LEN Not LENGTH
+
+```sql
+SELECT LEN(name) -- CORRECT
+SELECT LENGTH(name) -- WRONG: doesn't exist
+```
+
+## No IF/IFF - Use CASE or DECODE
+
+```sql
+-- No ternary function, use CASE
+SELECT CASE WHEN status = 'active' THEN 'Yes' ELSE 'No' END
+
+-- DECODE for switch/case style
+SELECT DECODE(status, 'A', 'Active', 'I', 'Inactive', 'Unknown')
+
+-- DECODE handles NULL specially
+SELECT DECODE(col, NULL, 'N/A', col)
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+-- Must wrap in subquery to filter on window functions
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) AS rn
+ FROM sales
+) WHERE rn = 1
+```
+
+## JSON Functions (Verbose, No Operators)
+
+Redshift has no JSON type or operator syntax. Uses verbose function names:
+
+```sql
+SELECT JSON_EXTRACT_PATH_TEXT(json_col, 'field') -- Top-level
+SELECT JSON_EXTRACT_PATH_TEXT(json_col, 'nested', 'field') -- Nested (multiple args)
+SELECT JSON_EXTRACT_ARRAY_ELEMENT_TEXT(json_col, 0) -- Array element (0-indexed)
+SELECT JSON_ARRAY_LENGTH(json_col)
+SELECT IS_VALID_JSON(col)
+```
+
+## No TRY_CAST
+
+Invalid casts raise errors. Validate data first or use CASE:
+
+```sql
+SELECT CASE WHEN col ~ '^[0-9]+$' THEN col::INTEGER ELSE NULL END
+```
+
+## GETDATE Not NOW
+
+```sql
+SELECT GETDATE() -- Current timestamp (preferred)
+SELECT CURRENT_TIMESTAMP -- Also works
+SELECT NOW() -- May not work as expected
+```
+
+## APPROXIMATE COUNT (Note the Space)
+
+```sql
+SELECT APPROXIMATE COUNT(DISTINCT user_id) -- HyperLogLog estimate
+-- Note: space between APPROXIMATE and COUNT
+```
+
+## NULL Handling
+
+```sql
+SELECT NVL(nullable, 'default') -- Two-arg (like COALESCE)
+SELECT NVL2(col, 'not null', 'is null') -- Three-arg conditional
+SELECT NULLIF(col, '')
+-- No IFNULL(), use NVL() or COALESCE()
+```
+
+## String Aggregation (LISTAGG)
+
+```sql
+SELECT LISTAGG(name, ', ') WITHIN GROUP (ORDER BY name)
+FROM t GROUP BY category
+```
+
+## Type Casting
+
+```sql
+SELECT col::VARCHAR -- Shorthand (like PostgreSQL)
+SELECT col::DECIMAL(10,2)
+SELECT CAST(col AS INTEGER)
+```
+
+## Date Functions
+
+```sql
+SELECT DATE_TRUNC('month', order_date)
+SELECT DATEADD(day, 7, order_date)
+SELECT DATEDIFF(day, start_date, end_date) -- end - start
+SELECT EXTRACT(year FROM dt)
+SELECT CHARINDEX('search', s) -- Find substring position (0 if not found)
+```
+
+## Key Limitations vs PostgreSQL
+
+Redshift lacks many PostgreSQL features:
+- **No arrays** or array functions
+- **No LATERAL** joins (use correlated subqueries)
+- **No RETURNING** clause on INSERT/UPDATE/DELETE
+- **No ON CONFLICT** / UPSERT (use MERGE in recent versions)
+- **No GENERATE_SERIES** (use recursive CTE or stl_scan trick)
+- **No regex capture groups** in REGEXP_SUBSTR
+- **Limited JSON** (stored as VARCHAR, verbose functions)
diff --git a/resources/llm/prompts/dialects/snowflake.md b/resources/llm/prompts/dialects/snowflake.md
new file mode 100644
index 00000000000..f47ed9144a9
--- /dev/null
+++ b/resources/llm/prompts/dialects/snowflake.md
@@ -0,0 +1,171 @@
+# Snowflake Dialect - Key Differences
+
+This guide covers only Snowflake-specific syntax that differs from standard SQL.
+
+## Identifier Quoting
+
+**CRITICAL: Snowflake UPPERCASES unquoted identifiers (opposite of PostgreSQL).**
+
+| Syntax | Meaning |
+|--------|---------|
+| `"Name"` | Identifier (preserves case) |
+| `'text'` | String literal |
+
+```sql
+-- Unquoted identifiers are UPPERCASED
+SELECT myColumn FROM t -- Actually selects "MYCOLUMN"
+
+-- Use double quotes to preserve case
+SELECT "myColumn", "order" FROM "My Table"
+```
+
+**Schema-qualified names:** Database, schema, and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "MyDatabase"."MySchema"."MyTable"
+SELECT * FROM MY_DB.MY_SCHEMA.MY_TABLE -- unquoted is uppercased
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "MY_DB.MY_SCHEMA.MY_TABLE" -- Looks for table literally named "MY_DB.MY_SCHEMA.MY_TABLE"
+```
+
+## IFF Not IF
+
+**CRITICAL: Snowflake uses `IFF()`, not `IF()` for ternary expressions.**
+
+```sql
+SELECT IFF(status = 'active', 'Yes', 'No') AS is_active -- CORRECT
+SELECT IF(status = 'active', 'Yes', 'No') -- WRONG: syntax error
+```
+
+## JSON/VARIANT Colon Notation
+
+Snowflake uses colon `:` for JSON access (not arrow operators like PostgreSQL):
+
+```sql
+-- Colon notation (returns VARIANT)
+SELECT json_col:name -- Top-level key
+SELECT json_col:address.city -- Nested path
+SELECT json_col:items[0] -- Array access (0-indexed)
+SELECT json_col['key-with-dashes'] -- Bracket for special chars
+
+-- Cast to get typed value
+SELECT json_col:name::VARCHAR
+SELECT json_col:age::INTEGER
+
+-- JSON null vs SQL NULL
+SELECT * FROM t WHERE IS_NULL_VALUE(json_col:field) -- JSON null literal
+SELECT * FROM t WHERE json_col:field IS NULL -- Missing key or SQL NULL
+```
+
+## FLATTEN (Unnest Arrays/Objects)
+
+Snowflake uses `FLATTEN` instead of `UNNEST`. Requires `LATERAL` and `=>` syntax:
+
+```sql
+-- Flatten array to rows
+SELECT t.id, f.value, f.index
+FROM my_table t, LATERAL FLATTEN(input => t.arr_col) f
+
+-- Flatten nested JSON array
+SELECT t.id, f.value:name::VARCHAR AS name
+FROM my_table t, LATERAL FLATTEN(input => t.json_col:items) f
+
+-- Flatten with path
+SELECT f.value
+FROM my_table t, LATERAL FLATTEN(input => t.json_col, path => 'nested.array') f
+```
+
+## Arrays (0-Indexed)
+
+Snowflake arrays are **0-indexed** (like BigQuery, unlike PostgreSQL).
+
+```sql
+SELECT arr[0] AS first_element -- 0-indexed!
+SELECT ARRAY_CONSTRUCT(1, 2, 3)
+SELECT ARRAY_SIZE(arr)
+
+-- ARRAY_CONTAINS requires VARIANT type for the value
+SELECT ARRAY_CONTAINS(value::VARIANT, arr) -- Note: cast value to VARIANT
+```
+
+## Safe Division
+
+```sql
+SELECT DIV0(numerator, denominator) -- Returns 0 if denom is 0
+SELECT DIV0NULL(numerator, denominator) -- Returns NULL if denom is 0
+```
+
+## QUALIFY (Filter on Window Results)
+
+Like BigQuery, Snowflake supports `QUALIFY`:
+
+```sql
+-- Top N per group
+SELECT * FROM sales
+QUALIFY ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) <= 3
+
+-- Deduplicate
+SELECT * FROM t
+QUALIFY ROW_NUMBER() OVER (PARTITION BY id ORDER BY updated_at DESC) = 1
+```
+
+## COUNT_IF (Conditional Counting)
+
+```sql
+SELECT
+ COUNT_IF(status = 'active') AS active_count,
+ SUM(IFF(type = 'revenue', amount, 0)) AS revenue
+FROM t
+```
+
+## NULL Handling
+
+```sql
+SELECT NVL(nullable, 'default') -- Two-arg (like COALESCE)
+SELECT IFNULL(nullable, 'default') -- Alias for NVL
+SELECT NVL2(col, 'not null', 'is null') -- Three-arg: if NOT NULL then... else...
+SELECT ZEROIFNULL(num) -- NULL -> 0
+SELECT NULLIFZERO(num) -- 0 -> NULL
+```
+
+## Type Casting
+
+```sql
+SELECT col::VARCHAR -- Shorthand (like PostgreSQL)
+SELECT col::NUMBER(10,2)
+SELECT TRY_CAST(col AS INTEGER) -- NULL on failure (like BigQuery's SAFE_CAST)
+SELECT TRY_TO_DATE(str, 'YYYY-MM-DD') -- NULL on parse failure
+```
+
+## String Aggregation (LISTAGG)
+
+```sql
+SELECT LISTAGG(name, ', ') WITHIN GROUP (ORDER BY name)
+FROM t GROUP BY category
+```
+
+## Date Spine Generation
+
+```sql
+SELECT DATEADD(day, seq4(), '2024-01-01'::DATE) AS dt
+FROM TABLE(GENERATOR(ROWCOUNT => 365))
+```
+
+## SAMPLE (Random Rows)
+
+```sql
+SELECT * FROM t SAMPLE (1000 ROWS) -- Fixed row count
+SELECT * FROM t SAMPLE (10) -- 10% of rows
+```
+
+## String Functions
+
+```sql
+SELECT SPLIT(csv, ',') -- Returns ARRAY
+SELECT SPLIT_PART(csv, ',', 1) -- Returns element (1-indexed)
+SELECT STARTSWITH(s, 'prefix') -- Boolean
+SELECT ENDSWITH(s, 'suffix')
+SELECT CONTAINS(s, 'search')
+```
diff --git a/resources/llm/prompts/dialects/sqlite.md b/resources/llm/prompts/dialects/sqlite.md
new file mode 100644
index 00000000000..1a3c240e4c4
--- /dev/null
+++ b/resources/llm/prompts/dialects/sqlite.md
@@ -0,0 +1,153 @@
+# SQLite Dialect - Key Differences
+
+SQLite is a lightweight embedded database with unique constraints.
+
+## Critical Limitations
+
+- **No RIGHT JOIN or FULL OUTER JOIN** - use reversed LEFT JOIN
+- **No native BOOLEAN** - uses INTEGER (0=false, 1=true)
+- **Dynamic typing** - columns can store any type
+
+## Identifier Quoting
+
+Double quotes or backticks both work:
+
+```sql
+SELECT "Column Name", `select` FROM "My Table"
+```
+
+**Schema-qualified names:** When using attached databases, database and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "attached_db"."My Table"
+SELECT * FROM main.orders -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "main.orders" -- Looks for table literally named "main.orders"
+```
+
+## LIKE vs GLOB
+
+```sql
+SELECT * FROM t WHERE name LIKE 'A%' -- Case-INSENSITIVE for ASCII!
+SELECT * FROM t WHERE name GLOB 'A*' -- Case-sensitive, uses * and ?
+```
+
+## No DATE_TRUNC - Use Date Modifiers
+
+```sql
+SELECT DATE(order_date) -- Truncate to date
+SELECT DATE(order_date, 'start of month') -- To month
+SELECT DATE(order_date, 'start of year') -- To year
+SELECT DATE(order_date, 'weekday 0', '-7 days') -- To week (Sunday)
+```
+
+## Date Arithmetic with Modifiers
+
+```sql
+SELECT DATE(order_date, '+7 days')
+SELECT DATE(order_date, '-1 month')
+SELECT DATE(order_date, '+1 year', '-1 day') -- Chain modifiers
+SELECT DATETIME(ts, '+2 hours', '+30 minutes')
+```
+
+## No DATEDIFF - Use JULIANDAY
+
+```sql
+SELECT JULIANDAY(end_date) - JULIANDAY(start_date) AS days_diff
+SELECT CAST((JULIANDAY(end_date) - JULIANDAY(start_date)) AS INTEGER)
+```
+
+## Date Extraction with STRFTIME
+
+```sql
+SELECT STRFTIME('%Y', order_date) AS year -- Returns TEXT '2024'
+SELECT STRFTIME('%m', order_date) AS month -- '01'
+SELECT STRFTIME('%w', order_date) AS dow -- 0=Sunday, 6=Saturday
+SELECT STRFTIME('%s', 'now') AS unix_timestamp
+```
+
+## Current Date/Time
+
+```sql
+SELECT DATE('now') -- 'YYYY-MM-DD'
+SELECT DATETIME('now') -- 'YYYY-MM-DD HH:MM:SS'
+SELECT DATETIME('now', 'localtime') -- Local timezone
+```
+
+## String Aggregation (GROUP_CONCAT)
+
+```sql
+SELECT GROUP_CONCAT(name) FROM t GROUP BY category
+SELECT GROUP_CONCAT(name, '; ') FROM t GROUP BY category -- Custom separator
+SELECT GROUP_CONCAT(DISTINCT name) FROM t GROUP BY category
+```
+
+## TOTAL vs SUM
+
+```sql
+SELECT SUM(amount) -- Returns NULL for empty set
+SELECT TOTAL(amount) -- Returns 0.0 for empty set
+```
+
+## IIF for Ternary (3.32+)
+
+```sql
+SELECT IIF(status = 'active', 'Yes', 'No')
+SELECT IIF(col IS NULL, 'N/A', col)
+```
+
+## JSON Operators (3.38+)
+
+```sql
+SELECT JSON_EXTRACT(json_col, '$.key')
+SELECT json_col -> '$.key' -- Returns JSON
+SELECT json_col ->> '$.key' -- Returns TEXT
+SELECT JSON_EXTRACT(json_col, '$.items[0]')
+```
+
+## Upsert (3.24+)
+
+```sql
+INSERT INTO t (id, name, count) VALUES (1, 'foo', 1)
+ON CONFLICT(id) DO UPDATE SET count = count + 1, name = excluded.name
+
+INSERT OR REPLACE INTO t (id, name) VALUES (1, 'new') -- Replace on conflict
+INSERT OR IGNORE INTO t (id, name) VALUES (1, 'foo') -- Skip on conflict
+```
+
+## Boolean Handling
+
+```sql
+SELECT * FROM t WHERE active = 1 -- Explicit check
+SELECT * FROM t WHERE active -- Truthy (non-zero)
+SELECT * FROM t WHERE NOT active -- Falsy (zero or NULL)
+```
+
+## No RIGHT JOIN - Reverse It
+
+```sql
+-- Instead of: SELECT * FROM a RIGHT JOIN b ON a.id = b.id
+SELECT * FROM b LEFT JOIN a ON a.id = b.id
+```
+
+## Date Series with Recursive CTE
+
+```sql
+WITH RECURSIVE dates(dt) AS (
+ SELECT '2024-01-01'
+ UNION ALL
+ SELECT DATE(dt, '+1 day') FROM dates WHERE dt < '2024-12-31'
+)
+SELECT * FROM dates
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY updated_at DESC) AS rn
+ FROM products
+) WHERE rn = 1
+```
diff --git a/resources/llm/prompts/dialects/sqlserver.md b/resources/llm/prompts/dialects/sqlserver.md
new file mode 100644
index 00000000000..df073730115
--- /dev/null
+++ b/resources/llm/prompts/dialects/sqlserver.md
@@ -0,0 +1,179 @@
+# SQL Server (T-SQL) Dialect - Key Differences
+
+This guide covers only T-SQL-specific syntax that differs from standard SQL.
+
+## Identifier Quoting
+
+**CRITICAL: SQL Server uses square brackets for identifiers.**
+
+| Syntax | Meaning |
+|--------|---------|
+| `[Name]` | Identifier (column, table) |
+| `'text'` | String literal |
+
+```sql
+SELECT [Column Name], [select], [order] FROM [My Table]
+```
+
+Double quotes also work if `QUOTED_IDENTIFIER` is ON (default).
+
+**Schema-qualified names:** Database, schema, and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM [MyDatabase].[dbo].[My Table]
+SELECT * FROM dbo.orders -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM [dbo.orders] -- Looks for table literally named "dbo.orders"
+```
+
+## No LIMIT - Use TOP or OFFSET FETCH
+
+**CRITICAL: SQL Server has no `LIMIT` clause.**
+
+```sql
+-- First N rows
+SELECT TOP 10 * FROM orders ORDER BY order_date DESC
+SELECT TOP 10 PERCENT * FROM orders
+
+-- Pagination (SQL 2012+, requires ORDER BY)
+SELECT * FROM orders
+ORDER BY order_date DESC
+OFFSET 20 ROWS
+FETCH NEXT 10 ROWS ONLY
+```
+
+## String Concatenation
+
+`+` operator propagates NULL. Use `CONCAT()` for NULL-safety:
+
+```sql
+SELECT first_name + ' ' + last_name -- NULL if any part is NULL!
+SELECT CONCAT(first_name, ' ', last_name) -- NULL becomes empty string
+SELECT CONCAT_WS(' ', first_name, middle, last) -- With separator (SQL 2017+)
+```
+
+## IIF for Ternary (Not IF)
+
+```sql
+SELECT IIF(status = 'active', 'Yes', 'No') -- SQL 2012+
+SELECT CASE WHEN status = 'active' THEN 'Yes' ELSE 'No' END
+```
+
+## DATETRUNC is 2022+ Only
+
+For older versions, use workarounds:
+
+```sql
+-- SQL Server 2022+
+SELECT DATETRUNC(month, order_date)
+
+-- Older versions
+SELECT CAST(order_date AS DATE) -- To day
+SELECT DATEFROMPARTS(YEAR(order_date), MONTH(order_date), 1) -- To month
+SELECT DATEFROMPARTS(YEAR(order_date), 1, 1) -- To year
+```
+
+## Date Functions
+
+```sql
+SELECT GETDATE() -- Current timestamp
+SELECT DATEADD(day, 7, order_date) -- Add interval
+SELECT DATEDIFF(day, start_date, end_date) -- Difference
+SELECT YEAR(dt), MONTH(dt), DAY(dt) -- Extract parts
+SELECT DATEPART(weekday, dt) -- 1=Sunday (depends on DATEFIRST)
+SELECT DATENAME(month, dt) -- 'January'
+
+-- Formatting with style codes
+SELECT CONVERT(VARCHAR, order_date, 23) -- 'YYYY-MM-DD' (style 23)
+SELECT FORMAT(order_date, 'yyyy-MM-dd') -- .NET format strings
+```
+
+## LEN Not LENGTH
+
+```sql
+SELECT LEN(name) -- Character count (excludes trailing spaces)
+SELECT DATALENGTH(name) -- Byte count
+```
+
+## NULL Handling
+
+```sql
+SELECT ISNULL(nullable, 'default') -- Two-arg only (T-SQL specific)
+SELECT COALESCE(a, b, c) -- Multiple args (standard)
+SELECT NULLIF(col, '')
+```
+
+## TRY_CAST / TRY_CONVERT
+
+Returns NULL on failure instead of error:
+
+```sql
+SELECT TRY_CAST('invalid' AS INT) -- Returns NULL
+SELECT TRY_CONVERT(INT, 'invalid') -- Returns NULL
+```
+
+## String Aggregation (SQL 2017+)
+
+```sql
+SELECT STRING_AGG(name, ', ')
+FROM t GROUP BY category
+
+SELECT STRING_AGG(name, ', ') WITHIN GROUP (ORDER BY name)
+FROM t GROUP BY category
+```
+
+## STRING_SPLIT Returns Table
+
+```sql
+SELECT value FROM STRING_SPLIT('a,b,c', ',') -- Returns table, not array
+```
+
+## JSON Functions
+
+```sql
+SELECT JSON_VALUE(json_col, '$.key') -- Scalar as NVARCHAR
+SELECT JSON_QUERY(json_col, '$.object') -- Object/array as JSON string
+SELECT JSON_VALUE(json_col, '$.items[0].name') -- Nested access
+SELECT ISJSON(json_col) -- Returns 1 or 0
+
+-- Parse JSON to rows
+SELECT * FROM OPENJSON(json_col, '$.items')
+ WITH (id INT '$.id', name NVARCHAR(100) '$.name')
+```
+
+## CROSS APPLY (SQL Server's LATERAL)
+
+```sql
+SELECT u.*, recent.*
+FROM users u
+CROSS APPLY (
+ SELECT TOP 3 * FROM orders WHERE user_id = u.id ORDER BY created_at DESC
+) recent
+```
+
+## MERGE for Upsert
+
+```sql
+MERGE INTO target_table AS t
+USING source_table AS s ON t.id = s.id
+WHEN MATCHED THEN UPDATE SET t.name = s.name
+WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name);
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) AS rn
+ FROM sales
+) t WHERE rn = 1
+```
+
+## CHARINDEX for Substring Position
+
+```sql
+SELECT CHARINDEX('search', name) -- 1-indexed, 0 if not found
+SELECT PATINDEX('%pattern%', name) -- Pattern matching
+```
diff --git a/resources/llm/prompts/dialects/vertica.md b/resources/llm/prompts/dialects/vertica.md
new file mode 100644
index 00000000000..cee5e901c85
--- /dev/null
+++ b/resources/llm/prompts/dialects/vertica.md
@@ -0,0 +1,121 @@
+# Vertica Dialect - Key Differences
+
+Vertica is a columnar analytics database derived from PostgreSQL, but with differences.
+
+## Identifier Quoting
+
+Like PostgreSQL: double quotes, lowercases unquoted identifiers.
+
+```sql
+SELECT "CamelCase" FROM t -- Preserves case
+SELECT myColumn FROM t -- Becomes "mycolumn"
+```
+
+**Schema-qualified names:** Schema and table are SEPARATE identifiers. Quote each part individually if needed:
+
+```sql
+-- CORRECT: each identifier quoted separately
+SELECT * FROM "MySchema"."MyTable"
+SELECT * FROM public.orders -- unquoted if no special chars
+
+-- WRONG: quoting the whole path as one identifier
+SELECT * FROM "public.orders" -- Looks for table literally named "public.orders"
+```
+
+## Arrays (0-Indexed, Unlike PostgreSQL)
+
+**CRITICAL: Vertica arrays are 0-indexed, unlike PostgreSQL's 1-indexed.**
+
+```sql
+SELECT my_array[0] AS first_element -- 0-indexed!
+SELECT ARRAY_LENGTH(arr, 1)
+SELECT ARRAY_CONTAINS(arr, value)
+
+-- Flatten array
+SELECT t.id, elem FROM t, EXPLODE(t.arr) AS elem
+```
+
+## PostgreSQL Casting Shorthand
+
+```sql
+SELECT '123'::INT, '2024-01-15'::DATE, 123::VARCHAR
+```
+
+## ILIKE (Case-Insensitive LIKE)
+
+Like PostgreSQL, Vertica supports ILIKE:
+
+```sql
+SELECT * FROM t WHERE name ILIKE 'john%' -- Case-insensitive
+```
+
+## ZEROIFNULL / NULLIFZERO
+
+Vertica-specific convenience functions:
+
+```sql
+SELECT ZEROIFNULL(nullable_num) -- NULL -> 0
+SELECT NULLIFZERO(num_col) -- 0 -> NULL
+```
+
+## LISTAGG (String Aggregation)
+
+```sql
+SELECT LISTAGG(name, ', ') FROM t GROUP BY category
+```
+
+## APPROXIMATE_COUNT_DISTINCT
+
+```sql
+SELECT APPROXIMATE_COUNT_DISTINCT(user_id) -- HyperLogLog
+```
+
+## TIMESERIES (Gap Filling)
+
+Vertica-specific clause for time-series gap filling:
+
+```sql
+SELECT slice_time, TS_LAST_VALUE(value) AS last_val
+FROM metrics
+TIMESERIES slice_time AS '1 hour'
+ OVER (PARTITION BY device_id ORDER BY ts)
+```
+
+## RATIO_TO_REPORT
+
+Window function for percentage of group total:
+
+```sql
+SELECT category, amount,
+ RATIO_TO_REPORT(amount) OVER (PARTITION BY category) AS pct
+FROM sales
+```
+
+## Date Functions
+
+Like PostgreSQL:
+
+```sql
+SELECT DATE_TRUNC('month', order_date)
+SELECT order_date + INTERVAL '7 days'
+SELECT DATEDIFF('day', start_date, end_date)
+SELECT TO_CHAR(order_date, 'YYYY-MM-DD')
+```
+
+## MERGE for Upsert
+
+```sql
+MERGE INTO target_table t
+USING source_table s ON t.id = s.id
+WHEN MATCHED THEN UPDATE SET name = s.name
+WHEN NOT MATCHED THEN INSERT (id, name) VALUES (s.id, s.name)
+```
+
+## No QUALIFY - Use Subquery
+
+```sql
+SELECT * FROM (
+ SELECT *, ROW_NUMBER() OVER (PARTITION BY category ORDER BY amount DESC) AS rn
+ FROM sales
+) t WHERE rn = 1
+```
diff --git a/resources/llm/prompts/sql-generation-system.mustache b/resources/llm/prompts/sql-generation-system.mustache
new file mode 100644
index 00000000000..f4480d815a5
--- /dev/null
+++ b/resources/llm/prompts/sql-generation-system.mustache
@@ -0,0 +1,44 @@
+You are a SQL query generator for a {{dialect}} database.
+
+Your task is to convert natural language questions into valid SQL queries.
+
+## Available Tables
+
+{{{schema}}}
+
+## Instructions
+
+- Use only the tables and columns shown above
+- Always use fully qualified table names (include schema/catalog if present in the DDL)
+- If the request is ambiguous, make reasonable assumptions
+- If the request cannot be fulfilled, return an explanatory message as the sql value
+
+## Aggregation with JOINs
+
+When counting or summing across joined tables, be aware of row multiplication:
+
+- JOINs can produce multiple rows per entity (e.g., one order with 3 line items = 3 rows)
+- Use `COUNT(DISTINCT ...)` when counting entities from a table that might have multiple matches
+- Use `COUNT(...)` only when you genuinely want to count all matched rows
+
+{{#current_time}}
+## Current Context
+
+Current date and time: {{current_time}}
+{{/current_time}}
+
+{{#source_sql}}
+## Existing Query
+
+The user is viewing this SQL query:
+
+```sql
+{{{source_sql}}}
+```
+{{/source_sql}}
+
+{{#dialect_instructions}}
+## {{dialect}} Specific Rules
+
+{{{dialect_instructions}}}
+{{/dialect_instructions}}
diff --git a/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-0 b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-0
new file mode 100644
index 00000000000..b7b3ae6afcb
--- /dev/null
+++ b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-0
@@ -0,0 +1,62 @@
+{
+ "$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
+ "self": {
+ "vendor": "com.metabase",
+ "name": "token_usage",
+ "format": "jsonschema",
+ "version": "1-0-0"
+ },
+ "type": "object",
+ "description": "Event to capture the token usage of individual LLM invocations. Created for billing purposes.",
+ "properties": {
+ "hashed_metabase_license_token": {
+ "type": "string",
+ "description": "The hashed value of the Metabase license token",
+ "maxLength": 255
+ },
+ "request_id": {
+ "type": "string",
+ "description": "Unique ID that is assigned to each incoming request. Can be used to aggregate usage for a single request.",
+ "maxLength": 255
+ },
+ "model_id": {
+ "type": "string",
+ "description": "The ID of the model that was used to generate the tokens",
+ "maxLength": 255
+ },
+ "total_tokens": {
+ "type": "integer",
+ "description": "The total number of tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "prompt_tokens": {
+ "type": "integer",
+ "description": "The number of prompt tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "completion_tokens": {
+ "type": "integer",
+ "description": "The number of completion tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "estimated_costs_usd": {
+ "type": "number",
+ "description": "The estimated cost of for the tokens in USD",
+ "minimum": 0,
+ "maximum": 2147483647
+ }
+ },
+ "required": [
+ "hashed_metabase_license_token",
+ "request_id",
+ "model_id",
+ "total_tokens",
+ "prompt_tokens",
+ "completion_tokens",
+ "estimated_costs_usd"
+ ],
+ "additionalProperties": false
+ }
diff --git a/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-1 b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-1
new file mode 100644
index 00000000000..47e5b39013c
--- /dev/null
+++ b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-1
@@ -0,0 +1,80 @@
+{
+ "$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
+ "self": {
+ "vendor": "com.metabase",
+ "name": "token_usage",
+ "format": "jsonschema",
+ "version": "1-0-1"
+ },
+ "type": "object",
+ "description": "Event to capture the token usage of individual LLM invocations. Created for billing purposes.",
+ "properties": {
+ "hashed_metabase_license_token": {
+ "type": "string",
+ "description": "The hashed value of the Metabase license token",
+ "maxLength": 255
+ },
+ "user_id": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The ID of the user that send the request",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "request_id": {
+ "type": "string",
+ "description": "Unique ID that is assigned to each incoming request. Can be used to aggregate usage for a single request.",
+ "maxLength": 255
+ },
+ "model_id": {
+ "type": "string",
+ "description": "The ID of the model that was used to generate the tokens",
+ "maxLength": 255
+ },
+ "total_tokens": {
+ "type": "integer",
+ "description": "The total number of tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "prompt_tokens": {
+ "type": "integer",
+ "description": "The number of prompt tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "completion_tokens": {
+ "type": "integer",
+ "description": "The number of completion tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "estimated_costs_usd": {
+ "type": "number",
+ "description": "The estimated cost of for the tokens in USD",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "duration_ms": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The time it took to (in milliseconds) to receive an answer from the LLM.",
+ "minimum": 0,
+ "maximum": 2147483647
+ }
+ },
+ "required": [
+ "hashed_metabase_license_token",
+ "request_id",
+ "model_id",
+ "total_tokens",
+ "prompt_tokens",
+ "completion_tokens",
+ "estimated_costs_usd"
+ ],
+ "additionalProperties": false
+ }
diff --git a/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-2 b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-2
new file mode 100644
index 00000000000..857c0a74845
--- /dev/null
+++ b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-2
@@ -0,0 +1,88 @@
+{
+ "$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
+ "self": {
+ "vendor": "com.metabase",
+ "name": "token_usage",
+ "format": "jsonschema",
+ "version": "1-0-2"
+ },
+ "type": "object",
+ "description": "Event to capture the token usage of individual LLM invocations. Created for billing purposes.",
+ "properties": {
+ "hashed_metabase_license_token": {
+ "type": "string",
+ "description": "The hashed value of the Metabase license token",
+ "maxLength": 255
+ },
+ "user_id": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The ID of the user that send the request",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "request_id": {
+ "type": "string",
+ "description": "Unique ID that is assigned to each incoming request. Can be used to aggregate usage for a single request.",
+ "maxLength": 255
+ },
+ "model_id": {
+ "type": "string",
+ "description": "The ID of the model that was used to generate the tokens",
+ "maxLength": 255
+ },
+ "total_tokens": {
+ "type": "integer",
+ "description": "The total number of tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "prompt_tokens": {
+ "type": "integer",
+ "description": "The number of prompt tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "completion_tokens": {
+ "type": "integer",
+ "description": "The number of completion tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "estimated_costs_usd": {
+ "type": "number",
+ "description": "The estimated cost of for the tokens in USD",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "duration_ms": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The time it took to (in milliseconds) to receive an answer from the LLM.",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "tag": {
+ "type": [
+ "string",
+ "null"
+ ],
+ "description": "A classifier indicating the specific purpose or functionality for which the tokens were used.",
+ "maxLength": 255
+ }
+ },
+ "required": [
+ "hashed_metabase_license_token",
+ "request_id",
+ "model_id",
+ "total_tokens",
+ "prompt_tokens",
+ "completion_tokens",
+ "estimated_costs_usd"
+ ],
+ "additionalProperties": false
+}
diff --git a/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-3 b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-3
new file mode 100644
index 00000000000..85c8d072e54
--- /dev/null
+++ b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-3
@@ -0,0 +1,96 @@
+{
+ "$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
+ "self": {
+ "vendor": "com.metabase",
+ "name": "token_usage",
+ "format": "jsonschema",
+ "version": "1-0-3"
+ },
+ "type": "object",
+ "description": "Event to capture the token usage of individual LLM invocations. Created for billing purposes.",
+ "properties": {
+ "hashed_metabase_license_token": {
+ "type": "string",
+ "description": "The hashed value of the Metabase license token for instances with a token. For Open Source instances, this will use the snowplow anonymous uuid in the form `oss_<anonymous_uuid>` to allow unique counts of instances.",
+ "maxLength": 255
+ },
+ "user_id": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The ID of the user that send the request",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "request_id": {
+ "type": "string",
+ "description": "Unique ID that is assigned to each incoming request. Can be used to aggregate usage for a single request.",
+ "maxLength": 255
+ },
+ "session_id": {
+ "type": [
+ "string",
+ "null"
+ ],
+ "description": "An identifier for the conversation or session in which the tokens were used. This can help group related requests together. Can be null for non-conversational use cases.",
+ "maxLength": 255
+ },
+ "model_id": {
+ "type": "string",
+ "description": "The ID of the model that was used to generate the tokens",
+ "maxLength": 255
+ },
+ "total_tokens": {
+ "type": "integer",
+ "description": "The total number of tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "prompt_tokens": {
+ "type": "integer",
+ "description": "The number of prompt tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "completion_tokens": {
+ "type": "integer",
+ "description": "The number of completion tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "estimated_costs_usd": {
+ "type": "number",
+ "description": "The estimated cost of for the tokens in USD",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "duration_ms": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The time it took to (in milliseconds) to receive an answer from the LLM.",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "tag": {
+ "type": [
+ "string",
+ "null"
+ ],
+ "description": "A classifier indicating the specific purpose or functionality for which the tokens were used.",
+ "maxLength": 255
+ }
+ },
+ "required": [
+ "hashed_metabase_license_token",
+ "request_id",
+ "model_id",
+ "total_tokens",
+ "prompt_tokens",
+ "completion_tokens",
+ "estimated_costs_usd"
+ ],
+ "additionalProperties": false
+}
diff --git a/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-4 b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-4
new file mode 100644
index 00000000000..ba4d2bcd04b
--- /dev/null
+++ b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/1-0-4
@@ -0,0 +1,112 @@
+{
+ "$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
+ "self": {
+ "vendor": "com.metabase",
+ "name": "token_usage",
+ "format": "jsonschema",
+ "version": "1-0-4"
+ },
+ "type": "object",
+ "description": "Event to capture the token usage of individual LLM invocations. Created for billing purposes.",
+ "properties": {
+ "hashed_metabase_license_token": {
+ "type": "string",
+ "description": "The hashed value of the Metabase license token for instances with a token. For Open Source instances, this will use the snowplow anonymous uuid in the form `oss_<anonymous_uuid>` to allow unique counts of instances.",
+ "maxLength": 255
+ },
+ "user_id": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The ID of the user that send the request",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "request_id": {
+ "type": "string",
+ "description": "Unique ID that is assigned to each incoming request. Can be used to aggregate usage for a single request.",
+ "maxLength": 255
+ },
+ "session_id": {
+ "type": [
+ "string",
+ "null"
+ ],
+ "description": "An identifier for the conversation or session in which the tokens were used. This can help group related requests together. Can be null for non-conversational use cases.",
+ "maxLength": 255
+ },
+ "model_id": {
+ "type": "string",
+ "description": "The ID of the model that was used to generate the tokens",
+ "maxLength": 255
+ },
+ "total_tokens": {
+ "type": "integer",
+ "description": "The total number of tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "prompt_tokens": {
+ "type": "integer",
+ "description": "The number of prompt tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "completion_tokens": {
+ "type": "integer",
+ "description": "The number of completion tokens",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "estimated_costs_usd": {
+ "type": "number",
+ "description": "The estimated cost of for the tokens in USD",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "duration_ms": {
+ "type": [
+ "integer",
+ "null"
+ ],
+ "description": "The time it took to (in milliseconds) to receive an answer from the LLM.",
+ "minimum": 0,
+ "maximum": 2147483647
+ },
+ "tag": {
+ "type": [
+ "string",
+ "null"
+ ],
+ "description": "A classifier indicating the specific purpose or functionality for which the tokens were used.",
+ "maxLength": 255
+ },
+ "profile": {
+ "type": [
+ "string",
+ "null"
+ ],
+ "description": "The profile/configuration variant used for the request. Allows distinguishing different configuration variants and usage patterns.",
+ "maxLength": 255
+ },
+ "source": {
+ "type": [
+ "string",
+ "null"
+ ],
+ "description": "The source/trigger of the request (e.g., 'metabot_agent', 'sql_generation_workflow', 'analyze_chart'). Indicates which API endpoint or workflow initiated the LLM call.",
+ "maxLength": 255
+ }
+ },
+ "required": [
+ "hashed_metabase_license_token",
+ "request_id",
+ "model_id",
+ "total_tokens",
+ "prompt_tokens",
+ "completion_tokens",
+ "estimated_costs_usd"
+ ],
+ "additionalProperties": false
+}
diff --git a/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/README.md b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/README.md
new file mode 100644
index 00000000000..c956c995275
--- /dev/null
+++ b/snowplow/iglu-client-embedded/schemas/com.metabase/token_usage/jsonschema/README.md
@@ -0,0 +1,8 @@
+These token_usage schemas also live in the ai-service repository at
+
+https://github.com/metabase/ai-service/tree/main/snowplow/schemas/com.metabase/token_usage/jsonschema
+
+If you update the schema here, make sure to update it there as well.
+
+The schemas are duplicated because each repository has its own setup for running snowplow micro locally, and those
+require the schemas to be present in the local filesystem for validation.
diff --git a/src/metabase/analytics/core.clj b/src/metabase/analytics/core.clj
index 0285dc304e0..167cb456b0b 100644
--- a/src/metabase/analytics/core.clj
+++ b/src/metabase/analytics/core.clj
@@ -43,6 +43,7 @@
[metabase.analytics.settings
+ analytics-uuid
anon-tracking-enabled
anon-tracking-enabled!
instance-creation]
diff --git a/src/metabase/analytics/snowplow.clj b/src/metabase/analytics/snowplow.clj
index e3ae4e83ed0..0e0e3c17683 100644
--- a/src/metabase/analytics/snowplow.clj
+++ b/src/metabase/analytics/snowplow.clj
@@ -50,6 +50,7 @@
:snowplow/action "1-0-0"
:snowplow/embed_share "1-0-2"
:snowplow/llm_usage "1-0-0"
+ :snowplow/token_usage "1-0-4"
:snowplow/serialization "1-0-1"
:snowplow/simple_event "1-0-0"
:snowplow/cleanup "1-0-0"})
diff --git a/src/metabase/api_routes/routes.clj b/src/metabase/api_routes/routes.clj
index 66f7fa09005..3240b6d602c 100644
--- a/src/metabase/api_routes/routes.clj
+++ b/src/metabase/api_routes/routes.clj
@@ -26,6 +26,7 @@
[metabase.geojson.api]
[metabase.glossary.api]
[metabase.indexed-entities.api]
+ [metabase.llm.api]
[metabase.logger.api]
[metabase.login-history.api]
[metabase.measures.api]
@@ -162,6 +163,7 @@
"/glossary" (+auth 'metabase.glossary.api)
"/google" (+auth metabase.sso.api/google-auth-routes)
"/ldap" (+auth metabase.sso.api/ldap-routes)
+ "/llm" (+auth metabase.llm.api/routes)
"/logger" (+auth 'metabase.logger.api)
"/login-history" (+auth 'metabase.login-history.api)
"/measure" (+auth 'metabase.measures.api)
diff --git a/src/metabase/core/init.clj b/src/metabase/core/init.clj
index 8e38f9eaa77..e37d6a79995 100644
--- a/src/metabase/core/init.clj
+++ b/src/metabase/core/init.clj
@@ -34,6 +34,7 @@
[metabase.indexed-entities.init]
[metabase.lib-be.init]
[metabase.lib.init]
+ [metabase.llm.init]
[metabase.logger.init]
[metabase.login-history.init]
[metabase.model-persistence.init]
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index d923be84fc7..f006f6338ae 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -223,6 +223,15 @@
[_]
nil)
+(defmulti llm-sql-dialect-resource
+ "Returns the resource path for dialect-specific LLM prompt instructions,
+ or nil if no dialect-specific instructions exist for this driver."
+ {:added "0.59.0" :arglists '([driver])}
+ dispatch-on-uninitialized-driver
+ :hierarchy #'hierarchy)
+
+(defmethod llm-sql-dialect-resource :default [_] nil)
+
(defn dispatch-on-initialized-driver-safe-keys
"Dispatch on initialized driver, except checks for `classname`,
`subprotocol`, `connection-uri` in the details map in order to
diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.clj
index f4d616283d2..41b3601a985 100644
--- a/src/metabase/driver/h2.clj
+++ b/src/metabase/driver/h2.clj
@@ -674,3 +674,6 @@
(defmethod sql/default-schema :h2
[_]
"PUBLIC")
+
+(defmethod driver/llm-sql-dialect-resource :h2 [_]
+ "llm/prompts/dialects/h2.md")
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index a5162855770..cae2882d652 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -1218,3 +1218,6 @@
(defmethod driver/extra-info :mysql
[_driver]
nil)
+
+(defmethod driver/llm-sql-dialect-resource :mysql [_]
+ "llm/prompts/dialects/mysql.md")
diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.clj
index 93d90ca7ab0..1d469338487 100644
--- a/src/metabase/driver/postgres.clj
+++ b/src/metabase/driver/postgres.clj
@@ -1312,3 +1312,6 @@
(let [type (nippy/thaw-from-in! data-input)
value (nippy/thaw-from-in! data-input)]
(doto (PGobject.) (.setType type) (.setValue value))))
+
+(defmethod driver/llm-sql-dialect-resource :postgres [_]
+ "llm/prompts/dialects/postgresql.md")
diff --git a/src/metabase/llm/anthropic.clj b/src/metabase/llm/anthropic.clj
new file mode 100644
index 00000000000..9cc39e69170
--- /dev/null
+++ b/src/metabase/llm/anthropic.clj
@@ -0,0 +1,139 @@
+(ns metabase.llm.anthropic
+ "Anthropic API client for OSS LLM integration.
+
+ Provides synchronous chat completions for text-to-SQL generation
+ using tool_use for structured output."
+ (:require
+ [clj-http.client :as http]
+ [clojure.core.memoize :as memoize]
+ [clojure.string :as str]
+ [metabase.llm.settings :as llm.settings]
+ [metabase.util :as u]
+ [metabase.util.json :as json])
+ (:import
+ (com.fasterxml.jackson.core JsonParseException)))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private generate-sql-tool
+ "Tool definition for structured SQL output.
+ Forces the model to return a JSON object with sql and optional explanation."
+ {:name "generate_sql"
+ :description "Generate SQL query from the user's request. Always use this tool to return your response."
+ :input_schema {:type "object"
+ :properties {:sql {:type "string"
+ :description "The generated SQL query"}
+ :explanation {:type "string"
+ :description "Brief explanation of the query"}}
+ :required ["sql"]}})
+
+(defn- build-request-headers
+ "Build headers for Anthropic API request."
+ [api-key]
+ {"x-api-key" api-key
+ "anthropic-version" (llm.settings/llm-anthropic-api-version)
+ "content-type" "application/json"})
+
+(defn- build-request-body
+ "Build the request body for Anthropic messages API."
+ [{:keys [model system messages]}]
+ (cond-> {:model model
+ :max_tokens (llm.settings/llm-max-tokens)
+ :messages messages
+ :tools [generate-sql-tool]
+ :tool_choice {:type "tool" :name "generate_sql"}}
+ system (assoc :system system)))
+
+(defn- extract-tool-input
+ "Extract the tool input from Anthropic messages response.
+ Returns the parsed JSON input from the tool_use content block."
+ [response-body]
+ (let [content (:content response-body)]
+ (->> content
+ (filter #(= "tool_use" (:type %)))
+ first
+ :input)))
+
+(defn- handle-api-error
+ "Handle HTTP errors from Anthropic API."
+ [exception]
+ (if-let [response-body (some-> exception ex-data :body)]
+ (let [parsed (try
+ (json/decode response-body)
+ (catch JsonParseException _
+ {:error {:message response-body}}))]
+ (throw (ex-info (or (-> parsed :error :message)
+ "Anthropic API request failed")
+ {:type :anthropic-api-error
+ :status (some-> exception ex-data :status)
+ :body parsed}
+ exception)))
+ (throw exception)))
+
+(defn- get-api-key-or-throw
+ "Gets Anthropic API key from settings or throws an unconfigured error."
+ []
+ (let [api-key (llm.settings/llm-anthropic-api-key)]
+ (when (str/blank? api-key)
+ (throw (ex-info "LLM is not configured. Please set an Anthropic API key via MB_LLM_ANTHROPIC_API_KEY."
+ {:type :llm-not-configured})))
+ api-key))
+
+(def ^:private list-models*
+ "Memoized implementation of list-models with 5-minute TTL."
+ (memoize/ttl
+ (fn [api-key]
+ (try
+ (let [url (str (llm.settings/llm-anthropic-api-url) "/v1/models")
+ response (http/get url
+ {:headers {"x-api-key" api-key
+ "anthropic-version" (llm.settings/llm-anthropic-api-version)}})
+ body (json/decode+kw (:body response))
+ models (reverse (sort-by :created_at (:data body)))]
+ {:models (map #(select-keys % [:id :display_name]) models)})
+ (catch Exception e
+ (handle-api-error e))))
+ :ttl/threshold (* 5 60 1000)))
+
+(defn list-models
+ "Send a list models request to Anthropic.
+ Returns a map with :models. Results are cached for 5 minutes."
+ []
+ (list-models* (get-api-key-or-throw)))
+
+(defn chat-completion
+ "Send a chat completion request to Anthropic.
+ Returns a map with:
+ - :result - Map with :sql and optionally :explanation from the tool response
+ - :usage - Map with :model, :prompt (input tokens), :completion (output tokens)
+ - :duration-ms - Request duration in milliseconds
+
+ Options:
+ - :model - Model to use (default: configured model or claude-sonnet-4-20250514)
+ - :system - System prompt
+ - :messages - Vector of {:role :content} maps for conversation history"
+ [{:keys [model system messages]}]
+ (let [model (or model (llm.settings/llm-anthropic-model))
+ request {:model model
+ :system system
+ :messages messages}
+ start-time (u/start-timer)]
+ (try
+ (let [url (str (llm.settings/llm-anthropic-api-url) "/v1/messages")
+ response (http/post url
+ {:headers (build-request-headers (get-api-key-or-throw))
+ :body (json/encode (build-request-body request))
+ :as :json
+ :content-type :json
+ :socket-timeout (llm.settings/llm-request-timeout-ms)
+ :connection-timeout (llm.settings/llm-connection-timeout-ms)})
+ duration-ms (u/since-ms start-time)
+ body (:body response)
+ usage (:usage body)]
+ {:result (extract-tool-input body)
+ :duration-ms duration-ms
+ :usage {:model model
+ :prompt (:input_tokens usage)
+ :completion (:output_tokens usage)}})
+ (catch Exception e
+ (handle-api-error e)))))
diff --git a/src/metabase/llm/api.clj b/src/metabase/llm/api.clj
new file mode 100644
index 00000000000..7af29149197
--- /dev/null
+++ b/src/metabase/llm/api.clj
@@ -0,0 +1,277 @@
+(ns metabase.llm.api
+ "API endpoints for LLM-powered SQL generation (OSS)."
+ (:require
+ [buddy.core.codecs :as codecs]
+ [buddy.core.hash :as buddy-hash]
+ [clojure.java.io :as io]
+ [clojure.set :as set]
+ [clojure.string :as str]
+ [metabase.analytics.core :as analytics]
+ [metabase.analytics.snowplow :as snowplow]
+ [metabase.api.common :as api]
+ [metabase.api.macros :as api.macros]
+ [metabase.api.routes.common :refer [+auth]]
+ [metabase.api.util.handlers :as handlers]
+ [metabase.driver :as driver]
+ [metabase.llm.anthropic :as llm.anthropic]
+ [metabase.llm.context :as llm.context]
+ [metabase.llm.costs :as llm.costs]
+ [metabase.llm.settings :as llm.settings]
+ [metabase.premium-features.core :as premium-features]
+ [metabase.request.core :as request]
+ [metabase.util :as u]
+ [metabase.util.i18n :refer [tru]]
+ [stencil.core :as stencil]
+ [throttle.core :as throttle]
+ [toucan2.core :as t2])
+ (:import
+ (java.time LocalDateTime)
+ (java.time.format DateTimeFormatter)))
+
+(set! *warn-on-reflection* true)
+
+(def ^:private sql-gen-throttlers
+ "Throttlers for SQL generation endpoints.
+ - :user-id limits requests per user (default 20/minute)
+ - :ip-address limits requests per IP (default 100/minute)"
+ {:user-id (throttle/make-throttler :user-id
+ :attempts-threshold (llm.settings/llm-rate-limit-per-user)
+ :attempt-ttl-ms 60000)
+ :ip-address (throttle/make-throttler :ip-address
+ :attempts-threshold (llm.settings/llm-rate-limit-per-ip)
+ :attempt-ttl-ms 60000)})
+
+(defn- database-engine
+ "Get the engine keyword for a database."
+ [database-id]
+ (when database-id
+ (t2/select-one-fn :engine :model/Database :id database-id)))
+
+(def ^:private load-dialect-instructions
+ "Load dialect-specific instructions from resources, if available.
+ Returns nil if no instructions file exists for the given engine.
+ Memoized since dialect files are static resources."
+ (memoize
+ (fn [engine]
+ (when engine
+ (when-let [resource-path (driver/llm-sql-dialect-resource engine)]
+ (when-let [resource (io/resource resource-path)]
+ (slurp resource)))))))
+
+(def ^:private sql-generation-prompt-template "llm/prompts/sql-generation-system.mustache")
+
+(def ^:private datetime-formatter
+ (DateTimeFormatter/ofPattern "yyyy-MM-dd HH:mm:ss"))
+
+(defn- current-datetime-str
+ "Get current date/time as a formatted string for prompt context."
+ []
+ (.format (LocalDateTime/now) datetime-formatter))
+
+(defn- build-system-prompt
+ "Build the system prompt for SQL generation with dialect, schema, and optional dialect instructions.
+ When source-sql is provided, includes the existing query for context."
+ [{:keys [dialect schema-ddl dialect-instructions source-sql]}]
+ (stencil/render-file sql-generation-prompt-template
+ (cond-> {:dialect dialect
+ :schema schema-ddl
+ :current_time (current-datetime-str)
+ :dialect_instructions dialect-instructions}
+ source-sql (assoc :source_sql source-sql))))
+
+(defn- track-token-usage!
+ "Track token usage for LLM API calls via Snowplow."
+ [{:keys [model prompt completion duration-ms user-id source tag]}]
+ (let [request-id (-> (random-uuid)
+ str
+ ;; strip dashes and lower-case to mimic the ai-service uuid.hex formatting
+ (str/replace "-" "")
+ u/lower-case-en)
+ hashed-token (some-> (not-empty (premium-features/premium-embedding-token))
+ buddy-hash/sha256
+ codecs/bytes->hex)
+ token-or-uuid (or hashed-token
+ (str "oss__" (analytics/analytics-uuid)))
+ estimated-costs (llm.costs/estimate {:model model
+ :prompt prompt
+ :completion completion})]
+ (snowplow/track-event! :snowplow/token_usage
+ {:hashed-metabase-license-token token-or-uuid
+ :request-id request-id
+ :model-id model
+ :total-tokens (+ prompt completion)
+ :prompt-tokens prompt
+ :completion-tokens completion
+ :estimated-costs-usd estimated-costs
+ :user-id user-id
+ :duration-ms (some-> duration-ms long)
+ :source source
+ :tag tag}
+ user-id)))
+
+(defn- track-sqlgen-event!
+ "Track SQL generation usage via Snowplow simple_event."
+ [{:keys [duration-ms result engine]}]
+ (snowplow/track-event! :snowplow/simple_event
+ {:event "metabot_oss_sqlgen_used"
+ :event_detail (some-> engine name)
+ :duration_ms (some-> duration-ms long)
+ :result result}
+ api/*current-user-id*))
+
+(api.macros/defendpoint :get "/list-models"
+ :- [:map [:models [:sequential [:map
+ [:id :string]
+ [:display_name :string]]]]]
+ "List available LLM models from the configured provider.
+
+ Requires LLM to be configured (Anthropic API key set in admin settings)."
+ [_route-params
+ _query-params]
+ (when-not (llm.settings/llm-anthropic-api-key)
+ (throw (ex-info (tru "LLM is not configured. Please set an Anthropic API key in admin settings.")
+ {:status-code 403})))
+ (llm.anthropic/list-models))
+
+(def ^:private table-with-columns-schema
+ "Schema for table metadata with columns returned by /extract-tables."
+ [:map
+ [:id pos-int?]
+ [:name :string]
+ [:schema {:optional true} [:maybe :string]]
+ [:display_name {:optional true} [:maybe :string]]
+ [:description {:optional true} [:maybe :string]]
+ [:columns [:sequential
+ [:map
+ [:id pos-int?]
+ [:name :string]
+ [:database_type {:optional true} [:maybe :string]]
+ [:description {:optional true} [:maybe :string]]
+ [:semantic_type {:optional true} [:maybe :string]]
+ [:fk_target {:optional true}
+ [:map
+ [:table_name :string]
+ [:field_name :string]]]]]]])
+
+(api.macros/defendpoint :post "/extract-tables"
+ :- [:map [:tables [:sequential table-with-columns-schema]]]
+ "Parse SQL and return referenced tables with their columns.
+
+ Uses Macaw to parse the SQL, resolves table names to IDs,
+ and returns permission-filtered tables with column metadata.
+
+ This is a lightweight endpoint that does not trigger fingerprinting
+ or field value fetching."
+ [_route-params
+ _query-params
+ body :- [:map
+ [:database_id pos-int?]
+ [:sql :string]]]
+ (let [{:keys [database_id sql]} body
+ table-ids (llm.context/extract-tables-from-sql database_id sql)
+ tables (llm.context/get-tables-with-columns database_id table-ids)]
+ {:tables (or tables [])}))
+
+(api.macros/defendpoint :post "/generate-sql"
+ :- [:map
+ [:sql :string]
+ [:referenced_entities [:sequential
+ [:map
+ [:model :string]
+ [:id pos-int?]
+ [:name :string]
+ [:schema {:optional true} [:maybe :string]]
+ [:display_name {:optional true} [:maybe :string]]
+ [:description {:optional true} [:maybe :string]]
+ [:columns [:sequential
+ [:map
+ [:id pos-int?]
+ [:name :string]
+ [:database_type {:optional true} [:maybe :string]]
+ [:description {:optional true} [:maybe :string]]
+ [:semantic_type {:optional true} [:maybe :string]]
+ [:fk_target {:optional true}
+ [:map
+ [:table_name :string]
+ [:field_name :string]]]]]]]]]]
+ "Generate SQL from a natural language prompt.
+
+ Requires:
+ - LLM to be configured (Anthropic API key set in admin settings)
+ - At least one table reference (explicit @mention or implicit from source_sql)
+ - A database_id parameter
+
+ Returns generated SQL and the list of tables used for context."
+ [_route-params
+ _query-params
+ body :- [:map
+ [:prompt :string]
+ [:database_id pos-int?]
+ [:source_sql {:optional true} :string]
+ [:referenced_entities {:optional true}
+ [:sequential [:map
+ [:model :string]
+ [:id pos-int?]]]]]
+ request]
+ (when-not (llm.settings/llm-anthropic-api-key)
+ (throw (ex-info (tru "LLM SQL generation is not configured. Please set an Anthropic API key in admin settings.")
+ {:status-code 403})))
+ (throttle/with-throttling [(sql-gen-throttlers :ip-address) (request/ip-address request)
+ (sql-gen-throttlers :user-id) api/*current-user-id*]
+ (let [{:keys [prompt database_id source_sql referenced_entities]} body
+ ;; 2. Extract table IDs from all sources and merge them
+ frontend-table-ids (when (seq referenced_entities)
+ (->> referenced_entities
+ (filter #(= "table" (:model %)))
+ (map :id)
+ set))
+ explicit-table-ids (llm.context/parse-table-mentions prompt)
+ implicit-table-ids (when source_sql
+ (llm.context/extract-tables-from-sql database_id source_sql))
+ table-ids (set/union (or frontend-table-ids #{})
+ (or explicit-table-ids #{})
+ (or implicit-table-ids #{}))]
+ (when (empty? table-ids)
+ (throw (ex-info (if (and source_sql (empty? implicit-table-ids))
+ (tru "Failed to parse SQL. Use @mentions to provide table references.")
+ (tru "No tables found. Use @mentions or provide source SQL with table references."))
+ {:status-code 400})))
+ (let [{:keys [ddl tables]} (llm.context/build-schema-context database_id table-ids)]
+ (when-not ddl
+ (throw (ex-info (tru "No accessible tables found. Check table permissions.")
+ {:status-code 400})))
+ (let [engine (database-engine database_id)
+ dialect (if engine (driver/display-name engine) "SQL")
+ dialect-instructions (load-dialect-instructions engine)
+ system-prompt (build-system-prompt {:dialect dialect
+ :schema-ddl ddl
+ :dialect-instructions dialect-instructions
+ :source-sql source_sql})
+ start-timer (u/start-timer)]
+ (try
+ (let [{:keys [result usage duration-ms]} (llm.anthropic/chat-completion
+ {:system system-prompt
+ :messages [{:role "user" :content prompt}]})]
+ (track-token-usage! (assoc usage
+ :duration-ms duration-ms
+ :user-id api/*current-user-id*
+ ;; for some reason, :source convention is snake_case and :tag is (mostly) kebab
+ :source "oss_metabot"
+ :tag "oss-sqlgen"))
+ (track-sqlgen-event! {:duration-ms (u/since-ms start-timer)
+ :result "success"
+ :engine engine})
+ (let [sql (:sql result)
+ referenced-entities (mapv #(assoc % :model "table") tables)]
+ {:sql sql
+ :referenced_entities referenced-entities}))
+ (catch Exception e
+ (track-sqlgen-event! {:duration-ms (u/since-ms start-timer)
+ :result "failure"
+ :engine engine})
+ (throw e))))))))
+
+(def ^{:arglists '([request respond raise])} routes
+ "`/api/llm` routes."
+ (handlers/routes
+ (api.macros/ns-handler *ns* +auth)))
diff --git a/src/metabase/llm/context.clj b/src/metabase/llm/context.clj
new file mode 100644
index 00000000000..f4744eb3433
--- /dev/null
+++ b/src/metabase/llm/context.clj
@@ -0,0 +1,509 @@
+(ns metabase.llm.context
+ "Schema extraction for LLM context. Parses table mentions from prompts
+ and fetches table metadata formatted as DDL for SQL generation."
+ (:require
+ [clojure.string :as str]
+ [macaw.core :as macaw]
+ [metabase.api.common :as api]
+ [metabase.lib-be.core :as lib-be]
+ [metabase.lib.core :as lib]
+ [metabase.lib.metadata :as lib.metadata]
+ [metabase.models.interface :as mi]
+ [metabase.sync.core :as sync]
+ [metabase.util :as u]
+ [metabase.util.log :as log]
+ [metabase.warehouse-schema.models.field-values :as field-values]
+ [toucan2.core :as t2])
+ (:import
+ (java.io StringWriter Writer)))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------------ Mention Parsing ------------------------------------------------
+
+(def ^:private table-mention-pattern
+ "Regex to extract table IDs from markdown-style metabase:// links.
+ Frontend serializes @mentions as: [Display Name](metabase://table/123)"
+ #"\[[^\]]+\]\(metabase://table/(\d+)\)")
+
+(defn parse-table-mentions
+ "Extract table IDs from metabase://table/{id} links in prompt text.
+
+ Frontend serializes @mentions as markdown links:
+ [Display Name](metabase://table/123)
+
+ Returns a set of table IDs (integers), or nil if input is nil."
+ [prompt-text]
+ (when (string? prompt-text)
+ (->> (re-seq table-mention-pattern prompt-text)
+ (keep (fn [[_ id-str]]
+ (parse-long id-str)))
+ set)))
+
+(defn- table-match-clause
+ "Build a WHERE clause to match a table by name and optionally schema.
+ When schema is present, matches both; otherwise matches just the table name."
+ [{:keys [schema table]}]
+ (let [table-lower (u/lower-case-en table)]
+ (if schema
+ [:and
+ [:= [:lower :name] table-lower]
+ [:= [:lower :schema] (u/lower-case-en schema)]]
+ [:= [:lower :name] table-lower])))
+
+(defn extract-tables-from-sql
+ "Extract table IDs from a raw SQL string using Macaw parser.
+
+ Parses the SQL to identify referenced table names, then queries the
+ database to resolve those names to table IDs. When the SQL includes
+ schema-qualified references (e.g., schema.table), matches on both
+ schema and table name.
+
+ Returns a set of table IDs (integers), or empty set if parsing fails
+ or no tables are found."
+ [database-id sql-string]
+ (if (and database-id (seq sql-string))
+ (try
+ (let [result (macaw/query->tables sql-string {:mode :compound-select})
+ tables (:tables result)]
+ (if (seq tables)
+ (let [match-clauses (mapv table-match-clause tables)
+ matched-tables (t2/select :model/Table
+ {:where [:and
+ [:= :db_id database-id]
+ [:= :active true]
+ (into [:or] match-clauses)]})]
+ (into #{} (map :id) matched-tables))
+ #{}))
+ (catch Exception e
+ (log/warn e "Failed to extract tables from source SQL")
+ #{}))
+ #{}))
+
+;;; ------------------------------------------ Permission-Filtered Fetch ------------------------------------------
+
+(defn- fetch-accessible-tables
+ "Fetch tables by ID, filtering to only those the current user can access.
+ Returns a map of table-id -> table record."
+ [table-ids]
+ (when (seq table-ids)
+ (let [{:keys [clause with]} (mi/visible-filter-clause
+ :model/Table :id
+ {:user-id api/*current-user-id*
+ :is-superuser? api/*is-superuser?*}
+ {:perms/view-data :unrestricted
+ :perms/create-queries :query-builder-and-native})
+ tables (t2/select :model/Table
+ :id [:in table-ids]
+ :active true
+ :visibility_type nil
+ (cond-> {:where clause}
+ with (assoc :with with)))]
+ (into {} (map (juxt :id identity)) tables))))
+
+;;; ----------------------------------------- Metadata Provider Column Fetch -----------------------------------------
+
+(defn- fetch-table-columns
+ "Use metadata provider to get visible columns for a table.
+ Returns sequence of column maps with metadata for DDL generation.
+ By default excludes implicitly-joinable columns; pass opts to override."
+ ([mp table-id]
+ (fetch-table-columns mp table-id {}))
+ ([mp table-id opts]
+ (when-let [table-meta (lib.metadata/table mp table-id)]
+ (let [table-query (lib/query mp table-meta)
+ vis-opts (merge {:include-implicitly-joinable? false} opts)
+ columns (lib/visible-columns table-query -1 vis-opts)]
+ (mapv (fn [col]
+ {:id (:id col)
+ :name (:name col)
+ :database_type (or (:database-type col)
+ (some-> (:base-type col) name))
+ :description (:description col)
+ :semantic_type (:semantic-type col)
+ :fingerprint (:fingerprint col)
+ :fk_target_field_id (:fk-target-field-id col)})
+ columns)))))
+
+(defn- fetch-field-values
+ "Fetch or create FieldValues for columns that should have them.
+ Uses get-or-create-full-field-values! which will:
+ - Create field values if missing and field should have them
+ - Update field values if inactive (not used recently)
+ - Query source database if necessary to populate values
+ Returns map of field-id -> values vector."
+ [columns]
+ (let [field-ids (->> columns
+ (keep :id)
+ (filter pos-int?)
+ set)]
+ (when (seq field-ids)
+ (let [fields (t2/select :model/Field :id [:in field-ids])]
+ (into {}
+ (keep (fn [field]
+ (when-let [fv (field-values/get-or-create-full-field-values! field)]
+ (when-let [values (not-empty (:values fv))]
+ [(:id field) values]))))
+ fields)))))
+
+(defn- fetch-fk-targets
+ "Fetch table.field names for FK target fields.
+ Returns map of target-field-id -> {:table name :field name}"
+ [columns]
+ (let [target-ids (->> columns
+ (keep :fk_target_field_id)
+ set)]
+ (when (seq target-ids)
+ (let [fields (t2/select [:model/Field :id :name :table_id]
+ :id [:in target-ids])
+ table-ids (into #{} (map :table_id) fields)
+ table-names (when (seq table-ids)
+ (t2/select-pk->fn :name :model/Table :id [:in table-ids]))]
+ (into {}
+ (map (fn [{:keys [id name table_id]}]
+ [id {:table (get table-names table_id) :field name}]))
+ fields)))))
+
+;;; ----------------------------------------- On-Demand Metadata Enrichment -----------------------------------------
+
+(defn- enrich-fingerprints-on-demand!
+ "For columns missing fingerprints, trigger re-fingerprinting.
+ Returns a map of field-id -> fingerprint for columns that were missing them.
+ This queries the source database to compute fingerprints if they don't exist."
+ [columns]
+ (let [missing-fp-ids (->> columns
+ (filter #(and (:id %) (nil? (:fingerprint %))))
+ (map :id)
+ (filter pos-int?)
+ set)]
+ (when (seq missing-fp-ids)
+ (let [fields (t2/select :model/Field :id [:in missing-fp-ids])]
+ (doseq [field fields]
+ (sync/refingerprint-field! field))
+ (t2/select-pk->fn :fingerprint :model/Field :id [:in missing-fp-ids])))))
+
+;;; ------------------------------------------- Fingerprint Formatting -------------------------------------------
+
+(defn- format-numeric-stats
+ "Format numeric fingerprint as readable string."
+ [{:keys [min max avg]}]
+ (when (and min max)
+ (let [fmt #(if (integer? %) (str %) (format "%.2f" (double %)))]
+ (cond-> (str "range: " (fmt min) "-" (fmt max))
+ avg (str ", avg: " (fmt avg))))))
+
+(defn- format-temporal-stats
+ "Format temporal fingerprint as readable string."
+ [{:keys [earliest latest]}]
+ (when (and earliest latest)
+ (let [fmt-date #(some-> % (subs 0 (min 10 (count %))))]
+ (str (fmt-date earliest) " to " (fmt-date latest)))))
+
+(defn- format-text-stats
+ "Format text fingerprint as readable string.
+ Includes average-length from type fingerprint and distinct-count/nil% from global."
+ [type-fp global-fp]
+ (let [{:keys [average-length]} type-fp
+ {:keys [distinct-count nil%]} global-fp
+ parts (cond-> []
+ distinct-count
+ (conj (str "distinct: " distinct-count))
+
+ (and nil% (pos? nil%))
+ (conj (str "nil: " (format "%.0f%%" (* 100 nil%))))
+
+ (and average-length (> average-length 0))
+ (conj (str "avg-len: " (format "%.0f" (double average-length)))))]
+ (when (seq parts)
+ (str/join ", " parts))))
+
+(defn- semantic-type->hint
+ "Extract the type name from a semantic type keyword for DDL comments.
+ e.g. :type/Email -> \"Email\", :type/CreationTimestamp -> \"CreationTimestamp\"
+ Returns nil if semantic-type is nil."
+ [semantic-type]
+ (some-> semantic-type name))
+
+;;; -------------------------------------------- Column Comment Building --------------------------------------------
+
+(def ^:private max-sample-values 6)
+(def ^:private max-sample-value-length 30)
+
+(defn- truncate-value
+ "Truncate a sample value if too long."
+ [v]
+ (let [s (str v)]
+ (if (> (count s) max-sample-value-length)
+ (str (subs s 0 (- max-sample-value-length 3)) "...")
+ s)))
+
+(defn- build-column-comment
+ "Build an informative comment for a column based on available metadata.
+ Returns nil if no useful metadata is available."
+ [{:keys [description semantic_type fingerprint fk_target_field_id]}
+ field-values
+ fk-target-info]
+ (let [has-sample-values? (and (not-empty field-values)
+ (<= (count field-values) max-sample-values))
+ fp-global (get fingerprint :global)
+ fp-type (some-> fingerprint :type vals first)
+
+ parts (cond-> []
+ ;; User-provided description takes priority
+ (not-empty description)
+ (conj description)
+
+ ;; FK relationship
+ (and fk_target_field_id fk-target-info)
+ (conj (str "FK->" (:table fk-target-info) "." (:field fk-target-info))))
+
+ ;; Sample values for low-cardinality fields
+ parts (if has-sample-values?
+ (conj parts (str/join ", " (map truncate-value field-values)))
+ ;; Semantic type hint only when no description and no sample values
+ (if-let [sem-hint (and (nil? description)
+ semantic_type
+ (semantic-type->hint semantic_type))]
+ (conj parts sem-hint)
+ parts))
+
+ ;; Fingerprint stats based on field type
+ stats (cond
+ ;; Numeric range
+ (and (:min fp-type) (:max fp-type))
+ (format-numeric-stats fp-type)
+
+ ;; Temporal range
+ (and (:earliest fp-type) (:latest fp-type))
+ (format-temporal-stats fp-type)
+
+ ;; Text stats (distinct count, nil%, avg length)
+ (or (:average-length fp-type) (:distinct-count fp-global))
+ (format-text-stats fp-type fp-global))
+
+ all-parts (cond-> parts
+ stats (conj stats))]
+ (when (seq all-parts)
+ (str/join "; " all-parts))))
+
+;;; ------------------------------------------------ DDL Formatting ------------------------------------------------
+
+(defn- format-escaped
+ "Escape identifier if it contains special characters."
+ [^String identifier ^Writer writer]
+ (if (re-matches #"[\p{Alpha}_][\p{Alnum}_]*" identifier)
+ (.append writer identifier)
+ (do
+ (.append writer \")
+ (doseq [^Character c identifier]
+ (when (= c \")
+ (.append writer c))
+ (.append writer c))
+ (.append writer \"))))
+
+(defn- format-column-ddl
+ "Format a single column definition. If column has a :comment, emit it as a SQL comment
+ on the line before the column definition."
+ [{:keys [^String database_type ^String comment], col-name :name} ^Writer writer first?]
+ (when comment
+ (when-not first?
+ (.append writer "\n"))
+ (.append writer " -- ")
+ (.append writer comment)
+ (.append writer "\n"))
+ (when-not (or first? comment)
+ (.append writer "\n"))
+ (.append writer " ")
+ (format-escaped col-name writer)
+ (.append writer " ")
+ (.append writer (or database_type "UNKNOWN")))
+
+(defn- format-table-ddl
+ [{:keys [schema columns description], table-name :name} ^Writer writer]
+ (when (not-empty description)
+ (.append writer "-- ")
+ (.append writer ^String description)
+ (.append writer "\n"))
+ (.append writer "CREATE TABLE ")
+ (when (seq schema)
+ (format-escaped schema writer)
+ (.append writer \.))
+ (format-escaped table-name writer)
+ (.append writer " (")
+ (when (seq columns)
+ (.append writer "\n")
+ (format-column-ddl (first columns) writer true)
+ (doseq [column (rest columns)]
+ (.append writer ",")
+ (format-column-ddl column writer false)))
+ (.append writer "\n);"))
+
+(defn- format-schema-ddl
+ "Format multiple tables as DDL string."
+ [tables]
+ (let [sw (StringWriter.)]
+ (doseq [table tables]
+ (format-table-ddl table sw)
+ (.append sw \newline))
+ (str/trimr (str sw))))
+
+;;; ------------------------------------------------- Public API -------------------------------------------------
+
+(defn- enrich-columns-with-comments
+ "Add :comment to each column based on available metadata."
+ [columns field-values-map fk-targets-map]
+ (mapv (fn [col]
+ (let [fv (get field-values-map (:id col))
+ fk-info (get fk-targets-map (:fk_target_field_id col))
+ comment (build-column-comment col fv fk-info)]
+ (cond-> col
+ comment (assoc :comment comment))))
+ columns))
+
+(defn- merge-enriched-fingerprints
+ "Merge on-demand fingerprints into columns that were missing them."
+ [columns enriched-fp-map]
+ (if (seq enriched-fp-map)
+ (mapv (fn [col]
+ (if (and (nil? (:fingerprint col))
+ (contains? enriched-fp-map (:id col)))
+ (assoc col :fingerprint (get enriched-fp-map (:id col)))
+ col))
+ columns)
+ columns))
+
+(defn- format-columns-for-response
+ "Format columns for API response, resolving FK targets."
+ [columns fk-targets-map]
+ (mapv (fn [col]
+ (let [fk-info (get fk-targets-map (:fk_target_field_id col))]
+ (cond-> {:id (:id col)
+ :name (:name col)
+ :database_type (:database_type col)
+ :description (:description col)
+ :semantic_type (some-> (:semantic_type col) name)}
+ fk-info (assoc :fk_target {:table_name (:table fk-info)
+ :field_name (:field fk-info)}))))
+ columns))
+
+(defn build-schema-context
+ "Fetch table metadata for mentioned tables and format as DDL for LLM context.
+
+ Uses the metadata provider to get visible columns, respecting field
+ visibility settings. Includes field descriptions, sample values, and
+ statistical information when available.
+
+ For fields missing fingerprints or field values, this function will
+ trigger on-demand creation by querying the source database.
+
+ Parameters:
+ - database-id: Database containing the tables
+ - table-ids: Set of table IDs to include
+
+ Returns a map with:
+ - :ddl - DDL string for LLM context
+ - :tables - structured table metadata for API response
+ Or nil if no accessible tables found."
+ [database-id table-ids]
+ (when (and database-id (seq table-ids))
+ (let [accessible-tables (fetch-accessible-tables table-ids)]
+ (when (seq accessible-tables)
+ (lib-be/with-metadata-provider-cache
+ (let [mp (lib-be/application-database-metadata-provider database-id)
+ _ (lib.metadata/bulk-metadata mp :metadata/table (keys accessible-tables))
+
+ tables-with-columns
+ (keep (fn [[table-id table]]
+ (when-let [columns (seq (fetch-table-columns mp table-id))]
+ {:id table-id
+ :name (:name table)
+ :schema (:schema table)
+ :display_name (:display_name table)
+ :description (:description table)
+ :columns columns}))
+ accessible-tables)
+
+ ;; Gather all columns for batch operations
+ all-columns (mapcat :columns tables-with-columns)
+
+ ;; On-demand enrichment: trigger fingerprinting for columns missing fingerprints
+ enriched-fp-map (enrich-fingerprints-on-demand! all-columns)
+
+ ;; Update tables with enriched fingerprints
+ tables-with-enriched-fps
+ (mapv (fn [table]
+ (update table :columns merge-enriched-fingerprints enriched-fp-map))
+ tables-with-columns)
+
+ ;; Re-gather columns after fingerprint enrichment
+ all-enriched-columns (mapcat :columns tables-with-enriched-fps)
+
+ ;; Batch fetch FieldValues (on-demand) and FK targets
+ field-values-map (fetch-field-values all-enriched-columns)
+ fk-targets-map (fetch-fk-targets all-enriched-columns)
+
+ ;; Enrich columns with comments for DDL
+ enriched-tables
+ (mapv (fn [table]
+ (update table :columns
+ enrich-columns-with-comments
+ field-values-map
+ fk-targets-map))
+ tables-with-enriched-fps)
+
+ ;; Format tables for API response (without :comment, with :fk_target)
+ response-tables
+ (mapv (fn [table]
+ (update table :columns format-columns-for-response fk-targets-map))
+ tables-with-enriched-fps)]
+
+ (when (seq enriched-tables)
+ {:ddl (format-schema-ddl enriched-tables)
+ :tables response-tables})))))))
+
+(defn get-tables-with-columns
+ "Fetch tables with their columns for the extract-tables endpoint.
+ Returns lightweight metadata without triggering fingerprinting or field values.
+
+ Parameters:
+ - database-id: Database containing the tables
+ - table-ids: Set of table IDs to include
+
+ Returns a vector of table maps with :id, :name, :schema, :display_name,
+ :description, and :columns (with FK targets resolved), or nil."
+ [database-id table-ids]
+ (when (and database-id (seq table-ids))
+ (let [accessible-tables (fetch-accessible-tables table-ids)]
+ (when (seq accessible-tables)
+ (lib-be/with-metadata-provider-cache
+ (let [mp (lib-be/application-database-metadata-provider database-id)
+ _ (lib.metadata/bulk-metadata mp :metadata/table (keys accessible-tables))
+
+ tables-with-columns
+ (keep (fn [[table-id table]]
+ (when-let [columns (seq (fetch-table-columns mp table-id))]
+ {:id table-id
+ :name (:name table)
+ :schema (:schema table)
+ :display_name (:display_name table)
+ :description (:description table)
+ :columns columns}))
+ accessible-tables)
+
+ all-columns (mapcat :columns tables-with-columns)
+ fk-targets-map (fetch-fk-targets all-columns)]
+
+ (mapv (fn [table]
+ (update table :columns
+ (fn [cols]
+ (mapv (fn [col]
+ (let [fk-info (get fk-targets-map (:fk_target_field_id col))]
+ (cond-> {:id (:id col)
+ :name (:name col)
+ :database_type (:database_type col)
+ :description (:description col)
+ :semantic_type (some-> (:semantic_type col) name)}
+ fk-info (assoc :fk_target {:table_name (:table fk-info)
+ :field_name (:field fk-info)}))))
+ cols))))
+ tables-with-columns)))))))
diff --git a/src/metabase/llm/costs.clj b/src/metabase/llm/costs.clj
new file mode 100644
index 00000000000..406ecbe1cb8
--- /dev/null
+++ b/src/metabase/llm/costs.clj
@@ -0,0 +1,38 @@
+(ns metabase.llm.costs
+ "LLM cost estimation utils."
+ (:require
+ [clojure.string :as str]))
+
+(def ^:private model-pricing
+ "Model pricing by model family. Prices are in USD. :input and :output are per :scale tokens."
+ {"anthropic/claude-opus-4-5" {:input 5.00 :output 25.00 :scale 1000000}
+ "anthropic/claude-opus-4-1" {:input 15.00 :output 75.00 :scale 1000000}
+ "anthropic/claude-opus-4" {:input 15.00 :output 75.00 :scale 1000000}
+ "anthropic/claude-sonnet-4-5" {:input 3.00 :output 15.00 :scale 1000000}
+ "anthropic/claude-sonnet-4" {:input 3.00 :output 15.00 :scale 1000000}
+ "anthropic/claude-haiku-4-5" {:input 1.00 :output 5.00 :scale 1000000}
+ "anthropic/claude-haiku-3-5" {:input 0.80 :output 4.00 :scale 1000000}
+ "anthropic/claude-haiku-3" {:input 0.25 :output 1.25 :scale 1000000}})
+
+(defn- normalize-model-name
+ "Normalize a raw model name to the `provider/family` format used in the pricing table.
+ Strips the date suffix (e.g. -20250514) and adds the anthropic/ prefix if missing.
+
+ E.g. \"claude-sonnet-4-5-20250929\" -> \"anthropic/claude-sonnet-4-5\""
+ [model]
+ (when model
+ (let [stripped (str/replace model #"-\d{8}$" "")]
+ (if (str/starts-with? stripped "anthropic/")
+ stripped
+ (str "anthropic/" stripped)))))
+
+(defn estimate
+ "Estimate the cost in USD for token usage.
+
+ Accepts raw model names (e.g. \"claude-sonnet-4-5-20250929\") and normalizes
+ them to match the pricing table. Returns 0.0 for unknown models."
+ [{:keys [model prompt completion]}]
+ (if-let [{:keys [input output scale]} (get model-pricing (normalize-model-name model))]
+ (+ (* (/ (double prompt) scale) input)
+ (* (/ (double completion) scale) output))
+ 0.0))
diff --git a/src/metabase/llm/init.clj b/src/metabase/llm/init.clj
new file mode 100644
index 00000000000..fd1c8577932
--- /dev/null
+++ b/src/metabase/llm/init.clj
@@ -0,0 +1,3 @@
+(ns metabase.llm.init
+ (:require
+ [metabase.llm.settings]))
diff --git a/src/metabase/llm/settings.clj b/src/metabase/llm/settings.clj
new file mode 100644
index 00000000000..abc9efaa113
--- /dev/null
+++ b/src/metabase/llm/settings.clj
@@ -0,0 +1,94 @@
+(ns metabase.llm.settings
+ "Settings for OSS LLM integration."
+ (:require
+ [clojure.string :as str]
+ [metabase.premium-features.core :as premium-features]
+ [metabase.settings.core :as setting :refer [defsetting]]
+ [metabase.util.i18n :refer [deferred-tru tru]]))
+
+(defsetting llm-anthropic-api-key
+ (deferred-tru "Anthropic API key for AI-assisted SQL generation.")
+ :encryption :when-encryption-key-set
+ :visibility :settings-manager
+ :export? false
+ :setter (fn [new-value]
+ (let [trimmed (when (string? new-value)
+ (not-empty (str/trim new-value)))]
+ (when (and trimmed (not (str/starts-with? trimmed "sk-ant-")))
+ (throw (ex-info (tru "Invalid Anthropic API key format. Key must start with ''sk-ant-''.")
+ {:status-code 400})))
+ (setting/set-value-of-type! :string :llm-anthropic-api-key trimmed)))
+ :doc false)
+
+(defsetting llm-anthropic-model
+ (deferred-tru "Anthropic model for AI-assisted SQL generation.")
+ :encryption :no
+ :visibility :settings-manager
+ :default "claude-opus-4-5-20251101"
+ :export? false
+ :doc false)
+
+(defsetting llm-anthropic-api-url
+ (deferred-tru "Anthropic Base API URL for AI-assisted SQL generation.")
+ :encryption :no
+ :visibility :internal
+ :default "https://api.anthropic.com"
+ :export? false
+ :doc false)
+
+(defsetting llm-anthropic-api-version
+ (deferred-tru "Anthropic API version for AI-assisted SQL generation.")
+ :encryption :no
+ :visibility :internal
+ :default "2023-06-01"
+ :export? false
+ :doc false)
+
+(defsetting llm-max-tokens
+ (deferred-tru "Maximum tokens for LLM responses.")
+ :type :integer
+ :default 4096
+ :visibility :settings-manager
+ :export? false
+ :doc false)
+
+(defsetting llm-request-timeout-ms
+ (deferred-tru "Socket timeout in milliseconds for LLM API requests.")
+ :type :integer
+ :default 60000
+ :visibility :settings-manager
+ :export? false
+ :doc false)
+
+(defsetting llm-connection-timeout-ms
+ (deferred-tru "Connection timeout in milliseconds for LLM API requests.")
+ :type :integer
+ :default 5000
+ :visibility :settings-manager
+ :export? false
+ :doc false)
+
+(defsetting llm-rate-limit-per-user
+ (deferred-tru "Maximum SQL generation requests per user per minute.")
+ :type :integer
+ :default 20
+ :visibility :settings-manager
+ :export? false
+ :doc false)
+
+(defsetting llm-rate-limit-per-ip
+ (deferred-tru "Maximum SQL generation requests per IP address per minute.")
+ :type :integer
+ :default 100
+ :visibility :settings-manager
+ :export? false
+ :doc false)
+
+(defsetting llm-sql-generation-enabled
+ (deferred-tru "Whether AI-assisted SQL generation is enabled.")
+ :visibility :public
+ :type :boolean
+ :setter :none
+ :export? false
+ :getter (fn [] (or (some? (llm-anthropic-api-key))
+ (premium-features/enable-metabot-v3?))))
diff --git a/src/metabase/premium_features/core.clj b/src/metabase/premium_features/core.clj
index 14cb73222bb..32c184ac5da 100644
--- a/src/metabase/premium_features/core.clj
+++ b/src/metabase/premium_features/core.clj
@@ -85,8 +85,6 @@
has-attached-dwh?
hide-embed-branding?
is-hosted?
- offer-metabase-ai-trial?
- offer-metabase-ai-paid?
premium-embedding-token
site-uuid-for-premium-features-token-checks
table-data-editing?
diff --git a/src/metabase/premium_features/settings.clj b/src/metabase/premium_features/settings.clj
index 55b302a8cd7..7bf18b6a973 100644
--- a/src/metabase/premium_features/settings.clj
+++ b/src/metabase/premium_features/settings.clj
@@ -268,14 +268,6 @@
"Should Metabase do AI analysis on entities?"
:ai-entity-analysis)
-(define-premium-feature ^{:added "0.55.0"} offer-metabase-ai-trial?
- "Should we offer a trial of the Metabase AI add-on?"
- :offer-metabase-ai)
-
-(define-premium-feature ^{:added "0.56.0"} offer-metabase-ai-paid?
- "Should we offer the paid Metabase AI add-on?"
- :offer-metabase-ai-tiered)
-
(define-premium-feature ^{:added "0.56.0"} cloud-custom-smtp?
"Can Metabase have a custom smtp details separate from the default Cloud details."
:cloud-custom-smtp)
@@ -355,8 +347,6 @@
:hosting (is-hosted?)
:llm_autodescription (enable-llm-autodescription?)
:metabot_v3 (enable-metabot-v3?)
- :offer_metabase_ai (offer-metabase-ai-trial?)
- :offer_metabase_ai_tiered (offer-metabase-ai-paid?)
:official_collections (enable-official-collections?)
:query_reference_validation (enable-query-reference-validation?)
:remote_sync (enable-remote-sync?)
diff --git a/src/metabase/util/json.clj b/src/metabase/util/json.clj
index 04a998cddab..f81e196cda0 100644
--- a/src/metabase/util/json.clj
+++ b/src/metabase/util/json.clj
@@ -5,7 +5,8 @@
[cheshire.core :as cheshire]
[cheshire.factory]
[cheshire.generate :as json.generate]
- [clojure.java.io :as io])
+ [clojure.java.io :as io]
+ [clojure.string :as str])
(:import
(com.fasterxml.jackson.core JsonGenerator)
(java.io InputStream Reader)))
@@ -89,3 +90,31 @@
"Decode a value from a JSON from a string, InputStream, or Reader, keywordizing map keys."
[source]
(decode source true))
+
+(defn- parse-charset
+ "Parse charset from content-type header, e.g. 'application/json; charset=utf-8' -> 'utf-8'.
+ Returns nil if no charset is specified."
+ [content-type]
+ (when content-type
+ (second (re-find #"(?i)charset=([^\s;]+)" content-type))))
+
+(defn- decode-with-encoding
+ "Decode JSON from body, respecting the charset from content-type header."
+ [body content-type]
+ (let [encoding (or (parse-charset content-type) "UTF-8")]
+ (cond
+ (string? body) (decode body true)
+ (instance? InputStream body) (with-open [r (io/reader body :encoding encoding)]
+ (cheshire/parse-stream r true))
+ (instance? Reader body) (cheshire/parse-stream body true)
+ (nil? body) nil
+ :else (decode body true))))
+
+(defn decode-body
+ "Given a response map, decodes body if headers indicate it's a JSON response, or just slurps if it's not a string."
+ [res]
+ (let [ctype (get-in res [:headers "content-type"])]
+ (cond
+ (some-> ctype (str/starts-with? "application/json")) (update res :body decode-with-encoding ctype)
+ (string? (:body res)) res
+ :else (update res :body slurp))))
diff --git a/test/metabase/analytics/stats_test.clj b/test/metabase/analytics/stats_test.clj
index 11ddeaba5a1..e32bb1c75f4 100644
--- a/test/metabase/analytics/stats_test.clj
+++ b/test/metabase/analytics/stats_test.clj
@@ -568,9 +568,7 @@
:llm-autodescription
:query-reference-validation
:cloud-custom-smtp
- :session-timeout-config
- :offer-metabase-ai
- :offer-metabase-ai-tiered})
+ :session-timeout-config})
(deftest every-feature-is-accounted-for-test
(testing "Is every premium feature either tracked under the :features key, or intentionally excluded?"
diff --git a/test/metabase/llm/anthropic_test.clj b/test/metabase/llm/anthropic_test.clj
new file mode 100644
index 00000000000..29fe5f13245
--- /dev/null
+++ b/test/metabase/llm/anthropic_test.clj
@@ -0,0 +1,174 @@
+(ns metabase.llm.anthropic-test
+ (:require
+ [clj-http.client :as http]
+ [clojure.test :refer :all]
+ [metabase.llm.anthropic :as anthropic]
+ [metabase.test :as mt]
+ [metabase.util.json :as json]))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------- extract-tool-input Tests -------------------------------------------
+
+(deftest ^:parallel extract-tool-input-test
+ (testing "valid tool_use response extracts input"
+ (let [response {:content [{:type "tool_use"
+ :id "123"
+ :name "generate_sql"
+ :input {:sql "SELECT 1"}}]}]
+ (is (= {:sql "SELECT 1"}
+ (#'anthropic/extract-tool-input response)))))
+
+ (testing "multiple content blocks finds tool_use"
+ (let [response {:content [{:type "text" :text "thinking..."}
+ {:type "tool_use"
+ :id "456"
+ :name "generate_sql"
+ :input {:sql "SELECT 1" :explanation "Simple query"}}]}]
+ (is (= {:sql "SELECT 1" :explanation "Simple query"}
+ (#'anthropic/extract-tool-input response)))))
+
+ (testing "no tool_use block returns nil"
+ (let [response {:content [{:type "text" :text "no tool"}]}]
+ (is (nil? (#'anthropic/extract-tool-input response)))))
+
+ (testing "empty content returns nil"
+ (is (nil? (#'anthropic/extract-tool-input {:content []})))
+ (is (nil? (#'anthropic/extract-tool-input {}))))
+
+ (testing "tool_use without input returns nil"
+ (let [response {:content [{:type "tool_use" :id "123" :name "generate_sql"}]}]
+ (is (nil? (#'anthropic/extract-tool-input response)))))
+
+ (testing "returns first tool_use when multiple present"
+ (let [response {:content [{:type "tool_use"
+ :id "1"
+ :name "generate_sql"
+ :input {:sql "SELECT 1"}}
+ {:type "tool_use"
+ :id "2"
+ :name "generate_sql"
+ :input {:sql "SELECT 2"}}]}]
+ (is (= {:sql "SELECT 1"}
+ (#'anthropic/extract-tool-input response))))))
+
+;;; ------------------------------------------- build-request-headers Tests -------------------------------------------
+
+(deftest ^:parallel build-request-headers-test
+ (testing "includes required headers"
+ (let [headers (#'anthropic/build-request-headers "sk-test-key")]
+ (is (= "sk-test-key" (get headers "x-api-key")))
+ (is (= "2023-06-01" (get headers "anthropic-version")))
+ (is (= "application/json" (get headers "content-type"))))))
+
+;;; ------------------------------------------- build-request-body Tests -------------------------------------------
+
+(deftest build-request-body-test
+ (testing "includes required fields with default max_tokens"
+ (mt/with-temporary-setting-values [llm-max-tokens nil]
+ (let [body (#'anthropic/build-request-body {:model "claude-sonnet-4-5-20250929"
+ :messages [{:role "user" :content "test"}]})]
+ (is (= "claude-sonnet-4-5-20250929" (:model body)))
+ (is (= 4096 (:max_tokens body)))
+ (is (= [{:role "user" :content "test"}] (:messages body)))
+ (is (vector? (:tools body)))
+ (is (= {:type "tool" :name "generate_sql"} (:tool_choice body))))))
+
+ (testing "uses configured max_tokens setting"
+ (mt/with-temporary-setting-values [llm-max-tokens 8192]
+ (let [body (#'anthropic/build-request-body {:model "claude-sonnet-4-5-20250929"
+ :messages [{:role "user" :content "test"}]})]
+ (is (= 8192 (:max_tokens body))))))
+
+ (testing "includes system prompt when provided"
+ (let [body (#'anthropic/build-request-body {:model "claude-sonnet-4-5-20250929"
+ :system "You are a SQL expert"
+ :messages [{:role "user" :content "test"}]})]
+ (is (= "You are a SQL expert" (:system body)))))
+
+ (testing "omits system when not provided"
+ (let [body (#'anthropic/build-request-body {:model "claude-sonnet-4-5-20250929"
+ :messages [{:role "user" :content "test"}]})]
+ (is (not (contains? body :system))))))
+
+;;; ------------------------------------------- generate-sql-tool Tests -------------------------------------------
+
+(deftest ^:parallel generate-sql-tool-test
+ (testing "tool definition has correct structure"
+ (let [tool @#'anthropic/generate-sql-tool]
+ (is (= "generate_sql" (:name tool)))
+ (is (string? (:description tool)))
+ (is (= "object" (get-in tool [:input_schema :type])))
+ (is (contains? (get-in tool [:input_schema :properties]) :sql))
+ (is (= ["sql"] (get-in tool [:input_schema :required]))))))
+
+;;; ------------------------------------------- chat-completion Tests -------------------------------------------
+
+(deftest chat-completion-not-configured-test
+ (testing "throws when API key not configured"
+ (mt/with-temporary-setting-values [llm-anthropic-api-key nil]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"not configured"
+ (anthropic/chat-completion {:messages [{:role "user" :content "test"}]}))))))
+
+;;; ------------------------------------------- list-models Tests -------------------------------------------
+
+(deftest list-models-requires-api-key-test
+ (testing "list-models throws when API key is not configured"
+ (mt/with-temporary-setting-values [llm-anthropic-api-key nil]
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"not configured"
+ (anthropic/list-models))))))
+
+(deftest list-models-success-test
+ (testing "list-models returns sorted models with selected fields"
+ (let [mock-response-body {:data [{:id "claude-sonnet-4-20250514"
+ :display_name "Claude Sonnet 4"
+ :created_at "2025-05-14T00:00:00Z"
+ :type "model"}
+ {:id "claude-sonnet-4-5-20250929"
+ :display_name "Claude Sonnet 4.5"
+ :created_at "2025-09-29T00:00:00Z"
+ :type "model"}
+ {:id "claude-opus-4-5-20251101"
+ :display_name "Claude Opus 4.5"
+ :created_at "2025-11-01T00:00:00Z"
+ :type "model"}]
+ :first_id "claude-sonnet-4-20250514"
+ :last_id "claude-opus-4-5-20251101"
+ :has_more false}]
+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test-key"]
+ (with-redefs [http/get (constantly {:status 200
+ :body (json/encode mock-response-body)})]
+ (let [result (anthropic/list-models)]
+ ;; Should be sorted by created_at descending (newest first)
+ (is (= [{:id "claude-opus-4-5-20251101" :display_name "Claude Opus 4.5"}
+ {:id "claude-sonnet-4-5-20250929" :display_name "Claude Sonnet 4.5"}
+ {:id "claude-sonnet-4-20250514" :display_name "Claude Sonnet 4"}]
+ (:models result)))))))))
+
+(deftest chat-completion-returns-usage-test
+ (testing "chat-completion returns result, usage, and duration"
+ (let [mock-response {:body {:id "msg_123"
+ :model "claude-sonnet-4-5-20250929"
+ :content [{:type "tool_use"
+ :id "tool_123"
+ :name "generate_sql"
+ :input {:sql "SELECT * FROM users"
+ :explanation "Fetches all users"}}]
+ :usage {:input_tokens 1500
+ :output_tokens 250}}}]
+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test-key"
+ llm-anthropic-model "claude-sonnet-4-5-20250929"]
+ (with-redefs [http/post (constantly mock-response)]
+ (let [result (anthropic/chat-completion {:system "You are a SQL expert"
+ :messages [{:role "user" :content "get all users"}]})]
+ (is (=? {:result {:sql "SELECT * FROM users"
+ :explanation "Fetches all users"}
+ :duration-ms #(and (number? %) (pos? %))
+ :usage {:model "claude-sonnet-4-5-20250929"
+ :prompt 1500
+ :completion 250}}
+ result))))))))
diff --git a/test/metabase/llm/api_test.clj b/test/metabase/llm/api_test.clj
new file mode 100644
index 00000000000..912b0c4d0af
--- /dev/null
+++ b/test/metabase/llm/api_test.clj
@@ -0,0 +1,275 @@
+(ns metabase.llm.api-test
+ (:require
+ [clojure.set :as set]
+ [clojure.string :as str]
+ [clojure.test :refer :all]
+ [metabase.analytics.core :as analytics]
+ [metabase.analytics.snowplow :as snowplow]
+ [metabase.llm.anthropic :as llm.anthropic]
+ [metabase.llm.api :as api]
+ [metabase.llm.context :as llm.context]
+ [metabase.premium-features.core :as premium-features]
+ [metabase.test :as mt]))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------- database-dialect Tests -------------------------------------------
+
+(deftest database-engine-test
+ (mt/with-temp [:model/Database postgres-db {:engine :postgres}
+ :model/Database mysql-db {:engine :mysql}
+ :model/Database bigquery-db {:engine :bigquery}]
+ (testing "returns engine keyword for database"
+ (is (= :postgres (#'api/database-engine (:id postgres-db))))
+ (is (= :mysql (#'api/database-engine (:id mysql-db))))
+ (is (= :bigquery (#'api/database-engine (:id bigquery-db)))))
+
+ (testing "nil database returns nil"
+ (is (nil? (#'api/database-engine nil))))
+
+ (testing "non-existent database returns nil"
+ (is (nil? (#'api/database-engine 99999999))))))
+
+;;; ------------------------------------------- load-dialect-instructions Tests -------------------------------------------
+
+(deftest load-dialect-instructions-test
+ (testing "known engine with file returns content"
+ (let [instructions (#'api/load-dialect-instructions :postgres)]
+ (is (string? instructions))
+ (is (str/includes? instructions "PostgreSQL"))))
+
+ (testing "nil engine returns nil"
+ (is (nil? (#'api/load-dialect-instructions nil)))))
+
+;;; ------------------------------------------- Table ID extraction Tests -------------------------------------------
+
+(deftest extract-frontend-table-ids-test
+ (testing "extracts table IDs from referenced_entities"
+ (let [entities [{:model "table" :id 1}
+ {:model "table" :id 2}
+ {:model "card" :id 999}
+ {:model "table" :id 3}]]
+ (is (= #{1 2 3}
+ (->> entities
+ (filter #(= "table" (:model %)))
+ (map :id)
+ set)))))
+
+ (testing "returns empty set for empty input"
+ (is (= #{}
+ (->> []
+ (filter #(= "table" (:model %)))
+ (map :id)
+ set))))
+
+ (testing "returns empty set when no tables present"
+ (let [entities [{:model "card" :id 1}
+ {:model "question" :id 2}]]
+ (is (= #{}
+ (->> entities
+ (filter #(= "table" (:model %)))
+ (map :id)
+ set))))))
+
+(deftest table-id-union-test
+ (testing "all sources merged via set union"
+ (let [frontend #{1 2}
+ explicit #{2 3}
+ implicit #{3 4}]
+ (is (= #{1 2 3 4}
+ (set/union (or frontend #{})
+ (or explicit #{})
+ (or implicit #{}))))))
+
+ (testing "nil sources treated as empty sets"
+ (is (= #{1 2}
+ (set/union (or nil #{})
+ (or #{1 2} #{})
+ (or nil #{}))))))
+
+;;; ------------------------------------------- Integration with context Tests -------------------------------------------
+
+(deftest table-mention-parsing-integration-test
+ (testing "explicit mentions parsed from prompt"
+ (let [prompt "Join [Orders](metabase://table/123) with [Users](metabase://table/456)"]
+ (is (= #{123 456}
+ (llm.context/parse-table-mentions prompt)))))
+
+ (testing "multiple mentions of same table deduplicated"
+ (let [prompt "[Orders](metabase://table/123) and again [Orders](metabase://table/123)"]
+ (is (= #{123}
+ (llm.context/parse-table-mentions prompt))))))
+
+;;; ------------------------------------------- build-system-prompt Tests -------------------------------------------
+
+(deftest build-system-prompt-test
+ (testing "builds prompt with required parameters"
+ (let [prompt (#'api/build-system-prompt {:dialect "PostgreSQL"
+ :schema-ddl "CREATE TABLE users (id INTEGER);"})]
+ (is (string? prompt))
+ (is (str/includes? prompt "PostgreSQL"))
+ (is (str/includes? prompt "CREATE TABLE users"))))
+
+ (testing "includes dialect instructions when provided"
+ (let [prompt (#'api/build-system-prompt {:dialect "PostgreSQL"
+ :schema-ddl "CREATE TABLE users (id INTEGER);"
+ :dialect-instructions "Use LIMIT instead of TOP"})]
+ (is (str/includes? prompt "Use LIMIT instead of TOP"))))
+
+ (testing "includes source SQL when provided"
+ (let [prompt (#'api/build-system-prompt {:dialect "PostgreSQL"
+ :schema-ddl "CREATE TABLE users (id INTEGER);"
+ :source-sql "SELECT * FROM users"})]
+ (is (str/includes? prompt "SELECT * FROM users")))))
+
+;;; ------------------------------------------- Error handling Tests -------------------------------------------
+
+(deftest generate-sql-error-handling-test
+ (mt/with-temp [:model/Database db {:engine :postgres}]
+ (testing "403 when LLM not configured"
+ (mt/with-temporary-setting-values [llm-anthropic-api-key nil]
+ (let [response (mt/user-http-request :rasta :post 403 "llm/generate-sql"
+ {:prompt "test"
+ :database_id (:id db)})]
+ (is (str/includes? (str response) "not configured")))))
+
+ (testing "400 when no tables found"
+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test"]
+ (let [response (mt/user-http-request :rasta :post 400 "llm/generate-sql"
+ {:prompt "no table mentions here"
+ :database_id (:id db)})]
+ (is (str/includes? (str response) "No tables found")))))))
+
+(deftest list-models-unconfigured-test
+ (testing "Returns 403 when LLM is not configured"
+ (mt/with-temporary-setting-values [llm-anthropic-api-key nil]
+ (let [response (mt/user-http-request :rasta :get 403 "llm/list-models")]
+ (is (str/includes? (str response) "not configured"))))))
+
+;;; ------------------------------------------- Snowplow Tests -------------------------------------------
+
+(deftest generate-sql-snowplow-success-test
+ (testing "successful /generate-sql call tracks both token_usage and simple_event"
+ (mt/with-temp [:model/Database db {:engine :postgres}
+ :model/Table table {:db_id (:id db) :name "users" :schema "public"}
+ :model/Field _ {:table_id (:id table) :name "id" :base_type :type/Integer}
+ :model/Field _ {:table_id (:id table) :name "name" :base_type :type/Text}]
+ (let [tracked-events (atom [])
+ mock-chat-response {:result {:sql "SELECT * FROM users"}
+ :usage {:model "anthropic/claude-sonnet-4-5"
+ :prompt 1000
+ :completion 200}
+ :duration-ms 500}]
+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test"]
+ (with-redefs [llm.anthropic/chat-completion (constantly mock-chat-response)
+ snowplow/track-event! (fn [schema data user-id]
+ (swap! tracked-events conj {:schema schema
+ :data data
+ :user-id user-id}))]
+ (let [response (mt/user-http-request :rasta :post 200 "llm/generate-sql"
+ {:prompt "get all users"
+ :database_id (:id db)
+ :referenced_entities [{:model "table" :id (:id table)}]})]
+ (is (= "SELECT * FROM users" (:sql response)))
+ (testing "token_usage event"
+ (let [token-events (filter #(= :snowplow/token_usage (:schema %)) @tracked-events)]
+ (is (=? [{:schema :snowplow/token_usage
+ :data {:model-id "anthropic/claude-sonnet-4-5"
+ :prompt-tokens 1000
+ :completion-tokens 200
+ :total-tokens 1200
+ :duration-ms 500
+ :source "oss_metabot"
+ :tag "oss-sqlgen"}}]
+ token-events))))
+ (testing "simple_event"
+ (let [simple-events (filter #(= :snowplow/simple_event (:schema %)) @tracked-events)]
+ (is (=? [{:schema :snowplow/simple_event
+ :data {:event "metabot_oss_sqlgen_used"
+ :duration_ms int?
+ :result "success"
+ :event_detail "postgres"}}]
+ simple-events)))))))))))
+
+(deftest generate-sql-snowplow-failure-test
+ (testing "failed /generate-sql call tracks simple_event with failure result"
+ (mt/with-temp [:model/Database db {:engine :postgres}
+ :model/Table table {:db_id (:id db) :name "users" :schema "public"}
+ :model/Field _ {:table_id (:id table) :name "id" :base_type :type/Integer}]
+ (let [tracked-events (atom [])]
+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test"]
+ (with-redefs [llm.anthropic/chat-completion (fn [_] (throw (Exception. "API error")))
+ snowplow/track-event! (fn [schema data user-id]
+ (swap! tracked-events conj {:schema schema
+ :data data
+ :user-id user-id}))]
+ (mt/user-http-request :rasta :post 500 "llm/generate-sql"
+ {:prompt "get all users"
+ :database_id (:id db)
+ :referenced_entities [{:model "table" :id (:id table)}]})
+ (testing "no token_usage event on failure (chat-completion call failed)"
+ (let [token-events (filter #(= :snowplow/token_usage (:schema %)) @tracked-events)]
+ (is (empty? token-events))))
+ (testing "simple_event with failure result"
+ (let [simple-events (filter #(= :snowplow/simple_event (:schema %)) @tracked-events)]
+ (is (=? [{:schema :snowplow/simple_event
+ :data {:event "metabot_oss_sqlgen_used"
+ :duration_ms int?
+ :result "failure"
+ :event_detail "postgres"}}]
+ simple-events))))))))))
+
+;;; ------------------------------------------- Token Usage Tracking Tests -------------------------------------------
+
+(deftest track-token-usage-with-uuid-test
+ (testing "tracks usage with analytics uuid when no premium token is available"
+ (let [tracked-events (atom [])
+ test-analytics-uuid "test-analytics-uuid-12345"]
+ (with-redefs [snowplow/track-event! (fn [schema data user-id]
+ (swap! tracked-events conj {:schema schema
+ :data data
+ :user-id user-id}))
+ premium-features/premium-embedding-token (constantly nil)
+ analytics/analytics-uuid (constantly test-analytics-uuid)]
+ (#'api/track-token-usage! {:model "anthropic/claude-sonnet-4-5"
+ :prompt 1000
+ :completion 500
+ :duration-ms 1234
+ :user-id 42
+ :source "oss_metabot"
+ :tag "oss-sqlgen"})
+ (is (= 1 (count @tracked-events)))
+ (let [{:keys [schema data user-id]} (first @tracked-events)]
+ (is (= :snowplow/token_usage schema))
+ (is (= 42 user-id))
+ (is (=? {:hashed-metabase-license-token (str "oss__" test-analytics-uuid)
+ :request-id #"[a-h0-9]{32}" ; UUID hex format (no dashes)
+ :model-id "anthropic/claude-sonnet-4-5"
+ :total-tokens 1500
+ :prompt-tokens 1000
+ :completion-tokens 500
+ :estimated-costs-usd pos?
+ :duration-ms 1234
+ :source "oss_metabot"
+ :tag "oss-sqlgen"}
+ data)))))))
+
+(deftest track-token-usage-with-premium-token-test
+ (testing "hashes premium token when available"
+ (let [tracked-events (atom [])]
+ (with-redefs [snowplow/track-event! (fn [schema data user-id]
+ (swap! tracked-events conj {:schema schema
+ :data data
+ :user-id user-id}))
+ premium-features/premium-embedding-token (constantly "test-premium-token")]
+ (#'api/track-token-usage! {:model "anthropic/claude-sonnet-4-5"
+ :prompt 100
+ :completion 50
+ :duration-ms 100
+ :user-id 1
+ :source "test"
+ :tag "test"})
+ (let [{:keys [data]} (first @tracked-events)]
+ ;; Should be a SHA-256 hash (64 hex chars), not "oss__*"
+ (is (=? {:hashed-metabase-license-token #"[0-9a-f]{64}"}
+ data)))))))
diff --git a/test/metabase/llm/context_test.clj b/test/metabase/llm/context_test.clj
new file mode 100644
index 00000000000..706bc21679b
--- /dev/null
+++ b/test/metabase/llm/context_test.clj
@@ -0,0 +1,396 @@
+(ns metabase.llm.context-test
+ (:require
+ [clojure.string :as str]
+ [clojure.test :refer :all]
+ [metabase.llm.context :as context]
+ [metabase.test :as mt]))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------- parse-table-mentions Tests -------------------------------------------
+
+(deftest parse-table-mentions-test
+ (testing "extracts single table ID"
+ (is (= #{42}
+ (context/parse-table-mentions "Show [Orders](metabase://table/42)"))))
+
+ (testing "extracts multiple table IDs"
+ (is (= #{42 15}
+ (context/parse-table-mentions
+ "Join [Orders](metabase://table/42) with [Products](metabase://table/15)"))))
+
+ (testing "handles no mentions"
+ (is (= #{} (context/parse-table-mentions "Show me all sales"))))
+
+ (testing "handles nil input"
+ (is (nil? (context/parse-table-mentions nil))))
+
+ (testing "handles empty string"
+ (is (= #{} (context/parse-table-mentions ""))))
+
+ (testing "ignores malformed mentions - invalid ID"
+ (is (= #{42}
+ (context/parse-table-mentions
+ "[Valid](metabase://table/42) [Invalid](metabase://table/abc)"))))
+
+ (testing "handles display names with special characters"
+ (is (= #{123}
+ (context/parse-table-mentions
+ "[Order's & Items](metabase://table/123)"))))
+
+ (testing "handles multiple mentions of same table (returns set)"
+ (is (= #{42}
+ (context/parse-table-mentions
+ "[Orders](metabase://table/42) and also [Orders Table](metabase://table/42)"))))
+
+ (testing "handles text before and after mentions"
+ (is (= #{1 2}
+ (context/parse-table-mentions
+ "Please show me data from [Users](metabase://table/1) joined with [Orders](metabase://table/2) filtered by date")))))
+
+;;; ------------------------------------------ DDL Formatting Tests ------------------------------------------
+
+(deftest format-schema-ddl-test
+ (testing "formats simple table"
+ (let [tables [{:name "users" :schema nil :columns [{:name "id" :database_type "INTEGER"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (= "CREATE TABLE users (\n id INTEGER\n);" result))))
+
+ (testing "formats table with schema"
+ (let [tables [{:name "users" :schema "public" :columns [{:name "id" :database_type "INTEGER"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (= "CREATE TABLE public.users (\n id INTEGER\n);" result))))
+
+ (testing "formats table with multiple columns"
+ (let [tables [{:name "orders"
+ :schema nil
+ :columns [{:name "id" :database_type "INTEGER"}
+ {:name "total" :database_type "DECIMAL"}
+ {:name "created_at" :database_type "TIMESTAMP"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (str/includes? result "id INTEGER"))
+ (is (str/includes? result "total DECIMAL"))
+ (is (str/includes? result "created_at TIMESTAMP"))))
+
+ (testing "escapes special characters in identifiers"
+ (let [tables [{:name "user data" :schema nil :columns [{:name "first name" :database_type "VARCHAR"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (str/includes? result "\"user data\""))
+ (is (str/includes? result "\"first name\""))))
+
+ (testing "formats multiple tables"
+ (let [tables [{:name "users" :schema nil :columns [{:name "id" :database_type "INTEGER"}]}
+ {:name "orders" :schema nil :columns [{:name "id" :database_type "INTEGER"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (str/includes? result "CREATE TABLE users"))
+ (is (str/includes? result "CREATE TABLE orders"))))
+
+ (testing "formats table with description"
+ (let [tables [{:name "orders"
+ :schema "public"
+ :description "Customer purchase transactions"
+ :columns [{:name "id" :database_type "INTEGER"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (= "-- Customer purchase transactions\nCREATE TABLE public.orders (\n id INTEGER\n);" result))))
+
+ (testing "formats table without description (no comment line)"
+ (let [tables [{:name "orders"
+ :schema nil
+ :description nil
+ :columns [{:name "id" :database_type "INTEGER"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (= "CREATE TABLE orders (\n id INTEGER\n);" result)))))
+
+;;; ----------------------------------------- build-schema-context Tests -----------------------------------------
+
+(deftest build-schema-context-test
+ (mt/with-test-user :crowberto
+ (mt/with-temp [:model/Database db {}
+ :model/Table table1 {:db_id (:id db) :name "orders" :schema "public"}
+ :model/Field _f1 {:table_id (:id table1) :name "id" :database_type "INTEGER" :base_type :type/Integer}
+ :model/Field _f2 {:table_id (:id table1) :name "total" :database_type "DECIMAL" :base_type :type/Decimal}]
+ (testing "returns DDL for accessible tables"
+ (let [result (context/build-schema-context (:id db) #{(:id table1)})]
+ (is (some? result))
+ (is (str/includes? result "CREATE TABLE"))
+ (is (str/includes? result "orders"))
+ (is (str/includes? result "id"))
+ (is (str/includes? result "total"))))
+
+ (testing "returns nil for empty table-ids"
+ (is (nil? (context/build-schema-context (:id db) #{}))))
+
+ (testing "returns nil for nil table-ids"
+ (is (nil? (context/build-schema-context (:id db) nil))))
+
+ (testing "returns nil for nil database-id"
+ (is (nil? (context/build-schema-context nil #{(:id table1)}))))
+
+ (testing "returns nil for non-existent tables"
+ (is (nil? (context/build-schema-context (:id db) #{99999})))))))
+
+(deftest build-schema-context-multiple-tables-test
+ (mt/with-test-user :crowberto
+ (mt/with-temp [:model/Database db {}
+ :model/Table t1 {:db_id (:id db) :name "users" :schema "public"}
+ :model/Field _f1 {:table_id (:id t1) :name "id" :database_type "INTEGER" :base_type :type/Integer}
+ :model/Field _f2 {:table_id (:id t1) :name "email" :database_type "VARCHAR" :base_type :type/Text}
+ :model/Table t2 {:db_id (:id db) :name "orders" :schema "public"}
+ :model/Field _f3 {:table_id (:id t2) :name "id" :database_type "INTEGER" :base_type :type/Integer}
+ :model/Field _f4 {:table_id (:id t2) :name "user_id" :database_type "INTEGER" :base_type :type/Integer}]
+ (testing "returns DDL for multiple tables"
+ (let [result (context/build-schema-context (:id db) #{(:id t1) (:id t2)})]
+ (is (some? result))
+ (is (str/includes? result "users"))
+ (is (str/includes? result "orders"))
+ (is (str/includes? result "email"))
+ (is (str/includes? result "user_id")))))))
+
+;;; ----------------------------------------- Fingerprint Formatting Tests -----------------------------------------
+
+(deftest format-numeric-stats-test
+ (testing "formats integer range"
+ (is (= "range: 1-100, avg: 50"
+ (#'context/format-numeric-stats {:min 1 :max 100 :avg 50}))))
+
+ (testing "formats decimal range"
+ (is (= "range: 0.00-999.99, avg: 127.50"
+ (#'context/format-numeric-stats {:min 0.0 :max 999.99 :avg 127.5}))))
+
+ (testing "handles missing avg"
+ (is (= "range: 0-100"
+ (#'context/format-numeric-stats {:min 0 :max 100}))))
+
+ (testing "returns nil when min/max missing"
+ (is (nil? (#'context/format-numeric-stats {:avg 50})))
+ (is (nil? (#'context/format-numeric-stats {})))))
+
+(deftest format-temporal-stats-test
+ (testing "formats date range"
+ (is (= "2020-01-01 to 2024-12-31"
+ (#'context/format-temporal-stats {:earliest "2020-01-01T00:00:00Z"
+ :latest "2024-12-31T23:59:59Z"}))))
+
+ (testing "handles short date strings"
+ (is (= "2020-01-01 to 2024-12-31"
+ (#'context/format-temporal-stats {:earliest "2020-01-01"
+ :latest "2024-12-31"}))))
+
+ (testing "returns nil when dates missing"
+ (is (nil? (#'context/format-temporal-stats {:earliest "2020-01-01"})))
+ (is (nil? (#'context/format-temporal-stats {})))))
+
+;;; ----------------------------------------- Column Comment Building Tests -----------------------------------------
+
+(deftest build-column-comment-test
+ (testing "description takes priority"
+ (is (= "Customer email address"
+ (#'context/build-column-comment
+ {:description "Customer email address"
+ :semantic_type :type/Email}
+ nil nil))))
+
+ (testing "sample values for category"
+ (is (= "active, inactive, pending"
+ (#'context/build-column-comment
+ {:semantic_type :type/Category}
+ ["active" "inactive" "pending"]
+ nil))))
+
+ (testing "FK reference"
+ (is (= "FK->users.id"
+ (#'context/build-column-comment
+ {:fk_target_field_id 123}
+ nil
+ {:table "users" :field "id"}))))
+
+ (testing "semantic type hints"
+ (is (= "PK"
+ (#'context/build-column-comment {:semantic_type :type/PK} nil nil)))
+ (is (= "Email"
+ (#'context/build-column-comment {:semantic_type :type/Email} nil nil))))
+
+ (testing "numeric fingerprint stats"
+ (is (= "range: 0-1000, avg: 127.50"
+ (#'context/build-column-comment
+ {:fingerprint {:type {:type/Number {:min 0 :max 1000 :avg 127.5}}}}
+ nil nil))))
+
+ (testing "temporal fingerprint stats"
+ (is (= "2020-01-01 to 2024-12-31"
+ (#'context/build-column-comment
+ {:fingerprint {:type {:type/DateTime {:earliest "2020-01-01T00:00:00Z"
+ :latest "2024-12-31T23:59:59Z"}}}}
+ nil nil))))
+
+ (testing "combined metadata"
+ (is (= "Order status; pending, shipped, delivered"
+ (#'context/build-column-comment
+ {:description "Order status"}
+ ["pending" "shipped" "delivered"]
+ nil))))
+
+ (testing "returns nil for no metadata"
+ (is (nil? (#'context/build-column-comment {} nil nil)))))
+
+(deftest truncate-value-test
+ (testing "short values unchanged"
+ (is (= "active" (#'context/truncate-value "active"))))
+
+ (testing "long values truncated"
+ (is (= "this is a very long value t..."
+ (#'context/truncate-value "this is a very long value that exceeds the limit")))))
+
+;;; ----------------------------------------- DDL with Comments Tests -----------------------------------------
+
+(deftest format-schema-ddl-with-comments-test
+ (testing "formats table with column comments"
+ (let [tables [{:name "orders"
+ :schema nil
+ :columns [{:name "id" :database_type "INTEGER" :comment "PK"}
+ {:name "status" :database_type "VARCHAR" :comment "pending, shipped, delivered"}
+ {:name "total" :database_type "DECIMAL" :comment "range: 0-9999.99"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (str/includes? result "-- PK"))
+ (is (str/includes? result "-- pending, shipped, delivered"))
+ (is (str/includes? result "-- range: 0-9999.99"))
+ (is (str/includes? result "id INTEGER"))
+ (is (str/includes? result "status VARCHAR"))
+ (is (str/includes? result "total DECIMAL"))))
+
+ (testing "formats table with mixed commented and non-commented columns"
+ (let [tables [{:name "users"
+ :schema nil
+ :columns [{:name "id" :database_type "INTEGER" :comment "PK"}
+ {:name "name" :database_type "VARCHAR"}
+ {:name "email" :database_type "VARCHAR" :comment "email"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (str/includes? result "-- PK"))
+ (is (str/includes? result "-- email"))
+ (is (str/includes? result "id INTEGER"))
+ (is (str/includes? result "name VARCHAR"))
+ (is (str/includes? result "email VARCHAR"))))
+
+ (testing "formats table without any comments (backward compatible)"
+ (let [tables [{:name "simple"
+ :schema nil
+ :columns [{:name "a" :database_type "INT"}
+ {:name "b" :database_type "TEXT"}]}]
+ result (#'context/format-schema-ddl tables)]
+ (is (= "CREATE TABLE simple (\n a INT,\n b TEXT\n);" result)))))
+
+;;; ----------------------------------------- Enriched Context Integration Tests -----------------------------------------
+
+(deftest build-schema-context-with-description-test
+ (mt/with-test-user :crowberto
+ (mt/with-temp [:model/Database db {}
+ :model/Table table {:db_id (:id db) :name "products" :schema "public"}
+ :model/Field _f1 {:table_id (:id table)
+ :name "id"
+ :database_type "INTEGER"
+ :base_type :type/Integer
+ :semantic_type :type/PK}
+ :model/Field _f2 {:table_id (:id table)
+ :name "name"
+ :database_type "VARCHAR"
+ :base_type :type/Text
+ :description "Product display name"}]
+ (testing "includes field description in DDL comments"
+ (let [result (context/build-schema-context (:id db) #{(:id table)})]
+ (is (some? result))
+ (is (str/includes? result "products"))
+ (is (str/includes? result "-- Product display name")))))))
+
+(deftest build-schema-context-with-fk-test
+ (mt/with-test-user :crowberto
+ (mt/with-temp [:model/Database db {}
+ :model/Table users {:db_id (:id db) :name "users" :schema "public"}
+ :model/Field uid {:table_id (:id users) :name "id" :database_type "INTEGER" :base_type :type/Integer}
+ :model/Table orders {:db_id (:id db) :name "orders" :schema "public"}
+ :model/Field _oid {:table_id (:id orders) :name "id" :database_type "INTEGER" :base_type :type/Integer}
+ :model/Field _fk {:table_id (:id orders)
+ :name "user_id"
+ :database_type "INTEGER"
+ :base_type :type/Integer
+ :semantic_type :type/FK
+ :fk_target_field_id (:id uid)}]
+ (testing "includes FK relationship in DDL comments"
+ (let [result (context/build-schema-context (:id db) #{(:id orders)})]
+ (is (some? result))
+ (is (str/includes? result "FK->users.id")))))))
+
+(deftest build-schema-context-with-field-values-test
+ (mt/with-test-user :crowberto
+ (mt/with-temp [:model/Database db {}
+ :model/Table table {:db_id (:id db) :name "orders" :schema "public"}
+ :model/Field field {:table_id (:id table)
+ :name "status"
+ :database_type "VARCHAR"
+ :base_type :type/Text
+ :has_field_values :list}
+ :model/FieldValues _fv {:field_id (:id field)
+ :values ["pending" "shipped" "delivered"]
+ :type :full}]
+ (testing "includes sample values in DDL comments"
+ (let [result (context/build-schema-context (:id db) #{(:id table)})]
+ (is (some? result))
+ (is (str/includes? result "pending"))
+ (is (str/includes? result "shipped"))
+ (is (str/includes? result "delivered")))))))
+
+(deftest build-schema-context-with-table-description-test
+ (mt/with-test-user :crowberto
+ (mt/with-temp [:model/Database db {}
+ :model/Table table {:db_id (:id db)
+ :name "orders"
+ :schema "public"
+ :description "Customer purchase transactions"}
+ :model/Field _f1 {:table_id (:id table)
+ :name "id"
+ :database_type "INTEGER"
+ :base_type :type/Integer}]
+ (testing "includes table description as comment above CREATE TABLE"
+ (let [result (context/build-schema-context (:id db) #{(:id table)})]
+ (is (some? result))
+ (is (str/includes? result "-- Customer purchase transactions"))
+ (is (str/includes? result "CREATE TABLE")))))))
+
+;;; ----------------------------------------- extract-tables-from-sql Tests -----------------------------------------
+
+(deftest extract-tables-from-sql-test
+ (testing "returns empty set for nil inputs"
+ (is (= #{} (context/extract-tables-from-sql nil nil)))
+ (is (= #{} (context/extract-tables-from-sql 1 nil)))
+ (is (= #{} (context/extract-tables-from-sql nil "SELECT 1"))))
+
+ (testing "returns empty set for empty SQL"
+ (is (= #{} (context/extract-tables-from-sql 1 "")))
+ (is (= #{} (context/extract-tables-from-sql 1 " ")))))
+
+(deftest extract-tables-from-sql-with-sample-database-test
+ (mt/with-test-user :crowberto
+ (testing "extracts single table from simple SELECT"
+ (let [result (context/extract-tables-from-sql (mt/id) "SELECT * FROM ORDERS")]
+ (is (set? result))
+ (is (contains? result (mt/id :orders)))))
+
+ (testing "extracts multiple tables from JOIN"
+ (let [result (context/extract-tables-from-sql (mt/id)
+ "SELECT * FROM ORDERS o JOIN PRODUCTS p ON o.PRODUCT_ID = p.ID")]
+ (is (set? result))
+ (is (contains? result (mt/id :orders)))
+ (is (contains? result (mt/id :products)))))
+
+ (testing "extracts tables from subquery"
+ (let [result (context/extract-tables-from-sql (mt/id)
+ "SELECT * FROM (SELECT * FROM PEOPLE) sub")]
+ (is (set? result))
+ (is (contains? result (mt/id :people)))))
+
+ (testing "returns empty set for non-existent table"
+ (let [result (context/extract-tables-from-sql (mt/id) "SELECT * FROM NONEXISTENT_TABLE")]
+ (is (= #{} result))))
+
+ (testing "returns empty set for invalid SQL (graceful failure)"
+ (let [result (context/extract-tables-from-sql (mt/id) "THIS IS NOT SQL")]
+ (is (= #{} result))))))
diff --git a/test/metabase/llm/costs_test.clj b/test/metabase/llm/costs_test.clj
new file mode 100644
index 00000000000..a5b124a91b3
--- /dev/null
+++ b/test/metabase/llm/costs_test.clj
@@ -0,0 +1,68 @@
+(ns metabase.llm.costs-test
+ (:require
+ [clojure.test :refer :all]
+ [mb.hawk.assert-exprs.approximately-equal :as =?]
+ [metabase.llm.costs :as costs]))
+
+(set! *warn-on-reflection* true)
+
+(deftest ^:parallel estimate-sanity-test
+ (testing "known models calculate correct costs"
+ (is (=? (=?/approx [0.030 0.0001])
+ (costs/estimate {:model "anthropic/claude-opus-4-5"
+ :prompt 1000
+ :completion 1000})))
+ (is (=? (=?/approx [0.018 0.0001])
+ (costs/estimate {:model "anthropic/claude-sonnet-4-5"
+ :prompt 1000
+ :completion 1000})))))
+
+(deftest ^:parallel estimate-raw-model-names-test
+ (testing "raw model names with date suffixes are normalized correctly"
+ (is (=? (=?/approx [0.030 0.0001])
+ (costs/estimate {:model "claude-opus-4-5-20251101"
+ :prompt 1000
+ :completion 1000})))
+ (is (=? (=?/approx [0.018 0.0001])
+ (costs/estimate {:model "claude-sonnet-4-5-20250929"
+ :prompt 1000
+ :completion 1000}))))
+ (testing "raw model names without date suffixes are normalized correctly"
+ (is (=? (=?/approx [0.018 0.0001])
+ (costs/estimate {:model "claude-sonnet-4"
+ :prompt 1000
+ :completion 1000})))))
+
+(deftest ^:parallel estimate-zero-tokens-test
+ (testing "zero tokens returns zero cost"
+ (is (= 0.0
+ (costs/estimate {:model "anthropic/claude-sonnet-4-5"
+ :prompt 0
+ :completion 0})))))
+
+(deftest ^:parallel estimate-unknown-model-test
+ (testing "unknown model returns 0.0"
+ (is (= 0.0
+ (costs/estimate {:model "unknown-model-xyz"
+ :prompt 1000
+ :completion 1000})))))
+
+(deftest ^:parallel estimate-nil-model-test
+ (testing "nil model returns 0.0"
+ (is (= 0.0
+ (costs/estimate {:model nil
+ :prompt 1000
+ :completion 1000})))))
+
+(deftest ^:parallel estimate-all-models-test
+ (testing "all known models have valid pricing"
+ (doseq [model (keys @#'costs/model-pricing)]
+ (testing (str "model: " model)
+ (is (pos? (costs/estimate {:model model
+ :prompt 1000
+ :completion 1000}))
+ "should have positive cost for non-zero tokens")
+ (is (= 0.0 (costs/estimate {:model model
+ :prompt 0
+ :completion 0}))
+ "should have zero cost for zero tokens")))))
diff --git a/test/metabase/llm/settings_test.clj b/test/metabase/llm/settings_test.clj
new file mode 100644
index 00000000000..a4d460a979b
--- /dev/null
+++ b/test/metabase/llm/settings_test.clj
@@ -0,0 +1,86 @@
+(ns metabase.llm.settings-test
+ (:require
+ [clojure.test :refer :all]
+ [metabase.llm.settings :as llm.settings]
+ [metabase.test :as mt]))
+
+(set! *warn-on-reflection* true)
+
+;;; ------------------------------------------- llm-anthropic-api-key Setter Tests -------------------------------------------
+
+(deftest llm-anthropic-api-key-setter-test
+ (testing "accepts valid sk-ant- key and trims whitespace"
+ (mt/discard-setting-changes [llm-anthropic-api-key]
+ (llm.settings/llm-anthropic-api-key! " sk-ant-abc123 ")
+ (is (= "sk-ant-abc123" (llm.settings/llm-anthropic-api-key)))))
+
+ (testing "rejects keys without sk-ant- prefix"
+ (is (thrown-with-msg?
+ clojure.lang.ExceptionInfo
+ #"Invalid Anthropic API key format"
+ (llm.settings/llm-anthropic-api-key! "invalid-key"))))
+
+ (testing "empty/nil clears the setting"
+ (mt/discard-setting-changes [llm-anthropic-api-key]
+ (llm.settings/llm-anthropic-api-key! "sk-ant-abc123")
+ (llm.settings/llm-anthropic-api-key! "")
+ (is (nil? (llm.settings/llm-anthropic-api-key))))))
+
+;;; ------------------------------------------- llm-sql-generation-enabled Tests -------------------------------------------
+
+(deftest llm-sql-generation-enabled-test
+ (testing "returns true when Anthropic API key is set"
+ (mt/with-temporary-setting-values [llm-anthropic-api-key "sk-ant-test"]
+ (is (true? (llm.settings/llm-sql-generation-enabled)))))
+
+ (testing "returns false when no API key and metabot not enabled"
+ (mt/with-premium-features #{}
+ (mt/with-temporary-setting-values [llm-anthropic-api-key nil]
+ (is (false? (llm.settings/llm-sql-generation-enabled)))))))
+
+;;; ------------------------------------------- Settings Defaults Tests -------------------------------------------
+
+(deftest llm-max-tokens-test
+ (testing "default value is 4096"
+ (mt/with-temporary-setting-values [llm-max-tokens nil]
+ (is (= 4096 (llm.settings/llm-max-tokens)))))
+
+ (testing "can be overridden"
+ (mt/with-temporary-setting-values [llm-max-tokens 8192]
+ (is (= 8192 (llm.settings/llm-max-tokens))))))
+
+(deftest llm-request-timeout-ms-test
+ (testing "default value is 60000 (60 seconds)"
+ (mt/with-temporary-setting-values [llm-request-timeout-ms nil]
+ (is (= 60000 (llm.settings/llm-request-timeout-ms)))))
+
+ (testing "can be overridden"
+ (mt/with-temporary-setting-values [llm-request-timeout-ms 120000]
+ (is (= 120000 (llm.settings/llm-request-timeout-ms))))))
+
+(deftest llm-connection-timeout-ms-test
+ (testing "default value is 5000 (5 seconds)"
+ (mt/with-temporary-setting-values [llm-connection-timeout-ms nil]
+ (is (= 5000 (llm.settings/llm-connection-timeout-ms)))))
+
+ (testing "can be overridden"
+ (mt/with-temporary-setting-values [llm-connection-timeout-ms 10000]
+ (is (= 10000 (llm.settings/llm-connection-timeout-ms))))))
+
+(deftest llm-rate-limit-per-user-test
+ (testing "default value is 20 requests per minute"
+ (mt/with-temporary-setting-values [llm-rate-limit-per-user nil]
+ (is (= 20 (llm.settings/llm-rate-limit-per-user)))))
+
+ (testing "can be overridden"
+ (mt/with-temporary-setting-values [llm-rate-limit-per-user 50]
+ (is (= 50 (llm.settings/llm-rate-limit-per-user))))))
+
+(deftest llm-rate-limit-per-ip-test
+ (testing "default value is 100 requests per minute"
+ (mt/with-temporary-setting-values [llm-rate-limit-per-ip nil]
+ (is (= 100 (llm.settings/llm-rate-limit-per-ip)))))
+
+ (testing "can be overridden"
+ (mt/with-temporary-setting-values [llm-rate-limit-per-ip 200]
+ (is (= 200 (llm.settings/llm-rate-limit-per-ip))))))