koishi-plugin-chatluna-think-viewer 1.0.3 → 1.0.5
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 +11 -9
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -48,12 +48,14 @@ function formatThink(text) {
|
|
|
48
48
|
const parsed = JSON.parse(text);
|
|
49
49
|
return JSON.stringify(parsed, null, 2);
|
|
50
50
|
} catch {
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
.
|
|
51
|
+
// 保留原文,去掉多余空行与统一左侧缩进
|
|
52
|
+
const lines = text.split('\n').map((l) => l.trimEnd());
|
|
53
|
+
const filtered = lines.filter((l, idx, arr) => !(l === '' && arr[idx - 1] === ''));
|
|
54
|
+
const nonEmpty = filtered.filter((l) => l.trim().length > 0);
|
|
55
|
+
const minIndent = nonEmpty.length
|
|
56
|
+
? Math.min(...nonEmpty.map((l) => l.match(/^(\s*)/)?.[1]?.length ?? 0))
|
|
57
|
+
: 0;
|
|
58
|
+
return filtered.map((l) => l.slice(minIndent)).join('\n');
|
|
57
59
|
}
|
|
58
60
|
}
|
|
59
61
|
|
|
@@ -102,14 +104,14 @@ function apply(ctx, config) {
|
|
|
102
104
|
if (!text) return '未找到可解析的回复内容。';
|
|
103
105
|
|
|
104
106
|
const think = formatThink(extractThink(text));
|
|
105
|
-
if (!think) return '上一次回复中没有 <think> 字段。';
|
|
107
|
+
if (!think) return config.emptyMessage || '上一次回复中没有 <think> 字段。';
|
|
106
108
|
|
|
107
109
|
if (config.renderImage && ctx.chatluna?.renderer) {
|
|
108
110
|
try {
|
|
111
|
+
const title = `### 上一条思考(倒数第 ${targetIndex} 条)`;
|
|
109
112
|
const rendered = await ctx.chatluna.renderer.render({
|
|
110
113
|
content: [
|
|
111
|
-
{ type: 'text', text:
|
|
112
|
-
{ type: 'text', text: '```\n' + think + '\n```' },
|
|
114
|
+
{ type: 'text', text: `${title}\n\n\`\`\`\n${think}\n\`\`\`` },
|
|
113
115
|
],
|
|
114
116
|
}, { type: 'image', session });
|
|
115
117
|
if (rendered?.length) return rendered.map((r) => r.element);
|