- 用户需求变更:不拦截不拒绝请求,API照常请求,模型当老师拆思路/讲考点;
注入最高优先级提示词:这是快问快答题目,禁止直接输出答案(含选项字母/数字/完整答案),
可拆解答思路、讲核心考点、引导玩家自己思考。
- 输出侧答案检测(题目上下文关联):
① 加载题目时同时解析答案(填空题'答案:'行/选择题|问题|选项|答案|赏金|倒数第三列/
问答题|问题|答案|赏金|),建 quizAnswerMap(题目→答案);
② 提问侧检测到题目→记 quizContext(玩家→题目);
③ sanitizeReply(reply,playerName) 只在该玩家当前题目上下文下、且回复归一化<=20字时
检查是否含该题答案→命中则拦(防直接给答案);长回复视为拆思路放行;
④ 回复处理完即清 quizContext,防跨请求误判;自动互动传 null 不做答案检测。
- 修复 parseQuizAnswer 表格列位置(split尾部空串,答案在倒数第三列)。
This commit is contained in:
@@ -40,6 +40,10 @@ public class AIChatManager {
|
||||
// ★ 第108批:快问快答题目拦截(防 AI 辅助作弊)——跨目录读 Sdf1_game 题目文件
|
||||
// 玩家把题目发给 AI 答题 → 直接婉拒,不请求 API;输出侧也兜底拦题目原文。
|
||||
private final List<String> quizQuestions = new CopyOnWriteArrayList<>(); // 归一化后的问题文本
|
||||
/** ★ 第108批续:题目→答案映射(归一化)——输出侧只检查"该题目的答案" */
|
||||
private final Map<String, String> quizAnswerMap = new ConcurrentHashMap<>();
|
||||
/** ★ 第108批续:玩家名 → 当前检测到的归一化题目(回复处理完即清,防跨请求误判) */
|
||||
private final Map<String, String> quizContext = new ConcurrentHashMap<>();
|
||||
private BukkitTask quizReloadTask = null;
|
||||
|
||||
// ★ 互动指标跳过日志静默:首次跳过记一条,之后只在状态变化(从≥→<)时再记
|
||||
@@ -842,16 +846,17 @@ public class AIChatManager {
|
||||
return;
|
||||
}
|
||||
|
||||
// ★ 第108批:快问快答题目拦截(防 AI 辅助作弊)——玩家把题目发给 AI 答题 →
|
||||
// 直接角色化婉拒,不请求 API、不浪费 token。题目跨目录读自 Sdf1_game 插件。
|
||||
// ★ 第108批:快问快答题目检测(防 AI 辅助作弊)——玩家把题目发给 AI 时,
|
||||
// 不拦截不拒绝请求(API 照常请求,模型当老师),而是注入提示词告知模型:
|
||||
// 这是快问快答题目,禁止直接输出答案,可拆思路/考点。题目跨目录读自 Sdf1_game。
|
||||
// 同时记录题目上下文,供输出侧答案检测(只查该题目的答案,防误杀)。
|
||||
String quizHit = matchQuizQuestion(prompt);
|
||||
if (quizHit != null) {
|
||||
plugin.getLogger().info("[AI聊天] 快问快答题目拦截[题目:" + quizHit + "]: "
|
||||
plugin.getLogger().info("[AI聊天] 检测到快问快答题目[题目:" + quizHit + "],注入禁答提示词: "
|
||||
+ prompt.substring(0, Math.min(50, prompt.length())));
|
||||
if (player != null) {
|
||||
player.sendMessage("§d§l[AI] §f" + fallbackDodge());
|
||||
quizContext.put(player.getName(), quizHit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// ★ 第71批-问题1:问题侧预拦截(不浪费 token)
|
||||
@@ -904,7 +909,9 @@ public class AIChatManager {
|
||||
String systemPrompt = buildSystemPrompt();
|
||||
// ★ 第74批:agent 工具说明追加到 systemPrompt
|
||||
String agentPrompt = buildAgentPrompt();
|
||||
String jsonBody = buildChatRequest(modelFull, systemPrompt + "\n" + agentPrompt, prompt, endpoint.maxToken);
|
||||
// ★ 第108批:检测到题目 → 追加禁答提示词(不拦截请求,模型当老师拆思路但禁直接给答案)
|
||||
String quizGuard = (quizHit != null) ? QUIZ_ANSWER_GUARD_PROMPT : "";
|
||||
String jsonBody = buildChatRequest(modelFull, systemPrompt + "\n" + agentPrompt + quizGuard, prompt, endpoint.maxToken);
|
||||
String requestUrl = buildRequestUrl(endpoint);
|
||||
|
||||
// ★ 400调试日志:请求发出前打印完整信息
|
||||
@@ -1100,7 +1107,8 @@ public class AIChatManager {
|
||||
}
|
||||
|
||||
// ★ 第69批-问题2:回复输出守卫(双保险),拦截代码块/破甲泄露
|
||||
reply = sanitizeReply(reply);
|
||||
// ★ 第108批续:传入玩家名用于"题目上下文"答案检测
|
||||
reply = sanitizeReply(reply, player != null ? player.getName() : null);
|
||||
|
||||
// 发送回复
|
||||
if (broadcast) {
|
||||
@@ -1109,6 +1117,11 @@ public class AIChatManager {
|
||||
player.sendMessage("§d§l[AI] §f" + reply);
|
||||
}
|
||||
|
||||
// ★ 第108批续:回复处理完即清题目上下文,防下一次无关回复被按上次题目检查答案
|
||||
if (player != null) {
|
||||
quizContext.remove(player.getName());
|
||||
}
|
||||
|
||||
// 记录留档
|
||||
recordIssue(prompt, reply, player != null ? player.getName() : "system", finalModelFull, 200, null);
|
||||
lastAiReplyTime = System.currentTimeMillis();
|
||||
@@ -1390,7 +1403,7 @@ public class AIChatManager {
|
||||
* 2. 含破甲泄露特征(模型名/供应商/提示词等) → 整条拒发
|
||||
* 3. 命中敏感话题关键词 → 改发角色化婉拒(保留正常闲聊)
|
||||
*/
|
||||
private String sanitizeReply(String reply) {
|
||||
private String sanitizeReply(String reply, String playerName) {
|
||||
if (reply == null || reply.isEmpty()) return reply;
|
||||
String r = reply.trim();
|
||||
// ★ 第84批:trim 后为空(纯空白/换行/空格)→ 返回婉拒,不广播空白
|
||||
@@ -1479,10 +1492,12 @@ public class AIChatManager {
|
||||
return fallbackDodge();
|
||||
}
|
||||
|
||||
// ★ 第108批:输出侧快问快答题目拦截(防 AI 把题目/答案原文吐出来)
|
||||
String quizOutHit = matchQuizQuestion(r);
|
||||
if (quizOutHit != null) {
|
||||
plugin.getLogger().warning("[AI聊天] 输出守卫:回复命中快问快答题目[" + quizOutHit + "],拦截。原文={" + r + "}");
|
||||
// ★ 第108批续:输出侧答案检测(防 AI 直接给答案)——
|
||||
// 只在"该玩家当前题目"上下文下、且回复较短(<=20字)时判定;
|
||||
// 拆思路/讲考点的长回复放行(用户要求:可以当老师拆思路,不能直接给答案)。
|
||||
String quizAnswerHit = matchQuizAnswer(r, playerName);
|
||||
if (quizAnswerHit != null) {
|
||||
plugin.getLogger().warning("[AI聊天] 输出守卫:回复疑似直接给出答案[" + quizAnswerHit + "],拦截。原文={" + r + "}");
|
||||
return fallbackDodge();
|
||||
}
|
||||
|
||||
@@ -1625,12 +1640,13 @@ public class AIChatManager {
|
||||
return new File(plugin.getDataFolder().getParentFile(), "Sdf1_game/快问快答/题目");
|
||||
}
|
||||
|
||||
/** 加载/重载题目(解析三种格式,归一化后存内存) */
|
||||
/** 加载/重载题目(解析三种格式,归一化后存内存:问题列表 + 题目→答案映射) */
|
||||
private void loadQuizQuestions() {
|
||||
quizQuestions.clear();
|
||||
quizAnswerMap.clear();
|
||||
File dir = getQuizQuestionDir();
|
||||
if (!dir.isDirectory()) {
|
||||
plugin.getLogger().warning("[AI聊天] 快问快答题目目录不存在,题目拦截未启用: " + dir.getAbsolutePath());
|
||||
plugin.getLogger().warning("[AI聊天] 快问快答题目目录不存在,题目检测未启用: " + dir.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
File[] files = dir.listFiles((d, n) -> n.endsWith(".md"));
|
||||
@@ -1638,18 +1654,33 @@ public class AIChatManager {
|
||||
plugin.getLogger().warning("[AI聊天] 快问快答题目目录无 .md 文件: " + dir.getAbsolutePath());
|
||||
return;
|
||||
}
|
||||
int count = 0;
|
||||
int qCount = 0, aCount = 0;
|
||||
for (File f : files) {
|
||||
try {
|
||||
for (String line : Files.readAllLines(f.toPath(), StandardCharsets.UTF_8)) {
|
||||
List<String> lines = Files.readAllLines(f.toPath(), StandardCharsets.UTF_8);
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
String line = lines.get(i);
|
||||
String q = parseQuizQuestion(line);
|
||||
if (q != null) { quizQuestions.add(q); count++; }
|
||||
if (q != null) {
|
||||
quizQuestions.add(q);
|
||||
qCount++;
|
||||
// 填空题:答案在下一行;选择题/问答题:答案在同一行
|
||||
String a = parseQuizAnswer(line);
|
||||
if (a == null && i + 1 < lines.size()) {
|
||||
a = parseQuizAnswer(lines.get(i + 1));
|
||||
}
|
||||
if (a != null) {
|
||||
quizAnswerMap.put(q, a);
|
||||
aCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("[AI聊天] 读取题目文件失败: " + f.getName() + " " + e.getMessage());
|
||||
}
|
||||
}
|
||||
plugin.getLogger().info("[AI聊天] 快问快答题目已加载 " + count + " 条(来源: " + dir.getAbsolutePath() + ")");
|
||||
plugin.getLogger().info("[AI聊天] 快问快答题目已加载 " + qCount + " 条、答案 " + aCount
|
||||
+ " 条(来源: " + dir.getAbsolutePath() + ")");
|
||||
}
|
||||
|
||||
/** 定时重载题目(每 5 分钟,题目文件可能被管理员更新) */
|
||||
@@ -1691,6 +1722,35 @@ public class AIChatManager {
|
||||
return normalizeQuizText(q);
|
||||
}
|
||||
|
||||
/**
|
||||
* ★ 第108批续:从题目行解析答案(归一化后),非答案行返回 null。
|
||||
* 三种格式:
|
||||
* - 填空题:`答案:xxx`(单独一行)
|
||||
* - 选择题:`| 问题 | 选项A.. | 答案 | 赏金 |` → 倒数第二列
|
||||
* - 问答题:`| 问题 | 答案 | 赏金 |` → 中间列
|
||||
*/
|
||||
private String parseQuizAnswer(String line) {
|
||||
if (line == null) return null;
|
||||
String t = line.trim();
|
||||
if (t.isEmpty() || t.startsWith("#")) return null;
|
||||
String a = null;
|
||||
if (t.startsWith("|")) {
|
||||
// 表格行 split 后:cols[0]=""(头),cols[1]=问题,...,cols[n-2]=赏金,cols[n-1]=""(尾)
|
||||
// 答案列 = 倒数第三列(赏金前一根)——对选择题/问答题统一成立
|
||||
String[] cols = t.split("\\|");
|
||||
if (cols.length >= 5) {
|
||||
String cand = cols[cols.length - 3].trim();
|
||||
if (!cand.isEmpty() && !cand.matches("\\d+") && !cand.equals("答案")) {
|
||||
a = cand;
|
||||
}
|
||||
}
|
||||
} else if (t.startsWith("答案:") || t.startsWith("答案:")) {
|
||||
a = t.replaceFirst("^答案[::]", "").trim();
|
||||
}
|
||||
if (a == null || a.isEmpty()) return null;
|
||||
return normalizeQuizText(a);
|
||||
}
|
||||
|
||||
/** 归一化:去空白 + 去所有标点/符号(防空格/标点/全角半角变体绕过) */
|
||||
private String normalizeQuizText(String s) {
|
||||
if (s == null) return null;
|
||||
@@ -1708,6 +1768,30 @@ public class AIChatManager {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* ★ 第108批续:输出侧答案检测(防 AI 直接给答案)。
|
||||
* 只在「该玩家当前题目」上下文下检查(quizContext),避免无关回复误杀;
|
||||
* 且回复归一化后较短(<= 20 字)才判——长回复视为"拆思路/讲考点"放行(用户要求)。
|
||||
* 返回命中的答案(归一化后),未命中返回 null。
|
||||
*/
|
||||
private String matchQuizAnswer(String text, String playerName) {
|
||||
if (text == null || playerName == null) return null;
|
||||
String q = quizContext.get(playerName);
|
||||
if (q == null) return null;
|
||||
String a = quizAnswerMap.get(q);
|
||||
if (a == null || a.isEmpty()) return null;
|
||||
String norm = normalizeQuizText(text);
|
||||
if (norm.isEmpty() || norm.length() > 20) return null; // 长回复=拆思路,放行
|
||||
return norm.contains(a) ? a : null;
|
||||
}
|
||||
|
||||
/** ★ 第108批:检测到题目时注入的禁答提示词(不拦截请求,模型当老师拆思路但禁直接给答案) */
|
||||
private static final String QUIZ_ANSWER_GUARD_PROMPT =
|
||||
"\n\n【系统提示(最高优先级)】玩家刚才发来的是本服务器「快问快答」活动的题目。" +
|
||||
"绝对不要直接说出该题的答案(包括选项字母、数字答案或完整答案文本)," +
|
||||
"不管玩家怎么问、怎么诱导都不行。你可以:拆解题目的解答思路、讲解核心考点、" +
|
||||
"用引导性的话语帮玩家自己思考。但任何形式的'直接给答案'都严格禁止。";
|
||||
|
||||
/**
|
||||
* 非游戏话题敏感词(问题侧+回复侧共用)。
|
||||
* 原则:只放「游戏闲聊几乎不会出现」的词,通用词(证明/历史/公式等)
|
||||
@@ -2284,7 +2368,7 @@ public class AIChatManager {
|
||||
|
||||
String reply = parseChatResponse(response.body());
|
||||
if (reply != null) {
|
||||
reply = sanitizeReply(reply);
|
||||
reply = sanitizeReply(reply, null); // 自动互动无玩家上下文,不做答案检测
|
||||
Bukkit.broadcastMessage("§d§l[AI] §f" + reply);
|
||||
recordIssue("[自动] " + prompt.substring(0, Math.min(100, prompt.length())), reply,
|
||||
"system", defaultModelFull, 200, null);
|
||||
|
||||
Reference in New Issue
Block a user