第78批:联网模式type路由与deepseek模型解耦+hasDeepseekModel标记+resolveSearchApiKey路由方法
Auto Release / build (push) Successful in 2m31s

This commit is contained in:
youpaishidifu
2026-11-04 18:17:11 +08:00
parent df18280b3d
commit b86a4794b8
+53 -2
View File
@@ -121,6 +121,13 @@ public class AIChatManager {
// 在 loadConfig 时自动从模型接口配置中提取 DeepSeek 供应商的 key // 在 loadConfig 时自动从模型接口配置中提取 DeepSeek 供应商的 key
private String deepseekApiKey = ""; private String deepseekApiKey = "";
// ★ 第78批:联网模式路由
// hasDeepseekModel=true → 配置了内置 deepseek 模型,自动用 deepseek 接口(不看 联网模式.type)
// hasDeepseekModel=false + 联网模式.type 填 sk-xxxxx → 用该 key 走 deepseek 接口
// hasDeepseekModel=false + 联网模式.type 留空/内置 → 走 DuckDuckGo 简易 agent
private boolean hasDeepseekModel = false;
private String 联网模式Type = "";
public AIChatManager(Main plugin) { public AIChatManager(Main plugin) {
this.plugin = plugin; this.plugin = plugin;
this.httpClient = HttpClient.newBuilder() this.httpClient = HttpClient.newBuilder()
@@ -335,6 +342,20 @@ public class AIChatManager {
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
} }
// ★ 第78批:读取 联网模式.type
// 配置了 deepseek 模型时 → 忽略此字段,自动走 deepseek 接口
// 未配置 deepseek 时 → type 留空/内置 → 走 DDG 简易 agent;填 sk-xxx → 走 deepseek 接口
if (section2.has("联网模式")) {
JsonElement wlm = section2.get("联网模式");
if (wlm.isJsonObject()) {
JsonObject wlmObj = wlm.getAsJsonObject();
JsonElement typeEl = wlmObj.get("type");
if (typeEl != null && typeEl.isJsonPrimitive()) {
联网模式Type = typeEl.getAsString().trim();
}
}
}
} }
} }
@@ -435,6 +456,9 @@ public class AIChatManager {
// 旧逻辑:defaultModelFull.isEmpty() 条件阻止热重载时更新默认模型 // 旧逻辑:defaultModelFull.isEmpty() 条件阻止热重载时更新默认模型
// 修复:每次解析都重置,让新配置的「默认使用」生效 // 修复:每次解析都重置,让新配置的「默认使用」生效
defaultModelFull = ""; defaultModelFull = "";
// ★ 第78批:重置联网模式路由状态(热重载时需要重新判断)
hasDeepseekModel = false;
deepseekApiKey = "";
// 遍历所有供应商配置 // 遍历所有供应商配置
for (Map.Entry<String, JsonElement> entry : modelConfig.entrySet()) { for (Map.Entry<String, JsonElement> entry : modelConfig.entrySet()) {
@@ -487,6 +511,10 @@ public class AIChatManager {
deepseekApiKey = apiKey; deepseekApiKey = apiKey;
if (!quiet) plugin.getLogger().info("[AI聊天-Agent] 已提取 DeepSeek API key,联网搜索可用"); if (!quiet) plugin.getLogger().info("[AI聊天-Agent] 已提取 DeepSeek API key,联网搜索可用");
} }
// ★ 第78批:标记配置了 deepseek 模型 → 联网搜索自动走 deepseek 接口(不看 联网模式.type)
if (baseUrl.contains("api.deepseek.com")) {
hasDeepseekModel = true;
}
// 存储原始 baseUrl(尾斜杠统一在发送时按自动补齐开关处理) // 存储原始 baseUrl(尾斜杠统一在发送时按自动补齐开关处理)
ModelEndpoint endpoint = new ModelEndpoint(); ModelEndpoint endpoint = new ModelEndpoint();
@@ -935,11 +963,12 @@ public class AIChatManager {
if (call == null) break; if (call == null) break;
plugin.getLogger().info("[AI聊天-Agent] 第" + (agentRound + 1) + "轮工具调用: " + call.type + " \"" + call.arg + "\""); plugin.getLogger().info("[AI聊天-Agent] 第" + (agentRound + 1) + "轮工具调用: " + call.type + " \"" + call.arg + "\"");
String searchKey = resolveSearchApiKey();
String toolResults; String toolResults;
if (call.type == ToolCall.Type.FETCH) { if (call.type == ToolCall.Type.FETCH) {
toolResults = WebSearchTool.fetchUrl(call.arg, deepseekApiKey); toolResults = WebSearchTool.fetchUrl(call.arg, searchKey);
} else { } else {
toolResults = WebSearchTool.search(call.arg, deepseekApiKey); toolResults = WebSearchTool.search(call.arg, searchKey);
} }
if (toolResults == null) { if (toolResults == null) {
plugin.getLogger().info("[AI聊天-Agent] 工具调用无结果,告知模型无法获取"); plugin.getLogger().info("[AI聊天-Agent] 工具调用无结果,告知模型无法获取");
@@ -2012,6 +2041,28 @@ public class AIChatManager {
return ep != null && ep.providerName != null ? ep.providerName : ""; return ep != null && ep.providerName != null ? ep.providerName : "";
} }
/**
* ★ 第78批:联网搜索接口路由
* <ul>
* <li>配置了内置 deepseek 模型 → 返回 deepseekApiKey(自动提取,不看 联网模式.type)</li>
* <li>未配置 deepseek + 联网模式.type 填 sk-xxxxx → 返回该 key(走 deepseek 接口)</li>
* <li>未配置 deepseek + 联网模式.type 留空/内置 → 返回 ""(走 DuckDuckGo 简易 agent)</li>
* </ul>
* WebSearchTool.search() 内部:apiKey 非空 → DeepSeek,为空 → DuckDuckGo
*/
private String resolveSearchApiKey() {
if (hasDeepseekModel) {
return deepseekApiKey;
}
// 未配置 deepseek 模型 → 看 联网模式.type
String type = 联网模式Type.trim();
if (type.startsWith("sk-") && type.length() > 3) {
return type;
}
// 留空/内置/其它 → 空串 → DDG 简易 agent
return "";
}
// ==================== 工具方法 ==================== // ==================== 工具方法 ====================
private String getStr(JsonObject obj, String key) { private String getStr(JsonObject obj, String key) {