From b86a4794b894a5fe783c5ab96791f6955e586444 Mon Sep 17 00:00:00 2001 From: youpaishidifu <1@ypshidifu.cn> Date: Wed, 4 Nov 2026 18:17:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC78=E6=89=B9=EF=BC=9A=E8=81=94=E7=BD=91?= =?UTF-8?q?=E6=A8=A1=E5=BC=8Ftype=E8=B7=AF=E7=94=B1=E4=B8=8Edeepseek?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=A7=A3=E8=80=A6+hasDeepseekModel=E6=A0=87?= =?UTF-8?q?=E8=AE=B0+resolveSearchApiKey=E8=B7=AF=E7=94=B1=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/Sdf1_login/AIChatManager.java | 55 ++++++++++++++++++++- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/main/java/Sdf1_login/AIChatManager.java b/src/main/java/Sdf1_login/AIChatManager.java index ac0f426..3079253 100644 --- a/src/main/java/Sdf1_login/AIChatManager.java +++ b/src/main/java/Sdf1_login/AIChatManager.java @@ -121,6 +121,13 @@ public class AIChatManager { // 在 loadConfig 时自动从模型接口配置中提取 DeepSeek 供应商的 key 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) { this.plugin = plugin; this.httpClient = HttpClient.newBuilder() @@ -335,6 +342,20 @@ public class AIChatManager { } 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 = ""; + // ★ 第78批:重置联网模式路由状态(热重载时需要重新判断) + hasDeepseekModel = false; + deepseekApiKey = ""; // 遍历所有供应商配置 for (Map.Entry entry : modelConfig.entrySet()) { @@ -487,6 +511,10 @@ public class AIChatManager { deepseekApiKey = apiKey; if (!quiet) plugin.getLogger().info("[AI聊天-Agent] 已提取 DeepSeek API key,联网搜索可用"); } + // ★ 第78批:标记配置了 deepseek 模型 → 联网搜索自动走 deepseek 接口(不看 联网模式.type) + if (baseUrl.contains("api.deepseek.com")) { + hasDeepseekModel = true; + } // 存储原始 baseUrl(尾斜杠统一在发送时按自动补齐开关处理) ModelEndpoint endpoint = new ModelEndpoint(); @@ -935,11 +963,12 @@ public class AIChatManager { if (call == null) break; plugin.getLogger().info("[AI聊天-Agent] 第" + (agentRound + 1) + "轮工具调用: " + call.type + " \"" + call.arg + "\""); + String searchKey = resolveSearchApiKey(); String toolResults; if (call.type == ToolCall.Type.FETCH) { - toolResults = WebSearchTool.fetchUrl(call.arg, deepseekApiKey); + toolResults = WebSearchTool.fetchUrl(call.arg, searchKey); } else { - toolResults = WebSearchTool.search(call.arg, deepseekApiKey); + toolResults = WebSearchTool.search(call.arg, searchKey); } if (toolResults == null) { plugin.getLogger().info("[AI聊天-Agent] 工具调用无结果,告知模型无法获取"); @@ -2012,6 +2041,28 @@ public class AIChatManager { return ep != null && ep.providerName != null ? ep.providerName : ""; } + /** + * ★ 第78批:联网搜索接口路由 + * + * 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) {