koishi-plugin-chatluna-think-viewer 2.1.1 → 2.2.0

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.
Files changed (3) hide show
  1. package/README.md +8 -9
  2. package/index.js +26 -30
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # koishi-plugin-chatluna-think-viewer
2
2
 
3
- 通过命令/关键词查看 `chatluna-character` 最近一次回复的 `<think>` 思考内容,并提供“异常格式自动撤回/拦截”守卫。
3
+ 通过命令/关键词查看 `chatluna-character` 最近一次回复的 `<think>` 思考内容,并提供“发送后检测并自动撤回”守卫。
4
4
 
5
5
  ## 功能
6
6
  - 依赖 `chatluna-character` 存储的思考上下文,支持命令与前缀关键词调用。
7
7
  - 支持群聊使用(可配置是否允许私聊)。
8
- - **异常格式自动撤回/拦截**:默认检测 `<think>`、`<status>`、`<output>`、`<analysis>`、`<system>` 等块或调试 JSON、think/json/yaml 代码块;命中后可选择先发后撤回(recall)或直接阻止(block)。
9
- - **关键词模式**(默认开启):`guardKeywordMode=true` 时按不区分大小写的子串匹配拦截;想用正则可把它关掉。
10
- - **严格输出模式**(可选):仅当开启 `guardStrictOutputOnly` 时,要求 `<output><message>…</message></output>` 结构;@ 仅允许数字 user_id,1~5 条 message。默认关闭以避免误撤回。
8
+ - **异常格式自动撤回**:消息先发送,再检测;命中 `<think>`、`<status>`、`<output>`、`<analysis>`、`<system>` 等关键词/正则或你自定义的模式后,按延迟撤回。
9
+ - **关键词模式**(默认):`guardKeywordMode=true` 时用不区分大小写的子串匹配;关闭后改为正则匹配。
10
+ - **严格输出模式**(可选):仅当开启 `guardStrictOutputOnly` 时,要求 `<output><message>…</message></output>` 结构;默认关闭以避免误撤回。
11
11
 
12
12
  ## 安装
13
13
  ```bash
@@ -24,22 +24,21 @@ plugins:
24
24
  - 查看思考
25
25
  allowPrivate: false
26
26
  emptyMessage: 暂时没有可用的思考记录。
27
- # 守卫配置
27
+ # 守卫配置(发送后检测再撤回)
28
28
  guardEnabled: true
29
- guardMode: recall # recall | block
30
- guardDelay: 1 # 撤回延迟(秒),block 模式忽略
29
+ guardMode: recall # 保留字段,实际行为均为先发后撤回
30
+ guardDelay: 1 # 撤回延迟(秒)
31
31
  guardKeywordMode: true # 子串匹配关键词(默认)
32
32
  guardStrictOutputOnly: false # 严格格式校验默认关闭
33
33
  guardForbiddenPatterns:
34
34
  - '<think>'
35
35
  - '<status>'
36
36
  - '<output>'
37
- - '```think'
38
37
  ```
39
38
 
40
39
  ## 使用
41
40
  - 群聊里发送 `think` 或配置的关键词查看最近一次 `<think>` 内容;`think 2` 查看倒数第 2 条。
42
- - 守卫:当 bot 发送的消息命中禁用规则(或在你开启严格模式时不符合格式)会记录日志并阻止/撤回发送。
41
+ - 守卫:当 bot 发送的消息命中禁用规则(或在你开启严格模式时不符合格式)会记录日志并延迟撤回。
43
42
 
44
43
  ## 依赖
45
44
  - koishi >= 4.18.0
package/index.js CHANGED
@@ -220,36 +220,32 @@ function applyGuard(ctx, config) {
220
220
  const strictMode = !!config.guardStrictOutputOnly;
221
221
  const keywordMode = config.guardKeywordMode !== false;
222
222
  const forbidden = compileMatchers(config.guardForbiddenPatterns, keywordMode);
223
- const allowed = strictMode
224
- ? compileMatchers([config.guardStrictPattern || strictOutputPattern], false)
225
- : compileMatchers(config.guardAllowedPatterns, keywordMode);
226
- const original = Bot.prototype.sendMessage;
227
-
228
- Bot.prototype.sendMessage = async function patched(channelId, content, referrer, options = {}) {
229
- if (!shouldGuard(config, options)) {
230
- return original.call(this, channelId, content, referrer, options);
231
- }
232
-
233
- const text = extractText(content);
234
- const reason = detectAbnormal(text, forbidden, allowed);
235
-
236
- if (!reason) {
237
- return original.call(this, channelId, content, referrer, options);
238
- }
239
-
240
- const preview = shorten(text, config.guardContentPreview);
241
- if (config.guardMode === 'block') {
242
- if (config.guardLog) logger.warn(`[block] ${reason} | content: ${preview}`);
243
- return [];
244
- }
245
-
246
- const ids = await original.call(this, channelId, content, referrer, options);
247
- if (config.guardLog) logger.warn(`[recall] ${reason} | content: ${preview}`);
248
- const delay = Math.max(0, config.guardDelay) * 1000;
249
- if (Array.isArray(ids) && ids.length && typeof this.deleteMessage === 'function') {
250
- setTimeout(() => {
251
- for (const id of ids) {
252
- this.deleteMessage(channelId, id).catch((err) => {
223
+ const allowed = strictMode
224
+ ? compileMatchers([config.guardStrictPattern || strictOutputPattern], false)
225
+ : compileMatchers(config.guardAllowedPatterns, keywordMode);
226
+ const original = Bot.prototype.sendMessage;
227
+
228
+ Bot.prototype.sendMessage = async function patched(channelId, content, referrer, options = {}) {
229
+ const ids = await original.call(this, channelId, content, referrer, options);
230
+
231
+ if (!shouldGuard(config, options)) {
232
+ return ids;
233
+ }
234
+
235
+ const text = extractText(content);
236
+ const reason = detectAbnormal(text, forbidden, allowed, strictMode);
237
+
238
+ if (!reason) {
239
+ return ids;
240
+ }
241
+
242
+ const preview = shorten(text, config.guardContentPreview);
243
+ if (config.guardLog) logger.warn(`[recall] ${reason} | content: ${preview}`);
244
+ const delay = Math.max(0, config.guardDelay) * 1000;
245
+ if (Array.isArray(ids) && ids.length && typeof this.deleteMessage === 'function') {
246
+ setTimeout(() => {
247
+ for (const id of ids) {
248
+ this.deleteMessage(channelId, id).catch((err) => {
253
249
  logger.warn(`[recall-failed] id=${id} reason=${err?.message || err}`);
254
250
  });
255
251
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "main": "index.js",
5
5
  "description": "View chatluna <think> blocks and auto recall abnormal formatted replies.",
6
6
  "license": "MIT",