第58批续24: 互动指标(在线玩家阈值) + 上下文压缩 + credit_score同步PHP
1. AIChatManager.interactionMetric 字段:在线玩家数量阈值,默认1 - 配置加载:models.json → 配置部分 → 2 → 互动指标 → 指标 - checkAutoChat() 在线玩家数量达标才触发自动互动(手动@//不受影响) 2. buildChatRequest 移除 max_tokens 字段(让API用默认输出上限) 3. 新增 compressChatBuffer() 上下文压缩:超阈值从尾部截断只保留最近消息 - checkAutoChat 改用 compressChatBuffer(endpoint.maxToken) 构建提示词 4. models.json 新增「互动指标」配置模板 5. WebManager credit_score 字段同步到PHP(信誉积分双向对账)
This commit is contained in:
@@ -54,6 +54,9 @@ public class AIChatManager {
|
||||
// 自定义供应商 URL 是否自动拼接 chat/completions(自动补齐开关,默认 true)
|
||||
private boolean autoAppendCompletions = true;
|
||||
|
||||
// 互动指标:在线玩家数量达到此值才触发自动互动(手动 @// 不受影响)
|
||||
private int interactionMetric = 1;
|
||||
|
||||
// 空闲调度器
|
||||
private BukkitTask autoChatTask = null;
|
||||
private long lastAiReplyTime = 0;
|
||||
@@ -179,6 +182,16 @@ public class AIChatManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 互动指标:在线玩家数量达到此值才触发自动互动
|
||||
if (section2.has("互动指标")) {
|
||||
JsonObject metricObj = section2.getAsJsonObject("互动指标");
|
||||
if (metricObj.has("指标")) {
|
||||
try {
|
||||
interactionMetric = metricObj.get("指标").getAsInt();
|
||||
} catch (Exception ignored) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -503,6 +516,11 @@ public class AIChatManager {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建聊天请求体(OpenAI 兼容格式)
|
||||
* 注意:不发送 max_tokens 字段,让 API 使用默认输出上限
|
||||
* maxToken 参数仅用于本地上下文长度阈值判断(在 checkAutoChat 中做压缩)
|
||||
*/
|
||||
private String buildChatRequest(String model, String systemPrompt, String userPrompt, String maxToken) {
|
||||
Gson gson = new Gson();
|
||||
JsonObject body = new JsonObject();
|
||||
@@ -521,17 +539,44 @@ public class AIChatManager {
|
||||
|
||||
body.add("messages", messages);
|
||||
|
||||
// max_tokens
|
||||
try {
|
||||
int mt = Integer.parseInt(maxToken);
|
||||
if (mt > 0) {
|
||||
body.addProperty("max_tokens", mt);
|
||||
}
|
||||
} catch (NumberFormatException ignored) {}
|
||||
|
||||
return gson.toJson(body);
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文压缩:当聊天记录总长度超过阈值时,截断旧消息只保留最近的
|
||||
* 返回压缩后的聊天记录字符串
|
||||
*/
|
||||
private String compressChatBuffer(String maxToken) {
|
||||
int threshold;
|
||||
try {
|
||||
threshold = Integer.parseInt(maxToken);
|
||||
} catch (NumberFormatException e) {
|
||||
threshold = 200000;
|
||||
}
|
||||
if (threshold <= 0) threshold = 200000;
|
||||
|
||||
String joined = String.join("\n", recentChatBuffer);
|
||||
if (joined.length() <= threshold) {
|
||||
return joined; // 未超阈值,直接返回
|
||||
}
|
||||
|
||||
// 超阈值:从尾部往前保留消息,直到达到阈值的 80%(留余量给提示词模板)
|
||||
int targetLen = (int) (threshold * 0.8);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int totalLen = 0;
|
||||
// 从最新消息开始往前累加
|
||||
for (int i = recentChatBuffer.size() - 1; i >= 0; i--) {
|
||||
String line = recentChatBuffer.get(i);
|
||||
int lineLen = line.length() + 1; // +1 for newline
|
||||
if (totalLen + lineLen > targetLen) break;
|
||||
if (sb.length() > 0) sb.insert(0, "\n");
|
||||
sb.insert(0, line);
|
||||
totalLen += lineLen;
|
||||
}
|
||||
plugin.getLogger().info("[AI聊天] 上下文压缩: 原始" + joined.length() + "字符 → 压缩至" + sb.length() + "字符 (阈值" + threshold + ")");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String parseChatResponse(String responseBody) {
|
||||
try {
|
||||
JsonObject resp = JsonParser.parseString(responseBody).getAsJsonObject();
|
||||
@@ -627,11 +672,18 @@ public class AIChatManager {
|
||||
if (recentChatBuffer.isEmpty()) return;
|
||||
if (defaultModelFull.isEmpty()) return;
|
||||
|
||||
// ★ 互动指标:在线玩家数量达标才触发自动互动
|
||||
int onlineCount = Bukkit.getOnlinePlayers().size();
|
||||
if (onlineCount < interactionMetric) {
|
||||
plugin.getLogger().info("[AI聊天] 在线玩家 " + onlineCount + " < 互动指标 " + interactionMetric + ",跳过自动互动");
|
||||
return;
|
||||
}
|
||||
|
||||
ModelEndpoint endpoint = modelEndpoints.get(defaultModelFull);
|
||||
if (endpoint == null) return;
|
||||
|
||||
// 构建自动互动提示词
|
||||
String recentChat = String.join("\n", recentChatBuffer);
|
||||
// 构建自动互动提示词 — 用压缩方法(超阈值时截断旧消息)
|
||||
String recentChat = compressChatBuffer(endpoint.maxToken);
|
||||
String prompt = "以下是服务器最近的聊天记录:\n" + recentChat + "\n\n请根据聊天内容,自然地参与对话或回答问题。";
|
||||
|
||||
plugin.getLogger().info("[AI聊天] 触发自动互动");
|
||||
|
||||
@@ -8224,6 +8224,7 @@ public class WebManager {
|
||||
u.put("points", user.get("points"));
|
||||
u.put("gift_stage", user.get("gift_stage"));
|
||||
u.put("total_online_time", user.get("total_online_time"));
|
||||
u.put("credit_score", user.get("credit_score")); // ★ 第58批:信誉积分同步
|
||||
syncData.add(u);
|
||||
}
|
||||
// ★ 双向对账(多退少补):先与 PHP 点一遍,三差集全空就一条都不发
|
||||
|
||||
@@ -33,6 +33,10 @@
|
||||
"自动补齐": {
|
||||
"开关": true,
|
||||
"备注": "如果你的厂商"
|
||||
},
|
||||
"互动指标": {
|
||||
"指标": 1,
|
||||
"说明": "指标为在线玩家数量,当在线玩家大于等于指标才会触发自动互动。手动不受此影响"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user