* 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
117 lines
3.3 KiB
TypeScript
117 lines
3.3 KiB
TypeScript
import { ConfigYaml } from "@continuedev/config-yaml";
|
|
|
|
export const LOCAL_ONBOARDING_PROVIDER_TITLE = "Ollama";
|
|
export const LOCAL_ONBOARDING_FIM_MODEL = "qwen2.5-coder:1.5b-base";
|
|
export const LOCAL_ONBOARDING_FIM_TITLE = "Qwen2.5-Coder 1.5B";
|
|
export const LOCAL_ONBOARDING_CHAT_MODEL = "llama3.1:8b";
|
|
export const LOCAL_ONBOARDING_CHAT_TITLE = "Llama 3.1 8B";
|
|
export const LOCAL_ONBOARDING_EMBEDDINGS_MODEL = "nomic-embed-text:latest";
|
|
export const LOCAL_ONBOARDING_EMBEDDINGS_TITLE = "Nomic Embed";
|
|
|
|
const ANTHROPIC_MODEL_CONFIG = {
|
|
slugs: ["anthropic/claude-sonnet-4-6", "anthropic/claude-opus-4-6"],
|
|
apiKeyInputName: "ANTHROPIC_API_KEY",
|
|
};
|
|
const OPENAI_MODEL_CONFIG = {
|
|
slugs: ["openai/gpt-4.1", "openai/o3", "openai/gpt-4.1-mini"],
|
|
apiKeyInputName: "OPENAI_API_KEY",
|
|
};
|
|
|
|
// TODO: These need updating on the hub
|
|
const GEMINI_MODEL_CONFIG = {
|
|
slugs: ["google/gemini-3.1-pro-preview", "google/gemini-3-flash-preview"],
|
|
apiKeyInputName: "GEMINI_API_KEY",
|
|
};
|
|
|
|
/**
|
|
* We set the "best" chat + autocopmlete models by default
|
|
* whenever a user doesn't have a config.json
|
|
*/
|
|
export function setupBestConfig(config: ConfigYaml): ConfigYaml {
|
|
return {
|
|
...config,
|
|
models: config.models,
|
|
};
|
|
}
|
|
|
|
export function setupLocalConfig(config: ConfigYaml): ConfigYaml {
|
|
return {
|
|
...config,
|
|
models: [
|
|
{
|
|
name: LOCAL_ONBOARDING_CHAT_TITLE,
|
|
provider: "ollama",
|
|
model: LOCAL_ONBOARDING_CHAT_MODEL,
|
|
roles: ["chat", "edit", "apply"],
|
|
},
|
|
{
|
|
name: LOCAL_ONBOARDING_FIM_TITLE,
|
|
provider: "ollama",
|
|
model: LOCAL_ONBOARDING_FIM_MODEL,
|
|
roles: ["autocomplete"],
|
|
},
|
|
{
|
|
name: LOCAL_ONBOARDING_EMBEDDINGS_TITLE,
|
|
provider: "ollama",
|
|
model: LOCAL_ONBOARDING_EMBEDDINGS_MODEL,
|
|
roles: ["embed"],
|
|
},
|
|
...(config.models ?? []),
|
|
],
|
|
};
|
|
}
|
|
|
|
export function setupQuickstartConfig(config: ConfigYaml): ConfigYaml {
|
|
return config;
|
|
}
|
|
|
|
export function setupProviderConfig(
|
|
config: ConfigYaml,
|
|
provider: string,
|
|
apiKey: string,
|
|
): ConfigYaml {
|
|
let newModels;
|
|
|
|
switch (provider) {
|
|
case "openai":
|
|
newModels = OPENAI_MODEL_CONFIG.slugs.map((slug) => ({
|
|
uses: slug,
|
|
with: {
|
|
[OPENAI_MODEL_CONFIG.apiKeyInputName]: apiKey,
|
|
},
|
|
}));
|
|
break;
|
|
case "anthropic":
|
|
newModels = ANTHROPIC_MODEL_CONFIG.slugs.map((slug) => ({
|
|
uses: slug,
|
|
with: {
|
|
[ANTHROPIC_MODEL_CONFIG.apiKeyInputName]: apiKey,
|
|
},
|
|
}));
|
|
break;
|
|
case "gemini":
|
|
newModels = GEMINI_MODEL_CONFIG.slugs.map((slug) => ({
|
|
uses: slug,
|
|
with: {
|
|
[GEMINI_MODEL_CONFIG.apiKeyInputName]: apiKey,
|
|
},
|
|
}));
|
|
break;
|
|
default:
|
|
throw new Error(`Unknown provider: ${provider}`);
|
|
}
|
|
|
|
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] };
|
|
}
|