@nestim/koishi-plugin-qq-group-manager 1.0.3 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -51,6 +51,7 @@ export interface Config {
51
51
  joinRequestReviewTtlMinutes: number;
52
52
  pendingJoinFileName: string;
53
53
  bannedWords: string[];
54
+ strongKeywords: string[];
54
55
  blockCardMessage: boolean;
55
56
  blockForwardMessage: boolean;
56
57
  autoDeleteViolation: boolean;
@@ -82,6 +83,7 @@ export interface GroupRule {
82
83
  guildId: string;
83
84
  enableAiReply?: boolean;
84
85
  bannedWords: string[];
86
+ strongKeywords?: string[];
85
87
  blockCardMessage: boolean;
86
88
  blockForwardMessage: boolean;
87
89
  autoDeleteViolation: boolean;
package/lib/index.js CHANGED
@@ -31,6 +31,7 @@ exports.Config = koishi_1.Schema.intersect([
31
31
  koishi_1.Schema.object({
32
32
  bannedWords: koishi_1.Schema.array(String).role('table').default([]).description('违禁词列表。支持顺序模糊匹配(如“领红包”可命中“领xxx元红包”)。可用“关键词|分值”自定义权重,分值越高越容易单独触发。'),
33
33
  bannedWordScoreThreshold: koishi_1.Schema.natural().min(1).max(100).default(70).description('违禁词评分触发阈值:整条消息累计得分达到该值才触发管控(默认 70)。'),
34
+ strongKeywords: koishi_1.Schema.array(String).role('table').default([]).description('强关键词列表:命中即可触发管控,且【无视白名单豁免】。用于「有没有美女」这类与招新无关的骚扰/找对象话术——即使消息里带了「宣传部」等白名单词也照样命中,堵住「附一个白名单词即可绕过管控」的漏洞。支持“关键词|分值”语法,与违禁词共用同一评分阈值。'),
34
35
  blockCardMessage: koishi_1.Schema.boolean().default(true).description('是否禁止卡片消息(json/xml)。'),
35
36
  blockForwardMessage: koishi_1.Schema.boolean().default(true).description('是否禁止合并转发消息(forward)。'),
36
37
  autoDeleteViolation: koishi_1.Schema.boolean().default(true).description('违规消息是否自动撤回。'),
@@ -40,6 +41,7 @@ exports.Config = koishi_1.Schema.intersect([
40
41
  enableAiReply: koishi_1.Schema.boolean().description('该群是否启用 AI 回复(留空则跟随全局设置)。'),
41
42
  bannedWords: koishi_1.Schema.array(String).role('table').default([]).description('该群专属违禁词列表(同样支持“关键词|分值”与顺序模糊匹配)。'),
42
43
  whitelistKeywords: koishi_1.Schema.array(String).role('table').default([]).description('该群违禁词白名单:消息只要命中其中任意一个关键词,就整条跳过违禁词评分(用于校学生会招新等允许的宣传场景)。'),
44
+ strongKeywords: koishi_1.Schema.array(String).role('table').description('该群强关键词(留空则跟随全局设置)。命中后无视白名单豁免,用于堵住「附一个白名单词即可绕过管控」的漏洞。'),
43
45
  whitelistOnlyOwners: koishi_1.Schema.boolean().default(false).description('白名单是否仅对群主/管理员生效。关闭时所有群成员命中白名单均可豁免。'),
44
46
  blockCardMessage: koishi_1.Schema.boolean().default(true).description('该群是否禁止卡片消息(json/xml)。'),
45
47
  blockForwardMessage: koishi_1.Schema.boolean().default(true).description('该群是否禁止合并转发消息(forward)。'),
@@ -123,12 +125,12 @@ exports.Config = koishi_1.Schema.intersect([
123
125
  prompt: koishi_1.Schema.string().role('textarea').default('').description('该人格系统提示词(留空则使用默认提示词)。'),
124
126
  })).role('table').default([]).description('可保存多个人格配置,按 aiActivePersona 切换。'),
125
127
  aiReplyMode: koishi_1.Schema.union([
126
- koishi_1.Schema.const('threshold').description('达到消息阈值后回复'),
127
- koishi_1.Schema.const('random').description('按概率随机回复'),
128
- koishi_1.Schema.const('hybrid').description('阈值与随机同时生效'),
129
- ]).role('radio').default('hybrid').description('AI 触发策略。'),
130
- aiMessageThreshold: koishi_1.Schema.natural().default(16).description('阈值触发所需累计消息数。'),
131
- aiRandomReplyProbability: koishi_1.Schema.percent().default(0.08).description('随机触发概率(每条消息判定一次)。'),
128
+ koishi_1.Schema.const('threshold').description('只看消息数:累计达到阈值才触发(不看概率)'),
129
+ koishi_1.Schema.const('random').description('只看概率:每条消息掷一次骰(不看消息数)'),
130
+ koishi_1.Schema.const('hybrid').description('两者都要满足:先累计到阈值,再按概率掷中'),
131
+ ]).role('radio').default('hybrid').description('AI 触发策略。三道闸门依序为:①消息阈值 ②随机概率 ③兴趣评分,任一不过都不回复;@点名与点名后跟随可越过全部三道。'),
132
+ aiMessageThreshold: koishi_1.Schema.natural().default(16).description('阈值触发所需累计消息数。threshold / hybrid 模式下未达此数一律不触发;计数器在成功回复后清零,需重新累计。'),
133
+ aiRandomReplyProbability: koishi_1.Schema.percent().default(0.08).description('随机触发概率(每条消息掷一次骰)。random / hybrid 模式下未掷中直接跳过,不会调用 AI 接口。填 0 等同于关闭随机触发。'),
132
134
  aiMinReplyIntervalSeconds: koishi_1.Schema.natural().default(45).description('同一群两次 AI 回复的最短间隔(秒)。'),
133
135
  aiContextWindow: koishi_1.Schema.natural().default(24).description('参与推理的最近消息条数。'),
134
136
  aiTemperature: koishi_1.Schema.number().min(0).max(2).step(0.1).default(0.8).description('生成温度(0~2)。'),
@@ -144,7 +146,7 @@ exports.Config = koishi_1.Schema.intersect([
144
146
  aiOwnerUserId: koishi_1.Schema.string().default('').description('主人平台用户 ID(如 QQ 号,用于记忆主人身份)。'),
145
147
  aiHomePlatform: koishi_1.Schema.string().default('onebot').description('兼容字段:主页平台标识(建议改用 aiOwnerPlatform)。'),
146
148
  aiHomeUserId: koishi_1.Schema.string().default('').description('兼容字段:主页平台用户 ID(建议改用 aiOwnerUserId)。'),
147
- aiInterestMinScore: koishi_1.Schema.number().min(0).max(100).step(1).default(82).description('AI 兴趣触发最低分(非点名场景,分值越高越不容易回复)。'),
149
+ aiInterestMinScore: koishi_1.Schema.number().min(0).max(100).step(1).default(82).description('AI 兴趣触发最低分(最后一道闸门):非点名场景下,无论由阈值还是概率触发,都要再过一次兴趣评分,达到此分才真正回复。分值越高越不容易回复。'),
148
150
  aiInterestContextWindow: koishi_1.Schema.number().min(4).max(16).step(1).default(8).description('非阈值兴趣判定使用的上下文窗口条数。'),
149
151
  }).description('AI 回复'),
150
152
  koishi_1.Schema.object({
package/lib/service.js CHANGED
@@ -750,12 +750,25 @@ class QQGroupManagerService extends koishi_1.Service {
750
750
  const mode = this.config.aiReplyMode;
751
751
  const messageCount = this.aiCounters.get(key) ?? 0;
752
752
  const threshold = Math.max(1, this.config.aiMessageThreshold || 1);
753
- const thresholdHit = (mode === 'threshold' || mode === 'hybrid') && messageCount >= threshold;
754
- if (thresholdHit)
755
- return { ok: true, reason: `threshold(${messageCount}/${threshold})` };
756
- if (mode === 'threshold')
757
- return { ok: false, reason: 'not-triggered' };
758
- return { ok: true, reason: 'interest-check' };
753
+ // 闸门一(消息阈值):threshold / hybrid 模式下,累计消息数未达阈值一律不触发。
754
+ // 计数器只在成功回复后清零(见 maybeReplyWithAi 末尾),因此回复后需重新累计。
755
+ const thresholdRequired = mode === 'threshold' || mode === 'hybrid';
756
+ if (thresholdRequired && messageCount < threshold)
757
+ return { ok: false, reason: `below-threshold(${messageCount}/${threshold})` };
758
+ // 闸门二(随机概率):random / hybrid 模式下对每条消息掷一次骰,未掷中直接返回,
759
+ // 不会触发后续的兴趣判定与生成请求。
760
+ const randomRequired = mode === 'random' || mode === 'hybrid';
761
+ if (randomRequired) {
762
+ const probability = Math.max(0, Math.min(1, Number(this.config.aiRandomReplyProbability ?? 0.08)));
763
+ if (probability <= 0)
764
+ return { ok: false, reason: 'random-disabled' };
765
+ if (Math.random() >= probability)
766
+ return { ok: false, reason: `random-miss(${probability})` };
767
+ return thresholdRequired
768
+ ? { ok: true, reason: `threshold(${messageCount}/${threshold})+random(${probability})` }
769
+ : { ok: true, reason: `random(${probability})` };
770
+ }
771
+ return { ok: true, reason: `threshold(${messageCount}/${threshold})` };
759
772
  }
760
773
  parseInterestDecision(raw) {
761
774
  const text = raw.trim();
@@ -2249,13 +2262,22 @@ class QQGroupManagerService extends koishi_1.Service {
2249
2262
  if (!trigger.ok)
2250
2263
  return;
2251
2264
  const topicHint = (this.aiTopicMemory.get(groupKey)?.text || '').replace(/\s+/g, ' ').trim().slice(0, 120);
2252
- if (trigger.reason === 'interest-check') {
2265
+ const thresholdMode = typeof trigger.reason === 'string' && trigger.reason.startsWith('threshold(');
2266
+ // 闸门三(兴趣评分):所有非点名触发的回复都先过兴趣判定,达标才继续生成。
2267
+ // 点名(direct-mention)与点名后跟随(followup)不受此限。
2268
+ if (!directMention && !followupForce && !relatedContextFollowup) {
2253
2269
  const interest = await this.shouldReplyByInterest({ groupKey, text: effectiveText, hasSticker, hasImage: focusImages.length > 0 });
2254
- this.logCommandResult('ai-interest', { ok: interest.ok, message: `score=${interest.score} threshold=${interest.threshold} reason=${interest.reason}` }, session, { ctxSize: interest.ctxSize, topicHint: interest.topicHint || topicHint || undefined });
2255
- if (!interest.ok)
2270
+ this.logCommandResult(thresholdMode ? 'ai-threshold-interest' : 'ai-interest', { ok: interest.ok, message: `score=${interest.score} threshold=${interest.threshold} reason=${interest.reason} trigger=${trigger.reason}` }, session, { ctxSize: interest.ctxSize, topicHint: interest.topicHint || topicHint || undefined });
2271
+ if (!interest.ok) {
2272
+ this.logCommandResult(thresholdMode ? 'ai-threshold-skip-by-interest' : 'ai-skip-by-interest', { ok: true, message: 'triggered but skipped by low interest' }, session, {
2273
+ score: interest.score,
2274
+ threshold: interest.threshold,
2275
+ reason: interest.reason,
2276
+ trigger: trigger.reason,
2277
+ });
2256
2278
  return;
2279
+ }
2257
2280
  }
2258
- const thresholdMode = typeof trigger.reason === 'string' && trigger.reason.startsWith('threshold(');
2259
2281
  const topicMemory = this.aiTopicMemory.get(groupKey);
2260
2282
  const topicTokens = topicMemory?.tokens ?? [];
2261
2283
  let selectedRecord;
@@ -2272,18 +2294,6 @@ class QQGroupManagerService extends koishi_1.Service {
2272
2294
  }
2273
2295
  }
2274
2296
  }
2275
- if (thresholdMode) {
2276
- const interest = await this.shouldReplyByInterest({ groupKey, text: effectiveText, hasSticker, hasImage: focusImages.length > 0 });
2277
- this.logCommandResult('ai-threshold-interest', { ok: interest.ok, message: `score=${interest.score} threshold=${interest.threshold} reason=${interest.reason}` }, session, { ctxSize: interest.ctxSize, topicHint: interest.topicHint || topicHint || undefined });
2278
- if (!interest.ok) {
2279
- this.logCommandResult('ai-threshold-skip-by-interest', { ok: true, message: 'threshold hit but skipped by low interest' }, session, {
2280
- score: interest.score,
2281
- threshold: interest.threshold,
2282
- reason: interest.reason,
2283
- });
2284
- return;
2285
- }
2286
- }
2287
2297
  this.aiInFlight.add(groupKey);
2288
2298
  const startedAt = Date.now();
2289
2299
  // 已确认要回复,此时才把图片下载/转换为 base64(模型需要可直接读取的图片数据)
@@ -2876,6 +2886,7 @@ class QQGroupManagerService extends koishi_1.Service {
2876
2886
  if (!rule) {
2877
2887
  return {
2878
2888
  bannedWords: this.config.bannedWords,
2889
+ strongKeywords: this.config.strongKeywords ?? [],
2879
2890
  whitelistKeywords: [],
2880
2891
  whitelistOnlyOwners: false,
2881
2892
  blockCardMessage: this.config.blockCardMessage,
@@ -2892,6 +2903,9 @@ class QQGroupManagerService extends koishi_1.Service {
2892
2903
  }
2893
2904
  return {
2894
2905
  bannedWords: rule.bannedWords,
2906
+ strongKeywords: (Array.isArray(rule.strongKeywords) && rule.strongKeywords.length)
2907
+ ? rule.strongKeywords
2908
+ : (this.config.strongKeywords ?? []),
2895
2909
  whitelistKeywords: rule.whitelistKeywords || [],
2896
2910
  whitelistOnlyOwners: rule.whitelistOnlyOwners === true,
2897
2911
  blockCardMessage: rule.blockCardMessage,
@@ -3106,6 +3120,23 @@ class QQGroupManagerService extends koishi_1.Service {
3106
3120
  const text = this.extractTextForFilter(session);
3107
3121
  if (!text)
3108
3122
  return null;
3123
+ const threshold = Math.max(1, Math.min(100, Number(this.config.bannedWordScoreThreshold ?? 70)));
3124
+ // 强关键词:在【白名单豁免之前】判定。命中即可触发管控,白名单不豁免。
3125
+ // 用于「有没有美女」这类与招新无关的骚扰/找对象话术——即使消息里带了
3126
+ // 「宣传部」等白名单词也照样命中,堵住「附一个白名单词即可绕过管控」的漏洞。
3127
+ const strong = this.evaluateBannedWords(text, { bannedWords: policy?.strongKeywords ?? [] });
3128
+ if (strong.total >= threshold) {
3129
+ const strongTop = strong.evaluations.slice(0, 4)
3130
+ .map((item) => `${item.keyword}(+${item.gained}${item.exact ? '' : `/${item.confidence}`})`)
3131
+ .join(' ');
3132
+ this._logger.info(`[strong] 强关键词评分 ${strong.total}/${threshold}(无视白名单):${strongTop} | ${this.sessionTag(session)}`);
3133
+ return {
3134
+ type: 'banned-word',
3135
+ detail: `强关键词评分 ${strong.total}/${threshold}(无视白名单):${strongTop}`,
3136
+ assessment: strong,
3137
+ strong: true,
3138
+ };
3139
+ }
3109
3140
  // 白名单豁免:命中白名单关键词则跳过违禁词评分(招新等允许的宣传场景)。
3110
3141
  // 注意:卡片/合并转发的拦截在上面已判定,白名单只豁免「违禁词」,不放过卡片类消息。
3111
3142
  const bypass = await this.checkWhitelistBypass(session, policy, text);
@@ -3114,7 +3145,6 @@ class QQGroupManagerService extends koishi_1.Service {
3114
3145
  return null;
3115
3146
  }
3116
3147
  const assessment = this.evaluateBannedWords(text, policy);
3117
- const threshold = Math.max(1, Math.min(100, Number(this.config.bannedWordScoreThreshold ?? 70)));
3118
3148
  if (assessment.total >= threshold) {
3119
3149
  const top = assessment.evaluations.slice(0, 4)
3120
3150
  .map((item) => `${item.keyword}(+${item.gained}${item.exact ? '' : `/${item.confidence}`})`)
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nestim/koishi-plugin-qq-group-manager",
3
3
  "description": "Koishi QQ群管理插件(OneBot/LLOneBot):群管命令、权限校验、图片菜单、状态卡片、复读、AI群聊回复、记忆库、识图与自动管控(禁言/踢人)。",
4
- "version": "1.0.3",
4
+ "version": "1.1.1",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
package/readme.md CHANGED
@@ -23,12 +23,13 @@ Koishi 控制台「探索器 Explorer」里查看和手改。
23
23
  | **群管动作** | 禁言 / 解除禁言 / 踢人(可拒绝其再次加群);时长支持 `10`、`10m`、`1h`、`1d`、`1w`,上限 30 天 |
24
24
  | **权限校验** | 账号白名单 + 群主 / 群管理员;可要求 bot 为群主才放行管理动作 |
25
25
  | **消息管控** | 违禁词**整条消息评分制**(顺序模糊匹配 + 置信度衰减 + 可配权重),卡片消息、合并转发拦截,可自动撤回并群内提示 |
26
- | **按群覆盖** | `groupRules` 可为每个群单独设定违禁词、白名单关键词与各类开关,未配置则跟随全局 |
26
+ | **强关键词** | `strongKeywords`:命中即可触发管控,且**无视白名单豁免**,堵住「附一个白名单词即可绕过管控」的口子;判定顺序为 强关键词 → 白名单 → 普通违禁词 |
27
+ | **按群覆盖** | `groupRules` 可为每个群单独设定违禁词、强关键词、白名单关键词与各类开关,未配置则跟随全局 |
27
28
  | **自动处罚** | 统计窗口内累计违规达到阈值后自动禁言 / 自动踢人,阈值、时长、窗口均可配 |
28
29
  | **黑名单** | 按群独立存储(`banMember_<群号>.md`);入群自动比对并踢出,可选踢人时自动记录 |
29
30
  | **入群审核** | 新申请推送到群内,管理员用 `approve` / `reject` 放行或拒绝;待审单落盘,**重启不丢** |
30
31
  | **记忆库** | `[memory]` 保存 / 列出 / 查询 / 删除,落盘 `Memory.md`;开启后 AI 回复会检索并引用相关记忆 |
31
- | **AI 群聊回复** | 阈值 / 随机 / 混合三种触发,多人格、点名直触发、点名后话题跟随、兴趣分判定、上下文窗口、温度与输出上限 |
32
+ | **AI 群聊回复** | 三道闸门逐层收敛:①消息阈值 ②随机概率 ③兴趣评分,任一不过都不回复、也不调用模型;`@点名` 可越过全部闸门。多人格、点名后话题跟随、上下文窗口、温度与输出上限 |
32
33
  | **AI 识图** | 图片下载转 data URL 后送入模型,兼容 CQ 码 / HTML `<img>` / 元素三种形态与表情包 |
33
34
  | **复读** | 连续相同消息达阈值复读,支持图片引用回查;复读事件会进入 AI 上下文 |
34
35
  | **娱乐** | 自我约束(口球)与违规禁言惩罚,可开关与调节参数 |
@@ -87,8 +88,9 @@ npm i @nestim/koishi-plugin-qq-group-manager
87
88
  | `command` | `[bot]` | 菜单入口前缀 |
88
89
  | `allowedUserIds` | `[]` | 可直接使用管理指令的账号白名单 |
89
90
  | `bannedWords` | `[]` | 违禁词;支持 `关键词\|分值` 自定义权重 |
90
- | `bannedWordScoreThreshold` | `70` | 违禁词评分触发阈值 |
91
- | `groupRules` | `[]` | 按群覆盖策略(违禁词、白名单关键词、AI 开关、自动处罚阈值等) |
91
+ | `bannedWordScoreThreshold` | `70` | 违禁词评分触发阈值(强关键词共用) |
92
+ | `strongKeywords` | `[]` | 强关键词;命中即触发且**无视白名单豁免**,支持 `关键词\|分值` |
93
+ | `groupRules` | `[]` | 按群覆盖策略(违禁词、强关键词、白名单关键词、AI 开关、自动处罚阈值等) |
92
94
  | `memoryFileName` | `Memory.md` | 记忆库文件名(存放在 Koishi 数据目录) |
93
95
  | `blacklistFileName` | `banMember.md` | 黑名单文件名模板,实际为 `banMember_<群号>.md` |
94
96
  | `pendingJoinFileName` | `JoinRequests.md` | 待审入群申请落盘文件名 |
@@ -100,8 +102,13 @@ npm i @nestim/koishi-plugin-qq-group-manager
100
102
  ## AI 回复与识图
101
103
 
102
104
  - **接口**:`openai-compatible`(OpenAI 兼容 Chat Completions)与 `gemini`(原生 `generateContent`)两类。
103
- - **触发**:`threshold`(累计消息达阈值)、`random`(按概率)、`hybrid`(两者同时生效);
104
- 消息中命中智能体自称或直接 `@bot` 时忽略阈值直接触发。
105
+ - **触发**:三道闸门依序判定,任一不过都不回复,也不会产生模型调用:
106
+ 1. `aiMessageThreshold` 消息阈值(`threshold` / `hybrid`)——累计未达阈值不触发,计数器在成功回复后清零;
107
+ 2. `aiRandomReplyProbability` 随机概率(`random` / `hybrid`)——每条消息掷一次骰,未掷中直接跳过;
108
+ 3. `aiInterestMinScore` 兴趣评分——调模型给消息打分,达标才回复。
109
+
110
+ `hybrid` 表示**两者都需满足**(先够消息数,再掷中概率)。消息中命中智能体自称或直接 `@bot` 时,
111
+ 越过上述全部闸门直接触发。
105
112
  - **多人格**:`aiPersonas` 保存多套「自称 + 系统提示词」,用 `aiActivePersona` 切换。
106
113
  - **识图**:开启 `aiEnableImageRecognition` 后,图片会下载并转为 data URL 再送入模型,
107
114
  不依赖模型侧访问 QQ 图链;兼容 CQ 码、HTML `<img>`、消息元素与表情包。
@@ -125,10 +132,6 @@ npm i @nestim/koishi-plugin-qq-group-manager
125
132
 
126
133
  鉴权细节与指令执行结果只写日志,不回显到群聊。
127
134
 
128
- ## 更新日志
129
-
130
- 见 [CHANGELOG.md](./CHANGELOG.md)。
131
-
132
135
  ## License
133
136
 
134
137
  MIT