chatluna-plugin-kb 0.1.4 → 0.1.8

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
@@ -41,7 +41,7 @@ import { createSystemdController } from './src/engine/systemd.mjs'
41
41
  import { ensureBootstrap } from './src/engine/bootstrap.mjs'
42
42
  import { registerKbProxy } from './src/proxy/webui-proxy.mjs'
43
43
  import { registerKbWebUI } from './src/webui/server.mjs'
44
- import { registerInstallRoutes } from './src/webui/install-routes.mjs'
44
+ import { registerInstallRoutes, createInstallStatusBridge } from './src/webui/install-routes.mjs'
45
45
  import { registerModelRoutes } from './src/webui/model-routes.mjs'
46
46
  import { createModelBridge } from './src/webui/models.mjs'
47
47
  import { createInstaller, DEFAULT_RELEASE_MANIFEST_URL, platformInfo } from './src/engine/installer.mjs'
@@ -428,6 +428,14 @@ export function apply(ctx, config) {
428
428
  } catch {}
429
429
  }
430
430
 
431
+ // 安装器快照 → 事件桥:进度只推 install;进入终态(installed/failed/cancelled)或
432
+ // installed 翻转时补推 status,同一结论只发一次,三条安装路径(一键/URL/离线上传)共用。
433
+ const installEvents = createInstallStatusBridge({
434
+ emitInstall,
435
+ emitStatus,
436
+ isInstalled: () => existsSync(binaryPath)
437
+ })
438
+
431
439
  // ---- 安装器(仅 managed:负责下载/校验/解包到 <dataDir>/weknora-lite) ----
432
440
  let installer = null
