koishi-plugin-chatluna-think-viewer 1.0.13 → 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.
- package/index.js +27 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -37,8 +37,14 @@ function extractText(content) {
|
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
function extractThink(text) {
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
// 有些模型/中间件会在同一条消息里多次输出 <think>,取最后一段避免误用旧片段
|
|
41
|
+
let last = '';
|
|
42
|
+
const regex = /<think>([\s\S]*?)<\/think>/gi;
|
|
43
|
+
let m;
|
|
44
|
+
while ((m = regex.exec(text)) !== null) {
|
|
45
|
+
last = m[1];
|
|
46
|
+
}
|
|
47
|
+
return last.trim();
|
|
42
48
|
}
|
|
43
49
|
|
|
44
50
|
function formatThink(text) {
|
|
@@ -87,6 +93,21 @@ function getNthAiMessage(messages, n = 1) {
|
|
|
87
93
|
return null;
|
|
88
94
|
}
|
|
89
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
|
+
|
|
90
111
|
function apply(ctx, config) {
|
|
91
112
|
const cmd = ctx
|
|
92
113
|
.command(`${config.command} [index:string]`, '获取上一条回复中的 <think> 内容(可指定倒数第 N 条)')
|
|
@@ -110,10 +131,10 @@ function apply(ctx, config) {
|
|
|
110
131
|
|
|
111
132
|
const targetIndex = parseIndex(rawIndex ?? args?.[0]);
|
|
112
133
|
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
134
|
+
const rawThink = getNthThink(messages, targetIndex);
|
|
135
|
+
const think = rawThink
|
|
136
|
+
? formatThink(rawThink)
|
|
137
|
+
: formatThink(extractThink(extractText(getNthAiMessage(messages, targetIndex)?.content)));
|
|
117
138
|
if (!think) return config.emptyMessage;
|
|
118
139
|
|
|
119
140
|
if (config.renderImage && ctx.chatluna?.renderer) {
|
package/package.json
CHANGED