fix(ai): list mention tables from composer-selected database

This commit is contained in:
Melody Ruch authored and GitHub committed 2026-09-20 01:42:39 +08:00
1 parent 4f3be8ccfb
commit be36b45510
3 files changed
+38 -9

No files matched your search

+9 -1
View File
@@ -1,5 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { aiSchemaSelectionSupported, buildAiContext, resolveAiDatabaseTarget, resolveAiNamespaceSelection, resolveDefaultAiSchema, runAgentStream } from "@/lib/ai/ai";
import { aiSchemaSelectionSupported, buildAiContext, resolveAiDatabaseTarget, resolveAiMentionDatabase, resolveAiNamespaceSelection, resolveDefaultAiSchema, runAgentStream } from "@/lib/ai/ai";
import type { AiConfig } from "@/types/ai";
import type { ConnectionConfig, QueryTab } from "@/types/database";
@@ -145,6 +145,14 @@ describe("Dameng AI context routing", () => {
expect(resolveDefaultAiSchema(connection, ["REPORTING", "ARCHIVE"])).toBe("REPORTING");
});
it("lists @ mention tables from the database picked in the composer", () => {
const connection = postgresConnection();
expect(resolveAiMentionDatabase(queryTab("db_first"), connection, ["db_second"])).toBe("db_second");
expect(resolveAiMentionDatabase(queryTab("db_first"), connection, ["db_second", "db_first"])).toBe("db_second");
expect(resolveAiMentionDatabase(queryTab("db_first"), connection, [])).toBe("db_first");
expect(resolveAiMentionDatabase(queryTab("db_first"), damengConnection(), ["ignored"])).toBe("db_first");
});
it("does not change non-Dameng namespace behavior", () => {
expect(resolveAiDatabaseTarget(queryTab("analytics"), sqliteConnection())).toEqual({ database: "analytics" });
});
+11
View File
@@ -820,6 +820,17 @@ export function resolveAiNamespaceSelection(tab: QueryTab, connection: Connectio
return { kind: "database", value: tab.database || "" };
}
/**
* Database that `@` table mentions are listed from and resolved against. It has
* to match the request database (`selectedDatabases[0] ?? tab.database`), so a
* database picked in the composer wins over the query tab's own database.
*/
export function resolveAiMentionDatabase(tab: QueryTab, connection: ConnectionConfig, selectedDatabases: string[]): string {
const namespace = resolveAiNamespaceSelection(tab, connection);
if (namespace.kind !== "database") return tab.database || "";
return selectedDatabases[0] ?? tab.database ?? "";
}
export function resolveDefaultAiSchema(connection: ConnectionConfig, schemaOptions: string[]): string | undefined {
if (connection.db_type !== "dameng") return undefined;
const username = connection.username.trim().toLowerCase();