feat(ai): bind each conversation to its own connection
This commit is contained in:
1 parent
47e7f56b14
commit
c373f322d2
35 files changed
+1584
-272
No files matched your search
+112
-59
@@ -157,6 +157,7 @@ import { countActiveUpdateBlockingTasks } from "@/lib/app/appUpdateTaskGuard";
|
||||
import { initSavedSqlEditorPositions } from "@/lib/app/savedSqlEditorPosition";
|
||||
import { hasTreeNodeDatabaseContext } from "@/lib/sidebar/treeNodeContext";
|
||||
import { objectBrowserTablesToAiTreeNodes } from "@/lib/ai/objectBrowserToAiTargets";
|
||||
import type { AiConversationBinding } from "@/lib/ai/aiConversationBinding";
|
||||
import { isSchemaAware, isSingleDatabase, supportsConnectionQueryActions, usesTreeSchemaMode } from "@/lib/database/databaseFeatureSupport";
|
||||
import { codeMirrorSqlDialect, connectionUsesDatabaseObjectTreeMode, effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
|
||||
import { canFormatSqlForDatabaseType, formatSqlForEditing, sqlFormatDialectForDbType } from "@/lib/sql/sqlFormatter";
|
||||
@@ -200,7 +201,9 @@ type AiAssistantHandle = {
|
||||
openPluginConversation: (request: AiPluginConversationRequest) => void;
|
||||
triggerAction: (action: AiAction, instruction?: string) => void;
|
||||
setPrompt: (text: string) => void;
|
||||
addTableMention: (target: { schema?: string; table: string }) => void;
|
||||
addTableMention: (target: { schema?: string; table: string }, binding?: AiConversationBinding) => void;
|
||||
/** Retarget the conversation on its own, for entries that add no mention. */
|
||||
bindConversation: (binding: AiConversationBinding) => Promise<void>;
|
||||
clearContextReferences: () => void;
|
||||
focusSearch: () => boolean;
|
||||
/** Opens a conversation by id (used by the background-run toast, §9). */
|
||||
@@ -1745,7 +1748,6 @@ async function addToAi(nodesInput: TreeNode | TreeNode[]) {
|
||||
try {
|
||||
await connectionStore.ensureConnected(node.connectionId);
|
||||
if (requestId !== addToAiRequestId) return;
|
||||
connectionStore.activeConnectionId = node.connectionId;
|
||||
|
||||
let target: { database: string; schema?: string; catalog?: string } | null = null;
|
||||
if (node.type === "connection") {
|
||||
@@ -1766,22 +1768,19 @@ async function addToAi(nodesInput: TreeNode | TreeNode[]) {
|
||||
}
|
||||
if (!target) return;
|
||||
|
||||
const currentTab = activeTab.value;
|
||||
const contextChanged = currentTab?.connectionId !== node.connectionId || currentTab?.database !== target.database || (currentTab?.schema || "") !== (target.schema || "") || (currentTab?.catalog || "") !== (target.catalog || "");
|
||||
|
||||
const contextTab = queryStore.tabs.find((tab) => tab.connectionId === node.connectionId && tab.database === target.database && (tab.schema || "") === (target.schema || "") && (tab.catalog || "") === (target.catalog || ""));
|
||||
if (contextTab) {
|
||||
queryStore.switchTab(contextTab.id);
|
||||
} else {
|
||||
queryStore.createTab(node.connectionId, target.database, undefined, "query", target.schema, undefined, target.catalog);
|
||||
}
|
||||
|
||||
// The *conversation* is retargeted, not the editor: asking about a table must
|
||||
// not move the workspace's active connection or steal/create a tab (#9902).
|
||||
// The previous connection's mentions are cleared inside applyExternalBinding().
|
||||
const binding: AiConversationBinding = { connectionId: node.connectionId, database: target.database, schema: target.schema };
|
||||
const tableMentions = nodes.filter((entry) => entry.type === "table" && !!entry.label).map((entry) => ({ schema: entry.schema, table: entry.label }));
|
||||
|
||||
openRightSidebarPanel("ai");
|
||||
invokeWhenAiReady((handle) => {
|
||||
if (contextChanged) handle.clearContextReferences();
|
||||
for (const mention of tableMentions) handle.addTableMention(mention);
|
||||
// Applied independently of the mentions: "Ask AI" on a *connection* or
|
||||
// *database* node carries a target but adds no table mention, and it still
|
||||
// has to retarget the conversation (#9902).
|
||||
void handle.bindConversation(binding);
|
||||
for (const mention of tableMentions) handle.addTableMention(mention, binding);
|
||||
});
|
||||
} catch (e: any) {
|
||||
toast(t("connection.connectFailed", { message: translateBackendError(t, e) }), 5000);
|
||||
@@ -3105,74 +3104,127 @@ async function locateTabInSidebar(tab: QueryTab) {
|
||||
await appSidebarRef.value?.locateTabInSidebar(tab);
|
||||
}
|
||||
|
||||
function ensureQueryTab(): string {
|
||||
const tab = activeTab.value;
|
||||
if (tab && tab.mode === "query") return tab.id;
|
||||
const connId = connectionStore.activeConnectionId || connectionStore.connections[0]?.id || "";
|
||||
const sameConnectionTab = tab?.connectionId === connId ? tab : undefined;
|
||||
const db = sameConnectionTab?.database || connectionStore.getConfig(connId)?.database || "";
|
||||
const schema = sameConnectionTab?.schema ?? sameConnectionTab?.objectBrowser?.schema ?? sameConnectionTab?.tableMeta?.schema;
|
||||
const catalog = sameConnectionTab?.catalog ?? sameConnectionTab?.objectBrowser?.catalog ?? sameConnectionTab?.tableMeta?.catalog;
|
||||
return queryStore.createTab(connId, db, undefined, "query", schema, undefined, catalog);
|
||||
/**
|
||||
* Query tab an AI-initiated action must run in: the one belonging to the
|
||||
* *requested* connection, created on demand.
|
||||
*
|
||||
* Deliberately not `ensureQueryTab()` — that returns whichever query tab happens
|
||||
* to be active, which is how a conversation bound to one database appended its
|
||||
* SQL to, and executed it on, another connection's editor (#9902). Focus is left
|
||||
* alone (`activate: false`), so running a conversation never yanks the user's
|
||||
* editor away.
|
||||
*/
|
||||
function ensureQueryTabForConnection(target: AiConversationBinding): string {
|
||||
if (!target.connectionId) return "";
|
||||
const database = target.database || "";
|
||||
const schema = target.schema || undefined;
|
||||
// Schema is matched exactly, empty included. Skipping the comparison when the
|
||||
// target has no schema would reuse a tab on any schema, and execution inherits
|
||||
// that tab's schema — silently running in the wrong namespace.
|
||||
const existing = queryStore.tabs.find((tab) => tab.mode === "query" && tab.connectionId === target.connectionId && (tab.database || "") === database && (tab.schema || undefined) === schema);
|
||||
if (existing) return existing.id;
|
||||
return queryStore.createTab(target.connectionId, database, undefined, "query", schema, undefined, undefined, { activate: false });
|
||||
}
|
||||
|
||||
function routeAiRedisCommand(command: string, execute: boolean): boolean {
|
||||
if (activeConnection.value?.db_type !== "redis") return false;
|
||||
/** Bounded wait for a newly selected Redis console to mount. */
|
||||
const REDIS_CONSOLE_READY_TIMEOUT_MS = 2000;
|
||||
const REDIS_CONSOLE_READY_POLL_MS = 50;
|
||||
|
||||
// Redis has a dedicated console. Falling through to ensureQueryTab() would
|
||||
// recreate the original bug by opening a SQL tab for a Redis command.
|
||||
const routed = execute ? contentAreaRef.value?.executeRedisCommand(command) : contentAreaRef.value?.insertRedisCommand(command);
|
||||
if (!routed) {
|
||||
console.warn("[DBX] Redis AI command could not reach the active Redis console");
|
||||
return true;
|
||||
}
|
||||
void routed.then((handled: any) => {
|
||||
if (!handled) console.warn("[DBX] Redis AI command could not reach the active Redis console");
|
||||
});
|
||||
function routeAiRedisCommand(command: string, execute: boolean, target: AiConversationBinding): boolean {
|
||||
const connection = target.connectionId ? connectionStore.getConfig(target.connectionId) : undefined;
|
||||
if (connection?.db_type !== "redis") return false;
|
||||
|
||||
// Redis has no headless command path — the console *is* the execution vehicle,
|
||||
// and it renders only for the active tab. We deliberately do NOT switch to the
|
||||
// bound connection's console: "an AI action never moves the workspace" is the
|
||||
// whole point of #9902, and an exception here would reintroduce exactly the
|
||||
// behaviour it removes. The user opens that console and retries instead.
|
||||
void deliverRedisAiCommand(command, execute, target);
|
||||
return true;
|
||||
}
|
||||
|
||||
function onAiAppendSql(sql: string) {
|
||||
if (routeAiRedisCommand(sql, false)) return;
|
||||
const tabId = ensureQueryTab();
|
||||
const currentSql = queryStore.tabs.find((tab) => tab.id === tabId)?.sql ?? "";
|
||||
/**
|
||||
* Delivers to the bound connection's console when it is already on screen, and
|
||||
* otherwise refuses with a visible reason.
|
||||
*
|
||||
* The bounded wait covers the one benign case — the user just switched to that
|
||||
* console and it is still mounting. Readiness is polled through a side-effect-free
|
||||
* probe rather than re-issuing the command: a command that ran but reported
|
||||
* `false` (e.g. a dangerous command awaiting confirmation) must not execute twice.
|
||||
*/
|
||||
async function deliverRedisAiCommand(command: string, execute: boolean, target: AiConversationBinding): Promise<void> {
|
||||
const deadline = performance.now() + REDIS_CONSOLE_READY_TIMEOUT_MS;
|
||||
while (!contentAreaRef.value?.isRedisConsoleReady(target)) {
|
||||
if (performance.now() >= deadline) {
|
||||
// A console.warn is invisible in a desktop app, and the command the user
|
||||
// asked for is simply not going to run — say so, and say what to do.
|
||||
console.warn("[DBX] Redis AI command could not reach the bound Redis console");
|
||||
toast(t("ai.redisConsoleUnreachable"), 5000);
|
||||
return;
|
||||
}
|
||||
await nextTick();
|
||||
await new Promise((resolve) => setTimeout(resolve, REDIS_CONSOLE_READY_POLL_MS));
|
||||
}
|
||||
const routed = execute ? contentAreaRef.value?.executeRedisCommand(command, target) : contentAreaRef.value?.insertRedisCommand(command, target);
|
||||
const handled = await routed;
|
||||
if (!handled) {
|
||||
console.warn("[DBX] Redis AI command was not accepted by the bound Redis console");
|
||||
toast(t("ai.redisConsoleUnreachable"), 5000);
|
||||
}
|
||||
}
|
||||
|
||||
/** Current editor text of the tab an AI action targets. */
|
||||
function aiTargetTabSql(tabId: string): string {
|
||||
return queryStore.tabs.find((tab) => tab.id === tabId)?.sql ?? "";
|
||||
}
|
||||
|
||||
function onAiAppendSql(sql: string, target: AiConversationBinding) {
|
||||
if (routeAiRedisCommand(sql, false, target)) return;
|
||||
const tabId = ensureQueryTabForConnection(target);
|
||||
if (!tabId) return;
|
||||
const currentSql = aiTargetTabSql(tabId);
|
||||
const appendedSql = buildDeduplicatedAppendedEditorSql(currentSql, sql);
|
||||
if (appendedSql !== currentSql) queryStore.updateSql(tabId, appendedSql);
|
||||
}
|
||||
|
||||
function runAiGeneratedSql(sql: string, tabId = activeTab.value?.id) {
|
||||
function runAiGeneratedSql(sql: string, tabId: string) {
|
||||
if (!tabId) return;
|
||||
selectedSql.value = "";
|
||||
nextTick(() => tryExecute(sql, { tabId }));
|
||||
}
|
||||
|
||||
function onAiExecuteSql(sql: string) {
|
||||
if (routeAiRedisCommand(sql, true)) return;
|
||||
const tabId = ensureQueryTab();
|
||||
const currentSql = queryStore.tabs.find((tab) => tab.id === tabId)?.sql ?? "";
|
||||
function onAiExecuteSql(sql: string, target: AiConversationBinding) {
|
||||
if (routeAiRedisCommand(sql, true, target)) return;
|
||||
const tabId = ensureQueryTabForConnection(target);
|
||||
if (!tabId) return;
|
||||
const currentSql = aiTargetTabSql(tabId);
|
||||
const appendedSql = buildDeduplicatedAppendedEditorSql(currentSql, sql);
|
||||
if (appendedSql !== currentSql) queryStore.updateSql(tabId, appendedSql);
|
||||
runAiGeneratedSql(sql, tabId);
|
||||
}
|
||||
|
||||
function onAiTempRunSql(sql: string) {
|
||||
if (routeAiRedisCommand(sql, true)) return;
|
||||
const tabId = ensureQueryTab();
|
||||
runAiGeneratedSql(sql, tabId);
|
||||
function onAiTempRunSql(sql: string, target: AiConversationBinding) {
|
||||
if (routeAiRedisCommand(sql, true, target)) return;
|
||||
runAiGeneratedSql(sql, ensureQueryTabForConnection(target));
|
||||
}
|
||||
|
||||
function onAiRequestAutoExecuteSql(sql: string) {
|
||||
if (routeAiRedisCommand(sql, true)) return;
|
||||
const tabId = ensureQueryTab();
|
||||
queryStore.updateSql(tabId, buildAppendedEditorSql(activeTab.value?.sql || "", sql));
|
||||
function onAiRequestAutoExecuteSql(sql: string, target: AiConversationBinding) {
|
||||
if (routeAiRedisCommand(sql, true, target)) return;
|
||||
const tabId = ensureQueryTabForConnection(target);
|
||||
if (!tabId) return;
|
||||
// The production gate and the danger classifier must judge the connection the
|
||||
// SQL will actually run on, not whichever tab is visible (#9902).
|
||||
const connection = target.connectionId ? connectionStore.getConfig(target.connectionId) : undefined;
|
||||
queryStore.updateSql(tabId, buildAppendedEditorSql(aiTargetTabSql(tabId), sql));
|
||||
selectedSql.value = "";
|
||||
|
||||
const productionAssessment = assessProductionSql(sql, activeConnection.value, activeTab.value?.database);
|
||||
const productionAssessment = assessProductionSql(sql, connection, target.database);
|
||||
if (productionAssessment.active && productionAssessment.isMutation) {
|
||||
toast(t("production.aiReviewRequired"), 5000);
|
||||
return;
|
||||
}
|
||||
|
||||
const decision = classifyAiSqlExecution(sql, activeConnection.value);
|
||||
const decision = classifyAiSqlExecution(sql, connection);
|
||||
if (decision.action === "block") {
|
||||
toast(t("ai.autoSqlBlocked"), 5000);
|
||||
return;
|
||||
@@ -3187,9 +3239,10 @@ function onAiRequestAutoExecuteSql(sql: string) {
|
||||
});
|
||||
}
|
||||
|
||||
function onAiOpenExplainPlan(sql: string) {
|
||||
const tabId = ensureQueryTab();
|
||||
queryStore.updateSql(tabId, buildAppendedEditorSql(activeTab.value?.sql || "", sql));
|
||||
function onAiOpenExplainPlan(sql: string, target: AiConversationBinding) {
|
||||
const tabId = ensureQueryTabForConnection(target);
|
||||
if (!tabId) return;
|
||||
queryStore.updateSql(tabId, buildAppendedEditorSql(aiTargetTabSql(tabId), sql));
|
||||
selectedSql.value = "";
|
||||
nextTick(() => {
|
||||
void tryExplain(sql, { tabId });
|
||||
@@ -4397,8 +4450,8 @@ onUnmounted(() => {
|
||||
@execute-sql="onAiExecuteSql"
|
||||
@temp-run-sql="onAiTempRunSql"
|
||||
@request-auto-execute-sql="onAiRequestAutoExecuteSql"
|
||||
@insert-redis-command="(command: string) => routeAiRedisCommand(command, false)"
|
||||
@execute-redis-command="(command: string) => routeAiRedisCommand(command, true)"
|
||||
@insert-redis-command="(command: string, target: AiConversationBinding) => routeAiRedisCommand(command, false, target)"
|
||||
@execute-redis-command="(command: string, target: AiConversationBinding) => routeAiRedisCommand(command, true, target)"
|
||||
@open-explain-plan="onAiOpenExplainPlan"
|
||||
@toggle-maximize="toggleAiPanelMaximized"
|
||||
@open-settings="activateSettingsPage"
|
||||
|
||||
Reference in new issue
Block a user