koishi-plugin-chatluna-think-viewer 1.0.1 → 1.0.2

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 +23 -5
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -41,6 +41,22 @@ function extractThink(text) {
41
41
  return match?.[1]?.trim() ?? '';
42
42
  }
43
43
 
44
+ function formatThink(text) {
45
+ if (!text) return text;
46
+ // 尝试 JSON 美化
47
+ try {
48
+ const parsed = JSON.parse(text);
49
+ return JSON.stringify(parsed, null, 2);
50
+ } catch {
51
+ // 保留原文,简单压缩多余空行
52
+ return text
53
+ .split('\n')
54
+ .map((l) => l.trimEnd())
55
+ .filter((l, idx, arr) => !(l === '' && arr[idx - 1] === ''))
56
+ .join('\n');
57
+ }
58
+ }
59
+
44
60
  function getLastAiMessage(messages) {
45
61
  for (let i = messages.length - 1; i >= 0; i--) {
46
62
  const msg = messages[i];
@@ -75,15 +91,17 @@ function apply(ctx, config) {
75
91
  const text = extractText(lastAi.content);
76
92
  if (!text) return '未找到可解析的回复内容。';
77
93
 
78
- const think = extractThink(text);
94
+ const think = formatThink(extractThink(text));
79
95
  if (!think) return '上一次回复中没有 <think> 字段。';
80
96
 
81
97
  if (config.renderImage && ctx.chatluna?.renderer) {
82
98
  try {
83
- const rendered = await ctx.chatluna.renderer.render(
84
- { content: think },
85
- { type: 'image', session }
86
- );
99
+ const rendered = await ctx.chatluna.renderer.render({
100
+ content: [
101
+ { type: 'text', text: '上一条思考:\n' },
102
+ { type: 'text', text: '```\n' + think + '\n```' },
103
+ ],
104
+ }, { type: 'image', session });
87
105
  if (rendered?.length) return rendered.map((r) => r.element);
88
106
  } catch (err) {
89
107
  ctx.logger?.warn?.('[think-viewer] image render failed, fallback text', err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "description": "通过命令/关键词查看 chatluna-character 最近一次回复中的 <think> 思考内容。",
6
6
  "license": "MIT",