433
441
  if (managed) {
@@ -439,8 +447,8 @@ export function apply(ctx, config) {
439
447
  releaseManifestUrl: config.releaseManifestUrl || DEFAULT_RELEASE_MANIFEST_URL,
440
448
  mirrors: Array.isArray(config.installMirrors) ? config.installMirrors : [],
441
449
  pluginVersion: String(config.pluginVersion ?? '').trim(),
442
- onStatus: () => {
443
- emitInstall()
450
+ onStatus: (snap) => {
451
+ installEvents.handleStatus(snap)
444
452
  },
445
453
  logger
446
454
  })
@@ -979,12 +987,10 @@ export function apply(ctx, config) {
979
987
  const engineInstaller = requireInstaller('自动安装')
980
988
  try {
981
989
  const result = await engineInstaller.installFromRelease()
982
- emitInstall()
983
- emitStatus()
990
+ installEvents.handleStatus({ state: 'ready' })
984
991
  return result ?? { ok: true }
985
992
  } catch (err) {
986
- emitInstall()
987
- emitStatus()
993
+ installEvents.handleStatus({ state: 'error' })
988
994
  throw new Error(sanitizeError(err))
989
995
  }
990
996
  }
@@ -996,12 +1002,10 @@ export function apply(ctx, config) {
996
1002
  try {
997
1003
  const options = typeof input?.sha256 === 'string' && input.sha256 !== '' ? { sha256: input.sha256 } : {}
998
1004
  const result = await engineInstaller.installFromUrl(url, options)
999
- emitInstall()
1000
- emitStatus()
1005
+ installEvents.handleStatus({ state: 'ready' })
1001
1006
  return result ?? { ok: true }
1002
1007
  } catch (err) {
1003
- emitInstall()
1004
- emitStatus()
1008
+ installEvents.handleStatus({ state: 'error' })
1005
1009
  throw new Error(sanitizeError(err))
1006
1010
  }
1007
1011
  }
@@ -1018,12 +1022,10 @@ export function apply(ctx, config) {
1018
1022
  try {
1019
1023
  const options = typeof input?.sha256 === 'string' && input.sha256 !== '' ? { sha256: input.sha256 } : {}
1020
1024
  const result = await engineInstaller.installFromFile(path, options)
1021
- emitInstall()
1022
- emitStatus()
1025
+ installEvents.handleStatus({ state: 'ready' })
1023
1026
  return result ?? { ok: true }
1024
1027
  } catch (err) {
1025
- emitInstall()
1026
- emitStatus()
1028
+ installEvents.handleStatus({ state: 'error' })
1027
1029
  throw new Error(sanitizeError(err))
1028
1030
  }
1029
1031
  }
@@ -1626,6 +1628,7 @@ export function apply(ctx, config) {
1626
1628
  prefix: '/kb-admin',
1627
1629
  installer,
1628
1630
  authorize: (context, req) => isWebuiAuthorized(context ?? ctx, req, logger),
1631
+ onInstalled: (snapshot) => installEvents.handleStatus(snapshot),
1629
1632
  maxBytes: INSTALL_UPLOAD_MAX,
1630
1633
  logger
1631
1634
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chatluna-plugin-kb",
3
- "version": "0.1.4",
3
+ "version": "0.1.8",
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",
@@ -127,6 +127,65 @@ function disposeOf(handle) {
127
127
  else if (typeof handle?.dispose === 'function') handle.dispose()
128
128
  }
129
129
 
130
+ const INSTALLED_STATES = new Set(['ready', 'installed'])
131
+ const FAILED_STATES = new Set(['error', 'failed'])
132
+ const TERMINAL_STATES = new Set([...INSTALLED_STATES, ...FAILED_STATES, 'cancelled'])
133
+
134
+ /**
135
+ * Bridge installer snapshots to ChatLuna events.
136
+ *
137
+ * Every snapshot feeds the `install` event (progress). A `status` broadcast is added only when
138
+ * the installer reaches a terminal state (installed / failed / cancelled) or when `installed`
139
+ * flips, so the WebUI sees the outcome of all three install paths — release download, URL and
140
+ * offline upload — without a page reload. The same conclusion is emitted once: the terminal
141
+ * outcome plus the installed flag form the dedupe key, and the key resets when a new job starts
142
+ * (busy state entered from a settled one) so a retry still announces its own result.
143
+ *
144
+ * Snapshots only produce events; nothing here logs.
145
+ */
146
+ export function createInstallStatusBridge(options = {}) {
147
+ const emitInstall = typeof options.emitInstall === 'function' ? options.emitInstall : () => {}
148
+ const emitStatus = typeof options.emitStatus === 'function' ? options.emitStatus : () => {}
149
+ const isInstalled = typeof options.isInstalled === 'function' ? options.isInstalled : null
150
+
151
+ let lastState = null
152
+ let lastInstalled = false
153
+ let lastKey = ''
154
+
155
+ return {
156
+ /** @param {{ state?: string, message?: string, error?: string }} [snap] installer snapshot */
157
+ handleStatus(snap) {
158
+ emitInstall()
159
+ if (snap === null || typeof snap !== 'object') return
160
+
161
+ const state = typeof snap.state === 'string' ? snap.state.trim().toLowerCase() : ''
162
+ const previous = lastState
163
+ lastState = state
164
+
165
+ const previousBusy =
166
+ previous !== null && previous !== '' && previous !== 'idle' && !TERMINAL_STATES.has(previous)
167
+ const busy = state !== '' && state !== 'idle' && !TERMINAL_STATES.has(state)
168
+ if (busy && !previousBusy) lastKey = ''
169
+
170
+ let outcome = ''
171
+ if (INSTALLED_STATES.has(state)) outcome = 'installed'
172
+ else if (FAILED_STATES.has(state)) outcome = 'failed'
173
+ else if (state === 'cancelled' || (state === 'idle' && previousBusy)) outcome = 'cancelled'
174
+
175
+ let installed = lastInstalled
176
+ if (isInstalled !== null) installed = isInstalled() === true
177
+ const installedChanged = installed !== lastInstalled
178
+ lastInstalled = installed
179
+
180
+ if (outcome === '' && !installedChanged) return
181
+ const key = `${outcome}|${installed}`
182
+ if (key === lastKey) return
183
+ lastKey = key
184
+ emitStatus()
185
+ }
186
+ }
187
+ }
188
+
130
189
  /**
131
190
  * Register the install wizard endpoints on the ChatLuna server.
132
191
  *
@@ -135,6 +194,8 @@ function disposeOf(handle) {
135
194
  * @param {string} [options.prefix='/kb-admin']
136
195
  * @param {{ installFromStream: (stream: import('node:stream').Readable, meta: { sha256?: string, filename?: string }) => Promise<unknown>, status: () => unknown }} options.installer
137
196
  * @param {(ctx: object, req: object) => boolean | Promise<boolean>} options.authorize
197
+ * @param {(snapshot: unknown) => void} [options.onInstalled] runs after a successful upload with
198
+ * the fresh installer status so the host can broadcast `chatluna-kb/status`
138
199
  * @param {number} [options.maxBytes=314572800]
139
200
  * @param {object} [options.logger]
140
201
  * @returns {() => void} dispose
@@ -156,6 +217,9 @@ export function registerInstallRoutes(ctx, options = {}) {
156
217
  if (typeof options.authorize !== 'function') {
157
218
  throw new TypeError('registerInstallRoutes: options.authorize must be a function')
158
219
  }
220
+ if (options.onInstalled !== undefined && typeof options.onInstalled !== 'function') {
221
+ throw new TypeError('registerInstallRoutes: options.onInstalled must be a function')
222
+ }
159
223
 
160
224
  const prefix = normalizePrefix(options.prefix)
161
225
  const maxBytes = options.maxBytes ?? DEFAULT_MAX_BYTES
@@ -164,6 +228,7 @@ export function registerInstallRoutes(ctx, options = {}) {
164
228
  }
165
229
  const logger = options.logger ?? defaultLogger
166
230
  const authorize = options.authorize
231
+ const onInstalled = options.onInstalled
167
232
 
168
233
  const allowed = async (req) => {
169
234
  try {
@@ -254,6 +319,13 @@ export function registerInstallRoutes(ctx, options = {}) {
254
319
  sendError(res, 500, 'INSTALL_FAILED', '无法读取安装状态')
255
320
  return
256
321
  }
322
+ if (onInstalled !== undefined) {
323
+ try {
324
+ onInstalled(snapshot)
325
+ } catch {
326
+ // Broadcasting is best-effort: a listener failure must not turn a finished upload into a 500.
327
+ }
328
+ }
257
329
  sendJson(res, 200, { success: true, data: snapshot })
258
330
  }
259
331
 
package/web/client.css CHANGED
@@ -829,12 +829,6 @@
829
829
  font-size: 0.75rem;
830
830
  }
831
831
 
832
- .kb-engine-segmented {
833
- display: flex;
834
- align-items: center;
835
- gap: 0.5rem;
836
- }
837
-
838
832
  .kb-engine-panel {
839
833
  display: flex;
840
834
  min-width: 0;
@@ -846,19 +840,11 @@
846
840
  background: var(--card);
847
841
  }
848
842
 
849
- .kb-engine-log-panel,
850
- .kb-engine-model-panel {
843
+ .kb-engine-log-panel {
851
844
  height: clamp(28rem, calc(100dvh - 17rem), 48rem);
852
845
  min-height: 24rem;
853
846
  }
854
847
 
855
- .kb-engine-model-panel {
856
- display: flex;
857
- min-width: 0;
858
- flex-direction: column;
859
- overflow: hidden;
860
- }
861
-
862
848
  .kb-engine-panel-head {
863
849
  display: flex;
864
850
  flex-wrap: wrap;
@@ -891,9 +877,11 @@
891
877
  }
892
878
 
893
879
  .kb-log-console {
880
+ min-width: 0;
894
881
  min-height: 0;
895
882
  flex: 1;
896
- overflow: auto;
883
+ overflow-y: auto;
884
+ overflow-x: hidden;
897
885
  overscroll-behavior: contain;
898
886
  background: color-mix(in srgb, var(--muted) 45%, var(--card));
899
887
  padding: 0.5rem 0.75rem;
@@ -944,156 +932,184 @@
944
932
  color: var(--muted-foreground);
945
933
  }
946
934
 
947
- /* 模型工作台在引擎面板内填满高度,滚动只发生在列表内部。 */
948
- .kb-engine-model-panel > .kb-model-workbench {
935
+ /* ---- 模型复制 Dialog --------------------------------------------------- */
936
+ div.kb-model-copy-dialog {
949
937
  display: flex;
950
- min-height: 0;
951
- flex: 1;
938
+ width: min(56rem, calc(100vw - 2rem));
939
+ max-width: 56rem;
940
+ height: min(82dvh, 46rem);
941
+ max-height: calc(100dvh - 2rem);
952
942
  flex-direction: column;
953
- gap: 0.5rem;
954
- padding: 0.75rem;
955
- }
956
-
957
- .kb-model-workbench-card {
958
- min-height: 0;
959
- flex: 1;
943
+ gap: 0;
944
+ padding: 0;
945
+ overflow: hidden;
960
946
  }
961
947
 
962
- .kb-model-workbench-content {
948
+ div.kb-model-copy-dialog > .kb-model-workbench {
963
949
  display: flex;
964
950
  min-height: 0;
965
951
  flex: 1;
966
952
  flex-direction: column;
967
953
  }
968
954
 
969
- .kb-model-workbench-content > .kb-card-stack {
970
- display: flex;
971
- min-height: 0;
972
- flex: 1;
973
- flex-direction: column;
955
+ .kb-model-workbench-header {
956
+ flex-shrink: 0;
957
+ border-bottom: 1px solid var(--border);
958
+ padding: 1.25rem 3rem 0.875rem 1.25rem;
959
+ }
960
+
961
+ .kb-model-workbench-title {
962
+ margin: 0;
963
+ font-size: 1rem;
964
+ font-weight: 600;
965
+ line-height: 1.4;
966
+ }
967
+
968
+ .kb-model-workbench-subtitle {
969
+ margin: 0.25rem 0 0;
970
+ color: var(--muted-foreground);
971
+ font-size: 0.8125rem;
974
972
  }
975
973
 
976
974
  .kb-model-workbench .kb-model-toolbar {
977
975
  flex-shrink: 0;
976
+ border-bottom: 1px solid var(--border);
977
+ padding: 0.75rem 1.25rem;
978
978
  }
979
979
 
980
980
  .kb-model-workbench .kb-model-scroll {
981
981
  min-height: 0;
982
982
  max-height: none;
983
983
  flex: 1;
984
+ overflow-x: hidden;
985
+ overflow-y: auto;
986
+ padding: 0.875rem 1.25rem 1rem;
984
987
  }
985
988
 
986
- .kb-model-workbench .kb-model-actionbar {
987
- margin-top: auto;
988
- }
989
-
990
- .kb-model-actionbar-sticky {
989
+ .kb-model-workbench .kb-model-group-head {
991
990
  position: sticky;
992
- bottom: 0;
993
- flex-shrink: 0;
994
- border-top: 1px solid var(--border);
995
- background: var(--card);
996
- padding-top: 0.5rem;
991
+ top: 0;
992
+ z-index: 1;
993
+ background: var(--popover);
994
+ padding-block: 0.25rem;
997
995
  }
998
996
 
999
- .kb-model-empty-hint {
1000
- margin: 0;
1001
- color: var(--muted-foreground);
1002
- font-size: 0.75rem;
997
+ .kb-model-workbench .kb-model-list {
998
+ display: grid;
999
+ grid-template-columns: minmax(0, 1fr);
1000
+ gap: 0.5rem;
1003
1001
  }
1004
1002
 
1005
- @media (max-width: 1519.98px) {
1006
- .kb-engine-workbench[data-pane='logs'] .kb-engine-model-panel {
1007
- display: none;
1003
+ @media (min-width: 42rem) {
1004
+ .kb-model-workbench .kb-model-list {
1005
+ grid-template-columns: repeat(2, minmax(0, 1fr));
1008
1006
  }
1007
+ }
1009
1008
 
1010
- .kb-engine-workbench[data-pane='models'] .kb-engine-log-panel {
1011
- display: none;
1012
- }
1009
+ .kb-model-workbench .kb-model-item {
1010
+ min-width: 0;
1013
1011
  }
1014
1012
 
1015
- @media (min-width: 1040px) and (max-width: 1519.98px) {
1016
- .kb-engine-workbench {
1017
- display: grid;
1018
- grid-template-columns: 300px minmax(0, 1fr);
1019
- grid-template-areas:
1020
- 'hero hero'
1021
- 'rail segmented'
1022
- 'rail stack';
1023
- align-items: start;
1024
- }
1013
+ .kb-model-model-id {
1014
+ overflow: hidden;
1015
+ min-width: 0;
1016
+ font-weight: 500;
1017
+ text-overflow: ellipsis;
1018
+ white-space: nowrap;
1019
+ }
1025
1020
 
1026
- .kb-engine-hero {
1027
- grid-area: hero;
1028
- }
1021
+ .kb-model-result {
1022
+ flex-shrink: 0;
1023
+ padding: 0 1.25rem 0.5rem;
1024
+ }
1029
1025
 
1030
- .kb-engine-rail {
1031
- position: sticky;
1032
- top: 1rem;
1033
- grid-area: rail;
1034
- max-height: calc(100dvh - 2rem);
1035
- overflow-y: auto;
1036
- grid-template-columns: minmax(0, 1fr);
1037
- }
1026
+ .kb-model-dialog-footer {
1027
+ display: flex;
1028
+ flex-shrink: 0;
1029
+ flex-wrap: wrap;
1030
+ align-items: center;
1031
+ justify-content: space-between;
1032
+ gap: 0.75rem;
1033
+ border-top: 1px solid var(--border);
1034
+ padding: 0.75rem 1.25rem;
1035
+ }
1038
1036
 
1039
- .kb-engine-segmented {
1040
- grid-area: segmented;
1041
- }
1037
+ .kb-model-selection {
1038
+ display: flex;
1039
+ min-width: 0;
1040
+ flex: 1 1 18rem;
1041
+ flex-wrap: wrap;
1042
+ align-items: center;
1043
+ gap: 0.5rem;
1044
+ }
1042
1045
 
1043
- .kb-engine-log-panel,
1044
- .kb-engine-model-panel {
1045
- grid-area: stack;
1046
- }
1046
+ .kb-model-selection-empty {
1047
+ color: var(--muted-foreground);
1048
+ font-size: 0.75rem;
1047
1049
  }
1048
1050
 
1049
- @media (min-width: 1520px) {
1050
- .kb-engine-workbench {
1051
- display: grid;
1052
- grid-template-columns: 300px minmax(0, 1.15fr) minmax(0, 1fr);
1053
- grid-template-areas:
1054
- 'hero hero hero'
1055
- 'rail log model';
1056
- align-items: start;
1057
- }
1051
+ .kb-model-selection-text {
1052
+ overflow: hidden;
1053
+ min-width: 0;
1054
+ color: var(--muted-foreground);
1055
+ font-size: 0.75rem;
1056
+ text-overflow: ellipsis;
1057
+ white-space: nowrap;
1058
+ }
1058
1059
 
1059
- .kb-engine-hero {
1060
- grid-area: hero;
1061
- }
1060
+ .kb-model-selection > .kb-model-selection-text {
1061
+ max-width: 100%;
1062
+ flex: 0 1 auto;
1063
+ }
1062
1064
 
1063
- .kb-engine-rail {
1064
- position: sticky;
1065
- top: 1rem;
1066
- grid-area: rail;
1067
- max-height: calc(100dvh - 2rem);
1068
- overflow-y: auto;
1069
- grid-template-columns: minmax(0, 1fr);
1070
- }
1065
+ .kb-model-role {
1066
+ width: auto;
1067
+ max-width: 14rem;
1068
+ min-width: 0;
1069
+ flex: 0 0 auto;
1070
+ }
1071
1071
 
1072
- .kb-engine-segmented {
1073
- display: none;
1074
- }
1072
+ .kb-model-role [data-slot='select'],
1073
+ .kb-model-role [data-slot='select-trigger'] {
1074
+ width: 100%;
1075
+ max-width: 14rem;
1076
+ }
1075
1077
 
1076
- .kb-engine-log-panel {
1077
- grid-area: log;
1078
- }
1078
+ .kb-model-role [data-slot='select-trigger'] {
1079
+ min-width: 0;
1080
+ }
1079
1081
 
1080
- .kb-engine-model-panel {
1081
- grid-area: model;
1082
- }
1082
+ .kb-model-selection-name {
1083
+ color: var(--foreground);
1084
+ font-weight: 600;
1083
1085
  }
1084
1086
 
1085
- @media (max-width: 720px) {
1086
- .kb-engine-rail {
1087
- grid-template-columns: minmax(0, 1fr);
1088
- }
1087
+ .kb-model-selection-provider {
1088
+ color: var(--muted-foreground);
1089
1089
  }
1090
1090
 
1091
- /* 中宽屏切到模型工作台时,两张卡片可在宽主区并排。 */
1092
- @media (min-width: 1040px) and (max-width: 1519.98px) {
1093
- .kb-engine-workbench[data-pane='models'] .kb-model-list {
1094
- display: grid;
1095
- grid-template-columns: repeat(auto-fill, minmax(17.5rem, 1fr));
1096
- }
1091
+ .kb-model-footer-actions {
1092
+ display: flex;
1093
+ flex-shrink: 0;
1094
+ gap: 0.5rem;
1095
+ }
1096
+
1097
+ .kb-model-top-actions {
1098
+ display: flex;
1099
+ min-width: 0;
1100
+ flex-wrap: wrap;
1101
+ align-items: center;
1102
+ justify-content: flex-end;
1103
+ gap: 0.375rem;
1104
+ }
1105
+
1106
+ .kb-model-selected {
1107
+ overflow: hidden;
1108
+ max-width: 18rem;
1109
+ color: var(--muted-foreground);
1110
+ font-size: 0.75rem;
1111
+ text-overflow: ellipsis;
1112
+ white-space: nowrap;
1097
1113
  }
1098
1114
 
1099
1115
  /* ---- 模型复制 --------------------------------------------------------- */
@@ -1214,14 +1230,6 @@
1214
1230
  color: var(--primary);
1215
1231
  }
1216
1232
 
1217
- .kb-model-footer {
1218
- display: flex;
1219
- flex-wrap: wrap;
1220
- align-items: center;
1221
- justify-content: flex-end;
1222
- gap: 0.5rem;
1223
- }
1224
-
1225
1233
  /* ---- 安装向导 --------------------------------------------------------- */
1226
1234
  .kb-wizard {
1227
1235
  display: flex;