chatluna-plugin-kb 0.1.1 → 0.1.2

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,77 @@ 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
+ const text = value.trim()
278
+ return text === '' ? '' : text.slice(0, OPTION_TEXT_MAX)
279
+ }
280
+
281
+ function optionList(value) {
282
+ return Array.isArray(value) ? value.slice(0, OPTION_MAX) : []
283
+ }
284
+
285
+ /** 只投影规则编辑器需要的字段;bot/guild/user 的其他属性一律丢弃。 */
286
+ export function publicRuleOptions(input) {
287
+ const source = input !== null && typeof input === 'object' ? input : {}
288
+ const platforms = []
289
+ const seenPlatforms = new Set()
290
+ for (const raw of optionList(source.platforms)) {
291
+ const value = optionText(raw)
292
+ if (value === '' || seenPlatforms.has(value)) continue
293
+ seenPlatforms.add(value)
294
+ platforms.push(value)
295
+ }
296
+ const people = (key) => {
297
+ const out = []
298
+ const seen = new Set()
299
+ for (const raw of optionList(source[key])) {
300
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) continue
301
+ const platform = optionText(raw.platform)
302
+ const id = optionText(raw.id)
303
+ if (id === '') continue
304
+ const dedupe = `${platform}\u0000${id}`
305
+ if (seen.has(dedupe)) continue
306
+ seen.add(dedupe)
307
+ const item = { platform, id }
308
+ const name = optionText(raw.name)
309
+ if (name !== '') item.name = name
310
+ out.push(item)
311
+ }
312
+ return out
313
+ }
314
+ const kbs = []
315
+ const seenKbs = new Set()
316
+ for (const raw of optionList(source.kbs)) {
317
+ if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) continue
318
+ const id = optionText(raw.id)
319
+ if (id === '' || seenKbs.has(id)) continue
320
+ seenKbs.add(id)
321
+ const item = { id }
322
+ const name = optionText(raw.name)
323
+ if (name !== '') item.name = name
324
+ kbs.push(item)
325
+ }
326
+ return {
327
+ platforms,
328
+ guilds: people('guilds'),
329
+ users: people('users'),
330
+ kbs,
331
+ levels: [
332
+ { value: 0, label: '0 · 访客' },
333
+ { value: 1, label: '1 · 普通用户' },
334
+ { value: 2, label: '2 · 成员' },
335
+ { value: 3, label: '3 · 群管' },
336
+ { value: 4, label: '4 · 管理员' },
337
+ { value: 5, label: '5 · 主人' }
338
+ ]
339
+ }
340
+ }
341
+
267
342
  /**
268
343
  * 只接受 [{ ... }] 这种纯数据数组:JSON 往返顺带丢掉函数/原型并给出确定性大小上界。
269
344
  * 规则解释(match 字段、白名单语义)属于 policy 层,这里不重复实现。
@@ -402,7 +477,7 @@ const MODEL_URL_MAX = 2048
402
477
  const MODEL_CAP_MAX = 32
403
478
  const MODEL_CAP_LEN_MAX = 64
404
479
  const MODEL_DIMENSION_MAX = 65536
405
- const MODEL_ROLES = new Set(['embedding', 'vlm'])
480
+ const MODEL_ROLES = new Set(['embedding', 'chat', 'vlm', 'rerank'])
406
481
  const CONTROL_RE = /[\u0000-\u001f\u007f]/
407
482
 
408
483
  function cleanModelId(value) {
@@ -416,7 +491,7 @@ function cleanModelId(value) {
416
491
  /** 校验 `{ role, providerId, modelId }`;返回新对象,绝不放行多余字段。 */
