chatluna-plugin-kb 0.1.7 → 0.2.7
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 +52 -15
- package/package.json +1 -1
- package/src/engine/deps-installer.mjs +410 -0
- package/src/webui/install-routes.mjs +72 -0
- package/src/webui/model-routes.mjs +69 -0
- package/src/webui/models.mjs +491 -11
- package/src/webui/server.mjs +97 -1
- package/web/client.css +137 -54
- package/web/client.js +1273 -655
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
//
|
|
4
4
|
// GET <prefix>/chatluna-models ChatLuna 已配置模型(脱敏)
|
|
5
5
|
// POST <prefix>/models/import-from-chatluna 一次性复制 { role, providerId, modelId }
|
|
6
|
+
// POST <prefix>/models/sync-from-chatluna 批量同步 { items, bindDefaultKb }
|
|
6
7
|
// GET <prefix>/models WeKnora 当前模型(可选回调)
|
|
7
8
|
//
|
|
8
9
|
// 不变量(改动前必读):
|
|
@@ -29,6 +30,7 @@ const VALUE_NODES_MAX = 2000
|
|
|
29
30
|
const VALUE_ARRAY_MAX = 200
|
|
30
31
|
|
|
31
32
|
const IMPORT_ROLES = new Set(['embedding', 'chat', 'vlm', 'rerank'])
|
|
33
|
+
const SYNC_MAX_ITEMS = 32
|
|
32
34
|
const CONFLICT_CODES = new Set(['CONFLICT', 'ALREADY_EXISTS', 'DUPLICATE', 'MODEL_EXISTS', 'EXISTS'])
|
|
33
35
|
const VALIDATION_CODES = new Set([
|
|
34
36
|
'INVALID_ARGUMENT',
|
|
@@ -299,6 +301,29 @@ function validateImport(body) {
|
|
|
299
301
|
}
|
|
300
302
|
}
|
|
301
303
|
|
|
304
|
+
function validateSync(body) {
|
|
305
|
+
if (!isPlainObject(body)) {
|
|
306
|
+
throw new ModelRouteError('INVALID_ARGUMENT', '请求体必须是 JSON 对象')
|
|
307
|
+
}
|
|
308
|
+
if (!Array.isArray(body.items) || body.items.length === 0) {
|
|
309
|
+
throw new ModelRouteError('INVALID_ARGUMENT', 'items 必须是非空数组')
|
|
310
|
+
}
|
|
311
|
+
if (body.items.length > SYNC_MAX_ITEMS) {
|
|
312
|
+
throw new ModelRouteError('INVALID_ARGUMENT', `items 不能超过 ${SYNC_MAX_ITEMS} 条`)
|
|
313
|
+
}
|
|
314
|
+
const items = body.items.map((item, index) => {
|
|
315
|
+
try {
|
|
316
|
+
return validateImport(item)
|
|
317
|
+
} catch (error) {
|
|
318
|
+
throw new ModelRouteError(
|
|
319
|
+
'INVALID_ARGUMENT',
|
|
320
|
+
`items[${index}]:${error instanceof Error ? error.message : '参数无效'}`
|
|
321
|
+
)
|
|
322
|
+
}
|
|
323
|
+
})
|
|
324
|
+
return { items, bindDefaultKb: body.bindDefaultKb === true }
|
|
325
|
+
}
|
|
326
|
+
|
|
302
327
|
function statusForError(error) {
|
|
303
328
|
const status = Number(error?.status ?? error?.statusCode)
|
|
304
329
|
if (status === 409) return 409
|
|
@@ -323,6 +348,7 @@ function disposeOf(handle) {
|
|
|
323
348
|
* @param {(ctx: object, req: object) => boolean | Promise<boolean>} options.authorize
|
|
324
349
|
* @param {() => unknown | Promise<unknown>} options.listModels
|
|
325
350
|
* @param {(input: { role: 'embedding' | 'chat' | 'vlm' | 'rerank', providerId: string, modelId: string }) => unknown | Promise<unknown>} options.importModel
|
|
351
|
+
* @param {(input: { items: Array<{ role: 'embedding' | 'chat' | 'vlm' | 'rerank', providerId: string, modelId: string }>, bindDefaultKb: boolean }) => unknown | Promise<unknown>} [options.syncModels]
|
|
326
352
|
* @param {() => unknown | Promise<unknown>} [options.listWeKnoraModels]
|
|
327
353
|
* @param {object} [options.logger]
|
|
328
354
|
* @returns {() => void} dispose
|
|
@@ -350,6 +376,7 @@ export function registerModelRoutes(ctx, options = {}) {
|
|
|
350
376
|
const authorize = options.authorize
|
|
351
377
|
const listModels = options.listModels
|
|
352
378
|
const importModel = options.importModel
|
|
379
|
+
const syncModels = typeof options.syncModels === 'function' ? options.syncModels : null
|
|
353
380
|
const listWeKnoraModels =
|
|
354
381
|
typeof options.listWeKnoraModels === 'function' ? options.listWeKnoraModels : null
|
|
355
382
|
|
|
@@ -413,6 +440,47 @@ export function registerModelRoutes(ctx, options = {}) {
|
|
|
413
440
|
sendJson(res, 200, { success: true, data: sanitizeValue(result ?? null) })
|
|
414
441
|
}
|
|
415
442
|
|
|
443
|
+
const syncHandler = async (req, res) => {
|
|
444
|
+
if (!(await allowed(req))) {
|
|
445
|
+
sendError(res, 401, 'UNAUTHORIZED', '需要 ChatLuna WebUI 管理员登录')
|
|
446
|
+
return
|
|
447
|
+
}
|
|
448
|
+
if (syncModels === null) {
|
|
449
|
+
sendError(res, 501, 'NOT_IMPLEMENTED', '当前部署未提供批量模型同步')
|
|
450
|
+
return
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
let input
|
|
454
|
+
try {
|
|
455
|
+
input = validateSync(await readJsonBody(req, maxBytes))
|
|
456
|
+
} catch (error) {
|
|
457
|
+
if (error instanceof ModelRouteError) {
|
|
458
|
+
const status = error.code === 'PAYLOAD_TOO_LARGE' ? 413 : 400
|
|
459
|
+
sendError(res, status, error.code, clipMessage(error.message, '请求无效'))
|
|
460
|
+
return
|
|
461
|
+
}
|
|
462
|
+
logger.error?.('model-routes: cannot read sync body: %s', clipMessage(error?.message, 'error'))
|
|
463
|
+
sendError(res, 400, 'INVALID_ARGUMENT', '请求体无法解析')
|
|
464
|
+
return
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
let result
|
|
468
|
+
try {
|
|
469
|
+
result = await syncModels(input)
|
|
470
|
+
} catch (error) {
|
|
471
|
+
const status = statusForError(error)
|
|
472
|
+
const code =
|
|
473
|
+
status === 500
|
|
474
|
+
? 'SYNC_FAILED'
|
|
475
|
+
: safeCode(error?.code, status === 409 ? 'CONFLICT' : 'INVALID_ARGUMENT')
|
|
476
|
+
logger.error?.('model-routes: sync failed (status %d): %s', status, clipMessage(error?.message, code))
|
|
477
|
+
const message = status === 500 ? '模型同步失败' : clipMessage(error?.message, '模型同步失败')
|
|
478
|
+
sendError(res, status, code, message)
|
|
479
|
+
return
|
|
480
|
+
}
|
|
481
|
+
sendJson(res, 200, { success: true, data: sanitizeValue(result ?? null) })
|
|
482
|
+
}
|
|
483
|
+
|
|
416
484
|
const weknoraModelsHandler = async (req, res) => {
|
|
417
485
|
if (!(await allowed(req))) {
|
|
418
486
|
sendError(res, 401, 'UNAUTHORIZED', '需要 ChatLuna WebUI 管理员登录')
|
|
@@ -437,6 +505,7 @@ export function registerModelRoutes(ctx, options = {}) {
|
|
|
437
505
|
try {
|
|
438
506
|
registered.push(ctx.server.get(`${prefix}/chatluna-models`, chatlunaModelsHandler))
|
|
439
507
|
registered.push(ctx.server.post(`${prefix}/models/import-from-chatluna`, importHandler))
|
|
508
|
+
registered.push(ctx.server.post(`${prefix}/models/sync-from-chatluna`, syncHandler))
|
|
440
509
|
registered.push(ctx.server.get(`${prefix}/models`, weknoraModelsHandler))
|
|
441
510
|
} catch (error) {
|
|
442
511
|
for (const handle of registered) {
|