@nestim/koishi-plugin-qqgm-webui 0.2.21 → 0.2.23

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
@@ -115,7 +115,7 @@ function apply(ctx, config) {
115
115
  ctx.server.get(`${route}/api/health`, (koa) => json(koa, {
116
116
  ok: true,
117
117
  name,
118
- version: '0.2.21',
118
+ version: '0.2.23',
119
119
  route,
120
120
  tokenRequired: !!config.token,
121
121
  remoteAllowed: !!config.allowRemote,
@@ -19,7 +19,7 @@ function loadMainPlugin() {
19
19
 
20
20
  function humanLabel(key, description) {
21
21
  const text = String(description || '').replace(/\s+/g, ' ').trim();
22
- const first = text.split(/[::((]/)[0].trim();
22
+ const first = text.split(/[::((,,。;;]/)[0].trim();
23
23
  if (first && first.length >= 2 && first.length <= 28) return first;
24
24
  return key;
25
25
  }
@@ -37,6 +37,17 @@ function fieldInfo(key, field) {
37
37
  /prompt|system|persona|template|greeting|welcome|instruction|rule/i.test(key)
38
38
  || (typeof defaultValue === 'string' && defaultValue.length > 60)
39
39
  );
40
+ let arrayOf = null;
41
+ let itemFields = [];
42
+ if (type === 'array') {
43
+ if (/^string\[\]/.test(String(field))) arrayOf = 'string';
44
+ else if (field.inner && field.inner.dict) {
45
+ arrayOf = 'object';
46
+ itemFields = Object.keys(field.inner.dict)
47
+ .filter((name) => name !== 'guildId')
48
+ .map((name) => fieldInfo(name, field.inner.dict[name]));
49
+ }
50
+ }
40
51
  return {
41
52
  key,
42
53
  label: humanLabel(key, description),
@@ -47,6 +58,11 @@ function fieldInfo(key, field) {
47
58
  sensitive: /key|token|secret|password/i.test(key),
48
59
  json: type === 'array' || type === 'object' || type === 'intersect',
49
60
  long,
61
+ arrayOf,
62
+ itemFields,
63
+ list: arrayOf === 'string',
64
+ objectList: arrayOf === 'object',
65
+ scored: /bannedWords|strongKeywords/.test(key),
50
66
  };
51
67
  }
52
68
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestim/koishi-plugin-qqgm-webui",
3
- "version": "0.2.21",
3
+ "version": "0.2.23",
4
4
  "description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
5
5
  "main": "index.js",
6
6
  "files": [
package/public/app.js CHANGED
@@ -333,6 +333,127 @@ function tokenQuery() {
333
333
  return token ? `?token=${encodeURIComponent(token)}` : '';
334
334
  }
335
335
 
336
+ const escHtml = (text) => String(text == null ? '' : text).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
337
+
338
+ function splitScored(raw) {
339
+ const text = String(raw == null ? '' : raw);
340
+ const index = text.lastIndexOf('|');
341
+ if (index > 0 && /^\d+$/.test(text.slice(index + 1).trim())) {
342
+ return [text.slice(0, index), text.slice(index + 1).trim()];
343
+ }
344
+ return [text, ''];
345
+ }
346
+
347
+ function listRowHtml(scored, text = '', score = '') {
348
+ return `<div class="list-row">
349
+ <input class="list-text" value="${escHtml(text)}" placeholder="关键词" />
350
+ ${scored ? `<input class="list-score" type="number" value="${escHtml(score)}" placeholder="分值" />` : ''}
351
+ <button type="button" class="row-btn" data-remove-row title="删除">−</button>
352
+ <button type="button" class="row-btn add" data-add-row title="新增一行">+</button>
353
+ </div>`;
354
+ }
355
+
356
+ function renderListEditor(field, value) {
357
+ const rows = Array.isArray(value) ? value : [];
358
+ const html = rows.length
359
+ ? rows.map((item) => {
360
+ const [text, score] = splitScored(item);
361
+ return listRowHtml(field.scored, text, score);
362
+ }).join('')
363
+ : listRowHtml(field.scored);
364
+ return `<div class="list-editor" data-list-key="${field.key}" data-list-scored="${field.scored ? '1' : '0'}">
365
+ <div class="list-rows">${html}</div>
366
+ <button type="button" class="link-btn add-row" data-add-row>+ 添加一行</button>
367
+ </div>`;
368
+ }
369
+
370
+ function collectList(root, field) {
371
+ if (!root) return [];
372
+ const values = [];
373
+ for (const row of root.querySelectorAll('.list-row')) {
374
+ const text = row.querySelector('.list-text')?.value.trim() || '';
375
+ if (!text) continue;
376
+ const score = field.scored ? (row.querySelector('.list-score')?.value.trim() || '') : '';
377
+ values.push(score ? `${text}|${score}` : text);
378
+ }
379
+ return values;
380
+ }
381
+
382
+ function groupFieldHtml(field, value, index) {
383
+ const current = value == null ? '' : value;
384
+ if (field.type === 'boolean') {
385
+ return `<label class="group-field"><span>${escHtml(field.label)}</span>
386
+ <select data-group-field="${field.key}" data-group-kind="boolean">
387
+ <option value="">跟随全局</option>
388
+ <option value="true" ${current === true ? 'selected' : ''}>开启</option>
389
+ <option value="false" ${current === false ? 'selected' : ''}>关闭</option>
390
+ </select></label>`;
391
+ }
392
+ if (field.list) {
393
+ return `<div class="group-field block"><span>${escHtml(field.label)}</span>${renderListEditor(field, Array.isArray(current) ? current : [])}</div>`;
394
+ }
395
+ if (field.json) {
396
+ return `<div class="group-field block"><span>${escHtml(field.label)}</span><textarea rows="3" class="json-input" data-group-field="${field.key}" data-group-kind="json">${escHtml(JSON.stringify(current || (field.type === 'array' ? [] : {}), null, 2))}</textarea></div>`;
397
+ }
398
+ const type = field.type === 'number' ? 'number' : 'text';
399
+ return `<label class="group-field"><span>${escHtml(field.label)}</span>
400
+ <input type="${type}" data-group-field="${field.key}" data-group-kind="${type === 'number' ? 'number' : 'string'}" value="${escHtml(current)}" placeholder="留空=全局" /></label>`;
401
+ }
402
+
403
+ function groupCardHtml(field, group = {}, index = 0, collapsed = true) {
404
+ const body = (field.itemFields || []).map((item) => groupFieldHtml(item, group[item.key], index)).join('');
405
+ return `<article class="group-rule" data-group-card>
406
+ <div class="group-rule-head">
407
+ <input class="group-rule-id" value="${escHtml(group.guildId || '')}" placeholder="群号" />
408
+ <button type="button" class="row-btn" data-toggle-group>${collapsed ? '展开' : '收起'}</button>
409
+ <button type="button" class="row-btn" data-remove-group>×</button>
410
+ </div>
411
+ <div class="group-rule-body" ${collapsed ? 'hidden' : ''}>
412
+ <p class="group-rule-hint">不改的项目默认使用全局配置。</p>
413
+ ${body}
414
+ </div>
415
+ </article>`;
416
+ }
417
+
418
+ function renderGroupRules(field, value) {
419
+ const groups = Array.isArray(value) ? value : [];
420
+ const enabled = groups.length > 0;
421
+ return `<div class="group-rules" data-group-rules>
422
+ <label class="switch"><input type="checkbox" data-group-enabled ${enabled ? 'checked' : ''} /><i></i><span>${enabled ? '按群号覆盖' : '未开启,使用全局配置'}</span></label>
423
+ <div class="group-rules-list" ${enabled ? '' : 'hidden'}>
424
+ ${groups.map((group, index) => groupCardHtml(field, group, index, true)).join('')}
425
+ <button type="button" class="link-btn" data-add-group>+ 添加群规则</button>
426
+ </div>
427
+ </div>`;
428
+ }
429
+
430
+ function collectGroupRules(field, form) {
431
+ const root = form.querySelector('[data-group-rules]');
432
+ if (!root || !root.querySelector('[data-group-enabled]')?.checked) return [];
433
+ const groups = [];
434
+ for (const card of root.querySelectorAll('[data-group-card]')) {
435
+ const guildId = card.querySelector('.group-rule-id')?.value.trim() || '';
436
+ if (!guildId) continue;
437
+ const group = { guildId };
438
+ for (const item of field.itemFields || []) {
439
+ if (item.list) {
440
+ const list = collectList(card.querySelector(`[data-list-key="${item.key}"]`), item);
441
+ if (list.length) group[item.key] = list;
442
+ } else {
443
+ const input = card.querySelector(`[data-group-field="${item.key}"]`);
444
+ if (!input || input.value === '') continue;
445
+ if (item.type === 'boolean') group[item.key] = input.value === 'true';
446
+ else if (item.type === 'number') group[item.key] = Number(input.value);
447
+ else if (item.json) {
448
+ try { group[item.key] = JSON.parse(input.value); } catch { /* skip invalid */ }
449
+ } else group[item.key] = input.value;
450
+ }
451
+ }
452
+ groups.push(group);
453
+ }
454
+ return groups;
455
+ }
456
+
336
457
  function openMainGroup(index) {
337
458
  renderMainGroup(index);
338
459
  }
@@ -347,9 +468,12 @@ function renderMainGroup(index) {
347
468
  form.innerHTML = `<div class="form-head"><div><h2>${group.title}</h2><p>${group.fields.length} 个字段 · 与 Koishi 配置页同源</p></div><span class="status ok">运行中</span></div>`
348
469
  + group.fields.map((field) => {
349
470
  const value = mainSchema.values[field.key] ?? field.default;
350
- const esc = (text) => String(text == null ? '' : text).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
351
471
  let control = '';
352
- if (field.type === 'boolean') {
472
+ if (field.key === 'groupRules' && field.itemFields?.length) {
473
+ control = renderGroupRules(field, value);
474
+ } else if (field.list) {
475
+ control = renderListEditor(field, value);
476
+ } else if (field.type === 'boolean') {
353
477
  control = `<label class="switch"><input type="checkbox" data-key="${field.key}" data-kind="boolean" ${value ? 'checked' : ''} /><i></i><span>${value ? '已开启' : '已关闭'}</span></label>`;
354
478
  } else if (field.type === 'union') {
355
479
  const options = (field.options.length ? field.options : [String(value || '')]).map((opt) => `<option ${String(value) === opt ? 'selected' : ''}>${opt}</option>`).join('');
@@ -357,16 +481,16 @@ function renderMainGroup(index) {
357
481
  } else if (field.type === 'number') {
358
482
  control = `<input type="number" data-key="${field.key}" data-kind="number" value="${value ?? field.default ?? 0}" />`;
359
483
  } else if (field.json) {
360
- control = `<textarea rows="5" class="json-input" data-key="${field.key}" data-kind="json">${esc(JSON.stringify(value ?? field.default ?? (field.type === 'array' ? [] : {}), null, 2))}</textarea>`;
484
+ control = `<textarea rows="5" class="json-input" data-key="${field.key}" data-kind="json">${escHtml(JSON.stringify(value ?? field.default ?? (field.type === 'array' ? [] : {}), null, 2))}</textarea>`;
361
485
  } else if (field.long) {
362
- control = `<textarea rows="7" data-key="${field.key}" data-kind="string">${esc(value ?? field.default ?? '')}</textarea>`;
486
+ control = `<textarea rows="7" data-key="${field.key}" data-kind="string">${escHtml(value ?? field.default ?? '')}</textarea>`;
363
487
  } else {
364
- control = `<input ${field.sensitive ? 'type="password"' : ''} data-key="${field.key}" data-kind="string" value="${esc(value ?? field.default ?? '')}" />`;
488
+ control = `<input ${field.sensitive ? 'type="password"' : ''} data-key="${field.key}" data-kind="string" value="${escHtml(value ?? field.default ?? '')}" />`;
365
489
  }
366
490
  return `<div class="schema-field ${field.long ? 'long' : ''}">
367
491
  <div class="schema-field-info">
368
- <div class="schema-field-title">${esc(field.label || field.key)}</div>
369
- <p class="schema-field-desc">${esc(field.description || '')}</p>
492
+ <div class="schema-field-title">${escHtml(field.label || field.key)}</div>
493
+ <p class="schema-field-desc">${escHtml(field.description || '')}</p>
370
494
  <code class="schema-field-key">${field.key}</code>
371
495
  </div>
372
496
  <div class="schema-field-control">${control}</div>
@@ -375,17 +499,72 @@ function renderMainGroup(index) {
375
499
  + `<div class="form-foot"><span>保存只改本组字段;写前备份 koishi.yml,重启后生效</span></div>`;
376
500
  const save = $('#saveMainSchemaGroup');
377
501
  if (save) save.onclick = () => saveMainGroup(group);
378
- $$('#mainSchemaForm .switch input').forEach((input) => {
379
- input.addEventListener('change', () => {
380
- const text = input.parentElement?.querySelector('span');
381
- if (text) text.textContent = input.checked ? '已开启' : '已关闭';
382
- });
383
- });
502
+
503
+ form.onclick = (event) => {
504
+ const addRow = event.target.closest('[data-add-row]');
505
+ if (addRow) {
506
+ const editor = addRow.closest('.list-editor');
507
+ if (editor) editor.querySelector('.list-rows').insertAdjacentHTML('beforeend', listRowHtml(editor.dataset.listScored === '1'));
508
+ return;
509
+ }
510
+ const removeRow = event.target.closest('[data-remove-row]');
511
+ if (removeRow) {
512
+ const editor = removeRow.closest('.list-editor');
513
+ removeRow.closest('.list-row')?.remove();
514
+ if (editor && !editor.querySelector('.list-row')) editor.querySelector('.list-rows').innerHTML = listRowHtml(editor.dataset.listScored === '1');
515
+ return;
516
+ }
517
+ const addGroup = event.target.closest('[data-add-group]');
518
+ if (addGroup) {
519
+ const rules = addGroup.closest('[data-group-rules]');
520
+ const groupField = group.fields.find((item) => item.key === 'groupRules');
521
+ const list = rules?.querySelector('.group-rules-list');
522
+ if (list && groupField) list.insertAdjacentHTML('beforeend', groupCardHtml(groupField, {}, 0, false));
523
+ return;
524
+ }
525
+ const removeGroup = event.target.closest('[data-remove-group]');
526
+ if (removeGroup) { removeGroup.closest('[data-group-card]')?.remove(); return; }
527
+ const toggleGroup = event.target.closest('[data-toggle-group]');
528
+ if (toggleGroup) {
529
+ const body = toggleGroup.closest('[data-group-card]')?.querySelector('.group-rule-body');
530
+ if (body) {
531
+ body.hidden = !body.hidden;
532
+ toggleGroup.textContent = body.hidden ? '展开' : '收起';
533
+ }
534
+ }
535
+ };
536
+
537
+ form.onchange = (event) => {
538
+ if (event.target.matches('[data-group-enabled]')) {
539
+ const rules = event.target.closest('[data-group-rules]');
540
+ const list = rules?.querySelector('.group-rules-list');
541
+ if (list) list.hidden = !event.target.checked;
542
+ const text = event.target.parentElement?.querySelector('span');
543
+ if (text) text.textContent = event.target.checked ? '按群号覆盖' : '未开启,使用全局配置';
544
+ if (event.target.checked && list && !list.querySelector('[data-group-card]')) {
545
+ const groupField = group.fields.find((item) => item.key === 'groupRules');
546
+ if (groupField) list.insertAdjacentHTML('afterbegin', groupCardHtml(groupField, {}, 0, false));
547
+ }
548
+ }
549
+ if (event.target.matches('.switch input')) {
550
+ const text = event.target.parentElement?.querySelector('span');
551
+ if (text) text.textContent = event.target.checked ? '已开启' : '已关闭';
552
+ }
553
+ };
384
554
  }
385
555
 
386
556
  async function saveMainGroup(group) {
387
557
  const values = {};
558
+ const form = $('#mainSchemaForm');
388
559
  for (const field of group.fields) {
560
+ if (field.key === 'groupRules' && field.itemFields?.length) {
561
+ values[field.key] = collectGroupRules(field, form);
562
+ continue;
563
+ }
564
+ if (field.list) {
565
+ values[field.key] = collectList(form.querySelector(`[data-list-key="${field.key}"]`), field);
566
+ continue;
567
+ }
389
568
  const input = $(`[data-key="${field.key}"]`);
390
569
  if (!input) continue;
391
570
  if (field.type === 'boolean') values[field.key] = !!input.checked;
package/public/index.html CHANGED
@@ -37,9 +37,12 @@
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-schema">主插件配置</button>
41
- <button class="nav-item" data-page="config">
40
+ <button class="nav-sub nav-sub-icon" data-page="main-schema">
42
41
  <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>
42
+ <span>主插件配置</span>
43
+ </button>
44
+ <button class="nav-item" data-page="config">
45
+ <svg viewBox="0 0 24 24"><path d="m12 3 9 5-9 5-9-5 9-5Zm-9 9 9 5 9-5M3 16l9 5 9-5"/></svg>
43
46
  <span>附属配置</span>
44
47
  </button>
45
48
  <button class="nav-item" data-page="logs">
package/public/styles.css CHANGED
@@ -41,6 +41,9 @@ svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round
41
41
  .nav-sub:hover { color: #e8eefb; background: rgba(255,255,255,.04); }
42
42
  .nav-sub.active { color: #9db9f5; background: rgba(79,125,243,.1); }
43
43
  .nav-sub.active::before { background: #6f96ee; box-shadow: 0 0 0 3px rgba(111,150,238,.14); }
44
+ .nav-sub-icon { padding-left: 12px; gap: 10px; justify-content: flex-start; }
45
+ .nav-sub-icon::before { display: none; }
46
+ .nav-sub-icon svg { width: 17px; height: 17px; flex: 0 0 auto; }
44
47
  .nav-item { display: flex; align-items: center; gap: 12px; padding: 11px 12px; border-radius: 10px; color: #93a1b8; font-size: 13.5px; text-align: left; transition: .15s; position: relative; }
45
48
  .nav-item svg { width: 18px; height: 18px; opacity: .9; }
46
49
  .pulse { width: 7px; height: 7px; background: #34d399; border-radius: 50%; box-shadow: 0 0 0 4px rgba(52,211,153,.12); display: inline-block; flex: 0 0 auto; }
@@ -307,8 +310,8 @@ code { background: #f2f5f9; color: #55657d; border-radius: 4px; padding: 1px 4px
307
310
  .schema-field { display: grid; grid-template-columns: 260px minmax(0, 1fr); gap: 28px; align-items: start; padding: 18px 2px; border-bottom: 1px solid #f1f4f8; }
308
311
  .schema-field.long { grid-template-columns: 1fr; gap: 10px; }
309
312
  .schema-field-info { min-width: 0; }
310
- .schema-field-title { font-size: 12.5px; font-weight: 650; color: #2f4058; margin-bottom: 5px; }
311
- .schema-field-desc { margin: 0 0 7px; font-size: 10.5px; color: #8a98ab; line-height: 1.6; }
313
+ .schema-field-title { font-size: 13px; font-weight: 650; color: #2f4058; margin-bottom: 5px; }
314
+ .schema-field-desc { margin: 0 0 7px; font-size: 11.5px; color: #8190a4; line-height: 1.6; }
312
315
  .schema-field-key { display: inline-block; font-size: 9px; color: #a0adbe; background: #f5f7fa; border: 1px solid #eef2f7; border-radius: 4px; padding: 2px 5px; }
313
316
  .schema-field-control { min-width: 0; padding-top: 1px; }
314
317
  .schema-field-control > input:not([type=checkbox]), .schema-field-control > select, .schema-field-control > textarea { width: 100%; }
@@ -323,6 +326,29 @@ code { background: #f2f5f9; color: #55657d; border-radius: 4px; padding: 1px 4px
323
326
  .switch input:checked + i { background: #4f7df3; }
324
327
  .switch input:checked + i::after { transform: translateX(15px); }
325
328
  .switch span { font-size: 10.5px; color: #9aa7b8; }
329
+ .list-editor { border: 1px solid #e6ecf4; border-radius: 9px; padding: 8px; background: #fbfcfe; }
330
+ .list-rows { display: flex; flex-direction: column; gap: 6px; }
331
+ .list-row { display: grid; grid-template-columns: minmax(0, 1fr) 90px 26px 26px; gap: 6px; align-items: center; }
332
+ .list-row:not(:has(.list-score)) { grid-template-columns: minmax(0, 1fr) 26px 26px; }
333
+ .list-row input { height: 31px; border: 1px solid #e2e8f1; border-radius: 6px; background: #fff; padding: 0 8px; font-size: 11.5px; color: #3b4b61; outline: none; }
334
+ .list-row .list-score { text-align: center; }
335
+ .row-btn { width: 26px; height: 26px; border-radius: 6px; color: #8c9aae; background: #f0f4f9; font-size: 15px; line-height: 1; display: grid; place-items: center; }
336
+ .row-btn.add { color: #3f68d8; background: #edf3ff; }
337
+ .row-btn:hover { filter: brightness(.96); }
338
+ .add-row { margin-top: 7px; font-size: 11px; }
339
+ .group-rules { border: 1px solid #e6ecf4; border-radius: 9px; padding: 12px; background: #fbfcfe; }
340
+ .group-rules-list { margin-top: 12px; display: flex; flex-direction: column; gap: 10px; }
341
+ .group-rule { border: 1px solid #e4ebf4; border-radius: 9px; background: #fff; overflow: hidden; }
342
+ .group-rule-head { display: flex; gap: 8px; align-items: center; padding: 8px 10px; background: #f7f9fc; }
343
+ .group-rule-id { flex: 1; height: 30px; border: 1px solid #e2e8f1; border-radius: 6px; padding: 0 8px; font-size: 11.5px; background: #fff; outline: none; }
344
+ .group-rule-body { padding: 10px 12px 12px; border-top: 1px solid #eef2f7; }
345
+ .group-rule-hint { margin: 0 0 8px; font-size: 10px; color: #9aa7b8; }
346
+ .group-field { display: grid; grid-template-columns: 170px minmax(0, 1fr); gap: 10px; align-items: center; padding: 5px 0; font-size: 11.5px; color: #596a80; }
347
+ .group-field.block { display: block; }
348
+ .group-field > span { color: #6b7b90; }
349
+ .group-field > span + input, .group-field > span + select { height: 30px; border: 1px solid #e2e8f1; border-radius: 6px; background: #fff; padding: 0 8px; font-size: 11.5px; outline: none; }
350
+ .group-field.block > span { display: block; margin-bottom: 6px; }
351
+ .group-field .list-editor { padding: 6px; }
326
352
  .schema-field .field > span { flex: 0 0 200px; font-family: Consolas, monospace; font-size: 10.5px; color: #3f5270; }
327
353
  .schema-field .field input:not([type=checkbox]), .schema-field .field select, .schema-field .field textarea { width: 100%; }
328
354
  .form-hint { margin: 5px 0 0 200px; font-size: 9.5px; color: #9aa7b8; line-height: 1.5; }