Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(ai): use current form config for connection tests

zipg4efd815329b7
diff --git a/apps/desktop/src/components/editor/EditorSettingsDialog.vue b/apps/desktop/src/components/editor/EditorSettingsDialog.vue
index d0ebb180ee..254d487578 100644
--- a/apps/desktop/src/components/editor/EditorSettingsDialog.vue
+++ b/apps/desktop/src/components/editor/EditorSettingsDialog.vue
@@ -50,6 +50,7 @@ import {
} from "@/stores/settingsStore";
import { createRunStatementButtonDom, loadEditorTheme, editorFontTheme } from "@/lib/editor/editorThemes";
import { orderAiConfigsForDisplay } from "@/lib/ai/aiConfigOrdering";
+import { isAiConnectionTestConfigCurrent } from "@/lib/ai/aiConnectionTest";
import { MAX_AGENT_TURNS_DEFAULT, MAX_AGENT_TURNS_MAX, MAX_AGENT_TURNS_MIN, maxAgentTurnsOutOfRange, normalizeMaxAgentTurns } from "@/lib/ai/maxAgentTurns";
import ThemeCustomizerDialog from "./ThemeCustomizerDialog.vue";
import TunnelProfileManager from "@/components/connection/TunnelProfileManager.vue";
@@ -2712,6 +2713,7 @@ const aiTestResult = ref<"" | "success" | "error">("");
const aiTestError = ref("");
const aiTestLatency = ref<number | null>(null);
const aiTestErrorCopied = ref(false);
+let aiTestRequestId = 0;
const aiTestErrorCategoryKeys: Record<string, string> = {
auth: "ai.testErrorAuth",
modelNotFound: "ai.testErrorModelNotFound",
@@ -2944,6 +2946,8 @@ function currentAiEditConfig() {
}
function syncAiEditState() {
+ aiTestRequestId += 1;
+ aiTesting.value = false;
aiTestResult.value = "";
aiTestError.value = "";
aiTestLatency.value = null;
@@ -3126,20 +3130,19 @@ async function aiTestConn() {
aiTestError.value = "";
aiTestLatency.value = null;
aiTestErrorCopied.value = false;
+ const requestId = ++aiTestRequestId;
+ const config = currentAiEditConfig();
try {
- const config = currentAiEditConfig();
- const activeModel = settingsStore.activeModel;
- if (aiEditConfigId.value && activeModel?.configId === aiEditConfigId.value) {
- config.model = activeModel.modelId;
- }
const result = await aiTestConnection(config);
+ if (requestId !== aiTestRequestId || !isAiConnectionTestConfigCurrent(config, currentAiEditConfig())) return;
aiTestLatency.value = result.latencyMs ?? null;
aiTestResult.value = "success";
} catch (e: any) {
+ if (requestId !== aiTestRequestId || !isAiConnectionTestConfigCurrent(config, currentAiEditConfig())) return;
aiTestResult.value = "error";
aiTestError.value = translateBackendError(t, e);
} finally {
- aiTesting.value = false;
+ if (requestId === aiTestRequestId) aiTesting.value = false;
}
}
diff --git a/apps/desktop/src/lib/ai/__tests__/aiConnectionTest.spec.ts b/apps/desktop/src/lib/ai/__tests__/aiConnectionTest.spec.ts
new file mode 100644
index 0000000000..11c398fcdd
--- /dev/null
+++ b/apps/desktop/src/lib/ai/__tests__/aiConnectionTest.spec.ts
@@ -0,0 +1,26 @@
+import { describe, expect, it } from "vitest";
+import type { AiConfig } from "@/types/ai";
+import { isAiConnectionTestConfigCurrent } from "../aiConnectionTest";
+
+function config(overrides: Partial<AiConfig> = {}): AiConfig {
+ return {
+ provider: "openai-compatible",
+ apiKey: "key",
+ authMethod: "bearer",
+ endpoint: "https://example.com/v1",
+ model: "model-a",
+ apiStyle: "completions",
+ ...overrides,
+ };
+}
+
+describe("AI connection test configuration", () => {
+ it("accepts a result only while the tested form values remain current", () => {
+ const tested = config();
+
+ expect(isAiConnectionTestConfigCurrent(tested, config())).toBe(true);
+ expect(isAiConnectionTestConfigCurrent(tested, config({ model: "model-b" }))).toBe(false);
+ expect(isAiConnectionTestConfigCurrent(tested, config({ endpoint: "https://other.example/v1" }))).toBe(false);
+ expect(isAiConnectionTestConfigCurrent(tested, config({ apiKey: "new-key" }))).toBe(false);
+ });
+});
diff --git a/apps/desktop/src/lib/ai/aiConnectionTest.ts b/apps/desktop/src/lib/ai/aiConnectionTest.ts
new file mode 100644
index 0000000000..c82ef73772
--- /dev/null
+++ b/apps/desktop/src/lib/ai/aiConnectionTest.ts
@@ -0,0 +1,5 @@
+import type { AiConfig } from "@/types/ai";
+
+export function isAiConnectionTestConfigCurrent(testedConfig: AiConfig, currentConfig: AiConfig): boolean {
+ return JSON.stringify(testedConfig) === JSON.stringify(currentConfig);
+}
diff --git a/packages/app-tests/settingsStore.test.ts b/packages/app-tests/settingsStore.test.ts
index 079c14a771..3a3c815df5 100644
--- a/packages/app-tests/settingsStore.test.ts
+++ b/packages/app-tests/settingsStore.test.ts
@@ -648,6 +648,18 @@ test("API AI provider settings expose and persist a default model ID", () => {
assert.match(source, /model:\s*aiEditModel\.value/);
});
+test("AI connection test uses the model currently entered in the config form", () => {
+ const source = readFileSync("apps/desktop/src/components/editor/EditorSettingsDialog.vue", "utf8");
+ const testConnectionStart = source.indexOf("async function aiTestConn()");
+ const testConnectionEnd = source.indexOf("async function copyAiTestError()", testConnectionStart);
+ const testConnection = source.slice(testConnectionStart, testConnectionEnd);
+
+ assert.notEqual(testConnectionStart, -1);
+ assert.notEqual(testConnectionEnd, -1);
+ assert.match(testConnection, /const config = currentAiEditConfig\(\);[\s\S]*aiTestConnection\(config\)/);
+ assert.doesNotMatch(testConnection, /activeModel|config\.model\s*=/);
+});
+
test("normalizes legacy AI config and fills provider defaults", () => {
const legacy = normalizeAiConfig({
provider: "openai",