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

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.18',
118
+ version: '0.2.20',
119
119
  route,
120
120
  tokenRequired: !!config.token,
121
121
  remoteAllowed: !!config.allowRemote,
@@ -157,6 +157,7 @@ function apply(ctx, config) {
157
157
  json(koa, memory(ctx, {
158
158
  scope: koa.query.scope || '',
159
159
  query: koa.query.q || '',
160
+ category: koa.query.category || '',
160
161
  limit: koa.query.limit || 50,
161
162
  }));
162
163
  });
package/lib/data.js CHANGED
@@ -152,7 +152,7 @@ function parseScope(text) {
152
152
  return { kind: 'group', id: value };
153
153
  }
154
154
 
155
- function memory(ctx, { scope = '', query = '', limit = 50 } = {}) {
155
+ function memory(ctx, { scope = '', query = '', category = '', limit = 50 } = {}) {
156
156
  const base = ctx.baseDir || process.cwd();
157
157
  const file = path.join(base, 'data', 'qqgm-memory.sqlite');
158
158
  const cfg = readMemoryConfig(ctx);
@@ -177,6 +177,7 @@ function memory(ctx, { scope = '', query = '', limit = 50 } = {}) {
177
177
  const parsed = parseScope(scope);
178
178
  let filtered = source;
179
179
  if (parsed) filtered = filtered.filter((item) => item.scope_kind === parsed.kind && String(item.scope_id) === String(parsed.id));
180
+ if (category) filtered = filtered.filter((item) => (item.category || '未分类') === category);
180
181
  if (query) filtered = filtered.filter((item) => String(item.content).includes(query) || String(item.label || '').includes(query));
181
182
  const items = filtered.slice(0, Math.max(1, Math.min(200, Number(limit) || 50)));
182
183
  const scopes = new Map();
@@ -17,6 +17,13 @@ function loadMainPlugin() {
17
17
  }
18
18
  }
19
19
 
20
+ function humanLabel(key, description) {
21
+ const text = String(description || '').replace(/\s+/g, ' ').trim();
22
+ const first = text.split(/[::((]/)[0].trim();
23
+ if (first && first.length >= 2 && first.length <= 28) return first;
24
+ return key;
25
+ }
26
+
20
27
  function fieldInfo(key, field) {
21
28
  const meta = field.meta || {};
22
29
  const type = String(field.type || 'string');
@@ -24,14 +31,22 @@ function fieldInfo(key, field) {
24
31
  if (type === 'union') {
25
32
  for (const match of String(field).matchAll(/"([^"]+)"/g)) options.push(match[1]);
26
33
  }
34
+ const description = meta.description || '';
35
+ const defaultValue = meta.default;
36
+ const long = type === 'string' && (
37
+ /prompt|system|persona|template|greeting|welcome|instruction|rule/i.test(key)
38
+ || (typeof defaultValue === 'string' && defaultValue.length > 60)
39
+ );
27
40
  return {
28
41
  key,
42
+ label: humanLabel(key, description),
29
43
  type,
30
- description: meta.description || '',
31
- default: meta.default,
44
+ description,
45
+ default: defaultValue,
32
46
  options,
33
47
  sensitive: /key|token|secret|password/i.test(key),
34
48
  json: type === 'array' || type === 'object' || type === 'intersect',
49
+ long,
35
50
  };
36
51
  }
37
52
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestim/koishi-plugin-qqgm-webui",
3
- "version": "0.2.18",
3
+ "version": "0.2.20",
4
4
  "description": "QQGM 附属 WebUI:插件识别、监控看板、记忆库、配置与日志。",
5
5
  "main": "index.js",
6
6
  "files": [
package/public/app.js CHANGED
@@ -117,6 +117,42 @@ initRealData();
117
117
  let memoryItems = [];
118
118
  let memoryState = null;
119
119
 
120
+ function renderMemoryList(items) {
121
+ memoryItems = items || [];
122
+ const list = $('#memoryList');
123
+ if (!list) return;
124
+ if (!memoryItems.length) {
125
+ list.innerHTML = '<div class="memory-empty"><b>没有匹配的记忆</b><p>换个范围/分类,或先迁移 MD 到 SQLite。</p></div>';
126
+ return;
127
+ }
128
+ list.innerHTML = memoryItems.map((item, index) => `
129
+ <article class="memory-item" data-memory-index="${index}">
130
+ <div class="memory-head">
131
+ <span class="tag ${item.scope_kind === 'private' ? 'private' : 'group'}">${item.scope_kind === 'group' ? '群' : item.scope_kind === 'private' ? '私聊' : '全局'} ${item.scope_id || ''}</span>
132
+ <span class="tag soft">${item.category || '未分类'}</span>
133
+ <time>${new Date(item.created_at).toLocaleString('zh-CN', { hour12: false })}</time>
134
+ </div>
135
+ <p>${String(item.content || '').replace(/[<>&]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;' }[c]))}</p>
136
+ <div class="memory-foot"><code>${item.label || '记忆'}</code><span>#${item.id}</span><span class="memory-open">点击查看</span></div>
137
+ </article>`).join('');
138
+ }
139
+
140
+ async function filterMemory(scope, category, activeButton) {
141
+ const params = new URLSearchParams({ limit: '200' });
142
+ if (scope) params.set('scope', scope);
143
+ if (category) params.set('category', category);
144
+ const token = new URLSearchParams(location.search).get('token') || '';
145
+ if (token) params.set('token', token);
146
+ try {
147
+ const res = await fetch(`api/memory?${params}`, { cache: 'no-store' });
148
+ if (!res.ok) return;
149
+ const data = await res.json();
150
+ renderMemoryList(data.items || []);
151
+ $$('#memoryScopes .scope').forEach((btn) => btn.classList.remove('active'));
152
+ if (activeButton) activeButton.classList.add('active');
153
+ } catch { showToast('筛选失败'); }
154
+ }
155
+
120
156
  async function initMemoryData() {
121
157
  try {
122
158
  const token = new URLSearchParams(location.search).get('token') || '';
@@ -157,37 +193,29 @@ async function initMemoryData() {
157
193
  }
158
194
  const scopes = $('#memoryScopes');
159
195
  if (scopes) {
160
- const scopeButtons = [`<h3>范围</h3><button class="scope active"><span>全部</span><b>${data.total || 0}</b></button>`];
196
+ const scopeButtons = [`<h3>范围</h3><button class="scope active" data-scope="" data-category=""><span>全部</span><b>${data.total || 0}</b></button>`];
161
197
  for (const item of data.scopes || []) {
162
198
  const label = item.scope_kind === 'group' ? `群 ${item.scope_id}` : item.scope_kind === 'private' ? `私聊 ${item.scope_id}` : `全局 ${item.scope_id || ''}`;
163
- scopeButtons.push(`<button class="scope"><span>${label}</span><b>${item.n}</b></button>`);
199
+ scopeButtons.push(`<button class="scope" data-scope="${item.scope_kind}:${item.scope_id || ''}"><span>${label}</span><b>${item.n}</b></button>`);
164
200
  }
165
201
  scopeButtons.push('<h3 class="mt">分类</h3>');
166
202
  for (const item of data.categories || []) {
167
- scopeButtons.push(`<button class="scope"><span>${item.category}</span><b>${item.n}</b></button>`);
203
+ scopeButtons.push(`<button class="scope" data-category="${String(item.category).replace(/"/g, '&quot;')}"><span>${item.category}</span><b>${item.n}</b></button>`);
168
204
  }
169
205
  scopes.innerHTML = scopeButtons.join('');
170
206
  }
171
- const list = $('#memoryList');
172
- if (!list) return;
173
- if (!data.items.length) {
174
- list.innerHTML = '<div class="memory-empty"><b>还没有记忆</b><p>主插件当前使用 MD 后端,或 SQLite 里还没有数据。<br>切到 SQLite 并执行 <code>memdb migrate</code> 后会出现在这里。</p></div>';
175
- return;
176
- }
177
- list.innerHTML = data.items.map((item, index) => `
178
- <article class="memory-item" data-memory-index="${index}">
179
- <div class="memory-head">
180
- <span class="tag ${item.scope_kind === 'private' ? 'private' : 'group'}">${item.scope_kind === 'group' ? '群' : item.scope_kind === 'private' ? '私聊' : '全局'} ${item.scope_id || ''}</span>
181
- <span class="tag soft">${item.category || '未分类'}</span>
182
- <time>${new Date(item.created_at).toLocaleString('zh-CN', { hour12: false })}</time>
183
- </div>
184
- <p>${String(item.content || '').replace(/[<>&]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;' }[c]))}</p>
185
- <div class="memory-foot"><code>${item.label || '记忆'}</code><span>#${item.id}</span><span class="memory-open">点击查看</span></div>
186
- </article>`).join('');
207
+ renderMemoryList(data.items);
187
208
  } catch { /* 文件预览保留 mock */ }
188
209
  }
189
210
  initMemoryData();
190
211
 
212
+ $('#memoryScopes')?.addEventListener('click', (event) => {
213
+ const btn = event.target.closest('.scope');
214
+ if (!btn) return;
215
+ if (btn.dataset.scope === undefined && btn.dataset.category === undefined) return;
216
+ filterMemory(btn.dataset.scope || '', btn.dataset.category || '', btn).catch(() => {});
217
+ });
218
+
191
219
  function memoryScopeLabel(item) {
192
220
  if (item.scope_kind === 'group') return `群 ${item.scope_id}`;
193
221
  if (item.scope_kind === 'private') return `私聊 ${item.scope_id}`;
@@ -322,24 +350,40 @@ function renderMainGroup(index) {
322
350
  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
351
  + group.fields.map((field) => {
324
352
  const value = mainSchema.values[field.key] ?? field.default;
353
+ const esc = (text) => String(text == null ? '' : text).replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]));
325
354
  let control = '';
326
355
  if (field.type === 'boolean') {
327
- control = `<input type="checkbox" data-key="${field.key}" data-kind="boolean" ${value ? 'checked' : ''} />`;
356
+ control = `<label class="switch"><input type="checkbox" data-key="${field.key}" data-kind="boolean" ${value ? 'checked' : ''} /><i></i><span>${value ? '已开启' : '已关闭'}</span></label>`;
328
357
  } else if (field.type === 'union') {
329
358
  const options = (field.options.length ? field.options : [String(value || '')]).map((opt) => `<option ${String(value) === opt ? 'selected' : ''}>${opt}</option>`).join('');
330
359
  control = `<select data-key="${field.key}" data-kind="string">${options}</select>`;
331
360
  } else if (field.type === 'number') {
332
361
  control = `<input type="number" data-key="${field.key}" data-kind="number" value="${value ?? field.default ?? 0}" />`;
333
362
  } 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>`;
363
+ 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>`;
364
+ } else if (field.long) {
365
+ control = `<textarea rows="7" data-key="${field.key}" data-kind="string">${esc(value ?? field.default ?? '')}</textarea>`;
335
366
  } else {
336
- control = `<input ${field.sensitive ? 'type="password"' : ''} data-key="${field.key}" data-kind="string" value="${String(value ?? field.default ?? '').replace(/"/g, '&quot;')}" />`;
367
+ control = `<input ${field.sensitive ? 'type="password"' : ''} data-key="${field.key}" data-kind="string" value="${esc(value ?? field.default ?? '')}" />`;
337
368
  }
338
- return `<div class="schema-field"><label class="field"><span>${field.key}</span>${control}</label><p class="form-hint">${field.description || ''}</p></div>`;
369
+ return `<div class="schema-field ${field.long ? 'long' : ''}">
370
+ <div class="schema-field-info">
371
+ <div class="schema-field-title">${esc(field.label || field.key)}</div>
372
+ <p class="schema-field-desc">${esc(field.description || '')}</p>
373
+ <code class="schema-field-key">${field.key}</code>
374
+ </div>
375
+ <div class="schema-field-control">${control}</div>
376
+ </div>`;
339
377
  }).join('')
340
378
  + `<div class="form-foot"><span>保存只改本组字段;写前备份 koishi.yml,重启后生效</span></div>`;
341
379
  const save = $('#saveMainSchemaGroup');
342
380
  if (save) save.onclick = () => saveMainGroup(group);
381
+ $$('#mainSchemaForm .switch input').forEach((input) => {
382
+ input.addEventListener('change', () => {
383
+ const text = input.parentElement?.querySelector('span');
384
+ if (text) text.textContent = input.checked ? '已开启' : '已关闭';
385
+ });
386
+ });
343
387
  }
344
388
 
345
389
  async function saveMainGroup(group) {
package/public/styles.css CHANGED
@@ -304,7 +304,25 @@ code { background: #f2f5f9; color: #55657d; border-radius: 4px; padding: 1px 4px
304
304
  .word-list { display: flex; flex-wrap: wrap; gap: 7px; }
305
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; }
306
306
  .word-list button { color: #a6b2c2; font-size: 12px; line-height: 1; }
307
- .schema-field { padding: 11px 0; border-bottom: 1px solid #f1f4f8; }
307
+ .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
+ .schema-field.long { grid-template-columns: 1fr; gap: 10px; }
309
+ .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; }
312
+ .schema-field-key { display: inline-block; font-size: 9px; color: #a0adbe; background: #f5f7fa; border: 1px solid #eef2f7; border-radius: 4px; padding: 2px 5px; }
313
+ .schema-field-control { min-width: 0; padding-top: 1px; }
314
+ .schema-field-control > input:not([type=checkbox]), .schema-field-control > select, .schema-field-control > textarea { width: 100%; }
315
+ .schema-field-control > input:not([type=checkbox]), .schema-field-control > select { height: 36px; border: 1px solid #e2e8f1; border-radius: 8px; background: #fbfcfe; padding: 0 10px; font-size: 12px; color: #3b4b61; outline: none; }
316
+ .schema-field-control > textarea { border: 1px solid #e2e8f1; border-radius: 8px; background: #fbfcfe; padding: 9px 10px; font-size: 12px; color: #3b4b61; outline: none; resize: vertical; line-height: 1.65; font-family: inherit; }
317
+ .schema-field-control > textarea:focus, .schema-field-control > input:focus, .schema-field-control > select:focus { border-color: #9ab7ef; background: #fff; }
318
+ .schema-field .json-input { font-family: Consolas, monospace; font-size: 10.5px; }
319
+ .switch { display: inline-flex; align-items: center; gap: 9px; cursor: pointer; }
320
+ .switch input { display: none; }
321
+ .switch i { width: 34px; height: 19px; border-radius: 20px; background: #dfe5ee; position: relative; transition: .15s; }
322
+ .switch i::after { content: ""; position: absolute; width: 15px; height: 15px; border-radius: 50%; background: #fff; left: 2px; top: 2px; box-shadow: 0 1px 3px rgba(20,35,55,.2); transition: .15s; }
323
+ .switch input:checked + i { background: #4f7df3; }
324
+ .switch input:checked + i::after { transform: translateX(15px); }
325
+ .switch span { font-size: 10.5px; color: #9aa7b8; }
308
326
  .schema-field .field > span { flex: 0 0 200px; font-family: Consolas, monospace; font-size: 10.5px; color: #3f5270; }
309
327
  .schema-field .field input:not([type=checkbox]), .schema-field .field select, .schema-field .field textarea { width: 100%; }
310
328
  .form-hint { margin: 5px 0 0 200px; font-size: 9.5px; color: #9aa7b8; line-height: 1.5; }