-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathindex.ts
More file actions
568 lines (518 loc) · 21.2 KB
/
Copy pathindex.ts
File metadata and controls
568 lines (518 loc) · 21.2 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
import { spawn } from "node:child_process";
import { existsSync, readFileSync } from "node:fs";
import { fileURLToPath } from "node:url";
import { dirname, join } from "node:path";
import { request as httpRequest } from "node:http";
import { request as httpsRequest } from "node:https";
import { TriggerPipeline } from "./src/trigger/trigger-pipeline.js";
import { normalizeNaturalText, buildQaFromHistory } from "./src/capture/message-normalize.js";
import { buildAutoCaptureContent, hasOpenClawHeartbeatAssistant, isOpenClawHeartbeatText, prepareAutoCaptureUser, selectAutoCaptureAssistant } from "./src/capture/auto-capture.js";
const __dirname = dirname(fileURLToPath(import.meta.url));
// When loaded from dist/index.js, __dirname is <project>/dist — go up to project root
const rootDir = __dirname.endsWith("/dist") ? dirname(__dirname) : __dirname;
function stripEnvQuotes(value: string): string {
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'") && value.endsWith("'"))) {
return value.slice(1, -1);
}
return value;
}
function loadDotEnvSync() {
const envPath = join(rootDir, ".env");
if (!existsSync(envPath)) return;
const text = readFileSync(envPath, "utf8");
for (const rawLine of text.split(/\r?\n/)) {
if (!rawLine.trim() || rawLine.trim().startsWith("#")) continue;
const match = rawLine.match(/^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)\s*$/);
if (!match) continue;
const [, key, rawValue] = match;
if (process.env[key] != null) continue;
process.env[key] = stripEnvQuotes(rawValue.trim());
}
}
loadDotEnvSync();
function normalizeBaseUrl(url: string): string {
return String(url || "").replace(/\/+$/, "");
}
function resolveMemsenseApiUrl(): string {
if (process.env.MEMSENSE_API_URL) return normalizeBaseUrl(process.env.MEMSENSE_API_URL);
const hostPort = process.env.MEMSENSE_HOST_PORT || process.env.MEMSENSE_PORT || "8787";
return `http://127.0.0.1:${hostPort}`;
}
let MEMSENSE_API_URL = resolveMemsenseApiUrl();
const VALID_MEMORY_SCOPES = new Set(["user", "team", "org", "task"]);
function firstNonEmptyString(...values: unknown[]): string | null {
for (const value of values) {
if (typeof value !== "string") continue;
const trimmed = value.trim();
if (trimmed) return trimmed;
}
return null;
}
function directFetch(url: string, options: { method?: string; body?: string } = {}): Promise<{ ok: boolean; status: number; json: () => Promise<any> }> {
return new Promise((resolve, reject) => {
const parsed = new URL(url);
const isHttps = parsed.protocol === "https:";
const req = (isHttps ? httpsRequest : httpRequest)(
{
hostname: parsed.hostname,
port: parsed.port || (isHttps ? 443 : 80),
path: parsed.pathname + parsed.search,
method: options.method || "GET",
headers: options.body
? { "content-type": "application/json", "content-length": Buffer.byteLength(options.body) }
: {},
},
(res) => {
let raw = "";
res.on("data", (chunk: Buffer) => { raw += chunk; });
res.on("end", () => {
const status = res.statusCode ?? 0;
resolve({
ok: status >= 200 && status < 300,
status,
json: () => {
try { return Promise.resolve(JSON.parse(raw)); }
catch (e) { return Promise.reject(e); }
},
});
});
}
);
req.on("error", reject);
if (options.body) req.write(options.body);
req.end();
});
}
async function isMemsenseApiHealthy() {
try {
const res = await directFetch(`${MEMSENSE_API_URL}/healthz`);
if (!res.ok) return false;
const json = await res.json();
return json?.ok === true;
} catch {
return false;
}
}
function isDockerBootstrapEnv() {
const bgeEndpoint = String(process.env.MEMSENSE_BGE_ENDPOINT || "");
return Boolean(process.env.MEMSENSE_HOST_PORT) || /^https?:\/\/bge(?::|\/)/.test(bgeEndpoint);
}
function isWindowsRuntime() {
return process.platform === "win32";
}
function setupQuickStartHint() {
if (isWindowsRuntime()) {
return [
"Quick start (Windows PowerShell):",
"- Interactive: .\\scripts\\bootstrap.ps1",
"- OpenAI mode: .\\scripts\\bootstrap.ps1 openai",
"- Local mode: .\\scripts\\bootstrap.ps1 local",
].join("\n");
}
return [
"Quick start:",
"- Interactive: bash scripts/bootstrap.sh",
"- OpenAI mode: bash scripts/bootstrap.sh openai",
"- Local mode: bash scripts/bootstrap.sh local",
].join("\n");
}
async function shouldStartLocalService(ctx: any, pluginServiceMode?: string) {
const mode = String(process.env.MEMSENSE_SERVICE_MODE || pluginServiceMode || "auto").toLowerCase();
if (["external", "none", "off", "false", "0"].includes(mode)) {
ctx.logger.info(`memsense service autostart disabled; using ${MEMSENSE_API_URL}`);
return false;
}
if (await isMemsenseApiHealthy()) {
ctx.logger.info(`memsense API already healthy at ${MEMSENSE_API_URL}; local service start skipped`);
return false;
}
if (mode === "local" || mode === "managed") return true;
if (isDockerBootstrapEnv()) {
const bootstrapCommand = isWindowsRuntime() ? ".\\scripts\\bootstrap.ps1" : "bash scripts/bootstrap.sh";
ctx.logger.warn(`memsense Docker-mode env detected but ${MEMSENSE_API_URL}/healthz is not healthy; run ${bootstrapCommand} instead of starting local processes`);
return false;
}
return true;
}
async function getSetupStatusHint() {
try {
const res = await directFetch(`${MEMSENSE_API_URL}/v1/system/setup-status`);
const json = await res.json();
if (!res.ok || !json?.ok) return "";
const d = json.data || {};
if (d.ok) return "";
const checks = (d.checks || []).map((c: any) => `- ${c.key}: ${c.message}`).join("\n");
const steps = (d.next_steps || []).map((s: string) => `- ${s}`).join("\n");
return `\n\n[MEMSENSE_SETUP_STATUS]\nprovider=${d.provider}\n${checks}\n${steps}`;
} catch {
return "";
}
}
async function callApi(path: string, body: unknown) {
const bodyStr = JSON.stringify(body || {});
const res = await directFetch(`${MEMSENSE_API_URL}${path}`, { method: "POST", body: bodyStr });
const json = await res.json();
if (!res.ok || !json?.ok) {
const hint = await getSetupStatusHint();
throw new Error((json?.error || `api failed: ${res.status}`) + hint);
}
return json.data;
}
function withTrace(prefix: string) {
return `${prefix}_${Math.random().toString(16).slice(2, 10)}`;
}
function ok(data: unknown, traceId: string, degraded = false) {
const payload = { ok: true, degraded, trace_id: traceId, data };
return {
content: [{ type: "text" as const, text: JSON.stringify(payload, null, 2) }],
details: payload,
};
}
function withEmbeddingSetupHint(message: string) {
const m = String(message || "");
const looksLikeEmbeddingConfigError =
/MEMSENSE_OPENAI_API_KEY|embedding|bge_http|openai provider|required/i.test(m);
if (!looksLikeEmbeddingConfigError) return m;
return `${m}\n\n[MEMSENSE_SETUP_REQUIRED]\nPlease ask user to choose embedding strategy:\n1) openai-compatible (Qwen/OpenAI API)\n2) local-bge (one-click local model deployment)\n\n${setupQuickStartHint()}`;
}
function fail(errorCode: string, message: string, traceId: string, degraded = false) {
const payload = { ok: false, degraded, trace_id: traceId, error_code: errorCode, message: withEmbeddingSetupHint(message) };
return {
content: [{ type: "text" as const, text: JSON.stringify(payload, null, 2) }],
details: payload,
};
}
const sessionPendingAutoSave = new Map<string, { user: string; tags: string[]; taskTag?: string | null; source: string; agentId?: string | null; userId?: string | null }>();
const sessionInjected = new Set<string>();
const triggerPipeline = new TriggerPipeline();
function shouldSkipAutoCapture(sessionId: unknown, ctx: any, event: any) {
const sid = String(sessionId || '');
const agentId = String(ctx?.agentId || event?.agentId || '').trim();
const sessionKey = String(ctx?.sessionKey || '');
if (!sid) return true;
if (sid === 'memsense-tagger') return true;
if (sid.startsWith('memsense-internal:')) return true;
if (agentId === 'memsense-tagger') return true;
if (sessionKey.includes(':memsense_test_') ) return true;
return false;
}
function isMeaningfulQuery(text: string): boolean {
const t = String(text || "").trim();
if (!t) return false;
if (t.length < 4) return false;
if (isOpenClawHeartbeatText(t)) return false;
if (/^(hi|hello|hey|你好|在吗|在?|在吗?|谢谢|thanks|ok|好的|嗯嗯)$/i.test(t)) return false;
if (/^[??!!。.,,\s]+$/.test(t)) return false;
return true;
}
function renderMemoryContent(content: unknown): string {
const raw = String(content || "").trim();
if (!raw) return "";
try {
const qa = JSON.parse(raw);
const userText = String(qa?.user || "").trim();
const assistantText = String(qa?.assistant || "").trim();
if (userText && assistantText && assistantText !== userText) {
return `User:\n${userText}\n\nAssistant:\n${assistantText}`;
}
return userText || assistantText || raw;
} catch {
return raw;
}
}
function formatMemoryInjection(chunks: any[]): string {
if (!chunks || chunks.length === 0) return "";
console.log("[memsense] formatting", chunks.length, "chunks");
const parts: string[] = [];
const seen = new Set<string>();
for (let i = 0; i < chunks.length; i++) {
const c = chunks[i];
const content = c?.content || c?.text || "";
if (!content) continue;
const contentLen = content.length;
const taskTagLen = (c?.task_tag || "").length;
console.log(`[memsense] chunk[${i}]: content=${contentLen} chars, task_tag=${taskTagLen} chars`);
const meta: string[] = [];
if (c?.task_tag) meta.push(`Summary: ${c.task_tag}`);
if (c?.timestamp_ms) {
const dt = new Date(Number(c.timestamp_ms));
if (!isNaN(dt.getTime())) meta.push(`Date: ${dt.toISOString().split("T")[0]}`);
}
if (c?.memory_kind) meta.push(`Kind: ${c.memory_kind}`);
let block = "";
if (meta.length) block += `[${meta.join(" | ")}]\n`;
const displayContent = renderMemoryContent(content);
if (!displayContent) continue;
const contentKey = displayContent.replace(/\s+/g, " ").slice(0, 1000);
seen.add(contentKey);
block += displayContent;
const neighbors = c?.source !== "eval_ingest_session" && Array.isArray(c?.neighbors)
? [...c.neighbors].sort((a, b) => Number(a?.neighbor_distance || 0) - Number(b?.neighbor_distance || 0))
: [];
for (const n of neighbors) {
const neighborContent = renderMemoryContent(n?.content || "");
if (!neighborContent) continue;
const neighborKey = neighborContent.replace(/\s+/g, " ").slice(0, 1000);
if (seen.has(neighborKey)) continue;
seen.add(neighborKey);
const distance = Number(n?.neighbor_distance || 0);
const label = distance < 0 ? "Previous turn" : "Next turn";
block += `\n\n[${label}]\n${neighborContent}`;
}
parts.push(block);
}
if (!parts.length) return "";
return `<relevant_context>\n${parts.join("\n\n---\n\n")}\n</relevant_context>`;
}
const RetrieveSchema = {
type: "object",
additionalProperties: false,
properties: {
query: { type: "string" },
top_k: { type: "integer", minimum: 1, maximum: 20 },
maxResults: { type: "integer", minimum: 1, maximum: 20 },
},
required: ["query"],
};
export default {
id: "memsense",
name: "Memsense",
description: "Memsense memory plugin for OpenClaw",
kind: "memory",
register(api: OpenClawPluginApi) {
const pluginConfig = ((api as any).pluginConfig && typeof (api as any).pluginConfig === "object")
? (api as any).pluginConfig as Record<string, unknown>
: {};
const serviceUrl = typeof pluginConfig.serviceUrl === "string" ? pluginConfig.serviceUrl.trim() : "";
if (serviceUrl) MEMSENSE_API_URL = normalizeBaseUrl(serviceUrl);
const pluginServiceMode = typeof pluginConfig.serviceMode === "string"
? pluginConfig.serviceMode
: pluginConfig.localMode === false
? "external"
: undefined;
function resolveTenantId(): string {
return firstNonEmptyString(
pluginConfig.tenantId,
pluginConfig.tenant_id,
process.env.MEMSENSE_TENANT_ID,
) || "default";
}
function resolveMemoryScope(): string {
const scope = firstNonEmptyString(pluginConfig.scope, process.env.MEMSENSE_SCOPE) || "user";
return VALID_MEMORY_SCOPES.has(scope) ? scope : "user";
}
function resolveTopK(params: any, fallback = 4): number {
const configured = Number(pluginConfig.maxTopK);
const ceiling = Number.isFinite(configured)
? Math.max(1, Math.min(20, Math.trunc(configured)))
: 20;
const raw = params?.top_k ?? params?.maxResults ?? fallback;
const n = Number(raw);
const requested = Number.isFinite(n)
? Math.max(1, Math.min(20, Math.trunc(n)))
: Math.max(1, Math.min(20, Math.trunc(fallback)));
return Math.min(requested, ceiling);
}
let localServiceStarted = false;
api.registerService({
id: "memsense-server",
async start(ctx) {
if (!(await shouldStartLocalService(ctx, pluginServiceMode))) return;
if (isWindowsRuntime()) {
ctx.logger.warn("memsense native Windows local autostart is not supported because it uses Bash no-Docker scripts. Start MemSense with Docker Desktop from PowerShell: .\\scripts\\bootstrap.ps1 local or .\\scripts\\bootstrap.ps1 openai, then keep serviceMode=auto or set serviceMode=external.");
return;
}
const scriptPath = join(rootDir, "scripts", "start-bash.sh");
const child = spawn("bash", [scriptPath], { cwd: __dirname, stdio: "inherit", detached: true });
child.unref();
localServiceStarted = true;
ctx.logger.info(`memsense local service start requested; API URL ${MEMSENSE_API_URL}`);
},
async stop(ctx) {
if (!localServiceStarted) {
ctx.logger.info("memsense local service stop skipped; this plugin instance did not start local services");
return;
}
const scriptPath = join(rootDir, "scripts", "stop-bash.sh");
spawn("bash", [scriptPath], { cwd: __dirname, stdio: "inherit" });
ctx.logger.info("memsense server stopped");
},
});
function resolveUserId(ctx: any, event: any): string | null {
const direct = ctx?.userId || event?.userId || null;
if (direct) return direct;
const sk = String(ctx?.sessionKey || event?.sessionKey || '');
const m = sk.match(/openresponses-user:([^:\s]+)/);
return m ? m[1] : null;
}
api.on("llm_input", async (event: any, ctx: any) => {
const sid = ctx?.sessionId || event?.sessionId;
if (shouldSkipAutoCapture(sid, ctx, event)) return;
const prepared = prepareAutoCaptureUser(event?.prompt || "", triggerPipeline);
if (!prepared.shouldCapture || !prepared.decision) {
sessionPendingAutoSave.delete(String(sid));
return;
}
const resolvedUserId = resolveUserId(ctx, event);
sessionPendingAutoSave.set(String(sid), {
user: prepared.user,
tags: prepared.decision.tags || [],
taskTag: prepared.decision.tags?.[0] || null,
source: 'session_auto',
agentId: String(ctx?.agentId || event?.agentId || api.id || 'memsense'),
userId: resolvedUserId,
});
});
api.on("llm_output", async (event: any, ctx: any) => {
const sid = ctx?.sessionId || event?.sessionId;
if (shouldSkipAutoCapture(sid, ctx, event)) return;
if (hasOpenClawHeartbeatAssistant(event)) {
sessionPendingAutoSave.delete(String(sid));
return;
}
const pending = sessionPendingAutoSave.get(String(sid));
if (!pending) return;
try {
const assistant = selectAutoCaptureAssistant(event);
if (!assistant) {
console.warn('[memsense] auto-save skipped: empty assistant output', { sessionId: String(sid) });
return;
}
const content = buildAutoCaptureContent({ user: pending.user, assistant });
if (!content) {
console.warn('[memsense] auto-save skipped: empty canonical QA', { sessionId: String(sid) });
return;
}
await callApi("/v1/memory/save", {
tenant_id: resolveTenantId(),
scope: resolveMemoryScope(),
session_id: String(sid),
agent_id: pending.agentId || String(ctx?.agentId || event?.agentId || api.id || 'memsense'),
user_id: pending.userId || resolveUserId(ctx, event),
content,
task_tag: pending.taskTag,
tags: pending.tags,
type_hint: "qa_chunk",
source: pending.source || "session_auto",
timestamp: Date.now(),
score: 0.5,
confidence: 0.7,
});
} catch (e) {
console.error('[memsense] auto-save failed', e);
} finally {
sessionPendingAutoSave.delete(String(sid));
}
});
api.on("before_prompt_build", async (event: any, ctx: any) => {
console.log("[memsense] before_prompt_build triggered");
const sid = ctx?.sessionId;
const sessionKey = String(ctx?.sessionKey || '');
const isTestSession = sessionKey.includes(':memsense_test_');
if (!sid || (!isTestSession && sessionInjected.has(String(sid)))) {
console.log("[memsense] skipping - no sid or already injected");
return;
}
const prompt = normalizeNaturalText(String(event?.prompt || ""));
console.log("[memsense] normalized prompt:", prompt.slice(0, 100));
if (!isMeaningfulQuery(prompt)) {
console.log("[memsense] query not meaningful, skipping");
return;
}
try {
// For eval sessions with per-question user keys (e.g. memsense_test_eval-conv-26-q5),
// strip the -qN suffix so memory search finds chunks under the base user_id.
let searchUserId = resolveUserId(ctx, event);
if (searchUserId && /^memsense_test_.*-q\d+$/.test(searchUserId)) {
searchUserId = searchUserId.replace(/-q\d+$/, '');
console.log("[memsense] eval mode: stripped -qN suffix, searchUserId=", searchUserId);
}
console.log("[memsense] calling search API...", { searchUserId });
const result = await callApi("/v1/memory/search", {
tenant_id: resolveTenantId(),
scope: resolveMemoryScope(),
user_id: searchUserId,
query: prompt,
top_k: 4,
});
console.log("[memsense] search returned", result?.chunks?.length || 0, "chunks");
const injection = formatMemoryInjection(result?.chunks || []);
sessionInjected.add(String(sid));
if (!injection) {
console.log("[memsense] no injection content");
return;
}
console.log("[memsense] injecting", injection.length, "chars");
return { prependContext: injection };
} catch (e) {
console.error("[memsense] search failed:", e);
sessionInjected.add(String(sid));
return;
}
});
// v1 aliases aligned with PRD naming: search/fetch_recent
api.registerTool({
name: "memory_search",
label: "Memory Search",
description: "Search memories and return top-k raw chunks",
parameters: RetrieveSchema,
async execute(_toolCallId, params: any) {
const traceId = withTrace("search");
try {
const chunks = await callApi("/v1/memory/search", {
tenant_id: resolveTenantId(),
scope: resolveMemoryScope(),
query: params?.query,
top_k: resolveTopK(params, 4),
});
const rawChunks = chunks?.chunks || chunks || [];
const cleanChunks = rawChunks.map(({ embedding, ...rest }: any) => rest);
return ok({ chunks: cleanChunks }, traceId);
} catch (e: any) {
return fail("SEARCH_FAILED", e?.message || "search failed", traceId, true);
}
},
});
api.registerTool({
name: "memory_fetch_recent",
label: "Memory Fetch Recent",
description: "Fetch recent memory chunks",
parameters: {
type: "object",
additionalProperties: false,
properties: {
limit: { type: "integer", minimum: 1, maximum: 100 },
},
required: [],
},
async execute(_toolCallId, params: any) {
const traceId = withTrace("fetch_recent");
try {
const chunksResp = await callApi("/v1/memory/fetch_recent", {
tenant_id: resolveTenantId(),
scope: resolveMemoryScope(),
limit: params?.limit ?? 10,
});
return ok(chunksResp, traceId);
} catch (e: any) {
return fail("FETCH_RECENT_FAILED", e?.message || "fetch recent failed", traceId, true);
}
},
});
api.registerCli(
({ program }) => {
program
.command("memsense-ping")
.description("Ping memsense plugin")
.action(() => {
console.log("memsense: ok");
});
},
{
commands: ["memsense-ping"],
descriptors: [{ name: "memsense-ping", description: "Ping memsense plugin", hasSubcommands: false }],
},
);
},
};