mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
refactor(memory): share sync progress helpers
This commit is contained in:
@@ -1,24 +1,19 @@
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
import type { SyncProgressState } from "./sync-progress.js";
|
||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||
import { buildFileEntry, listMemoryFiles, type MemoryFileEntry } from "./internal.js";
|
||||
import { indexFileEntryIfChanged } from "./sync-index.js";
|
||||
import { bumpSyncProgressTotal } from "./sync-progress.js";
|
||||
import { deleteStaleIndexedPaths } from "./sync-stale.js";
|
||||
|
||||
const log = createSubsystemLogger("memory");
|
||||
|
||||
type ProgressState = {
|
||||
completed: number;
|
||||
total: number;
|
||||
label?: string;
|
||||
report: (update: { completed: number; total: number; label?: string }) => void;
|
||||
};
|
||||
|
||||
export async function syncMemoryFiles(params: {
|
||||
workspaceDir: string;
|
||||
extraPaths?: string[];
|
||||
db: DatabaseSync;
|
||||
needsFullReindex: boolean;
|
||||
progress?: ProgressState;
|
||||
progress?: SyncProgressState;
|
||||
batchEnabled: boolean;
|
||||
concurrency: number;
|
||||
runWithConcurrency: <T>(tasks: Array<() => Promise<T>>, concurrency: number) => Promise<T[]>;
|
||||
@@ -42,14 +37,11 @@ export async function syncMemoryFiles(params: {
|
||||
});
|
||||
|
||||
const activePaths = new Set(fileEntries.map((entry) => entry.path));
|
||||
if (params.progress) {
|
||||
params.progress.total += fileEntries.length;
|
||||
params.progress.report({
|
||||
completed: params.progress.completed,
|
||||
total: params.progress.total,
|
||||
label: params.batchEnabled ? "Indexing memory files (batch)..." : "Indexing memory files…",
|
||||
});
|
||||
}
|
||||
bumpSyncProgressTotal(
|
||||
params.progress,
|
||||
fileEntries.length,
|
||||
params.batchEnabled ? "Indexing memory files (batch)..." : "Indexing memory files…",
|
||||
);
|
||||
|
||||
const tasks = fileEntries.map((entry) => async () => {
|
||||
await indexFileEntryIfChanged({
|
||||
|
||||
38
src/memory/sync-progress.ts
Normal file
38
src/memory/sync-progress.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
export type SyncProgressState = {
|
||||
completed: number;
|
||||
total: number;
|
||||
label?: string;
|
||||
report: (update: { completed: number; total: number; label?: string }) => void;
|
||||
};
|
||||
|
||||
export function bumpSyncProgressTotal(
|
||||
progress: SyncProgressState | undefined,
|
||||
delta: number,
|
||||
label?: string,
|
||||
) {
|
||||
if (!progress) {
|
||||
return;
|
||||
}
|
||||
progress.total += delta;
|
||||
progress.report({
|
||||
completed: progress.completed,
|
||||
total: progress.total,
|
||||
label,
|
||||
});
|
||||
}
|
||||
|
||||
export function bumpSyncProgressCompleted(
|
||||
progress: SyncProgressState | undefined,
|
||||
delta = 1,
|
||||
label?: string,
|
||||
) {
|
||||
if (!progress) {
|
||||
return;
|
||||
}
|
||||
progress.completed += delta;
|
||||
progress.report({
|
||||
completed: progress.completed,
|
||||
total: progress.total,
|
||||
label,
|
||||
});
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { DatabaseSync } from "node:sqlite";
|
||||
import type { SessionFileEntry } from "./session-files.js";
|
||||
import type { SyncProgressState } from "./sync-progress.js";
|
||||
import { createSubsystemLogger } from "../logging/subsystem.js";
|
||||
import {
|
||||
buildSessionEntry,
|
||||
@@ -7,22 +8,16 @@ import {
|
||||
sessionPathForFile,
|
||||
} from "./session-files.js";
|
||||
import { indexFileEntryIfChanged } from "./sync-index.js";
|
||||
import { bumpSyncProgressCompleted, bumpSyncProgressTotal } from "./sync-progress.js";
|
||||
import { deleteStaleIndexedPaths } from "./sync-stale.js";
|
||||
|
||||
const log = createSubsystemLogger("memory");
|
||||
|
||||
type ProgressState = {
|
||||
completed: number;
|
||||
total: number;
|
||||
label?: string;
|
||||
report: (update: { completed: number; total: number; label?: string }) => void;
|
||||
};
|
||||
|
||||
export async function syncSessionFiles(params: {
|
||||
agentId: string;
|
||||
db: DatabaseSync;
|
||||
needsFullReindex: boolean;
|
||||
progress?: ProgressState;
|
||||
progress?: SyncProgressState;
|
||||
batchEnabled: boolean;
|
||||
concurrency: number;
|
||||
runWithConcurrency: <T>(tasks: Array<() => Promise<T>>, concurrency: number) => Promise<T[]>;
|
||||
@@ -46,35 +41,20 @@ export async function syncSessionFiles(params: {
|
||||
concurrency: params.concurrency,
|
||||
});
|
||||
|
||||
if (params.progress) {
|
||||
params.progress.total += files.length;
|
||||
params.progress.report({
|
||||
completed: params.progress.completed,
|
||||
total: params.progress.total,
|
||||
label: params.batchEnabled ? "Indexing session files (batch)..." : "Indexing session files…",
|
||||
});
|
||||
}
|
||||
bumpSyncProgressTotal(
|
||||
params.progress,
|
||||
files.length,
|
||||
params.batchEnabled ? "Indexing session files (batch)..." : "Indexing session files…",
|
||||
);
|
||||
|
||||
const tasks = files.map((absPath) => async () => {
|
||||
if (!indexAll && !params.dirtyFiles.has(absPath)) {
|
||||
if (params.progress) {
|
||||
params.progress.completed += 1;
|
||||
params.progress.report({
|
||||
completed: params.progress.completed,
|
||||
total: params.progress.total,
|
||||
});
|
||||
}
|
||||
bumpSyncProgressCompleted(params.progress);
|
||||
return;
|
||||
}
|
||||
const entry = await buildSessionEntry(absPath);
|
||||
if (!entry) {
|
||||
if (params.progress) {
|
||||
params.progress.completed += 1;
|
||||
params.progress.report({
|
||||
completed: params.progress.completed,
|
||||
total: params.progress.total,
|
||||
});
|
||||
}
|
||||
bumpSyncProgressCompleted(params.progress);
|
||||
return;
|
||||
}
|
||||
await indexFileEntryIfChanged({
|
||||
|
||||
Reference in New Issue
Block a user