fix: allow correcting API key after entering an invalid one for xAI/Gemini (#11854)

* fix: update existing model API key instead of creating duplicate when re-adding

When a user enters an invalid API key for a provider (e.g. xAI or Gemini)
and later tries to add the same model again with a correct key, the
addModel function previously either skipped (exact match) or created a
duplicate with a numbered suffix. Now it detects models with the same
provider and model name and updates the existing entry in-place, allowing
users to correct their API key by re-adding the model.

Closes #9995

* fix: update existing provider models in setupProviderConfig instead of appending duplicates

When re-entering an API key through the onboarding flow, setupProviderConfig
previously appended new model entries without removing the old ones with
the invalid key. Now it filters out existing models with the same hub slug
before adding the new ones, ensuring the user ends up with a single set
of correctly-configured models.

* fix: narrow addModel update to only change apiKey on existing models

Avoid overwriting user-customized fields (apiBase, completionOptions,
title, etc.) when updating an existing model. Only the apiKey is changed.

* fix: merge API key into existing models instead of replacing or duplicating

Address review feedback: update only the API key on existing model entries
to preserve user-customized fields (roles, options, etc.).

- setupProviderConfig: merge `with` into existing hub-slug models, only
  append entries for slugs not yet in config
- addModel: narrow update to only set apiKey on matching provider+model

* fix: resolve TS2339 error for apiKey on YAML config model union type

The YAML config model type is a union that includes uses-based models
without an apiKey property. Use 'provider' in m guard and cast to any
for the assignment.

* style: fix prettier formatting

* fix: show toast and open config when model already exists, merge API keys in onboarding

When a user tries to add a model that already exists (e.g. after entering
an invalid API key), show a warning toast and open the config file so they
can update the key directly. Also fix setupProviderConfig to merge API
keys into existing models instead of creating duplicates.

Closes #9995

* refactor: remove all :any type annotations from changed code

Use 'uses' in m type guards to narrow the union type instead of
casting to any.

* fix: use modelsByRole instead of non-existent models property on ContinueConfig
This commit is contained in:
Dallin Romney 2026-03-25 21:32:35 -07:00 • committed by GitHub
parent 3a997fe10c
commit 9396142fb0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 5 deletions

View file

@ -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] };
}

View file

@ -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)",