From 025a4fdcce2da1905154389839741dbe018e4ca7 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Tue, 9 Jun 2026 00:44:04 +0800 Subject: [PATCH] fix: support tree-schema default DB selection in AI assistant --- .../src/components/editor/AiAssistant.vue | 54 +++++++++++++++---- .../src/components/layout/EditorToolbar.vue | 12 ++--- apps/desktop/src/lib/defaultDatabase.ts | 27 +++++++++- packages/app-tests/databaseOptions.test.ts | 2 +- packages/app-tests/defaultDatabase.test.ts | 37 ++++++++++++- 5 files changed, 112 insertions(+), 20 deletions(-) diff --git a/apps/desktop/src/components/editor/AiAssistant.vue b/apps/desktop/src/components/editor/AiAssistant.vue index 4cac802fb..bc755fa72 100644 --- a/apps/desktop/src/components/editor/AiAssistant.vue +++ b/apps/desktop/src/components/editor/AiAssistant.vue @@ -61,7 +61,12 @@ import { import type { AiMessage } from "@/lib/api"; import type { ConnectionConfig, QueryTab, TableInfo } from "@/types/database"; import { useDatabaseOptions } from "@/composables/useDatabaseOptions"; -import { resolveDefaultDatabase } from "@/lib/defaultDatabase"; +import { + decodeSelectableDatabaseValue, + encodeSelectableDatabaseValue, + formatDatabaseLabel, + resolveDefaultDatabase, +} from "@/lib/defaultDatabase"; import { isSchemaAware } from "@/lib/databaseCapabilities"; import { copyToClipboard } from "@/lib/clipboard"; import { formatAiTableMention, parseAiTableMentions, type AiTableMention } from "@/lib/aiTableMentions"; @@ -191,6 +196,32 @@ const dbOptions = computed(() => { return allDbOptions.value[props.connection.id] || []; }); +const dbSelectOptions = computed(() => { + const connection = props.connection; + if (!connection) return []; + return dbOptions.value.map((database) => ({ + database, + value: encodeSelectableDatabaseValue(connection.db_type, database), + label: formatDatabaseLabel(connection, database, { + defaultDatabase: t("editor.defaultDatabase"), + noDatabase: t("editor.noDatabase"), + }), + })); +}); + +const selectedDatabaseSelectValue = computed(() => + props.connection ? encodeSelectableDatabaseValue(props.connection.db_type, props.tab?.database || "") : "", +); + +const selectedDatabaseLabel = computed(() => { + if (!props.connection) return t("editor.selectDatabase"); + if (!props.tab) return t("editor.selectDatabase"); + return formatDatabaseLabel(props.connection, props.tab.database || "", { + defaultDatabase: t("editor.defaultDatabase"), + noDatabase: t("editor.noDatabase"), + }); +}); + async function loadDatabases() { if (!props.connection) return; await loadDatabaseOptions(props.connection.id); @@ -217,10 +248,11 @@ async function changeConnection(connectionId: string) { } } -function changeDatabase(database: string) { +function changeDatabase(value: string) { const tab = props.tab; - if (!tab) return; - queryStore.updateDatabase(tab.id, database); + const connection = props.connection; + if (!tab || !connection) return; + queryStore.updateDatabase(tab.id, decodeSelectableDatabaseValue(connection.db_type, value)); } function appendAssistantDelta(assistantIdx: number, delta: string) { @@ -885,7 +917,7 @@ const messageRenderer = computed(() => {