@nestim/koishi-plugin-qqgm-webui 0.2.22 → 0.3.0

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.
@@ -0,0 +1,236 @@
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
+ function fieldInfo(key, field, groupTitle) {
53
+ const meta = (field && field.meta) || {};
54
+ const type = String((field && field.type) || 'string');
55
+ const options = [];
56
+ if (type === 'union') {
57
+ for (const match of String(field).matchAll(/"([^"]+)"/g)) options.push(match[1]);
58
+ } else if (type === 'const') {
59
+ const value = meta.default;
60
+ if (value != null) options.push(String(value));
61
+ }
62
+ const description = meta.description || '';
63
+ const defaultValue = meta.default;
64
+ const long = type === 'string' && (
65
+ /prompt|system|persona|template|greeting|welcome|instruction|rule/i.test(key)
66
+ || (typeof defaultValue === 'string' && defaultValue.length > 60)
67
+ );
68
+ return {
69
+ key,
70
+ group: groupTitle || '',
71
+ label: humanLabel(key, description),
72
+ type,
73
+ description,
74
+ default: defaultValue,
75
+ options,
76
+ sensitive: /key|token|secret|password/i.test(key),
77
+ long,
78
+ list: type === 'array' && /^string\[\]/.test(String(field)),
79
+ json: type === 'array' || type === 'object' || type === 'intersect',
80
+ };
81
+ }
82
+
83
+ function loadPlugin(pkg) {
84
+ try {
85
+ const mod = require(pkg);
86
+ return mod && typeof mod === 'object' ? mod : null;
87
+ } catch {
88
+ return null;
89
+ }
90
+ }
91
+
92
+ function pluginVersion(pkg) {
93
+ try {
94
+ const info = require(`${pkg}/package.json`);
95
+ return info && info.version ? String(info.version) : null;
96
+ } catch {
97
+ return null;
98
+ }
99
+ }
100
+
101
+ function flattenFields(config) {
102
+ if (!config) return [];
103
+ if (Array.isArray(config.list)) {
104
+ const out = [];
105
+ for (const group of config.list) {
106
+ const title = (group.meta && group.meta.description) || '';
107
+ const dict = group.dict || {};
108
+ for (const key of Object.keys(dict)) out.push(fieldInfo(key, dict[key], title));
109
+ }
110
+ return out;
111
+ }
112
+ const dict = config.dict || {};
113
+ return Object.keys(dict).map((key) => fieldInfo(key, dict[key], ''));
114
+ }
115
+
116
+ function readFile(ctx) {
117
+ const file = path.join(ctx.baseDir || process.cwd(), 'koishi.yml');
118
+ let text = '';
119
+ try { text = fs.readFileSync(file, 'utf8'); } catch { /* ignore */ }
120
+ return { file, text };
121
+ }
122
+
123
+ function readDoc(text) {
124
+ if (!yaml) return {};
125
+ try { return yaml.load(text) || {}; } catch { return {}; }
126
+ }
127
+
128
+ function findBlock(lines, instance) {
129
+ const re = new RegExp(`^(\\s*)'?${escapeRe(instance)}'?\\s*:\\s*$`);
130
+ const start = lines.findIndex((line) => re.test(line));
131
+ if (start < 0) throw new Error(`未找到插件配置块 ${instance}`);
132
+ const indent = (lines[start].match(/^\s*/) || [''])[0].length;
133
+ let end = start + 1;
134
+ while (end < lines.length) {
135
+ const line = lines[end];
136
+ if (!line.trim()) { end += 1; continue; }
137
+ const current = (line.match(/^\s*/) || [''])[0].length;
138
+ if (current <= indent && !line.trim().startsWith('#')) break;
139
+ end += 1;
140
+ }
141
+ return { start, end, indent };
142
+ }
143
+
144
+ function serialize(value) {
145
+ if (typeof value === 'string') return JSON.stringify(value);
146
+ if (typeof value === 'number' || typeof value === 'boolean') return String(value);
147
+ return JSON.stringify(value);
148
+ }
149
+
150
+ function setField(lines, instance, key, value) {
151
+ const block = findBlock(lines, instance);
152
+ const indent = ' '.repeat(block.indent + 2);
153
+ const re = new RegExp(`^(\\s*)${escapeRe(key)}\\s*:`);
154
+ let index = -1;
155
+ let fieldIndent = indent;
156
+ for (let i = block.start + 1; i < block.end; i += 1) {
157
+ const match = re.exec(lines[i]);
158
+ if (match) {
159
+ index = i;
160
+ fieldIndent = match[1];
161
+ break;
162
+ }
163
+ }
164
+ const line = `${fieldIndent}${key}: ${serialize(value)}`;
165
+ if (index < 0) {
166
+ lines.splice(block.end, 0, `${indent}${key}: ${serialize(value)}`);
167
+ return;
168
+ }
169
+ let end = index + 1;
170
+ while (end < block.end) {
171
+ const current = (lines[end].match(/^\s*/) || [''])[0].length;
172
+ if (!lines[end].trim() || current > fieldIndent.length) { end += 1; continue; }
173
+ break;
174
+ }
175
+ lines.splice(index, end - index, line);
176
+ }
177
+
178
+ function readPluginConfig(ctx, pkg) {
179
+ const known = knownByPackage(pkg);
180
+ if (!known && !/^@nestim\/koishi-plugin-[a-z0-9-]+$/.test(pkg)) {
181
+ throw new Error('只支持 @nestim/koishi-plugin-* 插件');
182
+ }
183
+ const mod = loadPlugin(pkg);
184
+ const installed = !!mod;
185
+ const { file, text } = readFile(ctx);
186
+ const doc = readDoc(text);
187
+ const instance = instanceOf(doc, pkg);
188
+ const plugins = doc.plugins || {};
189
+ const values = instance ? (plugins[instance] || {}) : {};
190
+ const fields = flattenFields(mod && mod.Config);
191
+ return {
192
+ available: installed,
193
+ installed,
194
+ configured: !!instance,
195
+ name: pkg,
196
+ title: known ? known.title : shortName(pkg),
197
+ version: installed ? pluginVersion(pkg) : null,
198
+ instance,
199
+ file,
200
+ fields,
201
+ values: values && typeof values === 'object' ? values : {},
202
+ };
203
+ }
204
+
205
+ function writePluginConfig(ctx, pkg, input) {
206
+ const info = readPluginConfig(ctx, pkg);
207
+ if (!info.installed) throw new Error(`${pkg} 未安装`);
208
+ if (!info.instance) throw new Error(`${shortName(pkg)} 还没有在 koishi.yml 里启用,先在 Koishi 控制台启用它`);
209
+ const allowed = new Set(info.fields.map((field) => field.key));
210
+ const updates = {};
211
+ for (const [key, value] of Object.entries(input || {})) {
212
+ if (!allowed.has(key) || value === undefined) continue;
213
+ if (info.fields.find((field) => field.key === key)?.sensitive && value === '__KEEP__') continue;
214
+ updates[key] = value;
215
+ }
216
+ if (!Object.keys(updates).length) throw new Error('没有可写入的字段');
217
+ const { file, text } = readFile(ctx);
218
+ const lines = text.split(/\r?\n/);
219
+ for (const [key, value] of Object.entries(updates)) setField(lines, info.instance, key, value);
220
+ const next = lines.join('\n');
221
+ if (yaml) yaml.load(next);
222
+ const stamp = new Date().toISOString().replace(/[-:T.Z]/g, '').slice(0, 14);
223
+ const backup = `${file}.bak-plugin-${stamp}`;
224
+ fs.copyFileSync(file, backup);
225
+ const tmp = `${file}.tmp`;
226
+ fs.writeFileSync(tmp, next, 'utf8');
227
+ fs.renameSync(tmp, file);
228
+ return { backup, keys: Object.keys(updates), instance: info.instance };
229
+ }
230
+
231
+ module.exports = {
232
+ KNOWN,
233
+ readPluginConfig,
234
+ writePluginConfig,
235
+ shortName,
236
+ };
package/package.json CHANGED
@@ -1,29 +1,33 @@
1
- {
2
- "name": "@nestim/koishi-plugin-qqgm-webui",
3
- "version": "0.2.22",
4
- "description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
5
- "main": "index.js",
6
- "files": [
7
- "index.js",
8
- "lib",
9
- "public",
10
- "README.md"
11
- ],
12
- "license": "MIT",
13
- "dependencies": {
14
- "js-yaml": "^4.1.0"
15
- },
16
- "keywords": ["koishi", "koishi-plugin", "qqgm", "webui", "admin"],
17
- "peerDependencies": {
18
- "koishi": "^4.18.7",
19
- "@nestim/koishi-plugin-qq-group-manager": ">=1.5.0"
20
- },
21
- "koishi": {
22
- "description": "QQGM 图形化管理台:插件/监控/记忆/配置/日志",
23
- "category": "manage"
24
- },
25
- "repository": {
26
- "type": "git",
27
- "url": "git+https://github.com/Miseat/koishi-plugin-qqgm-webui.git"
28
- }
29
- }
1
+ {
2
+ "name": "@nestim/koishi-plugin-qqgm-webui",
3
+ "version": "0.3.0",
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
+ }