feat(ai): cross-database query with multi database selection
This commit is contained in:
20 files changed
+240
-57
No files matched your search
+28
-2
@@ -1,5 +1,6 @@
|
||||
import { beforeAll, describe, expect, it } from "vitest";
|
||||
import { buildAgentRequest, buildSystemPrompt, buildUserPrompt, type AiContext } from "@/lib/ai/ai";
|
||||
import { beforeAll, describe, expect, it, vi } from "vitest";
|
||||
import { buildAgentRequest, buildSystemPrompt, buildUserPrompt, runAgentStream, type AiContext } from "@/lib/ai/ai";
|
||||
import * as api from "@/lib/backend/api";
|
||||
import { setLocale } from "@/i18n";
|
||||
|
||||
function context(overrides: Partial<AiContext> = {}): AiContext {
|
||||
@@ -18,6 +19,31 @@ function context(overrides: Partial<AiContext> = {}): AiContext {
|
||||
}
|
||||
|
||||
describe("AI SQL dialect prompt", () => {
|
||||
it("sends database selections to the backend as well as the model", async () => {
|
||||
const stream = vi.spyOn(api, "aiAgentStream").mockResolvedValue("done");
|
||||
try {
|
||||
await runAgentStream(
|
||||
{
|
||||
config: { provider: "openai", apiKey: "test", apiUrl: "https://example.invalid", model: "model" },
|
||||
action: "general",
|
||||
mode: "agent",
|
||||
instruction: "Join users and orders",
|
||||
context: context({ databaseType: "mysql", database: "db_a", selectedDatabases: ["db_a", "db_b"] }),
|
||||
},
|
||||
[],
|
||||
() => {},
|
||||
"multi-db-run",
|
||||
);
|
||||
const args = stream.mock.calls[0];
|
||||
expect(args[3]).toBe("db_a");
|
||||
expect(args[14]).toEqual(["db_a", "db_b"]);
|
||||
expect(args[1].systemPrompt).toContain('Selected databases: ["db_a","db_b"]');
|
||||
expect(args[8]).toBe(false);
|
||||
} finally {
|
||||
stream.mockRestore();
|
||||
}
|
||||
});
|
||||
|
||||
// buildSystemPrompt picks zh/en copy via currentLocale(); pin to en so the
|
||||
// English-string assertions are deterministic regardless of the host OS locale.
|
||||
beforeAll(async () => {
|
||||
|
||||
@@ -100,6 +100,8 @@ export interface AiContext {
|
||||
connectionName: string;
|
||||
databaseType: DatabaseType;
|
||||
database: string;
|
||||
/** Databases selected for this run; omitted by older callers. */
|
||||
selectedDatabases?: string[];
|
||||
/** Schema selected for metadata loading and agent tool execution. */
|
||||
schema?: string;
|
||||
currentSql: string;
|
||||
@@ -221,7 +223,8 @@ export async function runAgentStream(input: AiRequestInput, history: api.AiMessa
|
||||
const { messages, systemPrompt, taskContract, maxTokens } = buildAgentRequest(input, history, custom);
|
||||
const sid = sessionId || uuid();
|
||||
|
||||
return api.aiAgentStream(
|
||||
const selectedDatabases = input.context.selectedDatabases;
|
||||
const args = [
|
||||
sid,
|
||||
{
|
||||
config: input.config,
|
||||
@@ -241,7 +244,11 @@ export async function runAgentStream(input: AiRequestInput, history: api.AiMessa
|
||||
input.confirmedConnectionId,
|
||||
input.confirmedDatabase,
|
||||
input.confirmedSchema,
|
||||
);
|
||||
] as const;
|
||||
if (selectedDatabases?.length) {
|
||||
return api.aiAgentStream(...args, undefined, selectedDatabases);
|
||||
}
|
||||
return api.aiAgentStream(...args);
|
||||
}
|
||||
|
||||
export function buildUserPrompt(action: AiAction, context: AiContext, instruction: string, isZh: boolean): string {
|
||||
@@ -319,6 +326,11 @@ export function buildSystemPrompt(action: AiAction, context: AiContext, mode: Ai
|
||||
`Database type: ${context.databaseType}`,
|
||||
`Connection: ${context.connectionName}`,
|
||||
`Database: ${context.database}`,
|
||||
context.selectedDatabases?.length
|
||||
? isZh
|
||||
? `已选择数据库:${JSON.stringify(context.selectedDatabases)}。请在元数据工具中通过 database 参数分别检查这些数据库。跨库 JOIN 需要数据库引擎支持:MySQL 使用 database.table,SQL Server 使用 database.schema.table;PostgreSQL 不能直接连接不同数据库,除非已配置联邦查询。MCP 授权仍然生效,选择数据库不会绕过授权限制。`
|
||||
: `Selected databases: ${JSON.stringify(context.selectedDatabases)}. Use the database parameter on metadata tools to inspect each selected database. Cross-database joins require engine support: MySQL uses database.table, SQL Server uses database.schema.table; PostgreSQL cannot directly join separate databases without an existing federation setup. MCP authorization still applies; selecting databases does not override it.`
|
||||
: "",
|
||||
context.schema ? `Selected schema: ${context.schema}` : "",
|
||||
schemaCoverageLine(context, isZh),
|
||||
"",
|
||||
|
||||
@@ -1873,6 +1873,7 @@ export async function aiAgentStream(
|
||||
confirmedDatabase?: string,
|
||||
confirmedSchema?: string,
|
||||
signal?: AbortSignal,
|
||||
selectedDatabases?: string[],
|
||||
): Promise<string> {
|
||||
const res = await fetch(apiUrl("/api/ai/agent-stream"), {
|
||||
method: "POST",
|
||||
@@ -1890,6 +1891,7 @@ export async function aiAgentStream(
|
||||
confirmedConnectionId,
|
||||
confirmedDatabase,
|
||||
confirmedSchema,
|
||||
selectedDatabases,
|
||||
}),
|
||||
signal,
|
||||
});
|
||||
|
||||
@@ -587,6 +587,7 @@ export async function aiAgentStream(
|
||||
confirmedDatabase?: string,
|
||||
confirmedSchema?: string,
|
||||
_signal?: AbortSignal,
|
||||
selectedDatabases?: string[],
|
||||
): Promise<string> {
|
||||
const unlisten: UnlistenFn = await listen<TauriAgentEvent>("ai-agent-event", (event) => {
|
||||
const payload = event.payload;
|
||||
@@ -610,6 +611,7 @@ export async function aiAgentStream(
|
||||
confirmedConnectionId,
|
||||
confirmedDatabase,
|
||||
confirmedSchema,
|
||||
selectedDatabases,
|
||||
});
|
||||
} catch (e) {
|
||||
unlisten();
|
||||
|
||||
Reference in new issue
Block a user