chatluna-plugin-kb 0.1.1 → 0.1.3

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.
@@ -53,8 +53,9 @@ const RULES_CHARS_MAX = 256 * 1024
53
53
  * testEmbedding?: (input: {baseUrl: string, model?: string, apiKey?: string}) => unknown,
54
54
  * saveEmbedding?: (input: {baseUrl: string, model?: string, apiKeyFile?: string, apiKey?: string, dimension?: number}) => unknown,
55
55
  * listChatlunaModels?: () => unknown,
56
- * importChatlunaModel?: (input: {role: 'embedding' | 'vlm', providerId: string, modelId: string}) => unknown,
57
- * listWeKnoraModels?: () => unknown
56
+ * importChatlunaModel?: (input: {role: 'embedding' | 'chat' | 'vlm' | 'rerank', providerId: string, modelId: string}) => unknown,
57
+ * listWeKnoraModels?: () => unknown,
58
+ * ruleOptions?: () => unknown
58
59
  * }} deps
59
60
  * @returns {() => Promise<void>} dispose
60
61
  */
@@ -152,6 +153,9 @@ export function registerKbWebUI(ctx, deps) {
152
153
  return { ok: true }
153
154
  })
154
155
  }
156
+ if (has('ruleOptions')) {
157
+ action('kb.ruleOptions', async () => ({ ok: true, options: publicRuleOptions(await settings.ruleOptions()) }))
158
+ }
155
159
 
156
160
  // ---- Phase 4 安装向导 --------------------------------------------------
157
161
  // 所有安装/Embedding 动作都走同一套参数校验:只接受纯字符串/数字,绝不
@@ -264,6 +268,78 @@ export function registerKbWebUI(ctx, deps) {
264
268
  }
265
269
  }
266
270
 
271
+ // ---- 规则选项(只读、脱敏、有界) -----------------------------------------
272
+ const OPTION_MAX = 200
273
+ const OPTION_TEXT_MAX = 160
274
+
275
+ function optionText(value) {
276
+ if (typeof value !== 'string') return ''
277
+ // 群名/昵称来自适配器,可能夹带控制字符;先清洗再截断。
278
+ const text = value.replace(/[\u0000-\u001f\u007f]+/g, ' ').trim()
279
+ return text === '' ? '' : text.slice(0, OPTION_TEXT_MAX)
280
+ }
281
+
282
+ function optionList(value) {
283
+ return Array.isArray(value) ? value.slice(0, OPTION_MAX) : []
284
+ }
285
+
286
+ /** 只投影规则编辑器需要的字段;bot/guild/user 的其他属性一律丢弃。 */
287
+ export function publicRuleOptions(input) {
288
+ const source = input !== null && typeof input === 'object' ? input : {}
289
+ const platforms = []
290
+ const seenPlatforms = new Set()
291
+ for (const raw of optionList(source.platforms)) {
292
+ const value = optionText(raw)
293
+ if (value === '' || seenPlatforms.has(value)) continue
294
+ seenPlatforms.add(value)
295
+ platforms.push(value)
296
+ }
297
+ const people = (key) => {
298
+ const out = []
299
+ const seen = new Set()
300
+ for (const raw of optionList(source[key])) {
301
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) continue
302
+ const platform = optionText(raw.platform)
303
+ const id = optionText(raw.id)
304
+ if (id === '') continue
305
+ const dedupe = `${platform}\u0000${id}`
306
+ if (seen.has(dedupe)) continue
307
+ seen.add(dedupe)
308
+ const item = { platform, id }
309
+ const name = optionText(raw.name)
310
+ if (name !== '') item.name = name
311
+ out.push(item)
312
+ }
313
+ return out
314
+ }
315
+ const kbs = []
316
+ const seenKbs = new Set()
317
+ for (const raw of optionList(source.kbs)) {
318
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) continue
319
+ const id = optionText(raw.id)
320
+ if (id === '' || seenKbs.has(id)) continue
321
+ seenKbs.add(id)
322
+ const item = { id }
323
+ const name = optionText(raw.name)
324
+ if (name !== '') item.name = name
325
+ kbs.push(item)
326
+ }
327
+ return {
328
+ platforms,
329
+ guilds: people('guilds'),
330
+ users: people('users'),
331
+ kbs,
332
+ levels: [
333
+ { value: 0, label: '0 · 访客' },
334
+ { value: 1, label: '1 · 普通用户' },
335
+ { value: 2, label: '2 · 成员' },
336
+ { value: 3, label: '3 · 群管' },
337
+ { value: 4, label: '4 · 管理员' },
338
+ { value: 5, label: '5 · 主人' }
339
+ ]
340
+ }
341
+ }
342
+
267
343
  /**
268
344
  * 只接受 [{ ... }] 这种纯数据数组:JSON 往返顺带丢掉函数/原型并给出确定性大小上界。
269
345
  * 规则解释(match 字段、白名单语义)属于 policy 层,这里不重复实现。
@@ -402,7 +478,7 @@ const MODEL_URL_MAX = 2048
402
478
  const MODEL_CAP_MAX = 32
403
479
  const MODEL_CAP_LEN_MAX = 64
404
480
  const MODEL_DIMENSION_MAX = 65536
405
- const MODEL_ROLES = new Set(['embedding', 'vlm'])
481
+ const MODEL_ROLES = new Set(['embedding', 'chat', 'vlm', 'rerank'])
406
482
  const CONTROL_RE = /[\u0000-\u001f\u007f]/
407
483
 
408
484
  function cleanModelId(value) {
@@ -416,7 +492,7 @@ function cleanModelId(value) {
416
492
  /** 校验 `{ role, providerId, modelId }`;返回新对象,绝不放行多余字段。 */
