@nestim/koishi-plugin-qqgm-webui 0.2.23 → 0.3.1
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/CHANGELOG.md +66 -0
- package/README.md +41 -56
- package/index.js +172 -12
- package/lib/bot.js +44 -0
- package/lib/data.js +72 -13
- package/lib/logbuf.js +79 -0
- package/lib/plugin-config.js +288 -0
- package/package.json +33 -29
- package/public/app.js +1126 -241
- package/public/index.html +62 -148
- package/public/styles.css +118 -20
package/lib/logbuf.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日志环形缓冲:Koishi 默认只把日志打到控制台,不落文件。
|
|
3
|
+
* WebUI 通过给 reggol Logger 挂一个 record 目标拿到进程内日志,
|
|
4
|
+
* 内存里只留最近 N 条,页面关掉也不影响主进程。
|
|
5
|
+
*
|
|
6
|
+
* 注意:target 是全局的,插件卸载时要 remove,否则热重载会重复收集。
|
|
7
|
+
*/
|
|
8
|
+
function loadLogger() {
|
|
9
|
+
try {
|
|
10
|
+
const koishi = require('koishi');
|
|
11
|
+
if (koishi && koishi.Logger && Array.isArray(koishi.Logger.targets)) return koishi.Logger;
|
|
12
|
+
} catch { /* ignore */ }
|
|
13
|
+
try {
|
|
14
|
+
const logger = require('reggol');
|
|
15
|
+
if (logger && Array.isArray(logger.targets)) return logger;
|
|
16
|
+
} catch { /* ignore */ }
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function createLogBuffer(options = {}) {
|
|
21
|
+
const max = Math.max(50, Math.min(10_000, Number(options.max) || 2000));
|
|
22
|
+
const lines = [];
|
|
23
|
+
let dropped = 0;
|
|
24
|
+
let target = null;
|
|
25
|
+
const Logger = loadLogger();
|
|
26
|
+
|
|
27
|
+
if (Logger) {
|
|
28
|
+
target = {
|
|
29
|
+
record(record) {
|
|
30
|
+
try {
|
|
31
|
+
lines.push({
|
|
32
|
+
id: Number(record.id) || 0,
|
|
33
|
+
time: Number(record.timestamp) || Date.now(),
|
|
34
|
+
level: String(record.type || 'info'),
|
|
35
|
+
name: String(record.name || ''),
|
|
36
|
+
text: String(record.content || ''),
|
|
37
|
+
});
|
|
38
|
+
while (lines.length > max) {
|
|
39
|
+
lines.shift();
|
|
40
|
+
dropped += 1;
|
|
41
|
+
}
|
|
42
|
+
} catch { /* 任何异常都不能影响主进程日志 */ }
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
Logger.targets.push(target);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
available: !!Logger,
|
|
50
|
+
/** 返回最近 limit 条,可按级别/关键词过滤 */
|
|
51
|
+
snapshot({ limit = 400, level = '', query = '' } = {}) {
|
|
52
|
+
const wanted = String(level || '').toLowerCase();
|
|
53
|
+
const q = String(query || '').trim().toLowerCase();
|
|
54
|
+
let list = lines;
|
|
55
|
+
if (wanted && wanted !== 'all' && wanted !== '全部') {
|
|
56
|
+
list = list.filter((line) => line.level.toLowerCase() === wanted);
|
|
57
|
+
}
|
|
58
|
+
if (q) {
|
|
59
|
+
list = list.filter((line) => line.text.toLowerCase().includes(q)
|
|
60
|
+
|| line.name.toLowerCase().includes(q));
|
|
61
|
+
}
|
|
62
|
+
const n = Math.max(1, Math.min(max, Number(limit) || 400));
|
|
63
|
+
return {
|
|
64
|
+
lines: list.slice(-n),
|
|
65
|
+
total: lines.length,
|
|
66
|
+
dropped,
|
|
67
|
+
limit: n,
|
|
68
|
+
};
|
|
69
|
+
},
|
|
70
|
+
dispose() {
|
|
71
|
+
if (!target || !Logger) return;
|
|
72
|
+
const index = Logger.targets.indexOf(target);
|
|
73
|
+
if (index >= 0) Logger.targets.splice(index, 1);
|
|
74
|
+
target = null;
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { createLogBuffer };
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 附属插件配置:直接读插件导出的 Config Schema,生成字段(含说明)。
|
|
3
|
+
*
|
|
4
|
+
* - 值来自 koishi.yml 的插件实例块(如 `@nestim/qqgm-memory:xxxx`);
|
|
5
|
+
* - 写入只改该块内的字段,保留其它插件与注释,写前备份;
|
|
6
|
+
* - 没安装的插件返回 available:false,由前端给安装入口。
|
|
7
|
+
*/
|
|
8
|
+
const fs = require('node:fs');
|
|
9
|
+
const path = require('node:path');
|
|
10
|
+
let yaml = null;
|
|
11
|
+
try { yaml = require('js-yaml'); } catch { /* optional */ }
|
|
12
|
+
|
|
13
|
+
const KNOWN = [
|
|
14
|
+
{ id: 'qq-group-manager', name: '@nestim/koishi-plugin-qq-group-manager', title: '主插件' },
|
|
15
|
+
{ id: 'qqgm-webui', name: '@nestim/koishi-plugin-qqgm-webui', title: 'WebUI' },
|
|
16
|
+
{ id: 'qqgm-memory', name: '@nestim/koishi-plugin-qqgm-memory', title: '记忆管理' },
|
|
17
|
+
{ id: 'qqgm-monitor', name: '@nestim/koishi-plugin-qqgm-monitor', title: '监控' },
|
|
18
|
+
{ id: 'qqgm-meme', name: '@nestim/koishi-plugin-qqgm-meme', title: '表情包' },
|
|
19
|
+
{ id: 'qqgm-chat-style', name: '@nestim/koishi-plugin-qqgm-chat-style', title: '聊天风格' },
|
|
20
|
+
{ id: 'qqgm-private', name: '@nestim/koishi-plugin-qqgm-private', title: '私聊' },
|
|
21
|
+
];
|
|
22
|
+
|
|
23
|
+
function escapeRe(text) {
|
|
24
|
+
return String(text).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function shortName(pkg) {
|
|
28
|
+
return String(pkg || '').replace('@nestim/koishi-plugin-', '').replace('@nestim/', '');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function knownByPackage(pkg) {
|
|
32
|
+
return KNOWN.find((item) => item.name === pkg) || null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function instanceOf(doc, pkg) {
|
|
36
|
+
const plugins = (doc && doc.plugins) || {};
|
|
37
|
+
const short = shortName(pkg);
|
|
38
|
+
const aliases = new Set([pkg, `@nestim/${short}`, `@nestim/koishi-plugin-${short}`]);
|
|
39
|
+
for (const key of Object.keys(plugins)) {
|
|
40
|
+
if (aliases.has(String(key).split(':')[0])) return key;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function humanLabel(key, description) {
|
|
46
|
+
const text = String(description || '').replace(/\s+/g, ' ').trim();
|
|
47
|
+
const first = text.split(/[::((,,。;;]/)[0].trim();
|
|
48
|
+
if (first && first.length >= 2 && first.length <= 28) return first;
|
|
49
|
+
return key;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const LABELS = {
|
|
53
|
+
from: '原文',
|
|
54
|
+
to: '替换为',
|
|
55
|
+
term: '术语',
|
|
56
|
+
meaning: '含义',
|
|
57
|
+
style: '风格',
|
|
58
|
+
probability: '概率',
|
|
59
|
+
enabled: '启用',
|
|
60
|
+
maxChars: '字数上限',
|
|
61
|
+
maxLines: '行数上限',
|
|
62
|
+
groupId: '群号',
|
|
63
|
+
systemPrompt: '系统提示词',
|
|
64
|
+
primaryStyle: '主风格',
|
|
65
|
+
extraStyles: '追加风格',
|
|
66
|
+
bannedPatterns: '删除词/短语',
|
|
67
|
+
replacements: '字面替换',
|
|
68
|
+
glossary: '术语解释',
|
|
69
|
+
allowedUsers: '只回复这些 QQ 号',
|
|
70
|
+
blockedUsers: '不回复这些 QQ 号',
|
|
71
|
+
allowedUserIds: '允许的用户 ID',
|
|
72
|
+
dbFile: 'SQLite 文件',
|
|
73
|
+
bannedWords: '违禁词',
|
|
74
|
+
whitelistKeywords: '白名单关键词',
|
|
75
|
+
strongKeywords: '强关键词',
|
|
76
|
+
bannedWordScoreThreshold: '违禁词阈值',
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
function fieldInfo(key, field, groupTitle) {
|
|
80
|
+
const meta = (field && field.meta) || {};
|
|
81
|
+
const type = String((field && field.type) || 'string');
|
|
82
|
+
const options = [];
|
|
83
|
+
if (type === 'union') {
|
|
84
|
+
for (const match of String(field).matchAll(/"([^"]+)"/g)) options.push(match[1]);
|
|
85
|
+
} else if (type === 'const') {
|
|
86
|
+
const value = meta.default;
|
|
87
|
+
if (value != null) options.push(String(value));
|
|
88
|
+
}
|
|
89
|
+
const description = meta.description || '';
|
|
90
|
+
const defaultValue = meta.default;
|
|
91
|
+
const long = type === 'string' && (
|
|
92
|
+
meta.role === 'textarea'
|
|
93
|
+
|| /prompt|system|persona|template|greeting|welcome|instruction|rule|meaning/i.test(key)
|
|
94
|
+
|| (typeof defaultValue === 'string' && defaultValue.length > 60)
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
let list = false;
|
|
98
|
+
let objectList = false;
|
|
99
|
+
let itemFields = [];
|
|
100
|
+
let objectFields = [];
|
|
101
|
+
if (type === 'array') {
|
|
102
|
+
if (/^string\[\]/.test(String(field))) {
|
|
103
|
+
list = true;
|
|
104
|
+
} else if (field.inner && field.inner.dict) {
|
|
105
|
+
objectList = true;
|
|
106
|
+
itemFields = Object.keys(field.inner.dict).map((name) => fieldInfo(name, field.inner.dict[name], groupTitle));
|
|
107
|
+
}
|
|
108
|
+
} else if (type === 'object' && field.dict) {
|
|
109
|
+
objectFields = Object.keys(field.dict).map((name) => fieldInfo(name, field.dict[name], groupTitle));
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
key,
|
|
114
|
+
group: groupTitle || '',
|
|
115
|
+
label: LABELS[key] || humanLabel(key, description),
|
|
116
|
+
type,
|
|
117
|
+
description,
|
|
118
|
+
default: defaultValue,
|
|
119
|
+
options,
|
|
120
|
+
sensitive: /key|token|secret|password/i.test(key),
|
|
121
|
+
required: meta.required === true,
|
|
122
|
+
long,
|
|
123
|
+
list,
|
|
124
|
+
objectList,
|
|
125
|
+
itemFields,
|
|
126
|
+
objectFields,
|
|
127
|
+
scored: /bannedWords|strongKeywords/.test(key),
|
|
128
|
+
json: !list && !objectList && !objectFields.length && (
|
|
129
|
+
type === 'array' || type === 'object' || type === 'intersect'
|
|
130
|
+
),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function loadPlugin(pkg) {
|
|
135
|
+
try {
|
|
136
|
+
const mod = require(pkg);
|
|
137
|
+
return mod && typeof mod === 'object' ? mod : null;
|
|
138
|
+
} catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function pluginVersion(pkg) {
|
|
144
|
+
try {
|
|
145
|
+
const info = require(`${pkg}/package.json`);
|
|
146
|
+
return info && info.version ? String(info.version) : null;
|
|
147
|
+
} catch {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function flattenFields(config) {
|
|
153
|
+
if (!config) return [];
|
|
154
|
+
if (Array.isArray(config.list)) {
|
|
155
|
+
const out = [];
|
|
156
|
+
for (const group of config.list) {
|
|
157
|
+
const title = (group.meta && group.meta.description) || '';
|
|
158
|
+
const dict = group.dict || {};
|
|
159
|
+
for (const key of Object.keys(dict)) out.push(fieldInfo(key, dict[key], title));
|
|
160
|
+
}
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
163
|
+
const dict = config.dict || {};
|
|
164
|
+
return Object.keys(dict).map((key) => fieldInfo(key, dict[key], ''));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function readFile(ctx) {
|
|
168
|
+
const file = path.join(ctx.baseDir || process.cwd(), 'koishi.yml');
|
|
169
|
+
let text = '';
|
|
170
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch { /* ignore */ }
|
|
171
|
+
return { file, text };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function readDoc(text) {
|
|
175
|
+
if (!yaml) return {};
|
|
176
|
+
try { return yaml.load(text) || {}; } catch { return {}; }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function findBlock(lines, instance) {
|
|
180
|
+
const re = new RegExp(`^(\\s*)'?${escapeRe(instance)}'?\\s*:\\s*$`);
|
|
181
|
+
const start = lines.findIndex((line) => re.test(line));
|
|
182
|
+
if (start < 0) throw new Error(`未找到插件配置块 ${instance}`);
|
|
183
|
+
const indent = (lines[start].match(/^\s*/) || [''])[0].length;
|
|
184
|
+
let end = start + 1;
|
|
185
|
+
while (end < lines.length) {
|
|
186
|
+
const line = lines[end];
|
|
187
|
+
if (!line.trim()) { end += 1; continue; }
|
|
188
|
+
const current = (line.match(/^\s*/) || [''])[0].length;
|
|
189
|
+
if (current <= indent && !line.trim().startsWith('#')) break;
|
|
190
|
+
end += 1;
|
|
191
|
+
}
|
|
192
|
+
return { start, end, indent };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function serialize(value) {
|
|
196
|
+
if (typeof value === 'string') return JSON.stringify(value);
|
|
197
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
198
|
+
return JSON.stringify(value);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function setField(lines, instance, key, value) {
|
|
202
|
+
const block = findBlock(lines, instance);
|
|
203
|
+
const indent = ' '.repeat(block.indent + 2);
|
|
204
|
+
const re = new RegExp(`^(\\s*)${escapeRe(key)}\\s*:`);
|
|
205
|
+
let index = -1;
|
|
206
|
+
let fieldIndent = indent;
|
|
207
|
+
for (let i = block.start + 1; i < block.end; i += 1) {
|
|
208
|
+
const match = re.exec(lines[i]);
|
|
209
|
+
if (match) {
|
|
210
|
+
index = i;
|
|
211
|
+
fieldIndent = match[1];
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const line = `${fieldIndent}${key}: ${serialize(value)}`;
|
|
216
|
+
if (index < 0) {
|
|
217
|
+
lines.splice(block.end, 0, `${indent}${key}: ${serialize(value)}`);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
let end = index + 1;
|
|
221
|
+
while (end < block.end) {
|
|
222
|
+
const current = (lines[end].match(/^\s*/) || [''])[0].length;
|
|
223
|
+
if (!lines[end].trim() || current > fieldIndent.length) { end += 1; continue; }
|
|
224
|
+
break;
|
|
225
|
+
}
|
|
226
|
+
lines.splice(index, end - index, line);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function readPluginConfig(ctx, pkg) {
|
|
230
|
+
const known = knownByPackage(pkg);
|
|
231
|
+
if (!known && !/^@nestim\/koishi-plugin-[a-z0-9-]+$/.test(pkg)) {
|
|
232
|
+
throw new Error('只支持 @nestim/koishi-plugin-* 插件');
|
|
233
|
+
}
|
|
234
|
+
const mod = loadPlugin(pkg);
|
|
235
|
+
const installed = !!mod;
|
|
236
|
+
const { file, text } = readFile(ctx);
|
|
237
|
+
const doc = readDoc(text);
|
|
238
|
+
const instance = instanceOf(doc, pkg);
|
|
239
|
+
const plugins = doc.plugins || {};
|
|
240
|
+
const values = instance ? (plugins[instance] || {}) : {};
|
|
241
|
+
const fields = flattenFields(mod && mod.Config);
|
|
242
|
+
return {
|
|
243
|
+
available: installed,
|
|
244
|
+
installed,
|
|
245
|
+
configured: !!instance,
|
|
246
|
+
name: pkg,
|
|
247
|
+
title: known ? known.title : shortName(pkg),
|
|
248
|
+
version: installed ? pluginVersion(pkg) : null,
|
|
249
|
+
instance,
|
|
250
|
+
file,
|
|
251
|
+
fields,
|
|
252
|
+
values: values && typeof values === 'object' ? values : {},
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function writePluginConfig(ctx, pkg, input) {
|
|
257
|
+
const info = readPluginConfig(ctx, pkg);
|
|
258
|
+
if (!info.installed) throw new Error(`${pkg} 未安装`);
|
|
259
|
+
if (!info.instance) throw new Error(`${shortName(pkg)} 还没有在 koishi.yml 里启用,先在 Koishi 控制台启用它`);
|
|
260
|
+
const allowed = new Set(info.fields.map((field) => field.key));
|
|
261
|
+
const updates = {};
|
|
262
|
+
for (const [key, value] of Object.entries(input || {})) {
|
|
263
|
+
if (!allowed.has(key) || value === undefined) continue;
|
|
264
|
+
if (info.fields.find((field) => field.key === key)?.sensitive && value === '__KEEP__') continue;
|
|
265
|
+
updates[key] = value;
|
|
266
|
+
}
|
|
267
|
+
if (!Object.keys(updates).length) throw new Error('没有可写入的字段');
|
|
268
|
+
const { file, text } = readFile(ctx);
|
|
269
|
+
const lines = text.split(/\r?\n/);
|
|
270
|
+
for (const [key, value] of Object.entries(updates)) setField(lines, info.instance, key, value);
|
|
271
|
+
const next = lines.join('\n');
|
|
272
|
+
if (yaml) yaml.load(next);
|
|
273
|
+
const stamp = new Date().toISOString().replace(/[-:T.Z]/g, '').slice(0, 14);
|
|
274
|
+
const backup = `${file}.bak-plugin-${stamp}`;
|
|
275
|
+
fs.copyFileSync(file, backup);
|
|
276
|
+
const tmp = `${file}.tmp`;
|
|
277
|
+
fs.writeFileSync(tmp, next, 'utf8');
|
|
278
|
+
fs.renameSync(tmp, file);
|
|
279
|
+
return { backup, keys: Object.keys(updates), instance: info.instance };
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
module.exports = {
|
|
283
|
+
KNOWN,
|
|
284
|
+
readPluginConfig,
|
|
285
|
+
writePluginConfig,
|
|
286
|
+
flattenFields,
|
|
287
|
+
shortName,
|
|
288
|
+
};
|
package/package.json
CHANGED
|
@@ -1,29 +1,33 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@nestim/koishi-plugin-qqgm-webui",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"files": [
|
|
7
|
-
"index.js",
|
|
8
|
-
"lib",
|
|
9
|
-
"public",
|
|
10
|
-
"README.md"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
"
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
"
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
"
|
|
28
|
-
}
|
|
29
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@nestim/koishi-plugin-qqgm-webui",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"files": [
|
|
7
|
+
"index.js",
|
|
8
|
+
"lib",
|
|
9
|
+
"public",
|
|
10
|
+
"README.md",
|
|
11
|
+
"CHANGELOG.md"
|
|
12
|
+
],
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"js-yaml": "^4.1.0"
|
|
16
|
+
},
|
|
17
|
+
"keywords": ["koishi", "koishi-plugin", "qqgm", "webui", "admin"],
|
|
18
|
+
"peerDependencies": {
|
|
19
|
+
"koishi": "^4.18.7",
|
|
20
|
+
"@nestim/koishi-plugin-qq-group-manager": ">=1.5.0"
|
|
21
|
+
},
|
|
22
|
+
"koishi": {
|
|
23
|
+
"description": "QQGM 图形化管理台:插件/监控/记忆/配置/日志",
|
|
24
|
+
"category": "manage"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"test": "node --test \"test/*.test.mjs\""
|
|
28
|
+
},
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"url": "git+https://github.com/Miseat/koishi-plugin-qqgm-webui.git"
|
|
32
|
+
}
|
|
33
|
+
}
|