feat(ai-config): persist AI settings per provider (#2412)
## 背景说明 AI 设置原本只有一个配置槽,切换模型厂商时会覆盖之前填写的 endpoint、API Key、model 等信息。本次改动让每个模型厂商各自保存一份配置,并在设置页和对话窗口提供已配置状态与快速切换能力。 ## 修改内容 - 新增 ai_provider_configs 存储表,用于按 provider 保存独立 AI 配置 - 新增 Tauri/Web API:保存和加载 provider 级 AI 配置 - 设置页 provider 下拉展示“当前/已配置”状态 - 设置页切换 provider 前会先保存当前表单编辑内容,避免未保存输入丢失 - 对话窗口模型选择器支持查看并切换已配置过的其他 provider - provider 配置保存时强制 config.provider 与 provider key 一致,避免错位配置污染 - 所有语言补齐新增 i18n 文案 ## 修复的问题 - 切换模型厂商时原厂商配置会丢失 - 设置页切换 provider 时本地表单编辑态可能未保存 - provider key 与 config.provider 不一致时可能激活错误厂商 - 对话窗口模型选择器只能看到当前 provider 的模型,不能看到其他已配置厂商 ## 测试 - npx tsc --noEmit - cargo check -p dbx-core - cargo check -p dbx-web - cargo check --manifest-path src-tauri/Cargo.toml
This commit is contained in:
1 parent
486b9ee8ed
commit
a0754206d3
18 files changed
+301
-51
No files matched your search
@@ -738,6 +738,7 @@ function saveEditorSettings(settings: EditorSettings) {
|
||||
export const useSettingsStore = defineStore("settings", () => {
|
||||
const aiConfig = ref<AiConfig>(normalizeAiConfig({ provider: "claude" }));
|
||||
const isAiConfigLoaded = ref(false);
|
||||
const aiProviderConfigs = ref<Partial<Record<AiProvider, AiConfig>>>({});
|
||||
const desktopSettings = ref<DesktopSettings>({ ...DEFAULT_DESKTOP_SETTINGS });
|
||||
const isDesktopSettingsLoaded = ref(false);
|
||||
|
||||
@@ -770,24 +771,53 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
async function initAiConfig() {
|
||||
if (isAiConfigLoaded.value) return;
|
||||
const legacy = localStorage.getItem("dbx-ai-config");
|
||||
const saved = await api.loadAiConfig().catch(() => null);
|
||||
if (saved) {
|
||||
aiConfig.value = normalizeAiConfig(saved);
|
||||
const [savedActive, savedProviderConfigs] = await Promise.all([api.loadAiConfig().catch(() => null), api.loadAiProviderConfigs().catch(() => null) as Promise<Partial<Record<AiProvider, AiConfig>> | null>]);
|
||||
|
||||
if (savedActive) {
|
||||
aiConfig.value = normalizeAiConfig(savedActive);
|
||||
} else if (legacy) {
|
||||
aiConfig.value = normalizeAiConfig(JSON.parse(legacy));
|
||||
await api.saveAiConfig(aiConfig.value).catch(() => {});
|
||||
localStorage.removeItem("dbx-ai-config");
|
||||
}
|
||||
|
||||
if (savedProviderConfigs) {
|
||||
aiProviderConfigs.value = savedProviderConfigs;
|
||||
}
|
||||
|
||||
// Ensure active config is reflected in provider map (overwrite if exists, to guarantee consistency)
|
||||
const activeProvider = aiConfig.value.provider;
|
||||
aiProviderConfigs.value[activeProvider] = { ...aiConfig.value };
|
||||
|
||||
isAiConfigLoaded.value = true;
|
||||
}
|
||||
|
||||
function updateAiConfig(config: Partial<AiConfig>) {
|
||||
const previousProvider = aiConfig.value.provider;
|
||||
if (config.provider && config.provider !== previousProvider) {
|
||||
Object.assign(aiConfig.value, defaultConfigs[config.provider]);
|
||||
const targetProvider = config.provider;
|
||||
const switchingProvider = !!targetProvider && targetProvider !== previousProvider;
|
||||
|
||||
if (switchingProvider) {
|
||||
// Save current provider's config before switching
|
||||
aiProviderConfigs.value[previousProvider] = { ...aiConfig.value };
|
||||
api.saveAiProviderConfig(previousProvider, aiConfig.value).catch(() => {});
|
||||
|
||||
// Restore saved config for target provider, or use preset defaults
|
||||
const savedConfig = aiProviderConfigs.value[targetProvider];
|
||||
if (savedConfig) {
|
||||
aiConfig.value = normalizeAiConfig({ ...savedConfig, provider: targetProvider });
|
||||
} else {
|
||||
aiConfig.value = normalizeAiConfig({ provider: targetProvider });
|
||||
}
|
||||
// Don't merge caller fields when switching — caller passes only { provider }
|
||||
} else {
|
||||
// Not switching provider — apply partial update
|
||||
Object.assign(aiConfig.value, config);
|
||||
}
|
||||
Object.assign(aiConfig.value, config);
|
||||
|
||||
aiProviderConfigs.value[aiConfig.value.provider] = { ...aiConfig.value };
|
||||
api.saveAiConfig(aiConfig.value).catch(() => {});
|
||||
api.saveAiProviderConfig(aiConfig.value.provider, aiConfig.value).catch(() => {});
|
||||
}
|
||||
|
||||
function isConfigured(): boolean {
|
||||
@@ -796,6 +826,14 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
return !!aiConfig.value.endpoint && !!aiConfig.value.model && (!preset.requiresApiKey || !!aiConfig.value.apiKey);
|
||||
}
|
||||
|
||||
function isAiProviderConfigured(provider: AiProvider): boolean {
|
||||
const config = aiProviderConfigs.value[provider];
|
||||
if (!config) return false;
|
||||
if (provider === "codex-cli") return true;
|
||||
const preset = AI_PROVIDER_PRESETS[provider];
|
||||
return !!config.endpoint?.trim() && !!config.model?.trim() && (!preset.requiresApiKey || !!config.apiKey?.trim());
|
||||
}
|
||||
|
||||
function updateEditorSettings(partial: Partial<EditorSettings>) {
|
||||
if (partial.fontFamily !== undefined) editorSettings.value.fontFamily = partial.fontFamily;
|
||||
if (partial.fontSize !== undefined) editorSettings.value.fontSize = partial.fontSize;
|
||||
@@ -911,9 +949,11 @@ export const useSettingsStore = defineStore("settings", () => {
|
||||
return {
|
||||
aiConfig,
|
||||
isAiConfigLoaded,
|
||||
aiProviderConfigs,
|
||||
initAiConfig,
|
||||
updateAiConfig,
|
||||
isConfigured,
|
||||
isAiProviderConfigured,
|
||||
editorSettings,
|
||||
desktopSettings,
|
||||
updateEditorSettings,
|
||||
|
||||
Reference in new issue
Block a user