417
493
  export function modelImportInput(input) {
418
494
  const role = asText(input?.role)
419
- if (!MODEL_ROLES.has(role)) throw invalid('role must be embedding or vlm')
495
+ if (!MODEL_ROLES.has(role)) throw invalid('role must be embedding, chat, vlm or rerank')
420
496
  return {
421
497
  role,
422
498
  providerId: cleanModelId(input?.providerId),
@@ -452,6 +528,9 @@ export function publicModelCard(input) {
452
528
  const type = modelString(input.type, MODEL_TYPE_MAX)
453
529
  if (type !== undefined) out.type = type
454
530
  const baseUrl = modelString(input.baseUrl, MODEL_URL_MAX)
531
+ if (input.supportsVision === true) out.supportsVision = true
532
+ const protocolHint = modelString(input.protocolHint, MODEL_TYPE_MAX)
533
+ if (protocolHint !== undefined) out.protocolHint = protocolHint
455
534
  if (baseUrl !== undefined) out.baseUrl = baseUrl
456
535
  const source = modelString(input.source, MODEL_TYPE_MAX)
457
536
  if (source !== undefined) out.source = source
@@ -484,6 +563,7 @@ export function publicImportResult(input) {
484
563
  if (name !== undefined) result.name = name
485
564
  const type = modelString(value.type, MODEL_TYPE_MAX)
486
565
  if (type !== undefined) result.type = type
566
+ if (value.supportsVision === true) result.supportsVision = true
487
567
  const dimension = Number(value.dimension)
488
568
  if (Number.isSafeInteger(dimension) && dimension > 0 && dimension <= MODEL_DIMENSION_MAX) {
489
569
  result.dimension = dimension
package/web/client.css CHANGED
@@ -302,6 +302,144 @@
302
302
  max-width: 40rem;
303
303
  }
304
304
 
305
+ .kb-rule-dialog-wide {
306
+ max-width: 52rem;
307
+ }
308
+
309
+ .kb-rule-picker {
310
+ display: flex;
311
+ width: 100%;
312
+ flex-direction: column;
313
+ gap: 0.375rem;
314
+ }
315
+
316
+ .kb-rule-picker input,
317
+ .kb-rule-picker-input {
318
+ width: 100%;
319
+ }
320
+
321
+ .kb-rule-picker-name {
322
+ display: block;
323
+ overflow: hidden;
324
+ text-overflow: ellipsis;
325
+ white-space: nowrap;
326
+ }
327
+
328
+ .kb-rule-matrix {
329
+ display: flex;
330
+ flex-direction: column;
331
+ gap: 0.5rem;
332
+ border: 1px solid var(--border);
333
+ border-radius: var(--control-radius);
334
+ padding: 0.625rem 0.75rem;
335
+ }
336
+
337
+ .kb-rule-matrix-disabled {
338
+ opacity: 0.7;
339
+ }
340
+
341
+ .kb-rule-matrix-global,
342
+ .kb-rule-matrix-row {
343
+ display: grid;
344
+ grid-template-columns: minmax(0, 1fr) repeat(3, 4rem) 3rem;
345
+ align-items: center;
346
+ gap: 0.5rem;
347
+ }
348
+
349
+ .kb-rule-matrix-global {
350
+ border-bottom: 1px solid var(--border);
351
+ padding-bottom: 0.5rem;
352
+ }
353
+
354
+ .kb-rule-matrix-title {
355
+ font-size: 0.8125rem;
356
+ font-weight: 500;
357
+ }
358
+
359
+ .kb-rule-matrix-head {
360
+ color: var(--muted-foreground);
361
+ font-size: 0.75rem;
362
+ }
363
+
364
+ .kb-rule-matrix-head span:not(:first-child) {
365
+ text-align: center;
366
+ }
367
+
368
+ .kb-rule-matrix-body {
369
+ display: flex;
370
+ max-height: 16rem;
371
+ flex-direction: column;
372
+ gap: 0.375rem;
373
+ overflow-y: auto;
374
+ }
375
+
376
+ .kb-rule-matrix-cell {
377
+ display: inline-flex;
378
+ align-items: center;
379
+ justify-content: center;
380
+ }
381
+
382
+ .kb-rule-matrix-default-row {
383
+ border-top: 1px dashed var(--border);
384
+ padding-top: 0.375rem;
385
+ }
386
+
387
+ .kb-rule-matrix-default-label {
388
+ grid-column: 1 / span 4;
389
+ font-size: 0.75rem;
390
+ }
391
+
392
+ .kb-rule-matrix-row {
393
+ min-height: 2rem;
394
+ padding-block: 0.125rem;
395
+ }
396
+
397
+ .kb-rule-matrix-kb {
398
+ display: flex;
399
+ min-width: 0;
400
+ flex-direction: column;
401
+ gap: 0.0625rem;
402
+ }
403
+
404
+ .kb-rule-matrix-name {
405
+ overflow: hidden;
406
+ font-size: 0.8125rem;
407
+ text-overflow: ellipsis;
408
+ white-space: nowrap;
409
+ }
410
+
411
+ .kb-rule-matrix-id {
412
+ overflow: hidden;
413
+ color: var(--muted-foreground);
414
+ font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
415
+ font-size: 0.6875rem;
416
+ text-overflow: ellipsis;
417
+ white-space: nowrap;
418
+ }
419
+
420
+ .kb-rule-check {
421
+ display: inline-flex;
422
+ align-items: center;
423
+ justify-content: center;
424
+ gap: 0.25rem;
425
+ font-size: 0.75rem;
426
+ }
427
+
428
+ .kb-rule-advanced {
429
+ border-top: 1px dashed var(--border);
430
+ padding-top: 0.5rem;
431
+ }
432
+
433
+ .kb-rule-advanced summary {
434
+ cursor: pointer;
435
+ color: var(--muted-foreground);
436
+ font-size: 0.75rem;
437
+ }
438
+
439
+ .kb-rule-advanced > div {
440
+ margin-top: 0.5rem;
441
+ }
442
+
305
443
  .kb-rule-head:focus-visible {
306
444
  outline: 2px solid var(--ring);
307
445
  outline-offset: -2px;
@@ -406,6 +544,12 @@
406
544
  grid-template-columns: minmax(0, 1fr);
407
545
  }
408
546
 
547
+ .kb-rule-matrix-global,
548
+ .kb-rule-matrix-row {
549
+ grid-template-columns: minmax(0, 1fr) repeat(3, 3rem) 2.5rem;
550
+ gap: 0.25rem;
551
+ }
552
+
409
553
  .kb-rule-bar {
410
554
  flex-wrap: wrap;
411
555
  }
@@ -511,12 +655,45 @@
511
655
  }
512
656
 
513
657
  /* ---- 模型复制 --------------------------------------------------------- */
658
+ .kb-model-toolbar {
659
+ display: flex;
660
+ flex-wrap: wrap;
661
+ align-items: center;
662
+ gap: 0.5rem;
663
+ }
664
+
665
+ .kb-model-toolbar > :first-child {
666
+ min-width: 14rem;
667
+ flex: 1 1 16rem;
668
+ }
669
+
670
+ .kb-model-scroll {
671
+ display: flex;
672
+ max-height: 22rem;
673
+ flex-direction: column;
674
+ gap: 0.75rem;
675
+ overflow-y: auto;
676
+ overscroll-behavior: contain;
677
+ padding-right: 0.25rem;
678
+ }
679
+
514
680
  .kb-model-group {
515
681
  display: flex;
516
682
  flex-direction: column;
517
683
  gap: 0.375rem;
518
684
  }
519
685
 
686
+ .kb-model-group-head {
687
+ display: flex;
688
+ align-items: center;
689
+ justify-content: space-between;
690
+ gap: 0.5rem;
691
+ }
692
+
693
+ .kb-model-more {
694
+ align-self: flex-start;
695
+ }
696
+
520
697
  .kb-model-group-title {
521
698
  margin: 0;
522
699
  color: var(--muted-foreground);