feat(ai): add schema context selection to AI chat
This commit is contained in:
5 files changed
+194
-19
No files matched your search
+74
-1
@@ -1,5 +1,5 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { buildAiContext, resolveAiDatabaseTarget, resolveAiNamespaceSelection, resolveDefaultAiSchema, runAgentStream } from "@/lib/ai/ai";
|
||||
import { aiSchemaSelectionSupported, buildAiContext, resolveAiDatabaseTarget, resolveAiNamespaceSelection, resolveDefaultAiSchema, runAgentStream } from "@/lib/ai/ai";
|
||||
import type { AiConfig } from "@/types/ai";
|
||||
import type { ConnectionConfig, QueryTab } from "@/types/database";
|
||||
|
||||
@@ -54,6 +54,19 @@ function damengConnection(database = "APPDB"): ConnectionConfig {
|
||||
};
|
||||
}
|
||||
|
||||
function postgresConnection(database = "app"): ConnectionConfig {
|
||||
return {
|
||||
id: "postgres-1",
|
||||
name: "PostgreSQL",
|
||||
db_type: "postgres",
|
||||
host: "127.0.0.1",
|
||||
port: 5432,
|
||||
username: "app_user",
|
||||
password: "",
|
||||
database,
|
||||
};
|
||||
}
|
||||
|
||||
function aiConfig(): AiConfig {
|
||||
return {
|
||||
provider: "openai",
|
||||
@@ -136,3 +149,63 @@ describe("Dameng AI context routing", () => {
|
||||
expect(resolveAiDatabaseTarget(queryTab("analytics"), sqliteConnection())).toEqual({ database: "analytics" });
|
||||
});
|
||||
});
|
||||
|
||||
describe("PostgreSQL AI schema routing", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
apiMock.listTables.mockResolvedValue([]);
|
||||
apiMock.getColumns.mockResolvedValue([]);
|
||||
apiMock.listIndexes.mockResolvedValue([]);
|
||||
apiMock.listForeignKeys.mockResolvedValue([]);
|
||||
apiMock.aiAgentStream.mockResolvedValue("done");
|
||||
});
|
||||
|
||||
it("scopes metadata and agent tools to the selected schema", async () => {
|
||||
const tab = { ...queryTab("app", "main_chatdr"), connectionId: "postgres-1" };
|
||||
const connection = postgresConnection();
|
||||
const context = await buildAiContext(tab, connection);
|
||||
|
||||
expect(resolveAiDatabaseTarget(tab, connection)).toEqual({ database: "app", schema: "main_chatdr" });
|
||||
expect(context.schema).toBe("main_chatdr");
|
||||
expect(apiMock.listTables).toHaveBeenCalledWith("postgres-1", "app", "main_chatdr");
|
||||
|
||||
await runAgentStream(
|
||||
{
|
||||
config: aiConfig(),
|
||||
action: "general",
|
||||
mode: "agent",
|
||||
instruction: "inspect chat_dial_in",
|
||||
context,
|
||||
},
|
||||
[],
|
||||
vi.fn(),
|
||||
"session-postgres",
|
||||
);
|
||||
expect(apiMock.aiAgentStream).toHaveBeenCalledWith("session-postgres", expect.any(Object), "postgres-1", "app", "main_chatdr", "postgres", expect.any(Function), "agent", false, undefined, undefined, undefined, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe("AI schema selector visibility", () => {
|
||||
it("matches the effective type consumed by resolveAiDatabaseTarget", () => {
|
||||
const gbaseLikeMysql: ConnectionConfig = {
|
||||
id: "gbase-1",
|
||||
name: "GBase",
|
||||
db_type: "gbase",
|
||||
host: "localhost",
|
||||
port: 5258,
|
||||
username: "dbx",
|
||||
password: "",
|
||||
database: "app",
|
||||
};
|
||||
// gbase is schema-aware at the raw metadata level but maps to a
|
||||
// MySQL-like effective type, so the AI target ignores schema selections
|
||||
// and the selector must stay hidden to avoid a silently dropped choice.
|
||||
expect(aiSchemaSelectionSupported(gbaseLikeMysql)).toBe(false);
|
||||
const tab: QueryTab = queryTab("app", "sysmaster");
|
||||
expect(resolveAiDatabaseTarget(tab, gbaseLikeMysql)).toEqual({ database: "app" });
|
||||
|
||||
const postgres: ConnectionConfig = { ...gbaseLikeMysql, id: "pg-1", db_type: "postgres" };
|
||||
expect(aiSchemaSelectionSupported(postgres)).toBe(true);
|
||||
expect(resolveAiDatabaseTarget(queryTab("app", "public"), postgres)).toEqual({ database: "app", schema: "public" });
|
||||
});
|
||||
});
|
||||
@@ -100,7 +100,7 @@ export interface AiContext {
|
||||
connectionName: string;
|
||||
databaseType: DatabaseType;
|
||||
database: string;
|
||||
/** Selected schema when it is distinct from the connection database (for example Dameng). */
|
||||
/** Schema selected for metadata loading and agent tool execution. */
|
||||
schema?: string;
|
||||
currentSql: string;
|
||||
lastError?: string;
|
||||
@@ -720,6 +720,17 @@ function aiDatabaseTypeForConnection(connection: ConnectionConfig): DatabaseType
|
||||
return effectiveDatabaseTypeForConnection(connection) ?? connection.db_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the AI target resolution honors a schema selection for this
|
||||
* connection. Mirrors the `isSchemaAware(aiDatabaseTypeForConnection(...))`
|
||||
* gate inside `resolveAiDatabaseTarget` so UI visibility cannot diverge from
|
||||
* what the AI request actually consumes (e.g. gbase maps to a MySQL-like
|
||||
* effective type that ignores schemas).
|
||||
*/
|
||||
export function aiSchemaSelectionSupported(connection: ConnectionConfig): boolean {
|
||||
return isSchemaAware(aiDatabaseTypeForConnection(connection));
|
||||
}
|
||||
|
||||
function aiDatabaseNamespace(tab: QueryTab, connection: ConnectionConfig): string {
|
||||
return resolveAiDatabaseTarget(tab, connection).database;
|
||||
}
|
||||
@@ -751,7 +762,9 @@ export function resolveAiDatabaseTarget(tab: QueryTab, connection: ConnectionCon
|
||||
schema: resolveAiNamespaceSelection(tab, connection).value || undefined,
|
||||
};
|
||||
}
|
||||
return { database: connection.db_type === "sqlite" ? normalizeSqliteNamespace(database, connection) : database };
|
||||
const normalizedDatabase = connection.db_type === "sqlite" ? normalizeSqliteNamespace(database, connection) : database;
|
||||
const schema = isSchemaAware(aiDatabaseTypeForConnection(connection)) ? tab.schema?.trim() || undefined : undefined;
|
||||
return schema ? { database: normalizedDatabase, schema } : { database: normalizedDatabase };
|
||||
}
|
||||
|
||||
function prioritizeSchemas(schemas: string[]): string[] {
|
||||
|
||||
Reference in new issue
Block a user