koishi-plugin-chatluna-think-viewer 1.0.14 → 1.0.15

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 +19 -4
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -93,6 +93,21 @@ function getNthAiMessage(messages, n = 1) {
93
93
  return null;
94
94
  }
95
95
 
96
+ function getNthThink(messages, n = 1) {
97
+ if (!Array.isArray(messages) || n < 1) return null;
98
+ let count = 0;
99
+ for (let i = messages.length - 1; i >= 0; i--) {
100
+ const msg = messages[i];
101
+ const type = typeof msg?._getType === 'function' ? msg._getType() : msg?.type || msg?.role;
102
+ if (type !== 'ai' && type !== 'assistant') continue;
103
+ const think = extractThink(extractText(msg.content));
104
+ if (!think) continue;
105
+ count += 1;
106
+ if (count === n) return think;
107
+ }
108
+ return null;
109
+ }
110
+
96
111
  function apply(ctx, config) {
97
112
  const cmd = ctx
98
113
  .command(`${config.command} [index:string]`, '获取上一条回复中的 <think> 内容(可指定倒数第 N 条)')
@@ -116,10 +131,10 @@ function apply(ctx, config) {
116
131
 
117
132
  const targetIndex = parseIndex(rawIndex ?? args?.[0]);
118
133
 
119
- const targetMessage = getNthAiMessage(messages, targetIndex);
120
- if (!targetMessage) return config.emptyMessage;
121
-
122
- const think = formatThink(extractThink(extractText(targetMessage.content)));
134
+ const rawThink = getNthThink(messages, targetIndex);
135
+ const think = rawThink
136
+ ? formatThink(rawThink)
137
+ : formatThink(extractThink(extractText(getNthAiMessage(messages, targetIndex)?.content)));
123
138
  if (!think) return config.emptyMessage;
124
139
 
125
140
  if (config.renderImage && ctx.chatluna?.renderer) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "1.0.14",
3
+ "version": "1.0.15",
4
4
  "main": "index.js",
5
5
  "description": "Expose a command/shortcut to read the latest <think> block from chatluna-character.",
6
6
  "license": "MIT",