第121批续16-遗留4-问题①②:wiki独立超时5s/8s + agent第2轮失败兜底回复
Auto Release / build (push) Successful in 2m14s

问题①(WebSearchTool):wiki 连接超时 10s→5s、读取 10s→8s(独立 WIKI_CONNECT_TIMEOUT_MS/WIKI_READ_TIMEOUT_MS),
  zh.minecraft.wiki 走 Cloudflare 偶发 Connect timed out 阻塞 agent 循环 → 快速 fallthrough 到 so/bing/sogou。
  httpGet 新增 (urlStr, ua, connectTimeout, readTimeout) 重载,旧签名转调默认 10s/10s。

问题②(AIChatManager):agent 第2轮请求失败/HTTP非200/超时异常时,reply 仍是第1轮 TOOL 标记行 →
  sanitizeReply 拦裸关键词 → 玩家看到 fallbackDodge(换话题)而非基于工具结果的回答。
  修复:第2轮失败/异常时用已有工具结果拼兜底回复(「我查到了相关资料…基于搜索结果…详细内容请自行查阅上述链接」),
  trimmedToolResults 截断 200 字符防过长,删 TOOL 标记后输出。
This commit is contained in:
youpaishidifu
2026-11-13 22:50:06 +08:00
parent 14d3a4e477
commit 3e3452a1af
2 changed files with 26 additions and 5 deletions
@@ -1152,6 +1152,12 @@ public class AIChatManager {
HttpResponse<String> followUpResp = httpClient.send(followUp, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8)); HttpResponse<String> followUpResp = httpClient.send(followUp, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
if (followUpResp.statusCode() != 200) { if (followUpResp.statusCode() != 200) {
plugin.getLogger().warning("[AI聊天-Agent] 第" + (agentRound + 1) + "轮请求失败: HTTP " + followUpResp.statusCode()); plugin.getLogger().warning("[AI聊天-Agent] 第" + (agentRound + 1) + "轮请求失败: HTTP " + followUpResp.statusCode());
// ★ 第121批续16-遗留4-问题②:第2轮失败时 reply 仍是第1轮 TOOL 标记行,
// sanitizeReply 会拦裸关键词 → 玩家看到 fallbackDodge(换话题),
// 而非基于工具结果的回答。改为:用已有工具结果拼兜底回复,删 TOOL 标记。
reply = "我查到了相关资料,但整理答案时出了点问题。基于搜索结果,我可以告诉你:"
+ trimmedToolResults.substring(0, Math.min(200, trimmedToolResults.length()))
+ "……(详细内容请自行查阅上述链接)";
break; break;
} }
String followUpReply = parseChatResponse(followUpResp.body()); String followUpReply = parseChatResponse(followUpResp.body());
@@ -1161,6 +1167,11 @@ public class AIChatManager {
reply = followUpReply.replaceAll("TOOL:(SEARCH|FETCH):[^\\n]*", "").trim(); reply = followUpReply.replaceAll("TOOL:(SEARCH|FETCH):[^\\n]*", "").trim();
} catch (Exception e) { } catch (Exception e) {
plugin.getLogger().warning("[AI聊天-Agent] 第" + (agentRound + 1) + "轮请求异常: " + e.getMessage()); plugin.getLogger().warning("[AI聊天-Agent] 第" + (agentRound + 1) + "轮请求异常: " + e.getMessage());
// ★ 第121批续16-遗留4-问题②:第2轮超时/异常时同样兜底——用已有工具结果拼回复,
// 避免 reply 残留 TOOL 标记被 sanitizeReply 拦成 fallbackDodge。
reply = "我查到了相关资料,但整理答案时出了点问题。基于搜索结果,我可以告诉你:"
+ trimmedToolResults.substring(0, Math.min(200, trimmedToolResults.length()))
+ "……(详细内容请自行查阅上述链接)";
break; break;
} }
agentRound++; agentRound++;
+15 -5
View File
@@ -36,6 +36,11 @@ public class WebSearchTool {
private static final int MAX_RESULTS = 5; private static final int MAX_RESULTS = 5;
private static final int TIMEOUT_MS = 10000; private static final int TIMEOUT_MS = 10000;
// ★ 第121批续16-遗留4-问题①:wiki 独立超时——zh.minecraft.wiki 走 Cloudflare,
// TCP connect 偶发 10s+ 挂起(SocketTimeoutException: Connect timed out),
// 阻塞 agent 循环导致「模型迟迟不输出」。wiki 连接 5s/读取 8s,超时快速 fallthrough。
private static final int WIKI_CONNECT_TIMEOUT_MS = 5000;
private static final int WIKI_READ_TIMEOUT_MS = 8000;
private static final String USER_AGENT = private static final String USER_AGENT =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36";
@@ -254,7 +259,7 @@ public class WebSearchTool {
String url = WIKI_SEARCH_URL String url = WIKI_SEARCH_URL
+ URLEncoder.encode(query, StandardCharsets.UTF_8.name()).replace("+", "%20") + URLEncoder.encode(query, StandardCharsets.UTF_8.name()).replace("+", "%20")
+ WIKI_SEARCH_PARAMS; + WIKI_SEARCH_PARAMS;
String html = httpGet(url, WIKI_UA); String html = httpGet(url, WIKI_UA, WIKI_CONNECT_TIMEOUT_MS, WIKI_READ_TIMEOUT_MS);
if (html == null) return null; if (html == null) return null;
Pattern tp = Pattern.compile( Pattern tp = Pattern.compile(
@@ -295,7 +300,7 @@ public class WebSearchTool {
private static String mcWikiFetch(String url) { private static String mcWikiFetch(String url) {
try { try {
String encoded = encodeWikiUrl(url); String encoded = encodeWikiUrl(url);
String html = httpGet(encoded, WIKI_UA); String html = httpGet(encoded, WIKI_UA, WIKI_CONNECT_TIMEOUT_MS, WIKI_READ_TIMEOUT_MS);
if (html == null) return null; if (html == null) return null;
int s = html.indexOf("<div class=\"mw-content-ltr mw-parser-output\""); int s = html.indexOf("<div class=\"mw-content-ltr mw-parser-output\"");
@@ -473,17 +478,22 @@ public class WebSearchTool {
/** GET 请求拉整页;非 200 或异常返回 null(调用方决定下一个后端) */ /** GET 请求拉整页;非 200 或异常返回 null(调用方决定下一个后端) */
private static String httpGet(String urlStr) throws Exception { private static String httpGet(String urlStr) throws Exception {
return httpGet(urlStr, USER_AGENT); return httpGet(urlStr, USER_AGENT, TIMEOUT_MS, TIMEOUT_MS);
} }
/** GET 请求拉整页(自定义 UA——wiki 必须用 Java UA 过 Cloudflare) */ /** GET 请求拉整页(自定义 UA——wiki 必须用 Java UA 过 Cloudflare) */
private static String httpGet(String urlStr, String ua) throws Exception { private static String httpGet(String urlStr, String ua) throws Exception {
return httpGet(urlStr, ua, TIMEOUT_MS, TIMEOUT_MS);
}
/** GET 请求拉整页(自定义 UA + 独立 connect/read 超时——wiki 快速 fallthrough) */
private static String httpGet(String urlStr, String ua, int connectTimeout, int readTimeout) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection(); HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();
conn.setRequestMethod("GET"); conn.setRequestMethod("GET");
conn.setRequestProperty("User-Agent", ua); conn.setRequestProperty("User-Agent", ua);
conn.setRequestProperty("Accept", "text/html"); conn.setRequestProperty("Accept", "text/html");
conn.setConnectTimeout(TIMEOUT_MS); conn.setConnectTimeout(connectTimeout);
conn.setReadTimeout(TIMEOUT_MS); conn.setReadTimeout(readTimeout);
int code = conn.getResponseCode(); int code = conn.getResponseCode();
if (code != 200) { if (code != 200) {