Skip to content

Commit bade06f

Browse files
committed
自动选择模型
1 parent af851f4 commit bade06f

File tree

4 files changed

+106
-12
lines changed

4 files changed

+106
-12
lines changed

src/renderer/components/ChatWindow.jsx

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,76 @@ const ChatWindow = memo(({ session, onUpdateSession }) => {
603603
})
604604
.filter((item) => item.models.length > 0); // 只返回有启用模型的提供商
605605
};
606+
const providerModels = getProviderModels();
606607

607608
// 获取当前选择的模型
608609
const getCurrentModel = () => {
610+
// 首先检查配置中是否有providerId和modelId
609611
if (!config.providerId || !config.modelId) return null;
612+
// 检查当前选择的提供商是否存在于可用提供商列表中
613+
const provider = providerModels.find(
614+
(item) => item.provider.id === config.providerId
615+
);
616+
if (!provider) return null;
617+
618+
// 检查当前选择的模型是否存在于可用模型列表中
619+
const modelExists = provider.models.some(
620+
(model) => model.id === config.modelId
621+
);
622+
if (!modelExists) return null;
623+
624+
// 只有当提供商和模型都存在且启用时,才返回完整的选择值
610625
return `${config.providerId}|${config.modelId}`;
611626
};
612627

613-
const providerModels = getProviderModels();
628+
// 检查当前选中的模型是否可用,如果不可用则选择第一个可用模型
629+
useEffect(() => {
630+
if (!config.providerId || !config.modelId || providerModels.length === 0)
631+
return;
632+
633+
// 检查当前选择的提供商是否存在于可用提供商列表中
634+
const provider = providerModels.find(
635+
(item) => item.provider.id === config.providerId
636+
);
637+
638+
// 如果提供商不存在或没有模型,选择第一个可用模型
639+
if (!provider) {
640+
// 如果有可用的提供商和模型,选择第一个
641+
if (providerModels.length > 0 && providerModels[0].models.length > 0) {
642+
const firstProvider = providerModels[0];
643+
const firstModel = firstProvider.models[0];
644+
handleModelChange(`${firstProvider.provider.id}|${firstModel.id}`);
645+
console.log(`自动选择了第一个可用模型: ${firstModel.name}`);
646+
message.info(t("chat.autoSelectedModel", { model: firstModel.name }));
647+
}
648+
return;
649+
}
650+
651+
// 检查当前选择的模型是否存在于可用模型列表中
652+
const modelExists = provider.models.some(
653+
(model) => model.id === config.modelId
654+
);
655+
656+
// 如果模型不存在,选择该提供商的第一个可用模型
657+
if (!modelExists) {
658+
if (provider.models.length > 0) {
659+
const firstModel = provider.models[0];
660+
handleModelChange(`${provider.provider.id}|${firstModel.id}`);
661+
console.log(`当前模型不可用,自动选择了: ${firstModel.name}`);
662+
message.info(t("chat.autoSelectedModel", { model: firstModel.name }));
663+
} else if (
664+
providerModels.length > 0 &&
665+
providerModels[0].models.length > 0
666+
) {
667+
// 如果该提供商没有可用模型,选择第一个可用的提供商的第一个模型
668+
const firstProvider = providerModels[0];
669+
const firstModel = firstProvider.models[0];
670+
handleModelChange(`${firstProvider.provider.id}|${firstModel.id}`);
671+
console.log(`自动选择了第一个可用模型: ${firstModel.name}`);
672+
message.info(t("chat.autoSelectedModel", { model: firstModel.name }));
673+
}
674+
}
675+
}, [config.providerId, config.modelId, providerModels, handleModelChange]);
614676

