refactor: move vector rebuild actions into ui actions controller

This commit is contained in:
Youzini-afk
2026-03-29 17:21:02 +08:00
parent 6695f68f11
commit f18f05feb1
2 changed files with 73 additions and 41 deletions

View File

@@ -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,
});
}
// ==================== 初始化 ====================

View File

@@ -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();
}