koishi-plugin-chatluna-think-viewer 1.0.0 → 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 +33 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -4,6 +4,7 @@ const name = 'chatluna-think-viewer';
4
4
 
5
5
  const inject = {
6
6
  chatluna_character: { required: true },
7
+ chatluna: { required: false },
7
8
  };
8
9
 
9
10
  const Config = Schema.object({
@@ -11,6 +12,7 @@ const Config = Schema.object({
11
12
  keywords: Schema.array(Schema.string()).default(['查看思考', '上次思考']).description('无需前缀即可触发的关键词'),
12
13
  allowPrivate: Schema.boolean().default(false).description('是否允许在私聊中使用'),
13
14
  emptyMessage: Schema.string().default('暂时没有可用的思考记录。').description('没有记录时的提示文案'),
15
+ renderImage: Schema.boolean().default(false).description('尝试使用 ChatLuna 的 image renderer 将思考内容渲染为图片发送,失败则回退文本'),
14
16
  });
15
17
 
16
18
  function extractText(content) {
@@ -39,6 +41,22 @@ function extractThink(text) {
39
41
  return match?.[1]?.trim() ?? '';
40
42
  }
41
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
+
42
60
  function getLastAiMessage(messages) {
43
61
  for (let i = messages.length - 1; i >= 0; i--) {
44
62
  const msg = messages[i];
@@ -73,9 +91,23 @@ function apply(ctx, config) {
73
91
  const text = extractText(lastAi.content);
74
92
  if (!text) return '未找到可解析的回复内容。';
75
93
 
76
- const think = extractThink(text);
94
+ const think = formatThink(extractThink(text));
77
95
  if (!think) return '上一次回复中没有 <think> 字段。';
78
96
 
97
+ if (config.renderImage && ctx.chatluna?.renderer) {
98
+ try {
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 });
105
+ if (rendered?.length) return rendered.map((r) => r.element);
106
+ } catch (err) {
107
+ ctx.logger?.warn?.('[think-viewer] image render failed, fallback text', err);
108
+ }
109
+ }
110
+
79
111
  return `上一条思考:\n${think}`;
80
112
  });
81
113
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna-think-viewer",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "main": "index.js",
5
5
  "description": "通过命令/关键词查看 chatluna-character 最近一次回复中的 <think> 思考内容。",
6
6
  "license": "MIT",