update models select

This commit is contained in:
duanfuxiang 2025-06-18 08:02:32 +08:00
parent 5c383c0634
commit c3cc81624f
3 changed files with 51 additions and 34 deletions

View File

@ -293,7 +293,12 @@ const CustomProviderSettings: React.FC<CustomProviderSettingsProps> = ({ plugin,
console.log(`updateChatModelId: ${provider} -> ${modelId}, isCustom: ${isCustom}`) console.log(`updateChatModelId: ${provider} -> ${modelId}, isCustom: ${isCustom}`)
const providerSettingKey = getProviderSettingKey(provider); const providerSettingKey = getProviderSettingKey(provider);
const providerSettings = settings[providerSettingKey] || {}; const providerSettings = settings[providerSettingKey] || {};
const currentModels = providerSettings.models || new Set<string>(); const currentModels = providerSettings.models || [];
// 如果是自定义模型且不在列表中,则添加
const updatedModels = isCustom && !currentModels.includes(modelId)
? [...currentModels, modelId]
: currentModels;
handleSettingsUpdate({ handleSettingsUpdate({
...settings, ...settings,
@ -301,7 +306,7 @@ const CustomProviderSettings: React.FC<CustomProviderSettingsProps> = ({ plugin,
chatModelId: modelId, chatModelId: modelId,
[providerSettingKey]: { [providerSettingKey]: {
...providerSettings, ...providerSettings,
models: isCustom ? new Set([...currentModels, modelId]) : currentModels models: updatedModels
} }
}); });
}; };
@ -310,7 +315,12 @@ const CustomProviderSettings: React.FC<CustomProviderSettingsProps> = ({ plugin,
console.log(`updateApplyModelId: ${provider} -> ${modelId}, isCustom: ${isCustom}`) console.log(`updateApplyModelId: ${provider} -> ${modelId}, isCustom: ${isCustom}`)
const providerSettingKey = getProviderSettingKey(provider); const providerSettingKey = getProviderSettingKey(provider);
const providerSettings = settings[providerSettingKey] || {}; const providerSettings = settings[providerSettingKey] || {};
const currentModels = providerSettings.models || new Set<string>(); const currentModels = providerSettings.models || [];
// 如果是自定义模型且不在列表中,则添加
const updatedModels = isCustom && !currentModels.includes(modelId)
? [...currentModels, modelId]
: currentModels;
handleSettingsUpdate({ handleSettingsUpdate({
...settings, ...settings,
@ -318,7 +328,7 @@ const CustomProviderSettings: React.FC<CustomProviderSettingsProps> = ({ plugin,
applyModelId: modelId, applyModelId: modelId,
[providerSettingKey]: { [providerSettingKey]: {
...providerSettings, ...providerSettings,
models: isCustom ? new Set([...currentModels, modelId]) : currentModels models: updatedModels
} }
}); });
}; };
@ -327,7 +337,12 @@ const CustomProviderSettings: React.FC<CustomProviderSettingsProps> = ({ plugin,
console.log(`updateEmbeddingModelId: ${provider} -> ${modelId}, isCustom: ${isCustom}`) console.log(`updateEmbeddingModelId: ${provider} -> ${modelId}, isCustom: ${isCustom}`)
const providerSettingKey = getProviderSettingKey(provider); const providerSettingKey = getProviderSettingKey(provider);
const providerSettings = settings[providerSettingKey] || {}; const providerSettings = settings[providerSettingKey] || {};
const currentModels = providerSettings.models || new Set<string>(); const currentModels = providerSettings.models || [];
// 如果是自定义模型且不在列表中,则添加
const updatedModels = isCustom && !currentModels.includes(modelId)
? [...currentModels, modelId]
: currentModels;
handleSettingsUpdate({ handleSettingsUpdate({
...settings, ...settings,
@ -335,7 +350,7 @@ const CustomProviderSettings: React.FC<CustomProviderSettingsProps> = ({ plugin,
embeddingModelId: modelId, embeddingModelId: modelId,
[providerSettingKey]: { [providerSettingKey]: {
...providerSettings, ...providerSettings,
models: isCustom ? new Set([...currentModels, modelId]) : currentModels models: updatedModels
} }
}); });
}; };

View File

@ -207,13 +207,13 @@ export const ComboBoxComponent: React.FC<ComboBoxComponentProps> = ({
const combinedModelIds = useMemo(() => { const combinedModelIds = useMemo(() => {
const providerKey = getProviderSettingKey(modelProvider); const providerKey = getProviderSettingKey(modelProvider);
const providerModels = settings?.[providerKey]?.models; const providerModels = settings?.[providerKey]?.models;
console.log(`🔍 Custom models in settings for ${modelProvider}:`, providerModels ? Array.from(providerModels) : 'none') console.log(`🔍 Custom models in settings for ${modelProvider}:`, providerModels || 'none')
// Ensure providerModels is a Set of strings // Ensure providerModels is an array of strings
if (!providerModels || !(providerModels instanceof Set)) { if (!providerModels || !Array.isArray(providerModels)) {
console.log(`📋 Using only official models (${modelIds.length}):`, modelIds); console.log(`📋 Using only official models (${modelIds.length}):`, modelIds);
return modelIds; return modelIds;
} }
const additionalModels = Array.from(providerModels).filter((model): model is string => typeof model === 'string'); const additionalModels = providerModels.filter((model): model is string => typeof model === 'string');
console.log(`📋 Combined models: ${modelIds.length} official + ${additionalModels.length} custom`); console.log(`📋 Combined models: ${modelIds.length} official + ${additionalModels.length} custom`);
return [...modelIds, ...additionalModels]; return [...modelIds, ...additionalModels];
}, [modelIds, settings, modelProvider]); }, [modelIds, settings, modelProvider]);
@ -286,6 +286,8 @@ export const ComboBoxComponent: React.FC<ComboBoxComponentProps> = ({
if (isValidProvider(newProvider)) { if (isValidProvider(newProvider)) {
setModelProvider(newProvider); setModelProvider(newProvider);
// 当提供商变更时,清空模型选择并通知父组件
updateModel(newProvider, '', false);
} }
}; };

View File

@ -20,13 +20,13 @@ const InfioProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'Infio', name: 'Infio',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const OpenRouterProviderSchema = z.object({ const OpenRouterProviderSchema = z.object({
@ -34,13 +34,13 @@ const OpenRouterProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'OpenRouter', name: 'OpenRouter',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const SiliconFlowProviderSchema = z.object({ const SiliconFlowProviderSchema = z.object({
@ -48,13 +48,13 @@ const SiliconFlowProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'SiliconFlow', name: 'SiliconFlow',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const AlibabaQwenProviderSchema = z.object({ const AlibabaQwenProviderSchema = z.object({
@ -62,13 +62,13 @@ const AlibabaQwenProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'AlibabaQwen', name: 'AlibabaQwen',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const AnthropicProviderSchema = z.object({ const AnthropicProviderSchema = z.object({
@ -76,13 +76,13 @@ const AnthropicProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().optional(), baseUrl: z.string().optional(),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'Anthropic', name: 'Anthropic',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const DeepSeekProviderSchema = z.object({ const DeepSeekProviderSchema = z.object({
@ -90,13 +90,13 @@ const DeepSeekProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'DeepSeek', name: 'DeepSeek',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const GoogleProviderSchema = z.object({ const GoogleProviderSchema = z.object({
@ -104,13 +104,13 @@ const GoogleProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'Google', name: 'Google',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const OpenAIProviderSchema = z.object({ const OpenAIProviderSchema = z.object({
@ -118,13 +118,13 @@ const OpenAIProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().optional(), baseUrl: z.string().optional(),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'OpenAI', name: 'OpenAI',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const OpenAICompatibleProviderSchema = z.object({ const OpenAICompatibleProviderSchema = z.object({
@ -132,13 +132,13 @@ const OpenAICompatibleProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().optional(), baseUrl: z.string().optional(),
useCustomUrl: z.boolean().catch(true), useCustomUrl: z.boolean().catch(true),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'OpenAICompatible', name: 'OpenAICompatible',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: true, useCustomUrl: true,
models: new Set() models: []
}) })
const OllamaProviderSchema = z.object({ const OllamaProviderSchema = z.object({
@ -146,13 +146,13 @@ const OllamaProviderSchema = z.object({
apiKey: z.string().catch('ollama'), apiKey: z.string().catch('ollama'),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'Ollama', name: 'Ollama',
apiKey: 'ollama', apiKey: 'ollama',
baseUrl: '', baseUrl: '',
useCustomUrl: true, useCustomUrl: true,
models: new Set() models: []
}) })
const GroqProviderSchema = z.object({ const GroqProviderSchema = z.object({
@ -160,13 +160,13 @@ const GroqProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'Groq', name: 'Groq',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const GrokProviderSchema = z.object({ const GrokProviderSchema = z.object({
@ -174,13 +174,13 @@ const GrokProviderSchema = z.object({
apiKey: z.string().catch(''), apiKey: z.string().catch(''),
baseUrl: z.string().catch(''), baseUrl: z.string().catch(''),
useCustomUrl: z.boolean().catch(false), useCustomUrl: z.boolean().catch(false),
models: z.set(z.string()).catch(new Set()) models: z.array(z.string()).catch([])
}).catch({ }).catch({
name: 'Grok', name: 'Grok',
apiKey: '', apiKey: '',
baseUrl: '', baseUrl: '',
useCustomUrl: false, useCustomUrl: false,
models: new Set() models: []
}) })
const ollamaModelSchema = z.object({ const ollamaModelSchema = z.object({