feat(ai): add connection tree context

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-08-20 15:15:19 +08:00
1 parent e4577abeed
commit 0e0edcd206
15 files changed
+132 -4

No files matched your search

+58 -2
View File
@@ -62,7 +62,7 @@ import { isTauriRuntime } from "@/lib/backend/tauriRuntime";
import { openQueryResultArchiveFile } from "@/lib/query/queryResultArchiveFile";
import { rememberExternalSqlFileTarget, resolveExternalSqlFileTarget, unassociatedExternalSqlFileTarget } from "@/lib/sql/externalSqlFileTarget";
import { externalSqlFileOpenErrorMessage, readBrowserSqlFile, sqlFileTitleFromPath } from "@/lib/sql/sqlFileOpen";
import type { ConnectionConfig, DatabaseType, ObjectSourceKind, QueryTab } from "@/types/database";
import type { ConnectionConfig, DatabaseType, ObjectSourceKind, QueryTab, TreeNode } from "@/types/database";
import { parseConnectionDeepLink, type ConnectionDeepLinkDraft } from "@/lib/connection/connectionDeepLink";
import {
isBrowserReloadShortcut,
@@ -109,6 +109,7 @@ import { savedSqlErrorMessage } from "@/lib/savedSql/savedSqlErrors";
import { savedSqlDefaultTargetForWrite } from "@/lib/savedSql/savedSqlExecutionTarget";
import { countActiveUpdateBlockingTasks } from "@/lib/app/appUpdateTaskGuard";
import { initSavedSqlEditorPositions } from "@/lib/app/savedSqlEditorPosition";
import { hasTreeNodeDatabaseContext } from "@/lib/sidebar/treeNodeContext";
import { isSchemaAware, isSingleDatabase, usesTreeSchemaMode } from "@/lib/database/databaseFeatureSupport";
import { codeMirrorSqlDialect, connectionUsesDatabaseObjectTreeMode, effectiveDatabaseTypeForConnection } from "@/lib/database/jdbcDialect";
import { sqlFormatDialectForDbType } from "@/lib/sql/sqlFormatter";
@@ -120,7 +121,7 @@ import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { SearchableSelect } from "@/components/ui/searchable-select";
import type { HistoryEntry } from "@/lib/backend/tauri";
import type { AiAction } from "@/lib/ai/ai";
import { resolveDefaultAiSchema, type AiAction } from "@/lib/ai/ai";
import ExternalSqlFileChangeDialog from "@/components/editor/ExternalSqlFileChangeDialog.vue";
const AiAssistant = defineAsyncComponent(() => import("@/components/editor/AiAssistant.vue"));
@@ -139,6 +140,8 @@ const QueryEditorObjectSourceDialog = defineAsyncComponent(() => import("@/compo
type AiAssistantHandle = {
triggerAction: (action: AiAction, instruction?: string) => void;
setPrompt: (text: string) => void;
addTableMention: (target: { schema?: string; table: string }) => void;
clearContextReferences: () => void;
};
const { t } = useI18n();
@@ -857,6 +860,58 @@ function sendSelectionToAi(sql: string) {
invokeWhenAiReady((handle) => handle.setPrompt(sql));
}
let addToAiRequestId = 0;
async function addToAi(node: TreeNode) {
if ((node.type !== "connection" && node.type !== "database" && node.type !== "table") || !node.connectionId) return;
const connection = connectionStore.getConfig(node.connectionId);
if (!connection) return;
const requestId = ++addToAiRequestId;
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") {
const options = await getDatabaseOptions(node.connectionId);
if (requestId !== addToAiRequestId) return;
target =
connection.db_type === "dameng"
? {
database: resolveDefaultDatabase(connection, []),
schema: resolveDefaultAiSchema(connection, options),
}
: {
database: resolveDefaultDatabase(connection, options),
schema: connection.default_schema,
};
} else if (hasTreeNodeDatabaseContext(node)) {
target = { database: node.database, schema: node.schema, catalog: node.catalog };
}
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);
}
openRightSidebarPanel("ai");
invokeWhenAiReady((handle) => {
if (contextChanged) handle.clearContextReferences();
if (node.type === "table") handle.addTableMention({ schema: node.schema, table: node.label });
});
} catch (e: any) {
toast(t("connection.connectFailed", { message: translateBackendError(t, e) }), 5000);
}
}
function openAiPanel() {
openRightSidebarPanel("ai");
}
@@ -2669,6 +2724,7 @@ onUnmounted(() => {
@start-resize="startSidebarResize"
@collapse="setSidebarOpen(false)"
@open-settings="(initialTab) => openSettings(initialTab ?? 'appearance')"
@add-to-ai="addToAi"
/>
<div v-show="!sidebarOpen" class="flex h-full w-8 shrink-0 items-start justify-center border-r bg-background/80 pt-2" :class="isClassicLayout ? '' : 'rounded-md border border-border/80'">
<Button variant="ghost" size="icon" class="h-7 w-7" :title="t('sidebar.expand')" :aria-label="t('sidebar.expand')" @click="setSidebarOpen(true)">