417
492
  export function modelImportInput(input) {
418
493
  const role = asText(input?.role)
419
- if (!MODEL_ROLES.has(role)) throw invalid('role must be embedding or vlm')
494
+ if (!MODEL_ROLES.has(role)) throw invalid('role must be embedding, chat, vlm or rerank')
420
495
  return {
421
496
  role,
422
497
  providerId: cleanModelId(input?.providerId),
@@ -452,6 +527,9 @@ export function publicModelCard(input) {
452
527
  const type = modelString(input.type, MODEL_TYPE_MAX)
453
528
  if (type !== undefined) out.type = type
454
529
  const baseUrl = modelString(input.baseUrl, MODEL_URL_MAX)
530
+ if (input.supportsVision === true) out.supportsVision = true
531
+ const protocolHint = modelString(input.protocolHint, MODEL_TYPE_MAX)
532
+ if (protocolHint !== undefined) out.protocolHint = protocolHint
455
533
  if (baseUrl !== undefined) out.baseUrl = baseUrl
456
534
  const source = modelString(input.source, MODEL_TYPE_MAX)
457
535
  if (source !== undefined) out.source = source
@@ -484,6 +562,7 @@ export function publicImportResult(input) {
484
562
  if (name !== undefined) result.name = name
485
563
  const type = modelString(value.type, MODEL_TYPE_MAX)
486
564
  if (type !== undefined) result.type = type
565
+ if (value.supportsVision === true) result.supportsVision = true
487
566
  const dimension = Number(value.dimension)
488
567
  if (Number.isSafeInteger(dimension) && dimension > 0 && dimension <= MODEL_DIMENSION_MAX) {
489
568
  result.dimension = dimension
package/web/client.css CHANGED
@@ -302,6 +302,136 @@
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
+ width: 100%;
318
+ }
319
+
320
+ .kb-rule-matrix {
321
+ display: flex;
322
+ flex-direction: column;
323
+ gap: 0.5rem;
324
+ border: 1px solid var(--border);
325
+ border-radius: var(--control-radius);
326
+ padding: 0.625rem 0.75rem;
327
+ }
328
+
329
+ .kb-rule-matrix-disabled {
330
+ opacity: 0.7;
331
+ }
332
+
333
+ .kb-rule-matrix-global,
334
+ .kb-rule-matrix-row {
335
+ display: grid;
336
+ grid-template-columns: minmax(0, 1fr) repeat(3, 4rem) 3rem;
337
+ align-items: center;
338
+ gap: 0.5rem;
339
+ }
340
+
341
+ .kb-rule-matrix-global {
342
+ border-bottom: 1px solid var(--border);
343
+ padding-bottom: 0.5rem;
344
+ }
345
+
346
+ .kb-rule-matrix-title {
347
+ font-size: 0.8125rem;
348
+ font-weight: 500;
349
+ }
350
+
351
+ .kb-rule-matrix-head {
352
+ color: var(--muted-foreground);
353
+ font-size: 0.75rem;
354
+ }
355
+
356
+ .kb-rule-matrix-head span:not(:first-child) {
357
+ text-align: center;
358
+ }
359
+
360
+ .kb-rule-matrix-body {
361
+ display: flex;
362
+ max-height: 16rem;
363
+ flex-direction: column;
364
+ gap: 0.375rem;
365
+ overflow-y: auto;
366
+ }
367
+
368
+ .kb-rule-matrix-cell {
369
+ display: inline-flex;
370
+ align-items: center;
371
+ justify-content: center;
372
+ }
373
+
374
+ .kb-rule-matrix-default-row {
375
+ border-top: 1px dashed var(--border);
376
+ padding-top: 0.375rem;
377
+ }
378
+
379
+ .kb-rule-matrix-default-label {
380
+ grid-column: 1 / span 4;
381
+ font-size: 0.75rem;
382
+ }
383
+
384
+ .kb-rule-matrix-row {
385
+ min-height: 2rem;
386
+ padding-block: 0.125rem;
387
+ }
388
+
389
+ .kb-rule-matrix-kb {
390
+ display: flex;
391
+ min-width: 0;
392
+ flex-direction: column;
393
+ gap: 0.0625rem;
394
+ }
395
+
396
+ .kb-rule-matrix-name {
397
+ overflow: hidden;
398
+ font-size: 0.8125rem;
399
+ text-overflow: ellipsis;
400
+ white-space: nowrap;
401
+ }
402
+
403
+ .kb-rule-matrix-id {
404
+ overflow: hidden;
405
+ color: var(--muted-foreground);
406
+ font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
407
+ font-size: 0.6875rem;
408
+ text-overflow: ellipsis;
409
+ white-space: nowrap;
410
+ }
411
+
412
+ .kb-rule-check {
413
+ display: inline-flex;
414
+ align-items: center;
415
+ justify-content: center;
416
+ gap: 0.25rem;
417
+ font-size: 0.75rem;
418
+ }
419
+
420
+ .kb-rule-advanced {
421
+ border-top: 1px dashed var(--border);
422
+ padding-top: 0.5rem;
423
+ }
424
+
425
+ .kb-rule-advanced summary {
426
+ cursor: pointer;
427
+ color: var(--muted-foreground);
428
+ font-size: 0.75rem;
429
+ }
430
+
431
+ .kb-rule-advanced > div {
432
+ margin-top: 0.5rem;
433
+ }
434
+
305
435
  .kb-rule-head:focus-visible {
306
436
  outline: 2px solid var(--ring);
307
437
  outline-offset: -2px;
@@ -406,6 +536,12 @@
406
536
  grid-template-columns: minmax(0, 1fr);
407
537
  }
408
538
 
539
+ .kb-rule-matrix-global,
540
+ .kb-rule-matrix-row {
541
+ grid-template-columns: minmax(0, 1fr) repeat(3, 3rem) 2.5rem;
542
+ gap: 0.25rem;
543
+ }
544
+
409
545
  .kb-rule-bar {
410
546
  flex-wrap: wrap;
411
547
  }