chatluna-plugin-kb 0.1.2 → 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.
package/index.mjs CHANGED
@@ -1265,10 +1265,44 @@ export function apply(ctx, config) {
1265
1265
  return out
1266
1266
  }
1267
1267
 
1268
- const [guilds, users] = await Promise.all([
1268
+ const [botGuilds, botUsers] = await Promise.all([
1269
1269
  collect('getGuildList'),
1270
1270
  collect('getFriendList')
1271
1271
  ])
1272
+ // routing.peers 记录了历史会话里的群/用户(含 QQ openid 映射后的名称);
1273
+ // 适配器不提供 getGuildList/getFriendList 时,这是唯一可靠的候选来源。
1274
+ const peersOf = async (kind) => {
1275
+ try {
1276
+ const service = typeof ctx.get === 'function' ? ctx.get('routing') : undefined
1277
+ if (typeof service?.peers?.search !== 'function') return []
1278
+ return await service.peers.search({ kind, limit })
1279
+ } catch {
1280
+ return []
1281
+ }
1282
+ }
1283
+ const mergePeers = (target, rows) => {
1284
+ const seen = new Set(target.map((item) => `${item.platform}\u0000${item.id}`))
1285
+ for (const row of Array.isArray(rows) ? rows : []) {
1286
+ const platform = String(row?.platform ?? '').trim().slice(0, 80)
1287
+ const id = String(row?.id ?? '').trim()
1288
+ if (id === '' || seen.has(`${platform}\u0000${id}`)) continue
1289
+ seen.add(`${platform}\u0000${id}`)
1290
+ const name = String(row?.name ?? '').trim().slice(0, 120)
1291
+ target.push(name === '' ? { platform, id } : { platform, id, name })
1292
+ if (target.length >= limit) break
1293
+ }
1294
+ return target
1295
+ }
1296
+ const [guildPeers, userPeers] = await Promise.all([peersOf('guild'), peersOf('user')])
1297
+ const guilds = mergePeers(botGuilds, guildPeers)
1298
+ const users = mergePeers(botUsers, userPeers)
1299
+ for (const item of [...guilds, ...users]) {
1300
+ const platform = String(item?.platform ?? '').trim()
1301
+ if (platform !== '' && !seenPlatforms.has(platform)) {
1302
+ seenPlatforms.add(platform)
1303
+ platforms.push(platform)
1304
+ }
1305
+ }
1272
1306
  let kbs = []
1273
1307
  try {
1274
1308
  const list = await catalog()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatluna-plugin-kb",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "ChatLuna v2 knowledge base plugin: WeKnora REST tools, managed weknora-lite engine and WebUI install wizard",
5
5
  "type": "module",
6
6
  "main": "index.mjs",
@@ -162,6 +162,34 @@ export function routeBaseUrlOf(route, role = '') {
162
162
  return ''
163
163
  }
164
164
 
165
+ /**
166
+ * 从路由的 apiKey 池里选出该模型实际使用的一把 key。
167
+ * ChatLuna 的 `route.apiKey` 可能是逗号/空白分隔的多把 key(splitApiKeys 去重保序),
168
+ * `model.keys` 是这些 key 的下标数组;取第一个有效下标,否则回退第 0 把。
169
+ */
170
+ function splitApiKeys(value) {
171
+ if (typeof value !== 'string' || value === '') return []
172
+ const keys = []
173
+ const seen = new Set()
174
+ for (const part of value.split(/[\s,]+/)) {
175
+ if (part === '' || seen.has(part)) continue
176
+ seen.add(part)
177
+ keys.push(part)
178
+ }
179
+ return keys
180
+ }
181
+
182
+ export function selectRouteApiKey(routeApiKey, modelKeys) {
183
+ const keys = splitApiKeys(routeApiKey)
184
+ if (keys.length === 0) return ''
185
+ const indexes = Array.isArray(modelKeys) ? modelKeys : []
186
+ for (const index of indexes) {
187
+ if (!Number.isSafeInteger(index) || index < 0 || index >= keys.length) continue
188
+ return keys[index]
189
+ }
190
+ return keys[0]
191
+ }
192
+
165
193
  function protocolHintOf(route, baseUrl) {
166
194
  const pkg = textOf(route?.packageName, ID_MAX)
167
195
  if (baseUrl === '') return undefined
@@ -459,14 +487,15 @@ export function createModelBridge(options = {}) {
459
487
  const materializedRoute =
460
488
  (index >= 0 ? routes[index] : undefined) ??
461
489
  routes.find((route) => isPlainObject(route) && route.id === found.route.id)
462
- const apiKey = textOf(materializedRoute?.apiKey, 8192)
490
+ const keyPool = textOf(materializedRoute?.apiKey, 8192)
491
+ const apiKey = selectRouteApiKey(keyPool, found.model?.keys)
463
492
  if (apiKey === '') {
464
493
  throw new ModelBridgeError('MISSING_CREDENTIAL', '该模型配置缺少可用的 API Key')
465
494
  }
466
495
 
467
496
  const token = await tokenOf()
468
- // 密钥只进这一次请求体;后续错误与返回值都按 secrets 擦除。
469
- const secrets = [apiKey, token]
497
+ // 密钥只进这一次请求体;错误按整池、单 key 与 token 一起擦除。
498
+ const secrets = [keyPool, ...splitApiKeys(keyPool), apiKey, token]
470
499
  const body = buildModelCreateBody({
471
500
  role: found.type,
472
501
  baseUrl: found.baseUrl,
@@ -274,7 +274,8 @@ const OPTION_TEXT_MAX = 160
274
274
 
275
275
  function optionText(value) {
276
276
  if (typeof value !== 'string') return ''
277
- const text = value.trim()
277
+ // 群名/昵称来自适配器,可能夹带控制字符;先清洗再截断。
278
+ const text = value.replace(/[\u0000-\u001f\u007f]+/g, ' ').trim()
278
279
  return text === '' ? '' : text.slice(0, OPTION_TEXT_MAX)
279
280
  }
280
281
 
package/web/client.css CHANGED
@@ -313,10 +313,18 @@
313
313
  gap: 0.375rem;
314
314
  }
315
315
 
316
- .kb-rule-picker input {
316
+ .kb-rule-picker input,
317
+ .kb-rule-picker-input {
317
318
  width: 100%;
318
319
  }
319
320
 
321
+ .kb-rule-picker-name {
322
+ display: block;
323
+ overflow: hidden;
324
+ text-overflow: ellipsis;
325
+ white-space: nowrap;
326
+ }
327
+
320
328
  .kb-rule-matrix {
321
329
  display: flex;
322
330
  flex-direction: column;
@@ -647,12 +655,45 @@
647
655
  }
648
656
 
649
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
+
650
680
  .kb-model-group {
651
681
  display: flex;
652
682
  flex-direction: column;
653
683
  gap: 0.375rem;
654
684
  }
655
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
+
656
697
  .kb-model-group-title {
657
698
  margin: 0;
658
699
  color: var(--muted-foreground);
package/web/client.js CHANGED
@@ -33,6 +33,12 @@ import {
33
33
  Collapsible,
34
34
  CollapsibleContent,
35
35
  CollapsibleTrigger,
36
+ Combobox,
37
+ ComboboxContent,
38
+ ComboboxEmpty,
39
+ ComboboxInput,
40
+ ComboboxItem,
41
+ ComboboxList,
36
42
  CopyButton,
37
43
  Dialog,
38
44
  DialogContent,
@@ -1149,6 +1155,84 @@ function modelSelectionKey(item) {
1149
1155
  return `${item?.providerId ?? ''}\u0000${item?.modelId ?? ''}\u0000${item?.type ?? ''}`
1150
1156
  }
1151
1157
 
1158
+ const MODEL_FILTER_LABELS = {
1159
+ chat: '对话',
1160
+ embedding: 'Embedding',
1161
+ vlm: 'VLM',
1162
+ rerank: 'ReRank'
1163
+ }
1164
+ const MODEL_GROUP_LIMIT = 5
1165
+
1166
+ const providerGroupOf = (item) => {
1167
+ const label = typeof item?.providerLabel === 'string' ? item.providerLabel.trim() : ''
1168
+ if (label !== '') return label
1169
+ const id = typeof item?.providerId === 'string' ? item.providerId.trim() : ''
1170
+ return id === '' ? '—' : id
1171
+ }
1172
+
1173
+ /**
1174
+ * 模型复制列表的搜索/类型筛选/厂商分组(纯函数,SSR 与单测共用)。
1175
+ * - 类型计数与 chips 基于完整目录,只展示有模型的类型;
1176
+ * - 每个厂商默认最多 5 行;选中项所在厂商自动展开到选中行,保证始终可见。
1177
+ * @returns {{matched: object[], counts: Record<string, number>, typeOptions: object[], activeType: string, groups: object[], empty: boolean}}
1178
+ */
1179
+ export function modelCopyReport(models, options = {}) {
1180
+ const list = Array.isArray(models) ? models.filter((item) => item !== null && typeof item === 'object') : []
1181
+ const query = typeof options.query === 'string' ? options.query.trim() : ''
1182
+ const requestedType = typeof options.type === 'string' && options.type !== '' ? options.type : 'all'
1183
+ const selectedKey = typeof options.selectedKey === 'string' ? options.selectedKey : ''
1184
+ const expanded = options.expanded !== null && typeof options.expanded === 'object' ? options.expanded : {}
1185
+ const matched =
1186
+ query === ''
1187
+ ? list
1188
+ : list.filter((item) =>
1189
+ matchesQuery(query, item.providerLabel, item.providerId, item.modelId, item.baseUrl)
1190
+ )
1191
+ // 计数基于完整目录,chips 在搜索过程中保持稳定;只展示有模型的类型。
1192
+ const counts = { all: list.length }
1193
+ for (const item of list) {
1194
+ if (MODEL_TYPE_LABELS[item.type] === undefined) continue
1195
+ counts[item.type] = (counts[item.type] ?? 0) + 1
1196
+ }
1197
+ const typeOptions = [
1198
+ { value: 'all', label: '全部', count: counts.all },
1199
+ ...MODEL_TYPE_ORDER.filter((type) => (counts[type] ?? 0) > 0).map((type) => ({
1200
+ value: type,
1201
+ label: MODEL_FILTER_LABELS[type] ?? type,
1202
+ count: counts[type]
1203
+ }))
1204
+ ]
1205
+ const activeType = requestedType === 'all' || counts[requestedType] > 0 ? requestedType : 'all'
1206
+ const typed = activeType === 'all' ? matched : matched.filter((item) => item.type === activeType)
1207
+ const groupMap = new Map()
1208
+ for (const item of typed) {
1209
+ const key = providerGroupOf(item)
1210
+ if (!groupMap.has(key)) groupMap.set(key, [])
1211
+ groupMap.get(key).push(item)
1212
+ }
1213
+ const groups = []
1214
+ for (const [key, items] of groupMap) {
1215
+ const selectedIndex = items.findIndex((item) => modelSelectionKey(item) === selectedKey)
1216
+ const isExpanded = expanded[key] === true
1217
+ const limit = isExpanded
1218
+ ? items.length
1219
+ : selectedIndex >= MODEL_GROUP_LIMIT
1220
+ ? selectedIndex + 1
1221
+ : Math.min(items.length, MODEL_GROUP_LIMIT)
1222
+ const visible = items.slice(0, limit)
1223
+ groups.push({
1224
+ key,
1225
+ label: key,
1226
+ items: visible,
1227
+ total: items.length,
1228
+ hidden: items.length - visible.length,
1229
+ expanded: isExpanded,
1230
+ hasSelected: selectedIndex >= 0
1231
+ })
1232
+ }
1233
+ return { matched, counts, typeOptions, activeType, groups, empty: typed.length === 0 }
1234
+ }
1235
+
1152
1236
  /**
1153
1237
  * 纯渲染层:所有状态与请求都在 ModelCopyPanel 里,便于 SSR 桩测试直接喂模型。
1154
1238
  * @param {{
@@ -1157,12 +1241,18 @@ function modelSelectionKey(item) {
1157
1241
  * models: Array<object>,
1158
1242
  * error?: string,
1159
1243
  * selectedKey?: string,
1244
+ * query?: string,
1245
+ * type?: string,
1246
+ * expanded?: object,
1160
1247
  * importing?: boolean,
1161
1248
  * copyError?: string,
1162
1249
  * result?: object|null,
1163
1250
  * onToggle?: Function,
1164
1251
  * onLoad?: Function,
1165
1252
  * onSelect?: Function,
1253
+ * onQueryChange?: Function,
1254
+ * onTypeChange?: Function,
1255
+ * onToggleGroup?: Function,
1166
1256
  * onCopy?: Function
1167
1257
  * }} m
1168
1258
  */
@@ -1170,10 +1260,13 @@ export function modelCopyView(m = {}) {
1170
1260
  const models = Array.isArray(m.models) ? m.models : []
1171
1261
  const selectedKey = typeof m.selectedKey === 'string' ? m.selectedKey : ''
1172
1262
  const selected = models.find((item) => modelSelectionKey(item) === selectedKey) ?? null
1173
- const groups = MODEL_TYPE_ORDER.map((type) => ({
1174
- type,
1175
- items: models.filter((item) => item?.type === type)
1176
- })).filter((group) => group.items.length > 0)
1263
+ const query = typeof m.query === 'string' ? m.query : ''
1264
+ const report = modelCopyReport(models, {
1265
+ query,
1266
+ type: m.type,
1267
+ expanded: m.expanded,
1268
+ selectedKey
1269
+ })
1177
1270
  const selectedRoles = selected === null ? [] : importRolesFor(selected)
1178
1271
  const activeImportRole = selectedRoles.includes(m.importRole) ? m.importRole : selected?.type
1179
1272
  const result = m.result !== null && typeof m.result === 'object' ? m.result : null
@@ -1239,64 +1332,132 @@ export function modelCopyView(m = {}) {
1239
1332
  state === 'ready' && models.length === 0
1240
1333
  ? h('p', { className: 'kb-muted-text' }, 'ChatLuna 暂无可用于复制的知识库模型。')
1241
1334
  : null,
1242
- groups.map((group) =>
1243
- h(
1244
- 'div',
1245
- { key: group.type, className: 'kb-model-group' },
1246
- h('p', { className: 'kb-model-group-title' }, MODEL_TYPE_LABELS[group.type] ?? group.type),
1247
- h(
1248
- 'ul',
1249
- { className: 'kb-model-list' },
1250
- group.items.map((item) => {
1251
- const key = modelSelectionKey(item)
1252
- const active = key === selectedKey
1253
- const role = modelRoleLabel(item.type)
1254
- const roles = importRolesFor(item)
1255
- return h(
1256
- 'li',
1257
- { key },
1335
+ state === 'ready' && models.length > 0
1336
+ ? h(
1337
+ 'div',
1338
+ { className: 'kb-model-toolbar' },
1339
+ h(SearchBox, {
1340
+ value: query,
1341
+ onChange: (value) =>
1342
+ typeof m.onQueryChange === 'function' ? m.onQueryChange(value) : undefined,
1343
+ placeholder: '搜索厂商、模型或 baseUrl…',
1344
+ clearLabel: '清除搜索'
1345
+ }),
1346
+ h(FilterChips, {
1347
+ options: report.typeOptions,
1348
+ value: report.activeType,
1349
+ onChange: (value) =>
1350
+ typeof m.onTypeChange === 'function' ? m.onTypeChange(value) : undefined,
1351
+ label: '按模型类型筛选'
1352
+ })
1353
+ )
1354
+ : null,
1355
+ state === 'ready' && models.length > 0 && report.empty
1356
+ ? h('p', { className: 'kb-muted-text' }, '没有匹配的模型;调整搜索关键词或类型筛选。')
1357
+ : null,
1358
+ state === 'ready' && report.groups.length > 0
1359
+ ? h(
1360
+ 'div',
1361
+ { className: 'kb-model-scroll' },
1362
+ report.groups.map((group) =>
1363
+ h(
1364
+ 'div',
1365
+ { key: group.key, className: 'kb-model-group' },
1258
1366
  h(
1259
- 'button',
1260
- {
1261
- type: 'button',
1262
- className: 'kb-model-item',
1263
- 'aria-pressed': active,
1264
- onClick: typeof m.onSelect === 'function' ? () => m.onSelect(key) : undefined
1265
- },
1266
- h(
1267
- 'span',
1268
- { className: 'kb-model-main' },
1269
- h(
1270
- 'span',
1271
- { className: 'kb-model-title' },
1272
- `${item.providerLabel || item.providerId || '—'} · ${item.modelId || '—'}`,
1273
- h(Badge, { variant: item.type === 'embedding' ? 'secondary' : 'outline' }, role),
1274
- item.supportsVision === true ? h(Badge, { variant: 'secondary' }, '支持视觉') : null,
1275
- item.protocolHint ? h(Badge, { variant: 'outline' }, '原生协议') : null
1276
- ),
1277
- h(
1278
- 'span',
1279
- { className: 'kb-model-url', title: item.baseUrl || '' },
1280
- shortenUrl(item.baseUrl)
1281
- ),
1282
- item.protocolHint
1283
- ? h(
1367
+ 'div',
1368
+ { className: 'kb-model-group-head' },
1369
+ h('p', { className: 'kb-model-group-title' }, group.label),
1370
+ h(Badge, { variant: 'outline' }, String(group.total))
1371
+ ),
1372
+ h(
1373
+ 'ul',
1374
+ { className: 'kb-model-list' },
1375
+ group.items.map((item) => {
1376
+ const key = modelSelectionKey(item)
1377
+ const active = key === selectedKey
1378
+ const role = modelRoleLabel(item.type)
1379
+ return h(
1380
+ 'li',
1381
+ { key },
1382
+ h(
1383
+ 'button',
1384
+ {
1385
+ type: 'button',
1386
+ className: 'kb-model-item',
1387
+ 'aria-pressed': active,
1388
+ onClick: typeof m.onSelect === 'function' ? () => m.onSelect(key) : undefined
1389
+ },
1390
+ h(
1284
1391
  'span',
1285
- { className: 'kb-muted-text' },
1286
- '该路由使用非 OpenAI 原生协议,复制后可能需要在 WeKnora 中调整接口类型。'
1287
- )
1288
- : null
1289
-
1290
- ),
1291
- active
1292
- ? h('span', { className: 'kb-model-check', 'aria-hidden': true }, h(UiIcon, { name: 'check' }))
1293
- : null
1294
- )
1392
+ { className: 'kb-model-main' },
1393
+ h(
1394
+ 'span',
1395
+ { className: 'kb-model-title' },
1396
+ `${item.providerLabel || item.providerId || '—'} · ${item.modelId || '—'}`,
1397
+ h(Badge, { variant: item.type === 'embedding' ? 'secondary' : 'outline' }, role),
1398
+ item.supportsVision === true
1399
+ ? h(Badge, { variant: 'secondary' }, '支持视觉')
1400
+ : null,
1401
+ item.protocolHint ? h(Badge, { variant: 'outline' }, '原生协议') : null
1402
+ ),
1403
+ h(
1404
+ 'span',
1405
+ { className: 'kb-model-url', title: item.baseUrl || '' },
1406
+ shortenUrl(item.baseUrl)
1407
+ ),
1408
+ item.protocolHint
1409
+ ? h(
1410
+ 'span',
1411
+ { className: 'kb-muted-text' },
1412
+ '该路由使用非 OpenAI 原生协议,复制后可能需要在 WeKnora 中调整接口类型。'
1413
+ )
1414
+ : null
1415
+ ),
1416
+ active
1417
+ ? h(
1418
+ 'span',
1419
+ { className: 'kb-model-check', 'aria-hidden': true },
1420
+ h(UiIcon, { name: 'check' })
1421
+ )
1422
+ : null
1423
+ )
1424
+ )
1425
+ })
1426
+ ),
1427
+ group.hidden > 0
1428
+ ? h(
1429
+ Button,
1430
+ {
1431
+ variant: 'ghost',
1432
+ size: 'sm',
1433
+ className: 'kb-model-more',
1434
+ onClick: () =>
1435
+ typeof m.onToggleGroup === 'function'
1436
+ ? m.onToggleGroup(group.key)
1437
+ : undefined
1438
+ },
1439
+ `显示更多 ${group.hidden}`
1440
+ )
1441
+ : null,
1442
+ group.expanded && group.total > MODEL_GROUP_LIMIT
1443
+ ? h(
1444
+ Button,
1445
+ {
1446
+ variant: 'ghost',
1447
+ size: 'sm',
1448
+ className: 'kb-model-more',
1449
+ onClick: () =>
1450
+ typeof m.onToggleGroup === 'function'
1451
+ ? m.onToggleGroup(group.key)
1452
+ : undefined
1453
+ },
1454
+ '收起'
1455
+ )
1456
+ : null
1295
1457
  )
1296
- })
1458
+ )
1297
1459
  )
1298
- )
1299
- ),
1460
+ : null,
1300
1461
  m.copyError ? errorAlert(m.copyError, '复制失败') : null,
1301
1462
  result !== null
1302
1463
  ? h(
@@ -1367,6 +1528,9 @@ function ModelCopyPanel() {
1367
1528
  const [error, setError] = useState('')
1368
1529
  const [selectedKey, setSelectedKey] = useState('')
1369
1530
  const [importRole, setImportRole] = useState('')
1531
+ const [query, setQuery] = useState('')
1532
+ const [modelType, setModelType] = useState('all')
1533
+ const [expanded, setExpanded] = useState({})
1370
1534
  const [importing, setImporting] = useState(false)
1371
1535
  const [copyError, setCopyError] = useState('')
1372
1536
  const [result, setResult] = useState(null)
@@ -1379,6 +1543,9 @@ function ModelCopyPanel() {
1379
1543
  const list = Array.isArray(response?.models) ? response.models : []
1380
1544
  setModels(list)
1381
1545
  setSelectedKey('')
1546
+ setQuery('')
1547
+ setModelType('all')
1548
+ setExpanded({})
1382
1549
  setState('ready')
1383
1550
  } catch (err) {
1384
1551
  setError(apiErrorMessage(err))
@@ -1414,6 +1581,9 @@ function ModelCopyPanel() {
1414
1581
  models,
1415
1582
  error,
1416
1583
  selectedKey,
1584
+ query,
1585
+ type: modelType,
1586
+ expanded,
1417
1587
  importRole,
1418
1588
  importing,
1419
1589
  copyError,
@@ -1426,6 +1596,10 @@ function ModelCopyPanel() {
1426
1596
  setCopyError('')
1427
1597
  setResult(null)
1428
1598
  },
1599
+ onQueryChange: (value) => setQuery(value),
1600
+ onTypeChange: (value) => setModelType(value === '' ? 'all' : value),
1601
+ onToggleGroup: (key) =>
1602
+ setExpanded((current) => ({ ...current, [key]: current[key] !== true })),
1429
1603
  onCopy: copy
1430
1604
  })
1431
1605
  }
@@ -2562,37 +2736,49 @@ function RuleLevelSelect({ value, options, disabled, onChange }) {
2562
2736
  }
2563
2737
 
2564
2738
  /**
2565
- * 可搜索 + 可手输:ui-kit Input 加原生 datalist(value=ID,label 带名称)。
2566
- * 目录为空时仍可输入任意 ID,留空表示不限。
2739
+ * 可搜索 + 可手输的候选选择器:Combobox 提供真实下拉列表,输入框本身负责手输兜底。
2740
+ * 目录为空时仍可输入任意 ID,留空表示不限;已选旧值不在目录中时照常回显。
2567
2741
  */
2568
- function RulePicker({ id, value, options, placeholder, disabled, onChange }) {
2569
- const listId = `${id}-options`
2742
+ function RulePicker({ id, value, options, placeholder, emptyLabel, disabled, onChange }) {
2743
+ const items = []
2744
+ const labels = new Map()
2745
+ for (const item of Array.isArray(options) ? options : []) {
2746
+ const raw = String(item.id)
2747
+ if (labels.has(raw)) continue
2748
+ items.push(raw)
2749
+ const name = typeof item.name === 'string' ? item.name.trim() : ''
2750
+ labels.set(raw, name === '' ? raw : `${name} · ${raw}`)
2751
+ }
2570
2752
  return h(
2571
- 'div',
2572
- { className: 'kb-rule-picker' },
2573
- h(Input, {
2753
+ Combobox,
2754
+ {
2755
+ items,
2756
+ // 只有命中目录的值才算选中;手输的任意 ID 依靠 inputValue 回显。
2757
+ value: items.includes(value) ? value : null,
2758
+ inputValue: value ?? '',
2759
+ disabled,
2760
+ onInputValueChange: (next) => onChange(typeof next === 'string' ? next : ''),
2761
+ onValueChange: (next) => onChange(next === null || next === undefined ? '' : String(next))
2762
+ },
2763
+ h(ComboboxInput, {
2574
2764
  id,
2575
- type: 'text',
2576
- value: value ?? '',
2765
+ className: 'kb-rule-picker-input',
2577
2766
  placeholder,
2578
2767
  disabled,
2579
2768
  autoComplete: 'off',
2580
- list: listId,
2581
- 'aria-label': placeholder,
2582
- onChange: (event) => onChange(event.target.value)
2769
+ 'aria-label': placeholder
2583
2770
  }),
2584
2771
  h(
2585
- 'datalist',
2586
- { id: listId },
2587
- (Array.isArray(options) ? options : []).map((item) => {
2588
- const raw = String(item.id)
2589
- const name = typeof item.name === 'string' ? item.name.trim() : ''
2590
- return h('option', {
2591
- key: `${item.platform ?? ''}\u0000${raw}`,
2592
- value: raw,
2593
- label: name === '' ? raw : `${name} · ${raw}`
2594
- })
2595
- })
2772
+ ComboboxContent,
2773
+ null,
2774
+ h(ComboboxEmpty, null, emptyLabel ?? '无匹配,可直接输入 ID'),
2775
+ h(ComboboxList, null, (raw) =>
2776
+ h(
2777
+ ComboboxItem,
2778
+ { key: raw, value: raw },
2779
+ h('span', { className: 'kb-rule-picker-name' }, labels.get(String(raw)) ?? String(raw))
2780
+ )
2781
+ )
2596
2782
  )
2597
2783
  )
2598
2784
  }
@@ -2897,6 +3083,7 @@ export function RuleEditor({
2897
3083
  value: draft.guildId ?? '',
2898
3084
  options: guilds,
2899
3085
  placeholder: '不限;搜索群名称或直接输入群 ID',
3086
+ emptyLabel: '引擎离线或暂无群,可直接输入群 ID',
2900
3087
  disabled: saving,
2901
3088
  onChange: change('guildId')
2902
3089
  })
@@ -2910,6 +3097,7 @@ export function RuleEditor({
2910
3097
  value: draft.userId ?? '',
2911
3098
  options: users,
2912
3099
  placeholder: '不限;搜索昵称或直接输入用户 ID',
3100
+ emptyLabel: '引擎离线或暂无用户,可直接输入用户 ID',
2913
3101
  disabled: saving,
2914
3102
  onChange: change('userId')
2915
3103
  })