memory-braid 0.3.6 → 0.4.0
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/README.md +56 -27
- package/openclaw.plugin.json +24 -24
- package/package.json +2 -2
- package/src/chunking.ts +0 -267
- package/src/config.ts +45 -57
- package/src/dedupe.ts +4 -4
- package/src/extract.ts +10 -2
- package/src/index.ts +678 -134
- package/src/mem0-client.ts +91 -5
- package/src/state.ts +55 -28
- package/src/types.ts +34 -52
- package/src/bootstrap.ts +0 -82
- package/src/reconcile.ts +0 -278
package/src/reconcile.ts
DELETED
|
@@ -1,278 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { buildMarkdownChunks, buildSessionChunks, sha256 } from "./chunking.js";
|
|
4
|
-
import type { MemoryBraidConfig } from "./config.js";
|
|
5
|
-
import { MemoryBraidLogger } from "./logger.js";
|
|
6
|
-
import { Mem0Adapter } from "./mem0-client.js";
|
|
7
|
-
import {
|
|
8
|
-
readReconcileState,
|
|
9
|
-
type StatePaths,
|
|
10
|
-
withStateLock,
|
|
11
|
-
writeReconcileState,
|
|
12
|
-
} from "./state.js";
|
|
13
|
-
import type { IndexedEntry, ManifestChunk, ReconcileSummary, TargetWorkspace } from "./types.js";
|
|
14
|
-
|
|
15
|
-
type OpenClawConfigLike = {
|
|
16
|
-
agents?: {
|
|
17
|
-
defaults?: {
|
|
18
|
-
workspace?: string;
|
|
19
|
-
};
|
|
20
|
-
list?: Array<{
|
|
21
|
-
id?: string;
|
|
22
|
-
workspace?: string;
|
|
23
|
-
default?: boolean;
|
|
24
|
-
}>;
|
|
25
|
-
};
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
function normalizeAgentId(value: string | undefined): string {
|
|
29
|
-
const trimmed = (value ?? "main").trim().toLowerCase();
|
|
30
|
-
return trimmed || "main";
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function resolveWorkspacePath(input: string | undefined, fallback?: string): string | undefined {
|
|
34
|
-
const value = (input ?? "").trim() || (fallback ?? "").trim();
|
|
35
|
-
if (!value) {
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
return path.resolve(value);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
async function workspaceHashForDir(workspaceDir: string): Promise<string> {
|
|
42
|
-
try {
|
|
43
|
-
const real = await fs.realpath(workspaceDir);
|
|
44
|
-
return sha256(real.toLowerCase());
|
|
45
|
-
} catch {
|
|
46
|
-
return sha256(path.resolve(workspaceDir).toLowerCase());
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export async function resolveTargets(params: {
|
|
51
|
-
config?: OpenClawConfigLike;
|
|
52
|
-
stateDir: string;
|
|
53
|
-
fallbackWorkspaceDir?: string;
|
|
54
|
-
}): Promise<TargetWorkspace[]> {
|
|
55
|
-
const targets: TargetWorkspace[] = [];
|
|
56
|
-
const seen = new Set<string>();
|
|
57
|
-
|
|
58
|
-
const fallbackWorkspace = resolveWorkspacePath(
|
|
59
|
-
params.config?.agents?.defaults?.workspace,
|
|
60
|
-
params.fallbackWorkspaceDir,
|
|
61
|
-
);
|
|
62
|
-
if (fallbackWorkspace) {
|
|
63
|
-
const agentId = "main";
|
|
64
|
-
const key = `${agentId}|${fallbackWorkspace}`;
|
|
65
|
-
seen.add(key);
|
|
66
|
-
targets.push({
|
|
67
|
-
workspaceDir: fallbackWorkspace,
|
|
68
|
-
stateDir: params.stateDir,
|
|
69
|
-
agentId,
|
|
70
|
-
workspaceHash: await workspaceHashForDir(fallbackWorkspace),
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
for (const entry of params.config?.agents?.list ?? []) {
|
|
75
|
-
const workspace = resolveWorkspacePath(entry.workspace, fallbackWorkspace);
|
|
76
|
-
if (!workspace) {
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
const agentId = normalizeAgentId(entry.id);
|
|
80
|
-
const key = `${agentId}|${workspace}`;
|
|
81
|
-
if (seen.has(key)) {
|
|
82
|
-
continue;
|
|
83
|
-
}
|
|
84
|
-
seen.add(key);
|
|
85
|
-
targets.push({
|
|
86
|
-
workspaceDir: workspace,
|
|
87
|
-
stateDir: params.stateDir,
|
|
88
|
-
agentId,
|
|
89
|
-
workspaceHash: await workspaceHashForDir(workspace),
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return targets;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export async function buildManagedManifest(params: {
|
|
97
|
-
targets: TargetWorkspace[];
|
|
98
|
-
cfg: MemoryBraidConfig;
|
|
99
|
-
}): Promise<ManifestChunk[]> {
|
|
100
|
-
const out: ManifestChunk[] = [];
|
|
101
|
-
for (const target of params.targets) {
|
|
102
|
-
if (params.cfg.bootstrap.includeMarkdown) {
|
|
103
|
-
out.push(...(await buildMarkdownChunks(target)));
|
|
104
|
-
}
|
|
105
|
-
if (params.cfg.bootstrap.includeSessions) {
|
|
106
|
-
out.push(...(await buildSessionChunks(target, params.cfg.bootstrap.sessionLookbackDays)));
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return out;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export async function runReconcileOnce(params: {
|
|
113
|
-
cfg: MemoryBraidConfig;
|
|
114
|
-
mem0: Mem0Adapter;
|
|
115
|
-
statePaths: StatePaths;
|
|
116
|
-
log: MemoryBraidLogger;
|
|
117
|
-
targets: TargetWorkspace[];
|
|
118
|
-
reason: string;
|
|
119
|
-
runId?: string;
|
|
120
|
-
deleteStale?: boolean;
|
|
121
|
-
}): Promise<ReconcileSummary> {
|
|
122
|
-
const runId = params.runId ?? params.log.newRunId();
|
|
123
|
-
const startedAt = Date.now();
|
|
124
|
-
|
|
125
|
-
return withStateLock(params.statePaths.stateLockFile, async () => {
|
|
126
|
-
params.log.debug("memory_braid.reconcile.begin", {
|
|
127
|
-
runId,
|
|
128
|
-
reason: params.reason,
|
|
129
|
-
targets: params.targets.length,
|
|
130
|
-
}, true);
|
|
131
|
-
|
|
132
|
-
const state = await readReconcileState(params.statePaths);
|
|
133
|
-
const manifest = await buildManagedManifest({
|
|
134
|
-
targets: params.targets,
|
|
135
|
-
cfg: params.cfg,
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
const byKey = new Map<string, ManifestChunk>();
|
|
139
|
-
for (const chunk of manifest) {
|
|
140
|
-
byKey.set(chunk.chunkKey, chunk);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
let upserted = 0;
|
|
144
|
-
let deleted = 0;
|
|
145
|
-
let unchanged = 0;
|
|
146
|
-
const now = Date.now();
|
|
147
|
-
|
|
148
|
-
// Mark managed entries that are no longer present.
|
|
149
|
-
for (const [chunkKey, entry] of Object.entries(state.entries)) {
|
|
150
|
-
if (entry.sourceType === "capture") {
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
if (byKey.has(chunkKey)) {
|
|
154
|
-
continue;
|
|
155
|
-
}
|
|
156
|
-
const missingCount = (entry.missingCount ?? 0) + 1;
|
|
157
|
-
const shouldDelete =
|
|
158
|
-
(params.deleteStale ?? params.cfg.reconcile.deleteStale) && missingCount >= 2;
|
|
159
|
-
if (!shouldDelete) {
|
|
160
|
-
state.entries[chunkKey] = {
|
|
161
|
-
...entry,
|
|
162
|
-
missingCount,
|
|
163
|
-
updatedAt: now,
|
|
164
|
-
};
|
|
165
|
-
continue;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const deletedRemote = await params.mem0.deleteMemory({
|
|
169
|
-
memoryId: entry.id,
|
|
170
|
-
scope: {
|
|
171
|
-
workspaceHash: entry.workspaceHash,
|
|
172
|
-
agentId: entry.agentId,
|
|
173
|
-
},
|
|
174
|
-
runId,
|
|
175
|
-
});
|
|
176
|
-
if (deletedRemote || !entry.id) {
|
|
177
|
-
delete state.entries[chunkKey];
|
|
178
|
-
deleted += 1;
|
|
179
|
-
} else {
|
|
180
|
-
state.entries[chunkKey] = {
|
|
181
|
-
...entry,
|
|
182
|
-
missingCount,
|
|
183
|
-
updatedAt: now,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
const allChunks = Array.from(byKey.values());
|
|
189
|
-
const batchSize = Math.max(1, params.cfg.reconcile.batchSize);
|
|
190
|
-
for (let offset = 0; offset < allChunks.length; offset += batchSize) {
|
|
191
|
-
const batch = allChunks.slice(offset, offset + batchSize);
|
|
192
|
-
for (const chunk of batch) {
|
|
193
|
-
const existing = state.entries[chunk.chunkKey];
|
|
194
|
-
if (existing && existing.contentHash === chunk.contentHash && existing.sourceType !== "capture") {
|
|
195
|
-
unchanged += 1;
|
|
196
|
-
state.entries[chunk.chunkKey] = {
|
|
197
|
-
...existing,
|
|
198
|
-
missingCount: 0,
|
|
199
|
-
updatedAt: now,
|
|
200
|
-
};
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
if (existing?.id && existing.sourceType !== "capture") {
|
|
205
|
-
await params.mem0.deleteMemory({
|
|
206
|
-
memoryId: existing.id,
|
|
207
|
-
scope: {
|
|
208
|
-
workspaceHash: existing.workspaceHash,
|
|
209
|
-
agentId: existing.agentId,
|
|
210
|
-
},
|
|
211
|
-
runId,
|
|
212
|
-
});
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
const metadata = {
|
|
216
|
-
sourceType: chunk.sourceType,
|
|
217
|
-
path: chunk.path,
|
|
218
|
-
workspaceHash: chunk.workspaceHash,
|
|
219
|
-
agentId: chunk.agentId,
|
|
220
|
-
chunkKey: chunk.chunkKey,
|
|
221
|
-
contentHash: chunk.contentHash,
|
|
222
|
-
indexedAt: new Date(now).toISOString(),
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
const addResult = await params.mem0.addMemory({
|
|
226
|
-
text: chunk.text,
|
|
227
|
-
scope: {
|
|
228
|
-
workspaceHash: chunk.workspaceHash,
|
|
229
|
-
agentId: chunk.agentId,
|
|
230
|
-
},
|
|
231
|
-
metadata,
|
|
232
|
-
runId,
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
const next: IndexedEntry = {
|
|
236
|
-
chunkKey: chunk.chunkKey,
|
|
237
|
-
id: addResult.id ?? existing?.id,
|
|
238
|
-
contentHash: chunk.contentHash,
|
|
239
|
-
sourceType: chunk.sourceType,
|
|
240
|
-
path: chunk.path,
|
|
241
|
-
workspaceHash: chunk.workspaceHash,
|
|
242
|
-
agentId: chunk.agentId,
|
|
243
|
-
updatedAt: now,
|
|
244
|
-
missingCount: 0,
|
|
245
|
-
};
|
|
246
|
-
state.entries[chunk.chunkKey] = next;
|
|
247
|
-
upserted += 1;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
params.log.debug("memory_braid.reconcile.progress", {
|
|
251
|
-
runId,
|
|
252
|
-
reason: params.reason,
|
|
253
|
-
processed: Math.min(offset + batch.length, allChunks.length),
|
|
254
|
-
total: allChunks.length,
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
const summary: ReconcileSummary = {
|
|
259
|
-
reason: params.reason,
|
|
260
|
-
total: allChunks.length,
|
|
261
|
-
upserted,
|
|
262
|
-
deleted,
|
|
263
|
-
unchanged,
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
state.lastRunAt = new Date(now).toISOString();
|
|
267
|
-
await writeReconcileState(params.statePaths, state);
|
|
268
|
-
|
|
269
|
-
params.log.debug("memory_braid.reconcile.complete", {
|
|
270
|
-
runId,
|
|
271
|
-
reason: params.reason,
|
|
272
|
-
...summary,
|
|
273
|
-
durMs: Date.now() - startedAt,
|
|
274
|
-
}, true);
|
|
275
|
-
|
|
276
|
-
return summary;
|
|
277
|
-
});
|
|
278
|
-
}
|