@nestim/koishi-plugin-qqgm-webui 0.2.15 → 0.2.17
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/index.js +24 -1
- package/lib/data.js +1 -0
- package/lib/main-schema.js +141 -0
- package/package.json +4 -1
- package/public/app.js +157 -15
- package/public/index.html +19 -2
- package/public/styles.css +21 -2
package/index.js
CHANGED
|
@@ -12,6 +12,7 @@ const { Schema } = require('koishi');
|
|
|
12
12
|
const { pluginList, monitor, memory } = require('./lib/data');
|
|
13
13
|
const { readMemoryConfig, writeMemoryConfig } = require('./lib/config');
|
|
14
14
|
const { scan: scanMemoryMd, scopeOfFile } = require('./lib/memory-md');
|
|
15
|
+
const { groups: mainGroups, readMainConfig, writeMainValues } = require('./lib/main-schema');
|
|
15
16
|
const run = promisify(execFile);
|
|
16
17
|
|
|
17
18
|
async function readBody(koa) {
|
|
@@ -114,7 +115,7 @@ function apply(ctx, config) {
|
|
|
114
115
|
ctx.server.get(`${route}/api/health`, (koa) => json(koa, {
|
|
115
116
|
ok: true,
|
|
116
117
|
name,
|
|
117
|
-
version: '0.2.
|
|
118
|
+
version: '0.2.17',
|
|
118
119
|
route,
|
|
119
120
|
tokenRequired: !!config.token,
|
|
120
121
|
remoteAllowed: !!config.allowRemote,
|
|
@@ -130,6 +131,28 @@ function apply(ctx, config) {
|
|
|
130
131
|
|
|
131
132
|
ctx.server.get(`${route}/api/plugins`, (koa) => json(koa, pluginList(ctx)));
|
|
132
133
|
|
|
134
|
+
ctx.server.get(`${route}/api/config/main`, (koa) => {
|
|
135
|
+
const schema = mainGroups();
|
|
136
|
+
const cfg = readMainConfig(ctx);
|
|
137
|
+
json(koa, {
|
|
138
|
+
available: schema.length > 0,
|
|
139
|
+
groups: schema,
|
|
140
|
+
values: cfg.values,
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
ctx.server.post(`${route}/api/config/main`, async (koa) => {
|
|
145
|
+
if (!guard(koa)) return;
|
|
146
|
+
const body = await readBody(koa);
|
|
147
|
+
try {
|
|
148
|
+
const result = writeMainValues(ctx, body.values || {});
|
|
149
|
+
json(koa, { ok: true, ...result, restartRequired: true });
|
|
150
|
+
} catch (error) {
|
|
151
|
+
koa.status = 400;
|
|
152
|
+
koa.body = { ok: false, error: String(error && error.message || error) };
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
|
|
133
156
|
ctx.server.get(`${route}/api/memory`, (koa) => {
|
|
134
157
|
json(koa, memory(ctx, {
|
|
135
158
|
scope: koa.query.scope || '',
|
package/lib/data.js
CHANGED
|
@@ -191,6 +191,7 @@ function memory(ctx, { scope = '', query = '', limit = 50 } = {}) {
|
|
|
191
191
|
installed: sqliteInstalled || md.entries.length > 0,
|
|
192
192
|
backend: cfg.backend,
|
|
193
193
|
sqliteInstalled,
|
|
194
|
+
sqliteTotal: sqliteItems.length,
|
|
194
195
|
useSqlite,
|
|
195
196
|
needsMigration: md.entries.length > 0 && !useSqlite,
|
|
196
197
|
items,
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 用主插件导出的 Config Schema 自动生成完整配置页,避免手写“残疾版”。
|
|
3
|
+
* 写入仍是只改主插件配置块的字段级编辑,保留其他 YAML。
|
|
4
|
+
*/
|
|
5
|
+
const fs = require('node:fs');
|
|
6
|
+
const path = require('node:path');
|
|
7
|
+
let yaml = null;
|
|
8
|
+
try { yaml = require('js-yaml'); } catch { /* optional */ }
|
|
9
|
+
|
|
10
|
+
const KEY_RE = /^(\s*)'?@nestim\/(?:koishi-plugin-)?qq-group-manager(?::[^']*)?'?\s*:\s*$/;
|
|
11
|
+
|
|
12
|
+
function loadMainPlugin() {
|
|
13
|
+
try {
|
|
14
|
+
return require('@nestim/koishi-plugin-qq-group-manager');
|
|
15
|
+
} catch {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function fieldInfo(key, field) {
|
|
21
|
+
const meta = field.meta || {};
|
|
22
|
+
const type = String(field.type || 'string');
|
|
23
|
+
const options = [];
|
|
24
|
+
if (type === 'union') {
|
|
25
|
+
for (const match of String(field).matchAll(/"([^"]+)"/g)) options.push(match[1]);
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
key,
|
|
29
|
+
type,
|
|
30
|
+
description: meta.description || '',
|
|
31
|
+
default: meta.default,
|
|
32
|
+
options,
|
|
33
|
+
sensitive: /key|token|secret|password/i.test(key),
|
|
34
|
+
json: type === 'array' || type === 'object' || type === 'intersect',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function groups() {
|
|
39
|
+
const plugin = loadMainPlugin();
|
|
40
|
+
if (!plugin || !plugin.Config || !Array.isArray(plugin.Config.list)) return [];
|
|
41
|
+
return plugin.Config.list.map((group) => {
|
|
42
|
+
const dict = group.dict || {};
|
|
43
|
+
return {
|
|
44
|
+
title: (group.meta && group.meta.description) || '配置',
|
|
45
|
+
fields: Object.keys(dict).map((key) => fieldInfo(key, dict[key])),
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function findBlock(lines) {
|
|
51
|
+
const start = lines.findIndex((line) => KEY_RE.test(line));
|
|
52
|
+
if (start < 0) throw new Error('未找到主插件配置块');
|
|
53
|
+
const indent = (lines[start].match(/^\s*/) || [''])[0].length;
|
|
54
|
+
let end = start + 1;
|
|
55
|
+
while (end < lines.length) {
|
|
56
|
+
const line = lines[end];
|
|
57
|
+
if (!line.trim()) { end += 1; continue; }
|
|
58
|
+
const current = (line.match(/^\s*/) || [''])[0].length;
|
|
59
|
+
if (current <= indent && !line.trim().startsWith('#')) break;
|
|
60
|
+
end += 1;
|
|
61
|
+
}
|
|
62
|
+
return { start, end, indent };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function serialize(value) {
|
|
66
|
+
if (typeof value === 'string') return JSON.stringify(value);
|
|
67
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
68
|
+
return JSON.stringify(value);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function setField(lines, key, value) {
|
|
72
|
+
const block = findBlock(lines);
|
|
73
|
+
const indent = ' '.repeat(block.indent + 2);
|
|
74
|
+
const re = new RegExp(`^(\\s*)${key}\\s*:`);
|
|
75
|
+
let index = -1;
|
|
76
|
+
let fieldIndent = indent;
|
|
77
|
+
for (let i = block.start + 1; i < block.end; i += 1) {
|
|
78
|
+
const match = re.exec(lines[i]);
|
|
79
|
+
if (match) {
|
|
80
|
+
index = i;
|
|
81
|
+
fieldIndent = match[1];
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const line = `${fieldIndent}${key}: ${serialize(value)}`;
|
|
86
|
+
if (index < 0) {
|
|
87
|
+
lines.splice(block.end, 0, `${indent}${key}: ${serialize(value)}`);
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
// 删掉旧字段的多行子内容
|
|
91
|
+
let end = index + 1;
|
|
92
|
+
while (end < block.end) {
|
|
93
|
+
const current = (lines[end].match(/^\s*/) || [''])[0].length;
|
|
94
|
+
if (!lines[end].trim() || current > fieldIndent.length) { end += 1; continue; }
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
lines.splice(index, end - index, line);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function readMainConfig(ctx) {
|
|
101
|
+
const file = path.join(ctx.baseDir || process.cwd(), 'koishi.yml');
|
|
102
|
+
let text = '';
|
|
103
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch { /* ignore */ }
|
|
104
|
+
const values = {};
|
|
105
|
+
if (yaml) {
|
|
106
|
+
try {
|
|
107
|
+
const doc = yaml.load(text) || {};
|
|
108
|
+
const plugins = doc.plugins || {};
|
|
109
|
+
const key = Object.keys(plugins).find((name) => name.includes('@nestim/qq-group-manager'));
|
|
110
|
+
if (key) Object.assign(values, plugins[key] || {});
|
|
111
|
+
} catch { /* ignore */ }
|
|
112
|
+
}
|
|
113
|
+
return { values, file };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function writeMainValues(ctx, input) {
|
|
117
|
+
const schema = groups();
|
|
118
|
+
const allowed = new Set();
|
|
119
|
+
for (const group of schema) for (const field of group.fields) allowed.add(field.key);
|
|
120
|
+
const updates = {};
|
|
121
|
+
for (const [key, value] of Object.entries(input || {})) {
|
|
122
|
+
if (!allowed.has(key) || value === undefined) continue;
|
|
123
|
+
updates[key] = value;
|
|
124
|
+
}
|
|
125
|
+
if (!Object.keys(updates).length) throw new Error('没有可写入的主插件字段');
|
|
126
|
+
const file = path.join(ctx.baseDir || process.cwd(), 'koishi.yml');
|
|
127
|
+
const before = fs.readFileSync(file, 'utf8');
|
|
128
|
+
const lines = before.split(/\r?\n/);
|
|
129
|
+
for (const [key, value] of Object.entries(updates)) setField(lines, key, value);
|
|
130
|
+
const next = lines.join('\n');
|
|
131
|
+
if (yaml) yaml.load(next);
|
|
132
|
+
const stamp = new Date().toISOString().replace(/[-:T.Z]/g, '').slice(0, 14);
|
|
133
|
+
const backup = `${file}.bak-mainconfig-${stamp}`;
|
|
134
|
+
fs.copyFileSync(file, backup);
|
|
135
|
+
const tmp = `${file}.tmp`;
|
|
136
|
+
fs.writeFileSync(tmp, next, 'utf8');
|
|
137
|
+
fs.renameSync(tmp, file);
|
|
138
|
+
return { backup, keys: Object.keys(updates) };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
module.exports = { groups, readMainConfig, writeMainValues };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestim/koishi-plugin-qqgm-webui",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.17",
|
|
4
4
|
"description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"files": [
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
"README.md"
|
|
11
11
|
],
|
|
12
12
|
"license": "MIT",
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"js-yaml": "^4.1.0"
|
|
15
|
+
},
|
|
13
16
|
"keywords": ["koishi", "koishi-plugin", "qqgm", "webui", "admin"],
|
|
14
17
|
"peerDependencies": {
|
|
15
18
|
"koishi": "^4.18.7",
|
package/public/app.js
CHANGED
|
@@ -7,6 +7,7 @@ const titles = {
|
|
|
7
7
|
'main-ai': ['AI 回复', '主插件 · 触发、上下文、模型与人设'],
|
|
8
8
|
'main-moderation': ['自动管控', '主插件 · 复读、广告、刷屏、入群审核'],
|
|
9
9
|
'main-words': ['违禁词', '主插件 · 词库、正则与命中动作'],
|
|
10
|
+
'main-all': ['主插件配置', '自动读取主插件 Schema · 全部功能分组'],
|
|
10
11
|
config: ['附属配置', '所有附属插件都在这里;未安装的也会显示,可一键安装'],
|
|
11
12
|
logs: ['日志', '实时事件 + 文件日志;默认只保留最近 2000 行'],
|
|
12
13
|
};
|
|
@@ -117,6 +118,9 @@ async function initRealData() {
|
|
|
117
118
|
}
|
|
118
119
|
initRealData();
|
|
119
120
|
|
|
121
|
+
let memoryItems = [];
|
|
122
|
+
let memoryState = null;
|
|
123
|
+
|
|
120
124
|
async function initMemoryData() {
|
|
121
125
|
try {
|
|
122
126
|
const token = new URLSearchParams(location.search).get('token') || '';
|
|
@@ -124,29 +128,35 @@ async function initMemoryData() {
|
|
|
124
128
|
if (!res.ok) return;
|
|
125
129
|
const data = await res.json();
|
|
126
130
|
if (!data || !data.installed) return;
|
|
131
|
+
memoryItems = data.items || [];
|
|
132
|
+
memoryState = data;
|
|
127
133
|
const badge = $('.nav-badge');
|
|
128
134
|
if (badge) badge.textContent = String(data.total || 0);
|
|
129
135
|
const headP = $('#page-memory .page-head p');
|
|
130
136
|
if (headP) {
|
|
131
137
|
headP.textContent = data.useSqlite
|
|
132
138
|
? `当前 SQLite · ${data.total || 0} 条;切换前备份 koishi.yml`
|
|
133
|
-
: (data.
|
|
134
|
-
? `当前 MD · 识别到 ${data.md
|
|
139
|
+
: (data.md?.total
|
|
140
|
+
? `当前 MD · 识别到 ${data.md.total} 条旧记忆,可迁移到 SQLite`
|
|
135
141
|
: '当前 MD · 没有识别到旧记忆文件');
|
|
136
142
|
}
|
|
137
|
-
const
|
|
138
|
-
if (
|
|
139
|
-
|
|
143
|
+
const pill = $('#memoryBackendPill');
|
|
144
|
+
if (pill) {
|
|
145
|
+
pill.textContent = data.useSqlite ? `当前:SQLite · ${data.total || 0} 条` : `当前:MD · ${data.total || 0} 条`;
|
|
146
|
+
pill.classList.toggle('sqlite', !!data.useSqlite);
|
|
147
|
+
}
|
|
140
148
|
const migrateBtn = $('#migrateMemory');
|
|
149
|
+
const switchSqliteBtn = $('#switchSqlite');
|
|
141
150
|
const switchBtn = $('#switchMd');
|
|
142
|
-
if (migrateBtn) migrateBtn.hidden = !data.
|
|
143
|
-
if (
|
|
151
|
+
if (migrateBtn) migrateBtn.hidden = !(data.md?.total > 0);
|
|
152
|
+
if (switchSqliteBtn) switchSqliteBtn.hidden = !(data.backend === 'md' && (data.sqliteTotal || 0) > 0);
|
|
153
|
+
if (switchBtn) switchBtn.hidden = data.backend !== 'sqlite';
|
|
144
154
|
const note = $('#memoryBackend');
|
|
145
155
|
if (note) {
|
|
146
156
|
note.innerHTML = data.useSqlite
|
|
147
157
|
? '当前使用 SQLite;修改前会备份 <code>koishi.yml</code>,旧 MD 文件保留可回滚。'
|
|
148
|
-
: (data.
|
|
149
|
-
? `当前使用 MD,识别到 ${data.md
|
|
158
|
+
: (data.md?.total
|
|
159
|
+
? `当前使用 MD,识别到 ${data.md.total} 条旧记忆、${data.md.files?.length || 0} 个文件。先点“迁移 MD → SQLite”导入,再点“切换到 SQLite”;迁移不会自动切换。`
|
|
150
160
|
: '默认使用 MD;没有识别到旧记忆文件。');
|
|
151
161
|
}
|
|
152
162
|
const scopes = $('#memoryScopes');
|
|
@@ -168,20 +178,51 @@ async function initMemoryData() {
|
|
|
168
178
|
list.innerHTML = '<div class="memory-empty"><b>还没有记忆</b><p>主插件当前使用 MD 后端,或 SQLite 里还没有数据。<br>切到 SQLite 并执行 <code>memdb migrate</code> 后会出现在这里。</p></div>';
|
|
169
179
|
return;
|
|
170
180
|
}
|
|
171
|
-
list.innerHTML = data.items.map((item) => `
|
|
172
|
-
<article class="memory-item">
|
|
181
|
+
list.innerHTML = data.items.map((item, index) => `
|
|
182
|
+
<article class="memory-item" data-memory-index="${index}">
|
|
173
183
|
<div class="memory-head">
|
|
174
184
|
<span class="tag ${item.scope_kind === 'private' ? 'private' : 'group'}">${item.scope_kind === 'group' ? '群' : item.scope_kind === 'private' ? '私聊' : '全局'} ${item.scope_id || ''}</span>
|
|
175
185
|
<span class="tag soft">${item.category || '未分类'}</span>
|
|
176
186
|
<time>${new Date(item.created_at).toLocaleString('zh-CN', { hour12: false })}</time>
|
|
177
187
|
</div>
|
|
178
188
|
<p>${String(item.content || '').replace(/[<>&]/g, (c) => ({ '<': '<', '>': '>', '&': '&' }[c]))}</p>
|
|
179
|
-
<div class="memory-foot"><code>${item.label || '记忆'}</code><span>#${item.id}</span></div>
|
|
189
|
+
<div class="memory-foot"><code>${item.label || '记忆'}</code><span>#${item.id}</span><span class="memory-open">点击查看</span></div>
|
|
180
190
|
</article>`).join('');
|
|
181
191
|
} catch { /* 文件预览保留 mock */ }
|
|
182
192
|
}
|
|
183
193
|
initMemoryData();
|
|
184
194
|
|
|
195
|
+
function memoryScopeLabel(item) {
|
|
196
|
+
if (item.scope_kind === 'group') return `群 ${item.scope_id}`;
|
|
197
|
+
if (item.scope_kind === 'private') return `私聊 ${item.scope_id}`;
|
|
198
|
+
return '全局';
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
function openMemoryModal(index) {
|
|
202
|
+
const item = memoryItems[index];
|
|
203
|
+
if (!item) return;
|
|
204
|
+
const modal = $('#memoryModal');
|
|
205
|
+
if (!modal) return;
|
|
206
|
+
$('#memoryModalTitle').textContent = String(item.content || '记忆').slice(0, 24) || '记忆';
|
|
207
|
+
$('#memoryModalMeta').textContent = `${memoryScopeLabel(item)} · ${item.category || '未分类'} · ${new Date(item.created_at).toLocaleString('zh-CN', { hour12: false })}`;
|
|
208
|
+
$('#memoryModalContent').textContent = String(item.content || '');
|
|
209
|
+
$('#memoryModalSource').textContent = item.source === 'md' ? '来源:MD(只读;迁移到 SQLite 后可编辑)' : `来源:SQLite · #${item.id}`;
|
|
210
|
+
modal.hidden = false;
|
|
211
|
+
}
|
|
212
|
+
function closeMemoryModal() {
|
|
213
|
+
const modal = $('#memoryModal');
|
|
214
|
+
if (modal) modal.hidden = true;
|
|
215
|
+
}
|
|
216
|
+
$('#memoryList')?.addEventListener('click', (event) => {
|
|
217
|
+
const item = event.target.closest('[data-memory-index]');
|
|
218
|
+
if (item) openMemoryModal(Number(item.dataset.memoryIndex));
|
|
219
|
+
});
|
|
220
|
+
$('#memoryModalClose')?.addEventListener('click', closeMemoryModal);
|
|
221
|
+
$('#memoryModalClose2')?.addEventListener('click', closeMemoryModal);
|
|
222
|
+
$('#memoryModal')?.addEventListener('click', (event) => {
|
|
223
|
+
if (event.target.id === 'memoryModal') closeMemoryModal();
|
|
224
|
+
});
|
|
225
|
+
|
|
185
226
|
async function memoryPost(path, body) {
|
|
186
227
|
const token = new URLSearchParams(location.search).get('token') || '';
|
|
187
228
|
const res = await fetch(`${path}${token ? `?token=${encodeURIComponent(token)}` : ''}`, {
|
|
@@ -195,17 +236,37 @@ async function memoryPost(path, body) {
|
|
|
195
236
|
}
|
|
196
237
|
|
|
197
238
|
$('#migrateMemory')?.addEventListener('click', async () => {
|
|
198
|
-
if (!confirm('把 Memory_*.md 导入 SQLite
|
|
239
|
+
if (!confirm('把 Memory_*.md 导入 SQLite?\\n\\n只导入,不切换后端;不会删除 MD,也不会自动重启。')) return;
|
|
199
240
|
try {
|
|
200
241
|
showToast('正在迁移…');
|
|
201
|
-
const data = await memoryPost('api/memory/migrate', { switchBackend:
|
|
202
|
-
showToast(
|
|
242
|
+
const data = await memoryPost('api/memory/migrate', { switchBackend: false });
|
|
243
|
+
showToast(`已导入 ${data.imported} 条到 SQLite;现在可以点“切换到 SQLite”`);
|
|
203
244
|
setTimeout(() => location.reload(), 1800);
|
|
204
245
|
} catch (error) {
|
|
205
246
|
showToast(`迁移失败:${String(error.message || error)}`);
|
|
206
247
|
}
|
|
207
248
|
});
|
|
208
249
|
|
|
250
|
+
$('#switchSqlite')?.addEventListener('click', async () => {
|
|
251
|
+
const sqliteTotal = memoryState?.sqliteTotal || 0;
|
|
252
|
+
if (!sqliteTotal) {
|
|
253
|
+
showToast('SQLite 里还没有数据:请先点“迁移 MD → SQLite”');
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const mdTotal = memoryState?.md?.total || 0;
|
|
257
|
+
const extra = mdTotal
|
|
258
|
+
? `\\n\\n注意:还有 ${mdTotal} 条 MD 未确认迁移,切换后主插件只读 SQLite。`
|
|
259
|
+
: '';
|
|
260
|
+
if (!confirm(`切换主插件记忆后端到 sqlite?${extra}\\n\\n会先备份 koishi.yml,重启后生效。`)) return;
|
|
261
|
+
try {
|
|
262
|
+
await memoryPost('api/config/memory', { backend: 'sqlite' });
|
|
263
|
+
showToast('已切换,重启 Koishi 后生效');
|
|
264
|
+
setTimeout(() => location.reload(), 1600);
|
|
265
|
+
} catch (error) {
|
|
266
|
+
showToast(`切换失败:${String(error.message || error)}`);
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
|
|
209
270
|
$('#switchMd')?.addEventListener('click', async () => {
|
|
210
271
|
if (!confirm('把主插件记忆后端切回 md?SQLite 数据不会删除,可随时切回。')) return;
|
|
211
272
|
try {
|
|
@@ -235,6 +296,87 @@ async function installPackage(pkg) {
|
|
|
235
296
|
}
|
|
236
297
|
$('#installMonitor')?.addEventListener('click', () => installPackage('@nestim/koishi-plugin-qqgm-monitor'));
|
|
237
298
|
|
|
299
|
+
// 主插件完整配置:Schema 驱动
|
|
300
|
+
let mainSchema = null;
|
|
301
|
+
let activeMainGroup = 0;
|
|
302
|
+
|
|
303
|
+
function tokenQuery() {
|
|
304
|
+
const token = new URLSearchParams(location.search).get('token') || '';
|
|
305
|
+
return token ? `?token=${encodeURIComponent(token)}` : '';
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function renderMainGroup(index) {
|
|
309
|
+
if (!mainSchema || !mainSchema.groups[index]) return;
|
|
310
|
+
activeMainGroup = index;
|
|
311
|
+
$$('#mainSchemaNav .config-plugin').forEach((btn, i) => btn.classList.toggle('active', i === index));
|
|
312
|
+
const group = mainSchema.groups[index];
|
|
313
|
+
const form = $('#mainSchemaForm');
|
|
314
|
+
if (!form) return;
|
|
315
|
+
form.innerHTML = `<div class="form-head"><div><h2>${group.title}</h2><p>${group.fields.length} 个字段 · 主插件 Schema</p></div><span class="status ok">运行中</span></div>`
|
|
316
|
+
+ group.fields.map((field) => {
|
|
317
|
+
const value = mainSchema.values[field.key] ?? field.default;
|
|
318
|
+
let control = '';
|
|
319
|
+
if (field.type === 'boolean') {
|
|
320
|
+
control = `<input type="checkbox" data-key="${field.key}" data-kind="boolean" ${value ? 'checked' : ''} />`;
|
|
321
|
+
} else if (field.type === 'union') {
|
|
322
|
+
const options = (field.options.length ? field.options : [String(value || '')]).map((opt) => `<option ${String(value) === opt ? 'selected' : ''}>${opt}</option>`).join('');
|
|
323
|
+
control = `<select data-key="${field.key}" data-kind="string">${options}</select>`;
|
|
324
|
+
} else if (field.type === 'number') {
|
|
325
|
+
control = `<input type="number" data-key="${field.key}" data-kind="number" value="${value ?? field.default ?? 0}" />`;
|
|
326
|
+
} else if (field.json) {
|
|
327
|
+
control = `<textarea rows="4" data-key="${field.key}" data-kind="json">${JSON.stringify(value ?? field.default ?? (field.type === 'array' ? [] : {}), null, 2)}</textarea>`;
|
|
328
|
+
} else {
|
|
329
|
+
control = `<input ${field.sensitive ? 'type="password"' : ''} data-key="${field.key}" data-kind="string" value="${String(value ?? field.default ?? '').replace(/"/g, '"')}" />`;
|
|
330
|
+
}
|
|
331
|
+
return `<div class="schema-field"><label class="field"><span>${field.key}</span>${control}</label><p class="form-hint">${field.description || ''}</p></div>`;
|
|
332
|
+
}).join('')
|
|
333
|
+
+ `<div class="form-foot"><span>保存只改当前组字段;写前备份 koishi.yml</span><div><button class="btn primary" id="saveMainGroup">保存本组</button></div></div>`;
|
|
334
|
+
$('#saveMainGroup')?.addEventListener('click', () => saveMainGroup(group));
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
async function saveMainGroup(group) {
|
|
338
|
+
const values = {};
|
|
339
|
+
for (const field of group.fields) {
|
|
340
|
+
const input = $(`[data-key="${field.key}"]`);
|
|
341
|
+
if (!input) continue;
|
|
342
|
+
if (field.type === 'boolean') values[field.key] = !!input.checked;
|
|
343
|
+
else if (field.type === 'number') values[field.key] = Number(input.value);
|
|
344
|
+
else if (field.json) {
|
|
345
|
+
try { values[field.key] = JSON.parse(input.value); }
|
|
346
|
+
catch { showToast(`${field.key} 的 JSON 格式不对`); return; }
|
|
347
|
+
} else values[field.key] = input.value;
|
|
348
|
+
}
|
|
349
|
+
try {
|
|
350
|
+
const res = await fetch(`api/config/main${tokenQuery()}`, {
|
|
351
|
+
method: 'POST',
|
|
352
|
+
headers: { 'content-type': 'application/json' },
|
|
353
|
+
body: JSON.stringify({ values }),
|
|
354
|
+
});
|
|
355
|
+
const data = await res.json();
|
|
356
|
+
if (!res.ok || !data.ok) throw new Error(data.error || `HTTP ${res.status}`);
|
|
357
|
+
showToast(`已保存 ${data.keys.length} 项,重启 Koishi 后生效`);
|
|
358
|
+
} catch (error) {
|
|
359
|
+
showToast(`保存失败:${String(error.message || error)}`);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
async function initMainSchema() {
|
|
364
|
+
try {
|
|
365
|
+
const res = await fetch(`api/config/main${tokenQuery()}`, { cache: 'no-store' });
|
|
366
|
+
if (!res.ok) return;
|
|
367
|
+
mainSchema = await res.json();
|
|
368
|
+
if (!mainSchema.available) {
|
|
369
|
+
$('#mainSchemaForm').innerHTML = '<div class="schema-loading">未找到主插件 Config Schema</div>';
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
$('#mainSchemaNav').innerHTML = '<div class="config-group-label">主插件分组</div>'
|
|
373
|
+
+ mainSchema.groups.map((group, index) => `<button class="config-plugin ${index === 0 ? 'active' : ''}" data-main-group="${index}"><span class="plugin-ico slate">${index + 1}</span><div><b>${group.title}</b><small>${group.fields.length} 项</small></div></button>`).join('');
|
|
374
|
+
$$('#mainSchemaNav .config-plugin').forEach((btn) => btn.addEventListener('click', () => renderMainGroup(Number(btn.dataset.mainGroup))));
|
|
375
|
+
renderMainGroup(0);
|
|
376
|
+
} catch { /* leave loading */ }
|
|
377
|
+
}
|
|
378
|
+
initMainSchema();
|
|
379
|
+
|
|
238
380
|
// memory backend note
|
|
239
381
|
const memoryNote = $('#memoryBackend');
|
|
240
382
|
if (memoryNote) {
|
package/public/index.html
CHANGED
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
<button class="nav-sub" data-page="main-ai">AI 回复</button>
|
|
42
42
|
<button class="nav-sub" data-page="main-moderation">自动管控</button>
|
|
43
43
|
<button class="nav-sub" data-page="main-words">违禁词</button>
|
|
44
|
+
<button class="nav-sub" data-page="main-all">完整配置</button>
|
|
44
45
|
<button class="nav-item" data-page="config">
|
|
45
46
|
<svg viewBox="0 0 24 24"><path d="M12 15.5A3.5 3.5 0 1 0 12 8.5a3.5 3.5 0 0 0 0 7Zm8-3.5c0-.6-.06-1.16-.18-1.7l2.02-1.58-2-3.46-2.38.96a8.2 8.2 0 0 0-2.94-1.7L14.2 2h-4.4l-.32 2.52a8.2 8.2 0 0 0-2.94 1.7L4.16 5.26l-2 3.46 2.02 1.58A8.6 8.6 0 0 0 4 12c0 .6.06 1.16.18 1.7l-2.02 1.58 2 3.46 2.38-.96a8.2 8.2 0 0 0 2.94 1.7L9.8 22h4.4l.32-2.52a8.2 8.2 0 0 0 2.94-1.7l2.38.96 2-3.46-2.02-1.58c.12-.54.18-1.1.18-1.7Z"/></svg>
|
|
46
47
|
<span>附属配置</span>
|
|
@@ -319,9 +320,10 @@
|
|
|
319
320
|
<div class="page-head">
|
|
320
321
|
<div><h1>记忆库</h1><p>默认 MD;已安装 qqgm-memory,切换主插件后端后可用 SQLite</p></div>
|
|
321
322
|
<div class="head-actions">
|
|
322
|
-
<
|
|
323
|
+
<span class="backend-pill" id="memoryBackendPill">当前:MD</span>
|
|
324
|
+
<button class="btn ghost" id="migrateMemory">迁移 MD → SQLite</button>
|
|
325
|
+
<button class="btn primary" id="switchSqlite">切换到 SQLite</button>
|
|
323
326
|
<button class="btn ghost" id="switchMd">切回 MD</button>
|
|
324
|
-
<button class="btn primary" id="migrateMemory">迁移到 SQLite</button>
|
|
325
327
|
</div>
|
|
326
328
|
</div>
|
|
327
329
|
<div class="memory-note" id="memoryBackend">默认使用 MD,兼容现有记忆文件;安装数据库附属插件 <b>qqgm-memory</b> 后可切换 SQLite,迁移前会先备份。</div>
|
|
@@ -351,6 +353,14 @@
|
|
|
351
353
|
</div>
|
|
352
354
|
</section>
|
|
353
355
|
|
|
356
|
+
<section class="page" id="page-main-all">
|
|
357
|
+
<div class="page-head"><div><h1>主插件配置</h1><p>自动读取主插件 Schema · 全部功能分组</p></div><button class="btn primary" id="saveMainAll">保存当前组</button></div>
|
|
358
|
+
<div class="config-layout">
|
|
359
|
+
<aside class="card config-nav" id="mainSchemaNav"><div class="schema-loading">读取 Schema…</div></aside>
|
|
360
|
+
<div class="card config-form" id="mainSchemaForm"><div class="schema-loading">读取配置…</div></div>
|
|
361
|
+
</div>
|
|
362
|
+
</section>
|
|
363
|
+
|
|
354
364
|
<section class="page" id="page-main-group">
|
|
355
365
|
<div class="page-head"><div><h1>群管</h1><p>主插件 · 命令、权限、入群与撤回</p></div><button class="btn primary" data-toast="群管配置已保存(原型)">保存群管配置</button></div>
|
|
356
366
|
<div class="config-layout">
|
|
@@ -575,6 +585,13 @@
|
|
|
575
585
|
</main>
|
|
576
586
|
</div>
|
|
577
587
|
<div class="toast" id="toast"></div>
|
|
588
|
+
<div class="modal" id="memoryModal" hidden>
|
|
589
|
+
<div class="modal-card">
|
|
590
|
+
<div class="modal-head"><div><h3 id="memoryModalTitle">记忆</h3><p id="memoryModalMeta"></p></div><button class="icon-btn" id="memoryModalClose">✕</button></div>
|
|
591
|
+
<div class="modal-body" id="memoryModalContent"></div>
|
|
592
|
+
<div class="modal-foot"><span id="memoryModalSource"></span><button class="btn ghost" id="memoryModalClose2">关闭</button></div>
|
|
593
|
+
</div>
|
|
594
|
+
</div>
|
|
578
595
|
<script src="./app.js"></script>
|
|
579
596
|
</body>
|
|
580
597
|
</html>
|
package/public/styles.css
CHANGED
|
@@ -62,7 +62,7 @@ svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round
|
|
|
62
62
|
.icon-btn:hover { background: rgba(255,255,255,.06); color: #fff; }
|
|
63
63
|
|
|
64
64
|
/* main */
|
|
65
|
-
.main { min-width: 0; display: flex; flex-direction: column; background: var(--bg); }
|
|
65
|
+
.main { min-width: 0; min-height: 0; display: flex; flex-direction: column; background: var(--bg); overflow: hidden; }
|
|
66
66
|
.topbar { height: 64px; flex: 0 0 64px; display: flex; align-items: center; justify-content: space-between; padding: 0 28px; background: rgba(255,255,255,.82); border-bottom: 1px solid var(--line); backdrop-filter: blur(12px); }
|
|
67
67
|
.crumb { display: flex; align-items: center; gap: 9px; font-size: 13px; }
|
|
68
68
|
.crumb-muted { color: #9aa6b7; }
|
|
@@ -77,7 +77,7 @@ svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round
|
|
|
77
77
|
.pill.online { color: #17845b; background: #e8f7f0; }
|
|
78
78
|
.pill.online i { width: 6px; height: 6px; background: #21b47b; border-radius: 50%; }
|
|
79
79
|
.top-actions .icon-btn { color: #7d8ba1; background: #fff; border: 1px solid var(--line); font-size: 15px; }
|
|
80
|
-
.content { flex: 1; overflow: auto; padding: 26px 28px 34px; }
|
|
80
|
+
.content { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; padding: 26px 28px 34px; }
|
|
81
81
|
.page { display: none; animation: fade .18s ease; }
|
|
82
82
|
.page.active { display: block; }
|
|
83
83
|
@keyframes fade { from { opacity: .2; transform: translateY(3px); } to { opacity: 1; transform: none; } }
|
|
@@ -303,6 +303,25 @@ code { background: #f2f5f9; color: #55657d; border-radius: 4px; padding: 1px 4px
|
|
|
303
303
|
.word-list { display: flex; flex-wrap: wrap; gap: 7px; }
|
|
304
304
|
.word-list span { display: inline-flex; align-items: center; gap: 5px; background: #f3f6fb; border: 1px solid #e6ecf5; color: #53647b; border-radius: 6px; padding: 5px 8px; font-size: 10.5px; }
|
|
305
305
|
.word-list button { color: #a6b2c2; font-size: 12px; line-height: 1; }
|
|
306
|
+
.schema-field { padding: 11px 0; border-bottom: 1px solid #f1f4f8; }
|
|
307
|
+
.schema-field .field > span { flex: 0 0 200px; font-family: Consolas, monospace; font-size: 10.5px; color: #3f5270; }
|
|
308
|
+
.schema-field .field input:not([type=checkbox]), .schema-field .field select, .schema-field .field textarea { width: 100%; }
|
|
309
|
+
.form-hint { margin: 5px 0 0 200px; font-size: 9.5px; color: #9aa7b8; line-height: 1.5; }
|
|
310
|
+
.schema-loading { padding: 24px; color: #97a4b6; font-size: 11px; }
|
|
311
|
+
.backend-pill.sqlite { color: #167a5b; background: #e9f8f1; border-color: #cfeee0; }
|
|
312
|
+
.memory-item { cursor: pointer; border-radius: 8px; transition: background .15s; }
|
|
313
|
+
.memory-item:hover { background: #fafcff; }
|
|
314
|
+
.memory-open { margin-left: auto; color: #6c8fc4; }
|
|
315
|
+
.backend-pill { font-size: 10px; color: #4f6f9f; background: #edf3fc; border: 1px solid #dbe7f7; border-radius: 20px; padding: 5px 10px; }
|
|
316
|
+
.modal { position: fixed; inset: 0; background: rgba(12,22,38,.45); display: grid; place-items: center; z-index: 30; }
|
|
317
|
+
.modal[hidden] { display: none; }
|
|
318
|
+
.modal-card { width: min(620px, 90vw); max-height: 80vh; overflow: auto; background: #fff; border-radius: 14px; box-shadow: 0 24px 70px rgba(15,25,45,.25); padding: 18px 20px; }
|
|
319
|
+
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 1px solid var(--line); padding-bottom: 12px; }
|
|
320
|
+
.modal-head h3 { margin: 0 0 4px; font-size: 16px; }
|
|
321
|
+
.modal-head p { margin: 0; font-size: 10px; color: #97a4b6; }
|
|
322
|
+
.modal-head .icon-btn { color: #8c9aae; background: #f4f7fb; }
|
|
323
|
+
.modal-body { padding: 15px 0; font-size: 12px; line-height: 1.75; color: #405066; white-space: pre-wrap; }
|
|
324
|
+
.modal-foot { display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--line); padding-top: 12px; font-size: 10px; color: #98a5b6; }
|
|
306
325
|
.toast { position: fixed; left: 50%; bottom: 45px; transform: translate(-50%, 12px); background: #18263a; color: #fff; font-size: 11.5px; padding: 9px 15px; border-radius: 8px; opacity: 0; pointer-events: none; transition: .2s; box-shadow: 0 8px 24px rgba(15,25,42,.2); z-index: 20; }
|
|
307
326
|
.toast.show { opacity: 1; transform: translate(-50%, 0); }
|
|
308
327
|
|