@nestim/koishi-plugin-qqgm-webui 0.2.16 → 0.2.18

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 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.16',
118
+ version: '0.2.18',
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.16",
3
+ "version": "0.2.18",
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
@@ -3,10 +3,7 @@ const titles = {
3
3
  plugins: ['插件', '自动扫描 koishi.yml 与 node_modules,只显示附属插件'],
4
4
  monitor: ['监控', '总失败率与分群失败率;延迟按回复消息拆阶段'],
5
5
  memory: ['记忆库', '默认 MD;安装 qqgm-memory 后可切换 SQLite'],
6
- 'main-group': ['群管', '主插件 · 命令、权限、入群与撤回'],
7
- 'main-ai': ['AI 回复', '主插件 · 触发、上下文、模型与人设'],
8
- 'main-moderation': ['自动管控', '主插件 · 复读、广告、刷屏、入群审核'],
9
- 'main-words': ['违禁词', '主插件 · 词库、正则与命中动作'],
6
+ 'main-schema': ['主插件配置', '按主插件 Schema 分组,每组独立页面'],
10
7
  config: ['附属配置', '所有附属插件都在这里;未安装的也会显示,可一键安装'],
11
8
  logs: ['日志', '实时事件 + 文件日志;默认只保留最近 2000 行'],
12
9
  };
@@ -117,6 +114,9 @@ async function initRealData() {
117
114
  }
118
115
  initRealData();
119
116
 