615677
return (
616678
<div className="chat-window">

src/renderer/hooks/useUserConfig.js

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -277,24 +277,54 @@ if (typeof window !== "undefined") {
277277
export function isAIConfigured() {
278278
const config = getUserConfig();
279279
const providersConfig = getProvidersConfig();
280-
console.log("providersConfig", providersConfig);
281-
console.log("config", config);
280+
console.log("检查AI配置:", { userConfig: config, providersConfig });
281+
282282
// 检查是否选择了提供商和模型
283283
if (!config.providerId || !config.modelId) {
284+
console.log("未选择提供商或模型");
284285
return false;
285286
}
286287

287288
// 检查提供商是否存在且已启用
288289
const provider = providersConfig[config.providerId];
289-
if (!provider || provider.enabled === false) {
290+
if (!provider) {
291+
console.log("提供商不存在:", config.providerId);
292+
return false;
293+
}
294+
295+
// 如果提供商被明确禁用,则返回false
296+
if (provider.enabled === false) {
297+
console.log("提供商被禁用:", config.providerId);
290298
return false;
291299
}
292300

293-
// 检查模型是否存在
294-
const providerConfig = provider || {};
301+
// 检查模型是否存在于配置中
302+
const savedModels = provider.models || [];
303+
const modelExists = savedModels.some(
304+
(m) => m.id === config.modelId && m.enabled !== false && m.deleted !== true
305+
);
306+
307+
if (!modelExists) {
308+
console.log("模型不存在或被禁用:", config.modelId);
309+
310+
// 在这里不返回false,因为模型可能在系统定义但不在用户配置中
311+
// 后续会通过getAllProviders自动选择合适的模型
312+
}
313+
314+
// 如果是模拟响应模式(mock)或开发环境,不需要API密钥
315+
if (provider.mockResponse || process.env.NODE_ENV === "development") {
316+
return true;
317+
}
318+
319+
// API密钥检查变为可选,如果提供商有baseUrl且能正常工作,即使没有API密钥也可以接受
320+
// 对于某些提供商,如果baseUrl有效,可以不需要apiKey
321+
if (provider.baseUrl) {
322+
return true;
323+
}
295324

296-
// 检查 API 密钥是否已设置
297-
if (!providerConfig.apiKey && !providerConfig.mockResponse) {
325+
// 如果都不满足,检查是否有API密钥
326+
if (!provider.apiKey) {
327+
console.log("提供商没有API密钥:", config.providerId);
298328
return false;
299329
}
300330

src/renderer/i18n/locales/en/translation.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@
117117
"toolsFoundSuccessMsg": "Successfully retrieved {{count}} tools",
118118
"refreshToolsFailedMsg": "Failed to refresh tool list: {{message}}",
119119
"loadMessagesFailed": "Failed to load messages",
120-
"sendMessageFailed": "Failed to send message",
121120
"missingProviderOrModel": "Please select a service provider and model first",
122121
"providerDisabled": "Service provider {{name}} is disabled, please enable it in settings first",
123122
"unsupportedProvider": "Unsupported provider: {{name}}",
@@ -144,7 +143,7 @@
144143
"addProvider": "Add Provider",
145144
"apiSettings": "API Settings",
146145
"apiSettingsInfo": "Provider API Configuration",
147-
"apiSettingsDescription": "Configure the API authentication and endpoint for this service provider. You need to set the API key to use this provider's models.",
146+
"apiSettingsDescription": "Configure the API authentication and endpoint for this service provider.",
148147
"apiBaseUrl": "API Base URL",
149148
"enterApiBaseUrl": "Enter API Base URL",
150149
"enterApiKey": "Enter API Key"
@@ -188,6 +187,8 @@
188187
"noToolCallsDirectComplete": "No tool calls, direct complete callback",
189188
"apiCallFailed": "AI API call failed",
190189
"requestCancelled": "Request cancelled by user",
190+
"autoSelectedModel": "Auto-selected model: {{model}}",
191+
"sendMessageFailed": "Failed to send message",
191192
"mcpTools": {
192193
"useTool": "Use MCP Tool",
193194
"loading": "Loading tools...",

src/renderer/i18n/locales/zh-CN/translation.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@
117117
"toolsFoundSuccessMsg": "成功获取到 {{count}} 个工具",
118118
"refreshToolsFailedMsg": "刷新工具列表失败:{{message}}",
119119
"loadMessagesFailed": "加载消息失败",
120-
"sendMessageFailed": "发送消息失败",
121120
"missingProviderOrModel": "请先选择服务提供商和模型",
122121
"providerDisabled": "服务提供商 {{name}} 已被禁用,请先在设置中启用",
123122
"unsupportedProvider": "不支持的提供商: {{name}}",
@@ -144,7 +143,7 @@
144143
"addProvider": "添加供应商",
145144
"apiSettings": "API 设置",
146145
"apiSettingsInfo": "供应商 API 配置",
147-
"apiSettingsDescription": "配置此服务提供商的 API 认证和端点。您需要设置 API 密钥才能使用该提供商的模型。",
146+
"apiSettingsDescription": "配置此服务提供商的 API 认证和端点。",
148147
"apiBaseUrl": "API 基础 URL",
149148
"enterApiBaseUrl": "输入 API 基础 URL",
150149
"enterApiKey": "输入 API 密钥"
@@ -188,6 +187,8 @@
188187
"noToolCallsDirectComplete": "没有工具调用,直接调用onComplete回调",
189188
"apiCallFailed": "调用 AI API 失败",
190189
"requestCancelled": "请求被用户取消",
190+
"autoSelectedModel": "已自动选择模型: {{model}}",
191+
"sendMessageFailed": "发送消息失败",
191192
"mcpTools": {
192193
"useTool": "使用MCP工具",
193194
"loading": "加载工具中...",

0 commit comments

Comments
 (0)