Provider loading fix (#623)
* add mistral and gemini provider type for profile provider field * load latest locally selected * env variables take precedence over json save * add gemini context windows and fix gemini defaulting for env * load on startup fix * fix failing tests * clarify test message * fix variable mismatches * fix failing test * delete keys and set profile.apiKey for mistral and gemini * switch model as well when switching provider * set model when adding a new model
This commit is contained in:
@@ -6,6 +6,14 @@ import {
|
||||
} from './config.js'
|
||||
import type { ModelOption } from './model/modelOptions.js'
|
||||
import { getPrimaryModel, parseModelList } from './providerModels.js'
|
||||
import {
|
||||
createProfileFile,
|
||||
saveProfileFile,
|
||||
buildGeminiProfileEnv,
|
||||
buildMistralProfileEnv,
|
||||
buildOpenAIProfileEnv,
|
||||
type ProviderProfile as ProviderProfileStartup,
|
||||
} from './providerProfile.js'
|
||||
|
||||
export type ProviderPreset =
|
||||
| 'anthropic'
|
||||
@@ -60,7 +68,14 @@ function normalizeBaseUrl(value: string): string {
|
||||
function sanitizeProfile(profile: ProviderProfile): ProviderProfile | null {
|
||||
const id = trimValue(profile.id)
|
||||
const name = trimValue(profile.name)
|
||||
const provider = profile.provider === 'anthropic' ? 'anthropic' : 'openai'
|
||||
const provider =
|
||||
profile.provider === 'anthropic'
|
||||
? 'anthropic'
|
||||
: profile.provider === 'mistral'
|
||||
? 'mistral'
|
||||
: profile.provider === 'gemini'
|
||||
? 'gemini'
|
||||
: 'openai'
|
||||
const baseUrl = normalizeBaseUrl(profile.baseUrl)
|
||||
const model = trimValue(profile.model)
|
||||
|
||||
@@ -161,7 +176,7 @@ export function getProviderPresetDefaults(
|
||||
}
|
||||
case 'gemini':
|
||||
return {
|
||||
provider: 'openai',
|
||||
provider: 'gemini',
|
||||
name: 'Google Gemini',
|
||||
baseUrl: 'https://generativelanguage.googleapis.com/v1beta/openai',
|
||||
model: 'gemini-3-flash-preview',
|
||||
@@ -170,7 +185,7 @@ export function getProviderPresetDefaults(
|
||||
}
|
||||
case 'mistral':
|
||||
return {
|
||||
provider: 'openai',
|
||||
provider: 'mistral',
|
||||
name: 'Mistral',
|
||||
baseUrl: 'https://api.mistral.ai/v1',
|
||||
model: 'devstral-latest',
|
||||
@@ -317,6 +332,7 @@ function hasConflictingProviderFlagsForProfile(
|
||||
|
||||
return (
|
||||
processEnv.CLAUDE_CODE_USE_GEMINI !== undefined ||
|
||||
processEnv.CLAUDE_CODE_USE_MISTRAL !== undefined ||
|
||||
processEnv.CLAUDE_CODE_USE_GITHUB !== undefined ||
|
||||
processEnv.CLAUDE_CODE_USE_BEDROCK !== undefined ||
|
||||
processEnv.CLAUDE_CODE_USE_VERTEX !== undefined ||
|
||||
@@ -358,6 +374,38 @@ function isProcessEnvAlignedWithProfile(
|
||||
)
|
||||
}
|
||||
|
||||
if (profile.provider === 'mistral') {
|
||||
return (
|
||||
processEnv.CLAUDE_CODE_USE_MISTRAL !== undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_GEMINI === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_OPENAI === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_GITHUB === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_BEDROCK === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_VERTEX === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_FOUNDRY === undefined &&
|
||||
sameOptionalEnvValue(processEnv.MISTRAL_BASE_URL, profile.baseUrl) &&
|
||||
sameOptionalEnvValue(processEnv.MISTRAL_MODEL, profile.model) &&
|
||||
(!includeApiKey ||
|
||||
sameOptionalEnvValue(processEnv.MISTRAL_API_KEY, profile.apiKey))
|
||||
)
|
||||
}
|
||||
|
||||
if (profile.provider === 'gemini') {
|
||||
return (
|
||||
processEnv.CLAUDE_CODE_USE_GEMINI !== undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_MISTRAL === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_OPENAI === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_GITHUB === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_BEDROCK === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_VERTEX === undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_FOUNDRY === undefined &&
|
||||
sameOptionalEnvValue(processEnv.GEMINI_BASE_URL, profile.baseUrl) &&
|
||||
sameOptionalEnvValue(processEnv.GEMINI_MODEL, profile.model) &&
|
||||
(!includeApiKey ||
|
||||
sameOptionalEnvValue(processEnv.GEMINI_API_KEY, profile.apiKey))
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
processEnv.CLAUDE_CODE_USE_OPENAI !== undefined &&
|
||||
processEnv.CLAUDE_CODE_USE_GEMINI === undefined &&
|
||||
@@ -407,6 +455,17 @@ export function clearProviderProfileEnvFromProcessEnv(
|
||||
delete processEnv[PROFILE_ENV_APPLIED_FLAG]
|
||||
delete processEnv[PROFILE_ENV_APPLIED_ID]
|
||||
|
||||
delete processEnv.GEMINI_MODEL
|
||||
delete processEnv.GEMINI_BASE_URL
|
||||
delete processEnv.GEMINI_API_KEY
|
||||
delete processEnv.GEMINI_AUTH_MODE
|
||||
delete processEnv.GEMINI_ACCESS_TOKEN
|
||||
delete processEnv.GOOGLE_API_KEY
|
||||
|
||||
delete processEnv.MISTRAL_MODEL
|
||||
delete processEnv.MISTRAL_BASE_URL
|
||||
delete processEnv.MISTRAL_API_KEY
|
||||
|
||||
// Clear provider-specific API keys
|
||||
delete processEnv.MINIMAX_API_KEY
|
||||
delete processEnv.NVIDIA_API_KEY
|
||||
@@ -435,6 +494,40 @@ export function applyProviderProfileToProcessEnv(profile: ProviderProfile): void
|
||||
return
|
||||
}
|
||||
|
||||
if (profile.provider === 'mistral') {
|
||||
process.env.CLAUDE_CODE_USE_MISTRAL = '1'
|
||||
process.env.MISTRAL_BASE_URL = profile.baseUrl
|
||||
process.env.MISTRAL_MODEL = profile.model
|
||||
|
||||
if (profile.apiKey) {
|
||||
process.env.MISTRAL_API_KEY = profile.apiKey
|
||||
} else {
|
||||
delete process.env.MISTRAL_API_KEY
|
||||
}
|
||||
|
||||
delete process.env.OPENAI_BASE_URL
|
||||
delete process.env.OPENAI_API_KEY
|
||||
delete process.env.OPENAI_MODEL
|
||||
return
|
||||
}
|
||||
|
||||
if (profile.provider === 'gemini') {
|
||||
process.env.CLAUDE_CODE_USE_GEMINI = '1'
|
||||
process.env.GEMINI_BASE_URL = profile.baseUrl
|
||||
process.env.GEMINI_MODEL = profile.model
|
||||
|
||||
if (profile.apiKey) {
|
||||
process.env.GEMINI_API_KEY = profile.apiKey
|
||||
} else {
|
||||
delete process.env.GEMINI_API_KEY
|
||||
}
|
||||
|
||||
delete process.env.OPENAI_BASE_URL
|
||||
delete process.env.OPENAI_API_KEY
|
||||
delete process.env.OPENAI_MODEL
|
||||
return
|
||||
}
|
||||
|
||||
process.env.CLAUDE_CODE_USE_OPENAI = '1'
|
||||
process.env.OPENAI_BASE_URL = profile.baseUrl
|
||||
process.env.OPENAI_MODEL = getPrimaryModel(profile.model)
|
||||
@@ -520,7 +613,7 @@ export function addProviderProfile(
|
||||
|
||||
const activeProfile = getActiveProviderProfile()
|
||||
if (activeProfile?.id === profile.id) {
|
||||
applyProviderProfileToProcessEnv(profile)
|
||||
setActiveProviderProfile(profile.id)
|
||||
clearActiveOpenAIModelOptionsCache()
|
||||
}
|
||||
|
||||
@@ -699,6 +792,68 @@ export function setActiveProviderProfile(
|
||||
}))
|
||||
|
||||
applyProviderProfileToProcessEnv(activeProfile)
|
||||
|
||||
// Keep startup persisted provider profile in sync so initial startup
|
||||
// uses the selected provider/model.
|
||||
const persistedProfile = (() => {
|
||||
if (activeProfile.provider === 'anthropic') return 'openai' as const
|
||||
return activeProfile.provider
|
||||
})()
|
||||
|
||||
const profileEnv = (() => {
|
||||
switch (activeProfile.provider) {
|
||||
case 'gemini':
|
||||
return (
|
||||
buildGeminiProfileEnv({
|
||||
model: activeProfile.model,
|
||||
baseUrl: activeProfile.baseUrl,
|
||||
apiKey: activeProfile.apiKey,
|
||||
authMode: 'api-key',
|
||||
processEnv: process.env,
|
||||
}) ?? null
|
||||
)
|
||||
case 'mistral':
|
||||
return (
|
||||
buildMistralProfileEnv({
|
||||
model: activeProfile.model,
|
||||
baseUrl: activeProfile.baseUrl,
|
||||
apiKey: activeProfile.apiKey,
|
||||
processEnv: process.env,
|
||||
}) ?? null
|
||||
)
|
||||
default:
|
||||
// anthropic and all openai-compatible providers
|
||||
return (
|
||||
buildOpenAIProfileEnv({
|
||||
model: activeProfile.model,
|
||||
baseUrl: activeProfile.baseUrl,
|
||||
apiKey: activeProfile.apiKey,
|
||||
processEnv: process.env,
|
||||
}) ?? null
|
||||
)
|
||||
}
|
||||
})()
|
||||
|
||||
if (profileEnv) {
|
||||
const startupProfile =
|
||||
activeProfile.provider === 'anthropic'
|
||||
? ({
|
||||
profile: 'openai' as ProviderProfileStartup,
|
||||
env: {
|
||||
OPENAI_BASE_URL: activeProfile.baseUrl,
|
||||
OPENAI_MODEL: activeProfile.model,
|
||||
OPENAI_API_KEY: activeProfile.apiKey,
|
||||
},
|
||||
} as const)
|
||||
: ({
|
||||
profile: activeProfile.provider as ProviderProfileStartup,
|
||||
env: profileEnv,
|
||||
} as const)
|
||||
|
||||
const file = createProfileFile(startupProfile.profile, startupProfile.env)
|
||||
saveProfileFile(file)
|
||||
}
|
||||
|
||||
return activeProfile
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user