koishi-plugin-chatluna-think-viewer 1.0.9 → 1.0.11
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 +23 -30
- 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(
|
|
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,23 @@ function apply(ctx, config) {
|
|
|
122
110
|
|
|
123
111
|
const targetIndex = parseIndex(rawIndex ?? args?.[0]);
|
|
124
112
|
|
|
125
|
-
const
|
|
126
|
-
if (!
|
|
127
|
-
|
|
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
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
122
|
+
const markdown = `<div align="center">\n${title}\n</div>\n\n<div align="left">\n${think}\n</div>`;
|
|
123
|
+
const rendered = await ctx.chatluna.renderer.render(
|
|
124
|
+
{
|
|
125
|
+
// 中间标题居中、正文左对齐,避免整段贴左侧
|
|
126
|
+
content: [{ type: 'text', text: markdown }],
|
|
127
|
+
},
|
|
128
|
+
{ type: 'image', session },
|
|
129
|
+
);
|
|
137
130
|
if (rendered?.length) return rendered.map((r) => r.element);
|
|
138
131
|
} catch (err) {
|
|
139
132
|
ctx.logger?.warn?.('[think-viewer] image render failed, fallback text', err);
|
|
@@ -149,4 +142,4 @@ module.exports = {
|
|
|
149
142
|
apply,
|
|
150
143
|
Config,
|
|
151
144
|
inject,
|
|
152
|
-
};
|
|
145
|
+
};
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
{
|
|
1
|
+
{
|
|
2
2
|
"name": "koishi-plugin-chatluna-think-viewer",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11",
|
|
4
4
|
"main": "index.js",
|
|
5
|
-
"description": "
|
|
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": "
|
|
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
|
+
}
|