diff --git a/core/config/onboarding.ts b/core/config/onboarding.ts index cffb78aa0..ed5f00198 100644 --- a/core/config/onboarding.ts +++ b/core/config/onboarding.ts @@ -101,8 +101,17 @@ export function setupProviderConfig( throw new Error(`Unknown provider: ${provider}`); } - return { - ...config, - models: [...(config.models ?? []), ...newModels], - }; + const existingModels = config.models ?? []; + + // Update API key on existing models; add new entries for any missing slugs + const updatedModels = existingModels.map((m) => { + if (!("uses" in m)) return m; + const match = newModels.find((n) => n.uses === m.uses); + return match ? { ...m, with: { ...m.with, ...match.with } } : m; + }); + const modelsToAdd = newModels.filter( + (n) => !existingModels.some((m) => "uses" in m && m.uses === n.uses), + ); + + return { ...config, models: [...updatedModels, ...modelsToAdd] }; } diff --git a/core/core.ts b/core/core.ts index a3dc80ad5..e72190320 100644 --- a/core/core.ts +++ b/core/core.ts @@ -378,8 +378,21 @@ export class Core { void DataLogger.getInstance().logDevData(msg.data); }); - on("config/addModel", (msg) => { + on("config/addModel", async (msg) => { const model = msg.data.model; + const { config } = await this.configHandler.loadConfig(); + const allModels = Object.values(config?.modelsByRole ?? {}).flat(); + const existing = allModels.find( + (m) => m.providerName === model.provider && m.model === model.model, + ); + if (existing) { + void this.ide.showToast( + "warning", + "Model already exists in config. Update the API key in the config file.", + ); + await this.configHandler.openConfigProfile(); + return; + } addModel(model, msg.data.role); void this.configHandler.reloadConfig( "Model added (config/addModel message)",