koishi-plugin-chatluna-think-viewer 1.0.2 → 1.0.3

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 +17 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -57,23 +57,30 @@ function formatThink(text) {
57
57
  }
58
58
  }
59
59
 
60
- function getLastAiMessage(messages) {
60
+ function getNthAiMessage(messages, n = 1) {
61
+ if (!Array.isArray(messages) || n < 1) return null;
62
+ let count = 0;
61
63
  for (let i = messages.length - 1; i >= 0; i--) {
62
64
  const msg = messages[i];
63
65
  const type = typeof msg?._getType === 'function' ? msg._getType() : msg?.type || msg?.role;
64
- if (type === 'ai' || type === 'assistant') return msg;
66
+ if (type === 'ai' || type === 'assistant') {
67
+ count++;
68
+ if (count === n) return msg;
69
+ }
65
70
  }
66
71
  return null;
67
72
  }
68
73
 
69
74
  function apply(ctx, config) {
70
- const cmd = ctx.command(config.command, '获取上一条回复中的 <think> 内容');
75
+ const cmd = ctx
76
+ .command(`${config.command} [index:number]`, '获取上一条回复中的 <think> 内容(可指定倒数第 N 条)')
77
+ .usage('不带参数默认读取最近一条;例如 think 2 读取倒数第二条 AI 回复的思考。');
71
78
 
72
79
  for (const keyword of config.keywords || []) {
73
80
  cmd.shortcut(keyword, { prefix: false });
74
81
  }
75
82
 
76
- cmd.action(async ({ session }) => {
83
+ cmd.action(async ({ session }, rawIndex) => {
77
84
  if (!config.allowPrivate && !session.guildId) {
78
85
  return '仅支持在群聊中查询。';
79
86
  }
@@ -85,10 +92,13 @@ function apply(ctx, config) {
85
92
  const messages = temp?.completionMessages || [];
86
93
  if (!messages.length) return config.emptyMessage;
87
94
 
88
- const lastAi = getLastAiMessage(messages);
89
- if (!lastAi) return config.emptyMessage;
95
+ let targetIndex = parseInt(rawIndex, 10);
96
+ if (!Number.isFinite(targetIndex) || targetIndex < 1) targetIndex = 1;
97
+
98
+ const targetAi = getNthAiMessage(messages, targetIndex);
99
+ if (!targetAi) return `找不到倒数第 ${targetIndex} 条 AI 回复的记录。`;
90
100
 
91
- const text = extractText(lastAi.content);
101
+ const text = extractText(targetAi.content);
92
102
  if (!text) return '未找到可解析的回复内容。';
93
103
 
94
104
  const think = formatThink(extractThink(text));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "main": "index.js",
5
5
  "description": "通过命令/关键词查看 chatluna-character 最近一次回复中的 <think> 思考内容。",
6
6
  "license": "MIT",