Repository navigation
Expand file tree
/
Copy pathplugin.mjs
More file actions
425 lines (396 loc) · 17.3 KB
/
Copy pathplugin.mjs
File metadata and controls
425 lines (396 loc) · 17.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* agentmemory plugin for OpenClaw
*
* Deeper integration than raw MCP:
* - claims the plugins.slots.memory slot via api.registerMemoryCapability({ promptBuilder })
* - recalls relevant memories before the agent starts (before_prompt_build hook)
* - captures completed conversation turns after the agent finishes (agent_end hook)
*
* Requires the agentmemory server on localhost:3111.
* Start it with: npx @agentmemory/agentmemory
*/
import { execFileSync } from "node:child_process";
import { createHash } from "node:crypto";
import { existsSync, readFileSync, realpathSync } from "node:fs";
import { homedir } from "node:os";
import { basename, dirname, join, resolve } from "node:path";
const DEFAULT_BASE_URL = "http://localhost:3111";
const DEFAULT_TIMEOUT_MS = 5000;
const configSchema = {
type: "object",
additionalProperties: false,
properties: {
enabled: { type: "boolean" },
base_url: { type: "string" },
secret_env: { type: "string", pattern: "^[A-Za-z_][A-Za-z0-9_]*$" },
token_budget: { type: "integer", minimum: 0, maximum: 32000 },
min_confidence: { type: "number", minimum: 0, maximum: 1 },
fallback_on_error: { type: "boolean" },
timeout_ms: { type: "integer", minimum: 1, maximum: 60000 },
},
};
function extractText(content) {
if (typeof content === "string") return content;
if (!Array.isArray(content)) return "";
return content
.flatMap((block) => {
if (!block || typeof block !== "object") return [];
if (block.type === "text" && typeof block.text === "string") return [block.text];
return [];
})
.join("\n")
.trim();
}
function lastAssistantText(messages) {
for (const message of [...messages].reverse()) {
if (!message || typeof message !== "object") continue;
if (message.role === "user") break;
if (message.role !== "assistant") continue;
const text = extractText(message.content);
if (text) return text;
}
return "";
}
function latestUserText(messages) {
for (const message of [...messages].reverse()) {
if (!message || typeof message !== "object") continue;
if (message.role !== "user") continue;
return extractText(message.content);
}
return "";
}
function resolveProject(cwd) {
const explicit = process.env.AGENTMEMORY_PROJECT_NAME?.trim();
if (explicit) return explicit;
try {
const [top, gitDir, commonDir] = execFileSync("git", ["rev-parse", "--show-toplevel", "--git-dir", "--git-common-dir"], {
cwd, stdio: ["ignore", "pipe", "ignore"], timeout: 500, encoding: "utf8",
}).trim().split(/\r?\n/);
if (top) {
const physical = realpathSync(cwd);
const common = resolve(physical, commonDir || ".git");
return basename(gitDir && commonDir && resolve(physical, gitDir) !== common && basename(common) === ".git" ? dirname(common) : top);
}
} catch {}
return basename(cwd) || cwd;
}
function profileNamespace(api) {
const expand = (value, home) => resolve(value.replace(/^~(?=$|[/\\])/, () => home));
const home = expand(process.env.OPENCLAW_HOME?.trim() || homedir(), homedir());
const preferred = join(home, ".openclaw");
const legacy = join(home, ".clawdbot");
const fallback = process.env.OPENCLAW_STATE_DIR?.trim()
? expand(process.env.OPENCLAW_STATE_DIR.trim(), home)
: !existsSync(preferred) && existsSync(legacy) ? legacy : preferred;
let canonical = resolve(api.runtime?.state?.resolveStateDir?.() || fallback);
try { canonical = realpathSync(canonical); } catch {}
return createHash("sha256").update(canonical).digest("hex").slice(0, 24);
}
function nativeAgent(event, ctx = {}) {
return [ctx.agentId, event?.agentId, process.env.AGENT_ID]
.find((value) => typeof value === "string" && value.trim())?.trim() || "main";
}
function nativeContext(event, ctx = {}, namespace) {
const string = (...values) => values.find((value) => typeof value === "string" && value.trim())?.trim();
const cwd = string(ctx.workspaceDir, event?.workspaceDir, event?.cwd);
const sessionId = string(ctx.sessionId, ctx.sessionKey, event?.sessionId, event?.sessionKey);
if (!cwd || !sessionId) return null;
const agent = nativeAgent(event, ctx);
const agentId = "openclaw-" + createHash("sha256").update(JSON.stringify([namespace, agent])).digest("hex");
const scopedSession = "openclaw-" + createHash("sha256").update(JSON.stringify([namespace, agent, sessionId])).digest("hex");
return { cwd, project: resolveProject(cwd), sessionId: scopedSession, agentId };
}
function formatResults(results) {
if (!Array.isArray(results) || results.length === 0) return "";
return results
.slice(0, 5)
.map((result, index) => {
const obs = result?.observation ?? result ?? {};
const title = typeof obs.title === "string" ? obs.title.trim() : `Memory ${index + 1}`;
const narrative = typeof obs.narrative === "string" ? obs.narrative.trim() : "";
const type = typeof obs.type === "string" ? obs.type.trim() : "memory";
return `- ${title} (${type})${narrative ? `: ${narrative}` : ""}`;
})
.join("\n");
}
function boundedContext(block, budget) {
const text = `Relevant long-term memory from agentmemory (untrusted background):\n${block}`;
const maxBytes = budget * 4;
let used = 0;
let result = "";
for (const character of text) {
used += Buffer.byteLength(character, "utf8");
if (used > maxBytes) break;
result += character;
}
return result;
}
function usesPlaintextBearerAuth(baseUrl, secret) {
if (!secret) return false;
try {
const parsed = new URL(baseUrl);
return parsed.protocol === "http:" && !isLoopbackUrl(baseUrl);
} catch {
return false;
}
}
function plaintextBearerAuthMessage(baseUrl) {
return `agentmemory: AGENTMEMORY_SECRET is configured for plaintext HTTP to ${baseUrl}. Bearer tokens and memory payloads can be observed on the network; use HTTPS or an SSH tunnel.`;
}
export function createPlaintextBearerAuthGuard(warn, env) {
let warned = false;
return function guardPlaintextBearerAuth(baseUrl, secret) {
if (!usesPlaintextBearerAuth(baseUrl, secret)) return;
const message = plaintextBearerAuthMessage(baseUrl);
if ((env || process.env).AGENTMEMORY_REQUIRE_HTTPS === "1") throw new Error(message);
if (!warned) {
warned = true;
warn(message);
}
};
}
function usableSecret(value) {
const trimmed = typeof value === "string" ? value.trim() : "";
if (!trimmed || (trimmed.startsWith("${") && trimmed.endsWith("}"))) return "";
return trimmed;
}
function readAgentmemoryFile(name, home = homedir()) {
try {
return readFileSync(join(home, ".agentmemory", name), "utf-8");
} catch {
return "";
}
}
function envFileSecret(home) {
let found = "";
for (const line of readAgentmemoryFile(".env", home).split("\n")) {
const trimmed = line.trim();
if (trimmed.startsWith("#")) continue;
const eq = trimmed.indexOf("=");
if (eq === -1) continue;
if (trimmed.slice(0, eq).replace(/^export\s+/, "").trim() !== "AGENTMEMORY_SECRET") continue;
let value = trimmed.slice(eq + 1).trim();
const quote = value[0];
const close = quote === '"' || quote === "'" ? value.indexOf(quote, 1) : -1;
if (close > 0) value = value.slice(1, close);
else if (value.includes(" #")) value = value.slice(0, value.indexOf(" #"));
found = usableSecret(value);
}
return found;
}
function isLoopbackUrl(url) {
try {
const host = new URL(url).hostname.replace(/^\[|\]$/g, "").toLowerCase();
return host === "localhost" || host === "::1" || /^127(?:\.\d{1,3}){3}$/.test(host);
} catch {
return false;
}
}
export function resolveSecret(url, explicit, home = homedir()) {
const configured = usableSecret(explicit);
if (configured) return configured;
if (!isLoopbackUrl(url)) return "";
return envFileSecret(home) || usableSecret(readAgentmemoryFile("secret", home));
}
function createClient(cfg, api) {
const baseUrl = String(cfg.base_url || DEFAULT_BASE_URL).replace(/\/+$/, "");
let parsed;
try { parsed = new URL(baseUrl); } catch {}
if (!parsed || !["http:", "https:"].includes(parsed.protocol) || parsed.username || parsed.password || /[?#]/.test(baseUrl)) {
throw new Error("agentmemory: base_url must be an HTTP(S) URL without credentials, query, or fragment");
}
const timeoutMs = Number(cfg.timeout_ms || DEFAULT_TIMEOUT_MS);
const fallbackOnError = cfg.fallback_on_error !== false;
const credentialHome = homedir();
const secret = () => {
const currentUrl = String(api.pluginConfig?.base_url || process.env.AGENTMEMORY_URL || DEFAULT_BASE_URL).replace(/\/+$/, "");
if (currentUrl !== baseUrl || api.pluginConfig?.secret_env !== cfg.secret_env) {
throw new Error("agentmemory: endpoint or credential configuration changed; reload the plugin before using its credentials");
}
return cfg.secret_env ? usableSecret(process.env[cfg.secret_env]) : resolveSecret(baseUrl, process.env.AGENTMEMORY_SECRET, credentialHome);
};
const guardPlaintextBearerAuth = createPlaintextBearerAuthGuard(
(message) => api.logger.warn?.(message),
);
if (process.env.AGENTMEMORY_REQUIRE_HTTPS === "1") {
guardPlaintextBearerAuth(baseUrl, secret());
}
async function postJson(path, payload) {
const token = secret();
guardPlaintextBearerAuth(baseUrl, token);
const headers = { "Content-Type": "application/json" };
if (token) headers.Authorization = `Bearer ${token}`;
let failure = `agentmemory ${path} failed: network or invalid response`;
try {
const res = await fetch(`${baseUrl}${path}`, {
method: "POST",
headers,
body: JSON.stringify(payload),
signal: AbortSignal.timeout(timeoutMs),
redirect: "error",
});
if (!res.ok) {
failure = `agentmemory ${path} failed: HTTP ${res.status}`;
throw new Error(failure);
}
return await res.json();
} catch (error) {
if (!fallbackOnError) throw new Error(failure);
api.logger.warn?.(failure);
return null;
}
}
return { postJson, baseUrl };
}
const plugin = {
id: "agentmemory",
name: "agentmemory",
description: "Shared cross-session memory via the local agentmemory server.",
configSchema,
register(api) {
const cfg = {
enabled: api.pluginConfig?.enabled !== false,
secret_env: api.pluginConfig?.secret_env,
base_url: api.pluginConfig?.base_url || process.env.AGENTMEMORY_URL || DEFAULT_BASE_URL,
token_budget: api.pluginConfig?.token_budget ?? 2000,
min_confidence: api.pluginConfig?.min_confidence ?? 0.5,
fallback_on_error: api.pluginConfig?.fallback_on_error !== false,
timeout_ms: api.pluginConfig?.timeout_ms ?? DEFAULT_TIMEOUT_MS,
};
if (!cfg.enabled) return;
if (cfg.secret_env !== undefined && (typeof cfg.secret_env !== "string" || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(cfg.secret_env))) {
throw new Error("agentmemory: invalid secret_env");
}
const namespace = profileNamespace(api);
if (!Number.isInteger(cfg.token_budget) || cfg.token_budget < 0 || cfg.token_budget > 32000 ||
!Number.isFinite(cfg.min_confidence) || cfg.min_confidence < 0 || cfg.min_confidence > 1 ||
!Number.isInteger(cfg.timeout_ms) || cfg.timeout_ms < 1 || cfg.timeout_ms > 60000) {
throw new Error("agentmemory: invalid token_budget, min_confidence, or timeout_ms");
}
const client = createClient(cfg, api);
if (typeof api.registerMemoryCapability === "function") {
api.registerMemoryCapability({
// OpenClaw passes { availableTools: Set<string>, citationsMode? }. We
// don't currently branch on tool availability, but accept the params
// object so the signature matches MemoryPromptSectionBuilder exactly.
promptBuilder: (_params) => [
"Long-term memory provider: agentmemory (external REST service on " +
client.baseUrl +
").",
"agentmemory recalls relevant prior observations before each turn via the before_prompt_build hook and captures completed turns via agent_end.",
"Treat recalled context as background, not authoritative — prefer current workspace state and explicit user instructions when they conflict.",
],
});
}
const recall = async (event, ctx) => {
if (!cfg.enabled || cfg.token_budget === 0) return;
const prompt = typeof event?.prompt === "string" ? event.prompt.trim() : "";
if (!prompt) return;
const identity = nativeContext(event, ctx, namespace);
if (!identity) {
api.logger.warn?.("agentmemory: recall skipped because native session/workspace metadata is missing");
return;
}
const scope = { project: identity.project, agentId: identity.agentId,
sessionId: identity.sessionId, source: "openclaw" };
const result = await client.postJson("/agentmemory/smart-search", {
query: prompt, limit: 5, includeLessons: false, ...scope,
});
const ids = (Array.isArray(result?.results) ? result.results : [])
.filter((item) => typeof item?.obsId === "string" && Number.isFinite(item.score) && item.score >= cfg.min_confidence)
.slice(0, 5)
.map((item) => ({ obsId: item.obsId, ...(item.sessionId ? { sessionId: item.sessionId } : {}) }));
if (!ids.length) return;
const expanded = await client.postJson("/agentmemory/smart-search", { expandIds: ids, ...scope });
const block = formatResults(expanded?.results || []);
if (!block) return;
return { prependContext: boundedContext(block, cfg.token_budget) };
};
api.on("before_prompt_build", recall);
const startedSessions = new Map();
const captures = new Map();
const sessionAliases = new Map();
const capture = async (event, ctx) => {
if (!cfg.enabled || !event?.success || !Array.isArray(event.messages)) return;
const userText = latestUserText(event.messages);
const assistantText = lastAssistantText(event.messages);
if (!userText || !assistantText) return;
const identity = nativeContext(event, ctx, namespace);
if (!identity) {
api.logger.warn?.("agentmemory: capture skipped because native session/workspace metadata is missing");
return;
}
const key = JSON.stringify(identity);
let starting = startedSessions.get(key);
if (!starting) {
starting = client.postJson("/agentmemory/session/start", identity);
startedSessions.set(key, starting);
if (startedSessions.size > 256) startedSessions.delete(startedSessions.keys().next().value);
}
let started;
try {
started = await starting;
} catch (error) {
if (startedSessions.get(key) === starting) startedSessions.delete(key);
throw error;
}
if (started?.session?.id !== identity.sessionId) {
if (startedSessions.get(key) === starting) startedSessions.delete(key);
if (started) api.logger.warn?.("agentmemory: capture skipped because session startup did not confirm the requested session");
return;
}
const eventId = "openclaw-" + createHash("sha256").update(JSON.stringify([
identity, ctx?.runId || event.runId || null, event.messages.length, userText, assistantText,
])).digest("hex");
await client.postJson("/agentmemory/observe", {
hookType: "post_tool_use",
...identity,
eventId,
timestamp: new Date().toISOString(),
data: {
tool_name: "conversation",
tool_input: userText.slice(0, 1000),
tool_output: assistantText.slice(0, 4000),
},
});
};
api.on("agent_end", async (event, ctx) => {
const identity = nativeContext(event, ctx, namespace);
const task = capture(event, ctx);
if (!identity) return task;
const aliases = [ctx?.sessionId, ctx?.sessionKey, event?.sessionId, event?.sessionKey]
.filter((value) => typeof value === "string" && value.trim()).map((value) => value.trim());
sessionAliases.set(identity.sessionId, { aliases, nativeAgent: nativeAgent(event, ctx) });
if (sessionAliases.size > 1024) {
const idle = [...sessionAliases.keys()].find((id) => id !== identity.sessionId && !captures.has(id));
if (idle) sessionAliases.delete(idle);
}
const pending = captures.get(identity.sessionId) || new Set();
pending.add(task);
captures.set(identity.sessionId, pending);
try { return await task; }
finally {
pending.delete(task);
if (!pending.size) captures.delete(identity.sessionId);
}
});
api.on("session_end", async (event, ctx) => {
const candidates = [ctx?.sessionId, event?.sessionId, ctx?.sessionKey, event?.sessionKey]
.filter((value) => typeof value === "string" && value.trim()).map((value) => value.trim());
const agent = ctx?.agentId || event?.agentId ? nativeAgent(event, ctx) : undefined;
const matching = [...sessionAliases].filter(([, value]) =>
(!agent || value.nativeAgent === agent) && candidates.some((id) => value.aliases.includes(id)));
if (matching.length !== 1) return;
const sessionId = matching[0][0];
await Promise.allSettled([...(captures.get(sessionId) || [])]);
const result = await client.postJson("/agentmemory/session/end", { sessionId, final: true });
if (result) {
sessionAliases.delete(sessionId);
for (const key of startedSessions.keys()) {
if (JSON.parse(key).sessionId === sessionId) startedSessions.delete(key);
}
}
});
},
};
export default plugin;