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

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