koishi-plugin-chatluna-think-viewer 1.0.9 → 1.0.10

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 (2) hide show
  1. package/index.js +21 -30
  2. package/package.json +5 -5
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const { Schema } = require('koishi');
1
+ const { Schema } = require('koishi');
2
2
 
3
3
  const name = 'chatluna-think-viewer';
4
4
 
@@ -53,7 +53,12 @@ function formatThink(text) {
53
53
  const filtered = lines.filter((l, idx, arr) => !(l === '' && arr[idx - 1] === ''));
54
54
  const nonEmpty = filtered.filter((l) => l.trim().length > 0);
55
55
  const minIndent = nonEmpty.length
56
- ? Math.min(...nonEmpty.map((l) => l.match(/^(\s*)/)?.[1]?.length ?? 0))
56
+ ? Math.min(
57
+ ...nonEmpty.map((l) => {
58
+ const m = l.match(/^(\s*)/);
59
+ return m ? m[1].length : 0;
60
+ }),
61
+ )
57
62
  : 0;
58
63
  return filtered.map((l) => l.slice(minIndent)).join('\n');
59
64
  }
@@ -75,34 +80,17 @@ function getNthAiMessage(messages, n = 1) {
75
80
  const msg = messages[i];
76
81
  const type = typeof msg?._getType === 'function' ? msg._getType() : msg?.type || msg?.role;
77
82
  if (type === 'ai' || type === 'assistant') {
78
- count++;
83
+ count += 1;
79
84
  if (count === n) return msg;
80
85
  }
81
86
  }
82
87
  return null;
83
88
  }
84
89
 
85
- function getNthThink(messages, n = 1) {
86
- let count = 0;
87
- for (let i = messages.length - 1; i >= 0; i--) {
88
- const msg = messages[i];
89
- const type = typeof msg?._getType === 'function' ? msg._getType() : msg?.type || msg?.role;
90
- if (type !== 'ai' && type !== 'assistant') continue;
91
- const text = extractText(msg.content);
92
- const thinkRaw = extractThink(text);
93
- if (!thinkRaw) continue;
94
- count += 1;
95
- if (count === n) {
96
- return { msg, think: formatThink(thinkRaw) };
97
- }
98
- }
99
- return null;
100
- }
101
-
102
90
  function apply(ctx, config) {
103
91
  const cmd = ctx
104
92
  .command(`${config.command} [index:string]`, '获取上一条回复中的 <think> 内容(可指定倒数第 N 条)')
105
- .usage('不带参数默认读取最近一条;例如 think 2 读取倒数第二条 AI 回复的思考。');
93
+ .usage('不带参数默认读取最近一条;例如 think 2 读取倒数第二条 AI 回复的思考');
106
94
 
107
95
  for (const keyword of config.keywords || []) {
108
96
  cmd.shortcut(keyword, { prefix: false });
@@ -122,18 +110,21 @@ function apply(ctx, config) {
122
110
 
123
111
  const targetIndex = parseIndex(rawIndex ?? args?.[0]);
124
112
 
125
- const result = getNthThink(messages, targetIndex);
126
- if (!result) return `找不到倒数第 ${targetIndex} 条含 <think> 的 AI 回复记录。`;
127
- const think = result.think;
113
+ const targetMessage = getNthAiMessage(messages, targetIndex);
114
+ if (!targetMessage) return config.emptyMessage;
115
+
116
+ const think = formatThink(extractThink(extractText(targetMessage.content)));
117
+ if (!think) return config.emptyMessage;
128
118
 
129
119
  if (config.renderImage && ctx.chatluna?.renderer) {
130
120
  try {
131
121
  const title = `### 上一条思考(倒数第 ${targetIndex} 条)`;
132
- const rendered = await ctx.chatluna.renderer.render({
133
- content: [
134
- { type: 'text', text: `${title}\n\n\`\`\`\n${think}\n\`\`\`` },
135
- ],
136
- }, { type: 'image', session });
122
+ const rendered = await ctx.chatluna.renderer.render(
123
+ {
124
+ content: [{ type: 'text', text: `${title}\n\n\`\`\`\n${think}\n\`\`\`` }],
125
+ },
126
+ { type: 'image', session },
127
+ );
137
128
  if (rendered?.length) return rendered.map((r) => r.element);
138
129
  } catch (err) {
139
130
  ctx.logger?.warn?.('[think-viewer] image render failed, fallback text', err);
@@ -149,4 +140,4 @@ module.exports = {
149
140
  apply,
150
141
  Config,
151
142
  inject,
152
- };
143
+ };
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
- {
1
+ {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "main": "index.js",
5
- "description": "通过命令/关键词查阅 chatluna-character 最近一次回复中的 <think> 思考内容。",
5
+ "description": "通过命令/关键词查看 chatluna-character 最近一次回复中的 <think> 思考内容。",
6
6
  "license": "MIT",
7
7
  "keywords": [
8
8
  "koishi",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "koishi": {
26
26
  "description": {
27
- "zh": "通过命令/关键词查阅 chatluna-character 最近一次回复的 <think> 思考内容。",
27
+ "zh": "通过命令/关键词查看 chatluna-character 最近一次回复的 <think> 思考内容。",
28
28
  "en": "Expose a command/shortcut to read the last <think> block from chatluna-character."
29
29
  },
30
30
  "service": {
@@ -37,4 +37,4 @@
37
37
  "index.js",
38
38
  "README.md"
39
39
  ]
40
- }
40
+ }