117
+ let memoryItems = [];
118
+ let memoryState = null;
119
+
120
120
  async function initMemoryData() {
121
121
  try {
122
122
  const token = new URLSearchParams(location.search).get('token') || '';
@@ -124,29 +124,35 @@ async function initMemoryData() {
124
124
  if (!res.ok) return;
125
125
  const data = await res.json();
126
126
  if (!data || !data.installed) return;
127
+ memoryItems = data.items || [];
128
+ memoryState = data;
127
129
  const badge = $('.nav-badge');
128
130
  if (badge) badge.textContent = String(data.total || 0);
129
131
  const headP = $('#page-memory .page-head p');
130
132
  if (headP) {
131
133
  headP.textContent = data.useSqlite
132
134
  ? `当前 SQLite · ${data.total || 0} 条;切换前备份 koishi.yml`
133
- : (data.needsMigration
134
- ? `当前 MD · 识别到 ${data.md?.total || 0} 条旧记忆,可迁移到 SQLite`
135
+ : (data.md?.total
136
+ ? `当前 MD · 识别到 ${data.md.total} 条旧记忆,可迁移到 SQLite`
135
137
  : '当前 MD · 没有识别到旧记忆文件');
136
138
  }
137
- const segButtons = $$('#page-memory .segmented button');
138
- if (segButtons[0]) segButtons[0].classList.toggle('active', !data.useSqlite);
139
- if (segButtons[1]) segButtons[1].classList.toggle('active', !!data.useSqlite);
139
+ const pill = $('#memoryBackendPill');
140
+ if (pill) {
141
+ pill.textContent = data.useSqlite ? `当前:SQLite · ${data.total || 0} 条` : `当前:MD · ${data.total || 0} 条`;
142
+ pill.classList.toggle('sqlite', !!data.useSqlite);
143
+ }
140
144
  const migrateBtn = $('#migrateMemory');
145
+ const switchSqliteBtn = $('#switchSqlite');
141
146
  const switchBtn = $('#switchMd');
142
- if (migrateBtn) migrateBtn.hidden = !data.needsMigration;
143
- if (switchBtn) switchBtn.hidden = !data.useSqlite;
147
+ if (migrateBtn) migrateBtn.hidden = !(data.md?.total > 0);
148
+ if (switchSqliteBtn) switchSqliteBtn.hidden = !(data.backend === 'md' && (data.sqliteTotal || 0) > 0);
149
+ if (switchBtn) switchBtn.hidden = data.backend !== 'sqlite';
144
150
  const note = $('#memoryBackend');
145
151
  if (note) {
146
152
  note.innerHTML = data.useSqlite
147
153
  ? '当前使用 SQLite;修改前会备份 <code>koishi.yml</code>,旧 MD 文件保留可回滚。'
148
- : (data.needsMigration
149
- ? `当前使用 MD,识别到 ${data.md?.total || 0} 条旧记忆、${data.md?.files?.length || 0} 个文件。迁移会导入 SQLite 并切换后端,迁移前不改 MD。`
154
+ : (data.md?.total
155
+ ? `当前使用 MD,识别到 ${data.md.total} 条旧记忆、${data.md.files?.length || 0} 个文件。先点“迁移 MD → SQLite”导入,再点“切换到 SQLite”;迁移不会自动切换。`
150
156
  : '默认使用 MD;没有识别到旧记忆文件。');
151
157
  }
152
158
  const scopes = $('#memoryScopes');
@@ -168,20 +174,51 @@ async function initMemoryData() {
168
174
  list.innerHTML = '<div class="memory-empty"><b>还没有记忆</b><p>主插件当前使用 MD 后端,或 SQLite 里还没有数据。<br>切到 SQLite 并执行 <code>memdb migrate</code> 后会出现在这里。</p></div>';
169
175
  return;
170
176
  }
171
- list.innerHTML = data.items.map((item) => `
172
- <article class="memory-item">
177
+ list.innerHTML = data.items.map((item, index) => `
178
+ <article class="memory-item" data-memory-index="${index}">
173
179
  <div class="memory-head">
174
180
  <span class="tag ${item.scope_kind === 'private' ? 'private' : 'group'}">${item.scope_kind === 'group' ? '群' : item.scope_kind === 'private' ? '私聊' : '全局'} ${item.scope_id || ''}</span>
175
181
  <span class="tag soft">${item.category || '未分类'}</span>
176
182
  <time>${new Date(item.created_at).toLocaleString('zh-CN', { hour12: false })}</time>
177
183
  </div>
178
184
  <p>${String(item.content || '').replace(/[<>&]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;' }[c]))}</p>
179
- <div class="memory-foot"><code>${item.label || '记忆'}</code><span>#${item.id}</span></div>
185
+ <div class="memory-foot"><code>${item.label || '记忆'}</code><span>#${item.id}</span><span class="memory-open">点击查看</span></div>
180
186
  </article>`).join('');
181
187
  } catch { /* 文件预览保留 mock */ }
182
188
  }
183
189
  initMemoryData();
184
190
 
191
+ function memoryScopeLabel(item) {
192
+ if (item.scope_kind === 'group') return `群 ${item.scope_id}`;
193
+ if (item.scope_kind === 'private') return `私聊 ${item.scope_id}`;
194
+ return '全局';
195
+ }
196
+
197
+ function openMemoryModal(index) {
198
+ const item = memoryItems[index];
199
+ if (!item) return;
200
+ const modal = $('#memoryModal');
201
+ if (!modal) return;
202
+ $('#memoryModalTitle').textContent = String(item.content || '记忆').slice(0, 24) || '记忆';
203
+ $('#memoryModalMeta').textContent = `${memoryScopeLabel(item)} · ${item.category || '未分类'} · ${new Date(item.created_at).toLocaleString('zh-CN', { hour12: false })}`;
204
+ $('#memoryModalContent').textContent = String(item.content || '');
205
+ $('#memoryModalSource').textContent = item.source === 'md' ? '来源:MD(只读;迁移到 SQLite 后可编辑)' : `来源:SQLite · #${item.id}`;
206
+ modal.hidden = false;
207
+ }
208
+ function closeMemoryModal() {
209
+ const modal = $('#memoryModal');
210
+ if (modal) modal.hidden = true;
211
+ }
212
+ $('#memoryList')?.addEventListener('click', (event) => {
213
+ const item = event.target.closest('[data-memory-index]');
214
+ if (item) openMemoryModal(Number(item.dataset.memoryIndex));
215
+ });
216
+ $('#memoryModalClose')?.addEventListener('click', closeMemoryModal);
217
+ $('#memoryModalClose2')?.addEventListener('click', closeMemoryModal);
218
+ $('#memoryModal')?.addEventListener('click', (event) => {
219
+ if (event.target.id === 'memoryModal') closeMemoryModal();
220
+ });
221
+
185
222
  async function memoryPost(path, body) {
186
223
  const token = new URLSearchParams(location.search).get('token') || '';
187
224
  const res = await fetch(`${path}${token ? `?token=${encodeURIComponent(token)}` : ''}`, {
@@ -195,17 +232,37 @@ async function memoryPost(path, body) {
195
232
  }
196
233
 
197
234
  $('#migrateMemory')?.addEventListener('click', async () => {
198
- if (!confirm('把 Memory_*.md 导入 SQLite,并把主插件后端切到 sqlite?\\n\\n会先备份 koishi.yml,MD 文件不改。')) return;
235
+ if (!confirm('把 Memory_*.md 导入 SQLite?\\n\\n只导入,不切换后端;不会删除 MD,也不会自动重启。')) return;
199
236
  try {
200
237
  showToast('正在迁移…');
201
- const data = await memoryPost('api/memory/migrate', { switchBackend: true });
202
- showToast(`迁移 ${data.imported} 条,已切换;重启后生效`);
238
+ const data = await memoryPost('api/memory/migrate', { switchBackend: false });
239
+ showToast(`已导入 ${data.imported} 条到 SQLite;现在可以点“切换到 SQLite”`);
203
240
  setTimeout(() => location.reload(), 1800);
204
241
  } catch (error) {
205
242
  showToast(`迁移失败:${String(error.message || error)}`);
206
243
  }
207
244
  });
208
245
 
246
+ $('#switchSqlite')?.addEventListener('click', async () => {
247
+ const sqliteTotal = memoryState?.sqliteTotal || 0;
248
+ if (!sqliteTotal) {
249
+ showToast('SQLite 里还没有数据:请先点“迁移 MD → SQLite”');
250
+ return;
251
+ }
252
+ const mdTotal = memoryState?.md?.total || 0;
253
+ const extra = mdTotal
254
+ ? `\\n\\n注意:还有 ${mdTotal} 条 MD 未确认迁移,切换后主插件只读 SQLite。`
255
+ : '';
256
+ if (!confirm(`切换主插件记忆后端到 sqlite?${extra}\\n\\n会先备份 koishi.yml,重启后生效。`)) return;
257
+ try {
258
+ await memoryPost('api/config/memory', { backend: 'sqlite' });
259
+ showToast('已切换,重启 Koishi 后生效');
260
+ setTimeout(() => location.reload(), 1600);
261
+ } catch (error) {
262
+ showToast(`切换失败:${String(error.message || error)}`);
263
+ }
264
+ });
265
+
209
266
  $('#switchMd')?.addEventListener('click', async () => {
210
267
  if (!confirm('把主插件记忆后端切回 md?SQLite 数据不会删除,可随时切回。')) return;
211
268
  try {
@@ -235,6 +292,105 @@ async function installPackage(pkg) {
235
292
  }
236
293
  $('#installMonitor')?.addEventListener('click', () => installPackage('@nestim/koishi-plugin-qqgm-monitor'));
237
294
 
295
+ // 主插件完整配置:Schema 驱动
296
+ let mainSchema = null;
297
+ let activeMainGroup = 0;
298
+
299
+ function tokenQuery() {
300
+ const token = new URLSearchParams(location.search).get('token') || '';
301
+ return token ? `?token=${encodeURIComponent(token)}` : '';
302
+ }
303
+
304
+ function openMainGroup(index) {
305
+ if (!mainSchema || !mainSchema.groups[index]) return;
306
+ go('main-schema');
307
+ renderMainGroup(index);
308
+ }
309
+
310
+ function renderMainGroup(index) {
311
+ if (!mainSchema || !mainSchema.groups[index]) return;
312
+ activeMainGroup = index;
313
+ const group = mainSchema.groups[index];
314
+ const form = $('#mainSchemaForm');
315
+ if (!form) return;
316
+ const title = $('#mainSchemaTitle');
317
+ const subtitle = $('#mainSchemaSubtitle');
318
+ if (title) title.textContent = group.title;
319
+ if (subtitle) subtitle.textContent = `主插件 · ${group.fields.length} 项 · 来自 Schema`;
320
+ $('#pageTitle').textContent = group.title;
321
+ $$('#mainSchemaSubnav .nav-sub').forEach((btn, i) => btn.classList.toggle('active', i === index));
322
+ form.innerHTML = `<div class="form-head"><div><h2>${group.title}</h2><p>${group.fields.length} 个字段 · 与 Koishi 配置页同源</p></div><span class="status ok">运行中</span></div>`
323
+ + group.fields.map((field) => {
324
+ const value = mainSchema.values[field.key] ?? field.default;
325
+ let control = '';
326
+ if (field.type === 'boolean') {
327
+ control = `<input type="checkbox" data-key="${field.key}" data-kind="boolean" ${value ? 'checked' : ''} />`;
328
+ } else if (field.type === 'union') {
329
+ const options = (field.options.length ? field.options : [String(value || '')]).map((opt) => `<option ${String(value) === opt ? 'selected' : ''}>${opt}</option>`).join('');
330
+ control = `<select data-key="${field.key}" data-kind="string">${options}</select>`;
331
+ } else if (field.type === 'number') {
332
+ control = `<input type="number" data-key="${field.key}" data-kind="number" value="${value ?? field.default ?? 0}" />`;
333
+ } else if (field.json) {
334
+ control = `<textarea rows="4" data-key="${field.key}" data-kind="json">${JSON.stringify(value ?? field.default ?? (field.type === 'array' ? [] : {}), null, 2)}</textarea>`;
335
+ } else {
336
+ control = `<input ${field.sensitive ? 'type="password"' : ''} data-key="${field.key}" data-kind="string" value="${String(value ?? field.default ?? '').replace(/"/g, '&quot;')}" />`;
337
+ }
338
+ return `<div class="schema-field"><label class="field"><span>${field.key}</span>${control}</label><p class="form-hint">${field.description || ''}</p></div>`;
339
+ }).join('')
340
+ + `<div class="form-foot"><span>保存只改本组字段;写前备份 koishi.yml,重启后生效</span></div>`;
341
+ const save = $('#saveMainSchemaGroup');
342
+ if (save) save.onclick = () => saveMainGroup(group);
343
+ }
344
+
345
+ async function saveMainGroup(group) {
346
+ const values = {};
347
+ for (const field of group.fields) {
348
+ const input = $(`[data-key="${field.key}"]`);
349
+ if (!input) continue;
350
+ if (field.type === 'boolean') values[field.key] = !!input.checked;
351
+ else if (field.type === 'number') values[field.key] = Number(input.value);
352
+ else if (field.json) {
353
+ try { values[field.key] = JSON.parse(input.value); }
354
+ catch { showToast(`${field.key} 的 JSON 格式不对`); return; }
355
+ } else values[field.key] = input.value;
356
+ }
357
+ try {
358
+ const res = await fetch(`api/config/main${tokenQuery()}`, {
359
+ method: 'POST',
360
+ headers: { 'content-type': 'application/json' },
361
+ body: JSON.stringify({ values }),
362
+ });
363
+ const data = await res.json();
364
+ if (!res.ok || !data.ok) throw new Error(data.error || `HTTP ${res.status}`);
365
+ showToast(`已保存 ${data.keys.length} 项,重启 Koishi 后生效`);
366
+ } catch (error) {
367
+ showToast(`保存失败:${String(error.message || error)}`);
368
+ }
369
+ }
370
+
371
+ async function initMainSchema() {
372
+ try {
373
+ const res = await fetch(`api/config/main${tokenQuery()}`, { cache: 'no-store' });
374
+ if (!res.ok) return;
375
+ mainSchema = await res.json();
376
+ if (!mainSchema.available) {
377
+ $('#mainSchemaForm').innerHTML = '<div class="schema-loading">未找到主插件 Config Schema</div>';
378
+ return;
379
+ }
380
+ const subnav = $('#mainSchemaSubnav');
381
+ if (subnav) {
382
+ subnav.innerHTML = mainSchema.groups.map((group, index) => `<button class="nav-sub" data-main-group="${index}">${group.title}<span class="sub-count">${group.fields.length}</span></button>`).join('');
383
+ $$('#mainSchemaSubnav .nav-sub').forEach((btn) => btn.addEventListener('click', () => openMainGroup(Number(btn.dataset.mainGroup))));
384
+ }
385
+ const params = new URLSearchParams(location.search);
386
+ const groupParam = Number(params.get('group'));
387
+ if (document.querySelector('#page-main-schema.active')) {
388
+ openMainGroup(Number.isInteger(groupParam) && groupParam >= 0 && groupParam < mainSchema.groups.length ? groupParam : 0);
389
+ }
390
+ } catch { /* leave loading */ }
391
+ }
392
+ initMainSchema();
393
+
238
394
  // memory backend note
239
395
  const memoryNote = $('#memoryBackend');
240
396
  if (memoryNote) {
package/public/index.html CHANGED
@@ -37,10 +37,7 @@
37
37
  <span class="nav-badge">128</span>
38
38
  </button>
39
39
  <div class="nav-section">主插件</div>
40
- <button class="nav-sub" data-page="main-group">群管</button>
41
- <button class="nav-sub" data-page="main-ai">AI 回复</button>
42
- <button class="nav-sub" data-page="main-moderation">自动管控</button>
43
- <button class="nav-sub" data-page="main-words">违禁词</button>
40
+ <div class="main-schema-subnav" id="mainSchemaSubnav"></div>
44
41
  <button class="nav-item" data-page="config">
45
42
  <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
43
  <span>附属配置</span>
@@ -319,9 +316,10 @@
319
316
  <div class="page-head">
320
317
  <div><h1>记忆库</h1><p>默认 MD;已安装 qqgm-memory,切换主插件后端后可用 SQLite</p></div>
321
318
  <div class="head-actions">
322
- <div class="segmented"><button class="active">MD</button><button data-toast="SQLite 需要迁移后切换">SQLite</button></div>
319
+ <span class="backend-pill" id="memoryBackendPill">当前:MD</span>
320
+ <button class="btn ghost" id="migrateMemory">迁移 MD → SQLite</button>
321
+ <button class="btn primary" id="switchSqlite">切换到 SQLite</button>
323
322
  <button class="btn ghost" id="switchMd">切回 MD</button>
324
- <button class="btn primary" id="migrateMemory">迁移到 SQLite</button>
325
323
  </div>
326
324
  </div>
327
325
  <div class="memory-note" id="memoryBackend">默认使用 MD,兼容现有记忆文件;安装数据库附属插件 <b>qqgm-memory</b> 后可切换 SQLite,迁移前会先备份。</div>
@@ -351,116 +349,9 @@
351
349
  </div>
352
350
  </section>
353
351
 
354
- <section class="page" id="page-main-group">
355
- <div class="page-head"><div><h1>群管</h1><p>主插件 · 命令、权限、入群与撤回</p></div><button class="btn primary" data-toast="群管配置已保存(原型)">保存群管配置</button></div>
356
- <div class="config-layout">
357
- <aside class="card config-nav main-nav">
358
- <button class="config-plugin active" data-nav="main-group"><span class="plugin-ico slate">G</span><div><b>群管</b><small>命令/权限</small></div></button>
359
- <button class="config-plugin" data-nav="main-ai"><span class="plugin-ico blue">A</span><div><b>AI 回复</b><small>触发/模型</small></div></button>
360
- <button class="config-plugin" data-nav="main-moderation"><span class="plugin-ico orange">M</span><div><b>自动管控</b><small>复读/广告</small></div></button>
361
- <button class="config-plugin" data-nav="main-words"><span class="plugin-ico purple">W</span><div><b>违禁词</b><small>词库/动作</small></div></button>
362
- </aside>
363
- <div class="card config-form">
364
- <div class="form-head"><div><h2>群管</h2><p>@nestim/koishi-plugin-qq-group-manager · v1.6.0</p></div><span class="status ok">运行中</span></div>
365
- <div class="form-section"><h3>基础</h3>
366
- <label class="field"><span>命令前缀</span><input value="[bot]" /></label>
367
- <label class="field"><span>启用群管</span><input type="checkbox" checked /></label>
368
- <label class="field"><span>入群欢迎语</span><input value="欢迎新朋友,先看群规~" /></label>
369
- </div>
370
- <div class="form-section"><h3>权限</h3>
371
- <label class="field"><span>管理员角色</span><input value="owner, admin" /></label>
372
- <label class="field"><span>允许调试的用户 ID</span><input placeholder="逗号分隔" /></label>
373
- </div>
374
- <div class="form-section"><h3>默认动作</h3>
375
- <div class="field-row"><label class="field"><span>默认禁言(秒)</span><input type="number" value="600" /></label><label class="field"><span>撤回违规消息</span><input type="checkbox" checked /></label></div>
376
- </div>
377
- <div class="form-foot"><span>只影响群管模块</span><div><button class="btn ghost" data-toast="已还原(原型)">还原</button><button class="btn primary" data-toast="群管配置已保存(原型)">保存</button></div></div>
378
- </div>
379
- </div>
380
- </section>
381
-
382
- <section class="page" id="page-main-ai">
383
- <div class="page-head"><div><h1>AI 回复</h1><p>主插件 · 触发、上下文、模型与人设</p></div><button class="btn primary" data-toast="AI 配置已保存(原型)">保存 AI 配置</button></div>
384
- <div class="config-layout">
385
- <aside class="card config-nav main-nav">
386
- <button class="config-plugin" data-nav="main-group"><span class="plugin-ico slate">G</span><div><b>群管</b><small>命令/权限</small></div></button>
387
- <button class="config-plugin active" data-nav="main-ai"><span class="plugin-ico blue">A</span><div><b>AI 回复</b><small>触发/模型</small></div></button>
388
- <button class="config-plugin" data-nav="main-moderation"><span class="plugin-ico orange">M</span><div><b>自动管控</b><small>复读/广告</small></div></button>
389
- <button class="config-plugin" data-nav="main-words"><span class="plugin-ico purple">W</span><div><b>违禁词</b><small>词库/动作</small></div></button>
390
- </aside>
391
- <div class="card config-form">
392
- <div class="form-head"><div><h2>AI 回复</h2><p>@nestim/koishi-plugin-qq-group-manager · v1.6.0</p></div><span class="status ok">运行中</span></div>
393
- <div class="form-section"><h3>触发</h3>
394
- <label class="field"><span>启用 AI 回复</span><input type="checkbox" checked /></label>
395
- <label class="field"><span>回复模式</span><select><option>hybrid(点名 + 概率)</option><option>mention(仅点名)</option><option>probability(仅概率)</option></select></label>
396
- <div class="field-row"><label class="field"><span>消息阈值</span><input type="number" value="15" /></label><label class="field"><span>随机概率</span><input type="number" value="0.08" step="0.01" /></label></div>
397
- </div>
398
- <div class="form-section"><h3>上下文</h3>
399
- <div class="field-row"><label class="field"><span>上下文条数</span><input type="number" value="40" /></label><label class="field"><span>最大回复字数</span><input type="number" value="120" /></label></div>
400
- <label class="field"><span>记忆库参与 AI</span><input type="checkbox" checked /></label>
401
- </div>
402
- <div class="form-section"><h3>模型</h3>
403
- <label class="field"><span>模型</span><input value="deepseek-chat" /></label>
404
- <label class="field"><span>温度</span><input type="number" value="0.8" step="0.1" /></label>
405
- <label class="field"><span>API Base URL</span><input value="https://api.deepseek.com" /></label>
406
- </div>
407
- <div class="form-foot"><span>密钥只显示掩码,不在此回传</span><div><button class="btn ghost" data-toast="已还原(原型)">还原</button><button class="btn primary" data-toast="AI 配置已保存(原型)">保存</button></div></div>
408
- </div>
409
- </div>
410
- </section>
411
-
412
- <section class="page" id="page-main-moderation">
413
- <div class="page-head"><div><h1>自动管控</h1><p>主插件 · 复读、广告、刷屏、入群审核</p></div><button class="btn primary" data-toast="管控配置已保存(原型)">保存管控配置</button></div>
414
- <div class="config-layout">
415
- <aside class="card config-nav main-nav">
416
- <button class="config-plugin" data-nav="main-group"><span class="plugin-ico slate">G</span><div><b>群管</b><small>命令/权限</small></div></button>
417
- <button class="config-plugin" data-nav="main-ai"><span class="plugin-ico blue">A</span><div><b>AI 回复</b><small>触发/模型</small></div></button>
418
- <button class="config-plugin active" data-nav="main-moderation"><span class="plugin-ico orange">M</span><div><b>自动管控</b><small>复读/广告</small></div></button>
419
- <button class="config-plugin" data-nav="main-words"><span class="plugin-ico purple">W</span><div><b>违禁词</b><small>词库/动作</small></div></button>
420
- </aside>
421
- <div class="card config-form">
422
- <div class="form-head"><div><h2>自动管控</h2><p>@nestim/koishi-plugin-qq-group-manager · v1.6.0</p></div><span class="status ok">运行中</span></div>
423
- <div class="form-section"><h3>群内行为</h3>
424
- <label class="field"><span>复读检测</span><input type="checkbox" checked /></label>
425
- <label class="field"><span>刷屏检测</span><input type="checkbox" checked /></label>
426
- <label class="field"><span>广告/链接检测</span><input type="checkbox" checked /></label>
427
- <div class="field-row"><label class="field"><span>刷屏阈值(条/10s)</span><input type="number" value="6" /></label><label class="field"><span>违规禁言(秒)</span><input type="number" value="600" /></label></div>
428
- </div>
429
- <div class="form-section"><h3>入群审核</h3>
430
- <label class="field"><span>入群验证</span><select><option>数字验证码</option><option>关闭</option></select></label>
431
- <label class="field"><span>超时自动拒绝(秒)</span><input type="number" value="120" /></label>
432
- <label class="field"><span>黑名单自动拒绝</span><input type="checkbox" checked /></label>
433
- </div>
434
- <div class="form-foot"><span>管控动作会记录到违规日志</span><div><button class="btn ghost" data-toast="已还原(原型)">还原</button><button class="btn primary" data-toast="管控配置已保存(原型)">保存</button></div></div>
435
- </div>
436
- </div>
437
- </section>
438
-
439
- <section class="page" id="page-main-words">
440
- <div class="page-head"><div><h1>违禁词</h1><p>主插件 · 词库、正则与命中动作</p></div><button class="btn primary" data-toast="词库已保存(原型)">保存词库</button></div>
441
- <div class="config-layout">
442
- <aside class="card config-nav main-nav">
443
- <button class="config-plugin" data-nav="main-group"><span class="plugin-ico slate">G</span><div><b>群管</b><small>命令/权限</small></div></button>
444
- <button class="config-plugin" data-nav="main-ai"><span class="plugin-ico blue">A</span><div><b>AI 回复</b><small>触发/模型</small></div></button>
445
- <button class="config-plugin" data-nav="main-moderation"><span class="plugin-ico orange">M</span><div><b>自动管控</b><small>复读/广告</small></div></button>
446
- <button class="config-plugin active" data-nav="main-words"><span class="plugin-ico purple">W</span><div><b>违禁词</b><small>词库/动作</small></div></button>
447
- </aside>
448
- <div class="card config-form">
449
- <div class="form-head"><div><h2>违禁词</h2><p>@nestim/koishi-plugin-qq-group-manager · v1.6.0</p></div><span class="status ok">运行中</span></div>
450
- <div class="form-section"><h3>词库</h3>
451
- <label class="field"><span>启用违禁词</span><input type="checkbox" checked /></label>
452
- <label class="field"><span>匹配方式</span><select><option>包含关键词</option><option>正则表达式</option></select></label>
453
- <label class="field"><span>命中动作</span><select><option>撤回 + 警告</option><option>撤回 + 禁言</option><option>仅记录</option></select></label>
454
- <label class="field"><span>词条</span><textarea rows="4" placeholder="每行一个,例如:广告、加群、代练">广告
455
- 加群
456
- 代练</textarea></label>
457
- </div>
458
- <div class="form-section"><h3>当前词条</h3>
459
- <div class="word-list"><span>广告 <button>×</button></span><span>加群 <button>×</button></span><span>代练 <button>×</button></span><span>刷单 <button>×</button></span><span>外挂 <button>×</button></span></div>
460
- </div>
461
- <div class="form-foot"><span>按群生效,支持导入/导出</span><div><button class="btn ghost" data-toast="已还原(原型)">还原</button><button class="btn primary" data-toast="词库已保存(原型)">保存</button></div></div>
462
- </div>
463
- </div>
352
+ <section class="page" id="page-main-schema">
353
+ <div class="page-head"><div><h1 id="mainSchemaTitle">主插件配置</h1><p id="mainSchemaSubtitle">按主插件 Schema 分组,每组独立页面</p></div><button class="btn primary" id="saveMainSchemaGroup">保存本组</button></div>
354
+ <div class="card config-form" id="mainSchemaForm"><div class="schema-loading">读取 Schema…</div></div>
464
355
  </section>
465
356
 
466
357
  <section class="page" id="page-config">
@@ -575,6 +466,13 @@
575
466
  </main>
576
467
  </div>
577
468
  <div class="toast" id="toast"></div>
469
+ <div class="modal" id="memoryModal" hidden>
470
+ <div class="modal-card">
471
+ <div class="modal-head"><div><h3 id="memoryModalTitle">记忆</h3><p id="memoryModalMeta"></p></div><button class="icon-btn" id="memoryModalClose">✕</button></div>
472
+ <div class="modal-body" id="memoryModalContent"></div>
473
+ <div class="modal-foot"><span id="memoryModalSource"></span><button class="btn ghost" id="memoryModalClose2">关闭</button></div>
474
+ </div>
475
+ </div>
578
476
  <script src="./app.js"></script>
579
477
  </body>
580
478
  </html>
package/public/styles.css CHANGED
@@ -35,7 +35,8 @@ svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round
35
35
  .brand-text span { font-size: 9px; letter-spacing: .22em; color: #7d8ca5; margin-top: 4px; }
36
36
  .nav { display: flex; flex-direction: column; gap: 4px; flex: 1; min-height: 0; overflow: auto; padding-right: 2px; padding-bottom: 74px; }
37
37
  .nav-section { font-size: 9px; letter-spacing: .16em; color: #61728c; margin: 16px 12px 4px; text-transform: uppercase; }
38
- .nav-sub { text-align: left; padding: 7px 12px 7px 44px; border-radius: 8px; font-size: 12px; color: #8493aa; position: relative; }
38
+ .nav-sub { display: flex; align-items: center; justify-content: space-between; text-align: left; padding: 7px 12px 7px 44px; border-radius: 8px; font-size: 12px; color: #8493aa; position: relative; }
39
+ .sub-count { font-size: 9px; color: #566b89; background: rgba(255,255,255,.04); border-radius: 8px; padding: 1px 5px; }
39
40
  .nav-sub::before { content: ""; position: absolute; left: 24px; top: 50%; width: 5px; height: 5px; margin-top: -2.5px; border-radius: 50%; background: #33445e; }
40
41
  .nav-sub:hover { color: #e8eefb; background: rgba(255,255,255,.04); }
41
42
  .nav-sub.active { color: #9db9f5; background: rgba(79,125,243,.1); }
@@ -303,6 +304,25 @@ code { background: #f2f5f9; color: #55657d; border-radius: 4px; padding: 1px 4px
303
304
  .word-list { display: flex; flex-wrap: wrap; gap: 7px; }
304
305
  .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
306
  .word-list button { color: #a6b2c2; font-size: 12px; line-height: 1; }
307
+ .schema-field { padding: 11px 0; border-bottom: 1px solid #f1f4f8; }
308
+ .schema-field .field > span { flex: 0 0 200px; font-family: Consolas, monospace; font-size: 10.5px; color: #3f5270; }
309
+ .schema-field .field input:not([type=checkbox]), .schema-field .field select, .schema-field .field textarea { width: 100%; }
310
+ .form-hint { margin: 5px 0 0 200px; font-size: 9.5px; color: #9aa7b8; line-height: 1.5; }
311
+ .schema-loading { padding: 24px; color: #97a4b6; font-size: 11px; }
312
+ .backend-pill.sqlite { color: #167a5b; background: #e9f8f1; border-color: #cfeee0; }
313
+ .memory-item { cursor: pointer; border-radius: 8px; transition: background .15s; }
314
+ .memory-item:hover { background: #fafcff; }
315
+ .memory-open { margin-left: auto; color: #6c8fc4; }
316
+ .backend-pill { font-size: 10px; color: #4f6f9f; background: #edf3fc; border: 1px solid #dbe7f7; border-radius: 20px; padding: 5px 10px; }
317
+ .modal { position: fixed; inset: 0; background: rgba(12,22,38,.45); display: grid; place-items: center; z-index: 30; }
318
+ .modal[hidden] { display: none; }
319
+ .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; }
320
+ .modal-head { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 1px solid var(--line); padding-bottom: 12px; }
321
+ .modal-head h3 { margin: 0 0 4px; font-size: 16px; }
322
+ .modal-head p { margin: 0; font-size: 10px; color: #97a4b6; }
323
+ .modal-head .icon-btn { color: #8c9aae; background: #f4f7fb; }
324
+ .modal-body { padding: 15px 0; font-size: 12px; line-height: 1.75; color: #405066; white-space: pre-wrap; }
325
+ .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
326
  .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
327
  .toast.show { opacity: 1; transform: translate(-50%, 0); }
308
328