diff --git a/index.js b/index.js index f8e8235..62a303b 100644 --- a/index.js +++ b/index.js @@ -81,9 +81,11 @@ import { onExportGraphController, onImportGraphController, onManualCompressController, + onRebuildVectorIndexController, onRebuildController, onTestEmbeddingController, onTestMemoryLLMController, + onReembedDirectController, onViewLastInjectionController, onViewGraphController, } from "./ui-actions-controller.js"; @@ -4873,50 +4875,32 @@ async function onManualEvolve() { } async function onRebuildVectorIndex(range = null) { - if (!ensureGraphMutationReady(range ? "范围重建向量" : "重建向量")) return; - ensureCurrentGraphRuntimeState(); - const config = getEmbeddingConfig(); - const validation = validateVectorConfig(config); - if (!validation.valid) { - toastr.warning(validation.error); - return; - } - - const vectorController = beginStageAbortController("vector"); - try { - const result = await syncVectorState({ - force: true, - purge: isBackendVectorConfig(config) && !range, - range, - signal: vectorController.signal, - }); - - saveGraphToChat({ reason: "vector-rebuild-complete" }); - if (result?.aborted) { - return; - } - if (result?.error) { - throw new Error(result.error); - } - toastr.success( - range - ? `范围向量重建完成:indexed=${result.stats.indexed}, pending=${result.stats.pending}` - : `当前聊天向量重建完成:indexed=${result.stats.indexed}, pending=${result.stats.pending}`, - ); - } finally { - finishStageAbortController("vector", vectorController); - refreshPanelLiveState(); - } + return await onRebuildVectorIndexController( + { + beginStageAbortController, + ensureCurrentGraphRuntimeState, + ensureGraphMutationReady, + finishStageAbortController, + getEmbeddingConfig, + isBackendVectorConfig, + refreshPanelLiveState, + saveGraphToChat, + syncVectorState, + toastr, + validateVectorConfig, + }, + range, + ); } async function onReembedDirect() { - const config = getEmbeddingConfig(); - if (!isDirectVectorConfig(config)) { - toastr.info("当前不是直连模式,无需执行重嵌"); - return; - } - - await onRebuildVectorIndex(); + return await onReembedDirectController({ + getEmbeddingConfig, + isDirectVectorConfig, + onRebuildVectorIndex: async () => + await onRebuildVectorIndex(), + toastr, + }); } // ==================== 初始化 ==================== diff --git a/ui-actions-controller.js b/ui-actions-controller.js index d89c997..4b178fd 100644 --- a/ui-actions-controller.js +++ b/ui-actions-controller.js @@ -330,3 +330,51 @@ export async function onImportGraphController(runtime) { input.click(); }); } + +export async function onRebuildVectorIndexController(runtime, range = null) { + if (!runtime.ensureGraphMutationReady(range ? "范围重建向量" : "重建向量")) return; + runtime.ensureCurrentGraphRuntimeState(); + + const config = runtime.getEmbeddingConfig(); + const validation = runtime.validateVectorConfig(config); + if (!validation.valid) { + runtime.toastr.warning(validation.error); + return; + } + + const vectorController = runtime.beginStageAbortController("vector"); + try { + const result = await runtime.syncVectorState({ + force: true, + purge: runtime.isBackendVectorConfig(config) && !range, + range, + signal: vectorController.signal, + }); + + runtime.saveGraphToChat({ reason: "vector-rebuild-complete" }); + if (result?.aborted) { + return; + } + if (result?.error) { + throw new Error(result.error); + } + runtime.toastr.success( + range + ? `范围向量重建完成:indexed=${result.stats.indexed}, pending=${result.stats.pending}` + : `当前聊天向量重建完成:indexed=${result.stats.indexed}, pending=${result.stats.pending}`, + ); + } finally { + runtime.finishStageAbortController("vector", vectorController); + runtime.refreshPanelLiveState(); + } +} + +export async function onReembedDirectController(runtime) { + const config = runtime.getEmbeddingConfig(); + if (!runtime.isDirectVectorConfig(config)) { + runtime.toastr.info("当前不是直连模式,无需执行重嵌"); + return; + } + + await runtime.onRebuildVectorIndex(); +}