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

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 +40 -12
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -41,23 +41,46 @@ function extractThink(text) {
41
41
  return match?.[1]?.trim() ?? '';
42
42
  }
43
43
 
44
- function getLastAiMessage(messages) {
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
+
60
+ function getNthAiMessage(messages, n = 1) {
61
+ if (!Array.isArray(messages) || n < 1) return null;
62
+ let count = 0;
45
63
  for (let i = messages.length - 1; i >= 0; i--) {
46
64
  const msg = messages[i];
47
65
  const type = typeof msg?._getType === 'function' ? msg._getType() : msg?.type || msg?.role;
48
- if (type === 'ai' || type === 'assistant') return msg;
66
+ if (type === 'ai' || type === 'assistant') {
67
+ count++;
68
+ if (count === n) return msg;
69
+ }
49
70
  }
50
71
  return null;
51
72
  }
52
73
 
53
74
  function apply(ctx, config) {
54
- const cmd = ctx.command(config.command, '获取上一条回复中的 <think> 内容');
75
+ const cmd = ctx
76
+ .command(`${config.command} [index:number]`, '获取上一条回复中的 <think> 内容(可指定倒数第 N 条)')
77
+ .usage('不带参数默认读取最近一条;例如 think 2 读取倒数第二条 AI 回复的思考。');
55
78
 
56
79
  for (const keyword of config.keywords || []) {
57
80
  cmd.shortcut(keyword, { prefix: false });
58
81
  }
59
82
 
60
- cmd.action(async ({ session }) => {
83
+ cmd.action(async ({ session }, rawIndex) => {
61
84
  if (!config.allowPrivate && !session.guildId) {
62
85
  return '仅支持在群聊中查询。';
63
86
  }
@@ -69,21 +92,26 @@ function apply(ctx, config) {
69
92
  const messages = temp?.completionMessages || [];
70
93
  if (!messages.length) return config.emptyMessage;
71
94
 
72
- const lastAi = getLastAiMessage(messages);
73
- if (!lastAi) return config.emptyMessage;
95
+ let targetIndex = parseInt(rawIndex, 10);
96
+ if (!Number.isFinite(targetIndex) || targetIndex < 1) targetIndex = 1;
97
+
98
+ const targetAi = getNthAiMessage(messages, targetIndex);
99
+ if (!targetAi) return `找不到倒数第 ${targetIndex} 条 AI 回复的记录。`;
74
100
 
75
- const text = extractText(lastAi.content);
101
+ const text = extractText(targetAi.content);
76
102
  if (!text) return '未找到可解析的回复内容。';
77
103
 
78
- const think = extractThink(text);
104
+ const think = formatThink(extractThink(text));
79
105
  if (!think) return '上一次回复中没有 <think> 字段。';
80
106
 
81
107
  if (config.renderImage && ctx.chatluna?.renderer) {
82
108
  try {
83
- const rendered = await ctx.chatluna.renderer.render(
84
- { content: think },
85
- { type: 'image', session }
86
- );
109
+ const rendered = await ctx.chatluna.renderer.render({
110
+ content: [
111
+ { type: 'text', text: '上一条思考:\n' },
112
+ { type: 'text', text: '```\n' + think + '\n```' },
113
+ ],
114
+ }, { type: 'image', session });
87
115
  if (rendered?.length) return rendered.map((r) => r.element);
88
116
  } catch (err) {
89
117
  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.3",
4
4
  "main": "index.js",
5
5
  "description": "通过命令/关键词查看 chatluna-character 最近一次回复中的 <think> 思考内容。",
6
6
  "license": "MIT",