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

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 +32 -12
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -59,6 +59,15 @@ function formatThink(text) {
59
59
  }
60
60
  }
61
61
 
62
+ function parseIndex(rawIndex) {
63
+ if (!rawIndex) return 1;
64
+ if (typeof rawIndex === 'number' && Number.isFinite(rawIndex) && rawIndex > 0) return Math.floor(rawIndex);
65
+ const match = String(rawIndex).match(/\d+/);
66
+ if (!match) return 1;
67
+ const num = parseInt(match[0], 10);
68
+ return Number.isFinite(num) && num > 0 ? num : 1;
69
+ }
70
+
62
71
  function getNthAiMessage(messages, n = 1) {
63
72
  if (!Array.isArray(messages) || n < 1) return null;
64
73
  let count = 0;
@@ -73,16 +82,33 @@ function getNthAiMessage(messages, n = 1) {
73
82
  return null;
74
83
  }
75
84
 
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
+
76
102
  function apply(ctx, config) {
77
103
  const cmd = ctx
78
- .command(`${config.command} [index:number]`, '获取上一条回复中的 <think> 内容(可指定倒数第 N 条)')
104
+ .command(`${config.command} [index:string]`, '获取上一条回复中的 <think> 内容(可指定倒数第 N 条)')
79
105
  .usage('不带参数默认读取最近一条;例如 think 2 读取倒数第二条 AI 回复的思考。');
80
106
 
81
107
  for (const keyword of config.keywords || []) {
82
108
  cmd.shortcut(keyword, { prefix: false });
83
109
  }
84
110
 
85
- cmd.action(async ({ session }, rawIndex) => {
111
+ cmd.action(async ({ session, args }, rawIndex) => {
86
112
  if (!config.allowPrivate && !session.guildId) {
87
113
  return '仅支持在群聊中查询。';
88
114
  }
@@ -94,17 +120,11 @@ function apply(ctx, config) {
94
120
  const messages = temp?.completionMessages || [];
95
121
  if (!messages.length) return config.emptyMessage;
96
122
 
97
- let targetIndex = parseInt(rawIndex, 10);
98
- if (!Number.isFinite(targetIndex) || targetIndex < 1) targetIndex = 1;
99
-
100
- const targetAi = getNthAiMessage(messages, targetIndex);
101
- if (!targetAi) return `找不到倒数第 ${targetIndex} 条 AI 回复的记录。`;
102
-
103
- const text = extractText(targetAi.content);
104
- if (!text) return '未找到可解析的回复内容。';
123
+ const targetIndex = parseIndex(rawIndex ?? args?.[0]);
105
124
 
106
- const think = formatThink(extractThink(text));
107
- if (!think) return config.emptyMessage || '上一次回复中没有 <think> 字段。';
125
+ const result = getNthThink(messages, targetIndex);
126
+ if (!result) return `找不到倒数第 ${targetIndex} 条含 <think> 的 AI 回复记录。`;
127
+ const think = result.think;
108
128
 
109
129
  if (config.renderImage && ctx.chatluna?.renderer) {
110
130
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "main": "index.js",
5
5
  "description": "通过命令/关键词查阅 chatluna-character 最近一次回复中的 <think> 思考内容。",
6
6
  "license": "MIT",