@mysten-incubation/oc-memwal 0.0.5-dev.0 → 0.0.5-dev.1
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/dist/capture.d.ts +4 -18
- package/dist/capture.d.ts.map +1 -1
- package/dist/capture.js +49 -39
- package/dist/capture.js.map +1 -1
- package/dist/config.d.ts +4 -28
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +35 -39
- package/dist/config.js.map +1 -1
- package/dist/hooks/recall.d.ts +0 -4
- package/dist/hooks/recall.d.ts.map +1 -1
- package/dist/hooks/recall.js +31 -17
- package/dist/hooks/recall.js.map +1 -1
- package/package.json +3 -2
package/dist/capture.d.ts
CHANGED
|
@@ -1,25 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Capture filtering — determines whether a conversation turn
|
|
3
3
|
* is worth sending to analyze() for fact extraction.
|
|
4
|
-
*
|
|
5
|
-
* Prevents wasted server calls on trivial turns like "ok", "thanks", emoji.
|
|
6
|
-
* Based on patterns from LanceDB's shouldCapture() implementation.
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* Check if text looks like a prompt injection attempt.
|
|
10
|
-
* Used by both capture (reject on store) and recall (reject on inject).
|
|
11
4
|
*/
|
|
5
|
+
/** Normalize text by stripping invisible/format characters, decomposing Unicode, and mapping confusable scripts. */
|
|
6
|
+
export declare function normalizeForInjectionCheck(text: string): string;
|
|
7
|
+
/** Check if text matches known prompt injection patterns. */
|
|
12
8
|
export declare function looksLikeInjection(text: string): boolean;
|
|
13
|
-
/**
|
|
14
|
-
* Determine whether a conversation text is worth capturing.
|
|
15
|
-
*
|
|
16
|
-
* Applies a multi-step filter chain: rejects short text, filler responses,
|
|
17
|
-
* XML/system content, emoji-heavy messages, and injection attempts.
|
|
18
|
-
* Accepts immediately if a trigger pattern matches (e.g. "remember", "prefer").
|
|
19
|
-
* Falls through to accept if text is long enough for the server LLM to evaluate.
|
|
20
|
-
*
|
|
21
|
-
* @param text - Raw message text (user or assistant)
|
|
22
|
-
* @returns `true` if the text likely contains memorable facts
|
|
23
|
-
*/
|
|
9
|
+
/** Determine whether a conversation turn is worth capturing. */
|
|
24
10
|
export declare function shouldCapture(text: string): boolean;
|
|
25
11
|
//# sourceMappingURL=capture.d.ts.map
|
package/dist/capture.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../src/capture.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../src/capture.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAkDH,oHAAoH;AACpH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAW/D;AAED,6DAA6D;AAC7D,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAKxD;AAED,gEAAgE;AAChE,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAYnD"}
|
package/dist/capture.js
CHANGED
|
@@ -1,76 +1,86 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Capture filtering — determines whether a conversation turn
|
|
3
3
|
* is worth sending to analyze() for fact extraction.
|
|
4
|
-
*
|
|
5
|
-
* Prevents wasted server calls on trivial turns like "ok", "thanks", emoji.
|
|
6
|
-
* Based on patterns from LanceDB's shouldCapture() implementation.
|
|
7
4
|
*/
|
|
8
5
|
import { MIN_CAPTURE_LENGTH, MAX_EMOJI_COUNT } from "./constants.js";
|
|
9
6
|
/** Filler patterns — exact-match trivial responses. */
|
|
10
7
|
const FILLER_PATTERN = /^(ok|okay|sure|thanks|thank you|thx|yes|yep|yeah|no|nope|nah|got it|hmm|hm|ah|oh|lol|haha|nice|cool|great|right|alright|fine|k|kk)\s*[.!?]*$/i;
|
|
11
|
-
/**
|
|
8
|
+
/** Visual lookalike character map (Cyrillic, Greek, Latin symbols). */
|
|
9
|
+
const VISUAL_CONFUSABLES = {
|
|
10
|
+
"а": "a", "А": "a", "б": "b", "Б": "b", "в": "b", "В": "b", "ь": "b", "Ь": "b",
|
|
11
|
+
"с": "c", "С": "c", "д": "d", "Д": "d", "е": "e", "Е": "e", "ѐ": "e", "Ѐ": "e",
|
|
12
|
+
"ё": "e", "Ё": "e", "є": "e", "Є": "e", "і": "i", "І": "i", "ї": "i", "Ї": "i",
|
|
13
|
+
"ј": "j", "Ј": "j", "к": "k", "К": "k", "л": "l", "Л": "l", "м": "m", "М": "m",
|
|
14
|
+
"н": "h", "Н": "h", "п": "n", "П": "n", "о": "o", "О": "o", "ѻ": "o", "Ѻ": "o",
|
|
15
|
+
"р": "p", "Р": "p", "ҏ": "p", "Ҏ": "p", "ѕ": "s", "Ѕ": "s", "т": "t", "Т": "t",
|
|
16
|
+
"и": "u", "И": "u", "й": "u", "Й": "u", "у": "y", "У": "y", "х": "x", "Х": "x",
|
|
17
|
+
"α": "a", "Α": "a", "β": "b", "Β": "b", "γ": "y", "Γ": "r", "δ": "d", "Δ": "d",
|
|
18
|
+
"ε": "e", "Ε": "e", "ζ": "z", "Ζ": "z", "η": "n", "Η": "h", "θ": "o", "Θ": "o",
|
|
19
|
+
"ι": "i", "Ι": "i", "κ": "k", "Κ": "k", "λ": "l", "Λ": "l", "μ": "u", "Μ": "m",
|
|
20
|
+
"ν": "v", "Ν": "n", "ξ": "x", "Ξ": "x", "ο": "o", "Ο": "o", "π": "n", "Π": "n",
|
|
21
|
+
"ρ": "p", "Ρ": "p", "σ": "o", "Σ": "e", "ς": "s", "τ": "t", "Τ": "t", "υ": "u",
|
|
22
|
+
"Υ": "y", "φ": "o", "Φ": "o", "χ": "x", "Χ": "x", "ψ": "y", "Ψ": "y", "ω": "w",
|
|
23
|
+
"Ω": "o", "ℓ": "l", "ø": "o", "Ø": "o", "đ": "d", "Đ": "d", "ħ": "h", "Ħ": "h",
|
|
24
|
+
"ł": "l", "Ł": "l", "ŋ": "n", "Ŋ": "n", "ŧ": "t", "Ŧ": "t",
|
|
25
|
+
};
|
|
26
|
+
/** Prompt injection patterns — never capture or recall these. */
|
|
12
27
|
const INJECTION_PATTERNS = [
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
/\
|
|
28
|
+
/\b(ign[o0a]re|disregard|override|f[o0]rget|bypass)\b[\s\S]{0,140}?\b(instruct\w*|instr\w{1,8}tions?|pr[o0]mpt|rules|c[o0]nstraints|guidelines|safety)\b/i,
|
|
29
|
+
/\b(ign[o0a]re|disregard|f[o0]rget|override)\b[\s\S]{0,80}?\b(everything|anything)\b[\s\S]{0,80}?\b(bef[o0]re|pri[o0]r|t[o0]ld|pr[o0]mpt)\b/i,
|
|
30
|
+
/\b(?:do\s+not|don\x27?t|st[o0]p)\s+f[o0]ll[o0]w(?:ing)?\b[\s\S]{0,80}?\b(?:system|devel[o0]per|safety|rules|instructions?|instr\w{1,8}tions?)\b/i,
|
|
31
|
+
/\b(?:new\s+instructions|system\s+override)\s*:\s*/i,
|
|
32
|
+
/\byou\s+are\s+now\s+(?:dan|unrestricted|jailbroken|unfiltered)\b/i,
|
|
33
|
+
/\bsystem\s+pr[o0]mpt\b/i,
|
|
34
|
+
/\breveal\s+(?:all\s+)?(?:your\s+)?(?:system\s+)?instructions?\b/i,
|
|
35
|
+
/<\s*\/?\s*(system|assistant|developer|tool|function|prompt)\b/i,
|
|
36
|
+
/\b(run|execute|call|invoke)\b.{0,40}\b(tool|command|shell|bash)\b/i,
|
|
18
37
|
];
|
|
19
|
-
/** Memory trigger patterns — always capture if matched
|
|
38
|
+
/** Memory trigger patterns — always capture if matched. */
|
|
20
39
|
const TRIGGER_PATTERNS = [
|
|
21
40
|
/remember|prefer|radši|zapamatuj/i,
|
|
22
41
|
/i (like|prefer|hate|love|want|need|use|am|work)/i,
|
|
23
42
|
/my\s+\w+\s+is|is\s+my/i,
|
|
24
43
|
/always|never|important/i,
|
|
25
44
|
/decided|will use|switched to/i,
|
|
26
|
-
/\+\d{10,}/,
|
|
27
|
-
/[\w.-]+@[\w.-]+\.\w+/,
|
|
45
|
+
/\+\d{10,}/,
|
|
46
|
+
/[\w.-]+@[\w.-]+\.\w+/,
|
|
28
47
|
];
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
48
|
+
/** Normalize text by stripping invisible/format characters, decomposing Unicode, and mapping confusable scripts. */
|
|
49
|
+
export function normalizeForInjectionCheck(text) {
|
|
50
|
+
const noInvisible = text.replace(/[\p{Cf}\p{Cc}\p{Co}\p{Cs}]/gu, "");
|
|
51
|
+
const decomposed = noInvisible
|
|
52
|
+
.normalize("NFKD")
|
|
53
|
+
.replace(/[\p{Diacritic}\p{M}]/gu, "");
|
|
54
|
+
let mapped = "";
|
|
55
|
+
for (const char of decomposed) {
|
|
56
|
+
mapped += VISUAL_CONFUSABLES[char] ?? char;
|
|
57
|
+
}
|
|
58
|
+
return mapped.replace(/\s+/g, " ").trim();
|
|
59
|
+
}
|
|
60
|
+
/** Check if text matches known prompt injection patterns. */
|
|
36
61
|
export function looksLikeInjection(text) {
|
|
37
|
-
|
|
62
|
+
if (!text)
|
|
63
|
+
return false;
|
|
64
|
+
const normalized = normalizeForInjectionCheck(text);
|
|
38
65
|
if (!normalized)
|
|
39
66
|
return false;
|
|
40
67
|
return INJECTION_PATTERNS.some((p) => p.test(normalized));
|
|
41
68
|
}
|
|
42
|
-
/**
|
|
43
|
-
* Determine whether a conversation text is worth capturing.
|
|
44
|
-
*
|
|
45
|
-
* Applies a multi-step filter chain: rejects short text, filler responses,
|
|
46
|
-
* XML/system content, emoji-heavy messages, and injection attempts.
|
|
47
|
-
* Accepts immediately if a trigger pattern matches (e.g. "remember", "prefer").
|
|
48
|
-
* Falls through to accept if text is long enough for the server LLM to evaluate.
|
|
49
|
-
*
|
|
50
|
-
* @param text - Raw message text (user or assistant)
|
|
51
|
-
* @returns `true` if the text likely contains memorable facts
|
|
52
|
-
*/
|
|
69
|
+
/** Determine whether a conversation turn is worth capturing. */
|
|
53
70
|
export function shouldCapture(text) {
|
|
54
|
-
// Too short to contain useful facts
|
|
55
71
|
if (text.length < MIN_CAPTURE_LENGTH)
|
|
56
72
|
return false;
|
|
57
|
-
// Pure filler response
|
|
58
73
|
if (FILLER_PATTERN.test(text.trim()))
|
|
59
74
|
return false;
|
|
60
|
-
// System/XML content (likely injected context, not user speech)
|
|
61
75
|
if (text.trim().startsWith("<") && text.includes("</"))
|
|
62
76
|
return false;
|
|
63
|
-
// Emoji-heavy (likely reactions, not factual content)
|
|
64
77
|
const emojiCount = (text.match(/[\u{1F300}-\u{1F9FF}]/gu) || []).length;
|
|
65
78
|
if (emojiCount > MAX_EMOJI_COUNT)
|
|
66
79
|
return false;
|
|
67
|
-
|
|
68
|
-
if (INJECTION_PATTERNS.some((p) => p.test(text)))
|
|
80
|
+
if (looksLikeInjection(text))
|
|
69
81
|
return false;
|
|
70
|
-
// If it matches a trigger pattern, definitely capture
|
|
71
82
|
if (TRIGGER_PATTERNS.some((p) => p.test(text)))
|
|
72
83
|
return true;
|
|
73
|
-
// Default: capture if it's long enough (the server LLM will decide what's worth keeping)
|
|
74
84
|
return true;
|
|
75
85
|
}
|
|
76
86
|
//# sourceMappingURL=capture.js.map
|
package/dist/capture.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capture.js","sourceRoot":"","sources":["../src/capture.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"capture.js","sourceRoot":"","sources":["../src/capture.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAErE,uDAAuD;AACvD,MAAM,cAAc,GAAG,+IAA+I,CAAC;AAEvK,uEAAuE;AACvE,MAAM,kBAAkB,GAA2B;IACjD,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE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AAkB,CAAC,IAAY;IAC7C,IAAI,CAAC,IAAI;QAAE,OAAO,KAAK,CAAC;IACxB,MAAM,UAAU,GAAG,0BAA0B,CAAC,IAAI,CAAC,CAAC;IACpD,IAAI,CAAC,UAAU;QAAE,OAAO,KAAK,CAAC;IAC9B,OAAO,kBAAkB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;AAC5D,CAAC;AAED,gEAAgE;AAChE,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,IAAI,IAAI,CAAC,MAAM,GAAG,kBAAkB;QAAE,OAAO,KAAK,CAAC;IACnD,IAAI,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QAAE,OAAO,KAAK,CAAC;IACnD,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IAErE,MAAM,UAAU,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,yBAAyB,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC;IACxE,IAAI,UAAU,GAAG,eAAe;QAAE,OAAO,KAAK,CAAC;IAE/C,IAAI,kBAAkB,CAAC,IAAI,CAAC;QAAE,OAAO,KAAK,CAAC;IAC3C,IAAI,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,IAAI,CAAC;IAE5D,OAAO,IAAI,CAAC;AACd,CAAC"}
|
package/dist/config.d.ts
CHANGED
|
@@ -1,40 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Config parsing, validation, and namespace resolution.
|
|
3
|
-
*
|
|
4
|
-
* Uses Zod for schema validation — all config errors surface at startup
|
|
5
|
-
* with clear messages instead of failing at runtime.
|
|
6
3
|
*/
|
|
7
4
|
import type { PluginConfig } from "./types.js";
|
|
8
|
-
/**
|
|
9
|
-
* Parse and validate raw plugin config from openclaw.json.
|
|
10
|
-
*
|
|
11
|
-
* Resolves ${ENV_VAR} placeholders in string fields, then validates
|
|
12
|
-
* the full config against the Zod schema. Throws with clear field-level
|
|
13
|
-
* error messages on invalid config.
|
|
14
|
-
*
|
|
15
|
-
* @param raw - Raw config object from `api.pluginConfig`
|
|
16
|
-
* @returns Validated config with all defaults applied
|
|
17
|
-
* @throws {Error} If config is missing, or any field fails validation
|
|
18
|
-
*/
|
|
19
5
|
export declare function parseConfig(raw: unknown): PluginConfig;
|
|
20
6
|
export interface ResolvedAgent {
|
|
21
7
|
namespace: string;
|
|
8
|
+
legacyNamespace?: string;
|
|
22
9
|
agentName: string;
|
|
23
10
|
}
|
|
24
|
-
/**
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
* Parses agent name from format "agent:\<name\>:\<uuid\>".
|
|
28
|
-
* Each agent gets its own namespace for memory isolation.
|
|
29
|
-
* Falls back to defaultNamespace for main agent or unknown sessions.
|
|
30
|
-
*
|
|
31
|
-
* @param defaultNamespace - Fallback namespace (used for main agent)
|
|
32
|
-
* @param sessionKey - OpenClaw session key, e.g. "agent:researcher:uuid-456"
|
|
33
|
-
* @returns Resolved namespace and human-readable agent name
|
|
34
|
-
*/
|
|
11
|
+
/** Derive a collision-resistant namespace for an agent name. */
|
|
12
|
+
export declare function deriveAgentNamespace(rawName: string): string;
|
|
13
|
+
/** Resolve agent namespace and human-readable name from sessionKey. */
|
|
35
14
|
export declare function resolveAgent(defaultNamespace: string, sessionKey?: string): ResolvedAgent;
|
|
36
|
-
/**
|
|
37
|
-
* Format key for safe logging (first 4 + last 4 chars).
|
|
38
|
-
*/
|
|
39
15
|
export declare function keyPreview(key: string): string;
|
|
40
16
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAqD/C,wBAAgB,WAAW,CAAC,GAAG,EAAE,OAAO,GAAG,YAAY,CAetD;AAMD,MAAM,WAAW,aAAa;IAC5B,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,gEAAgE;AAChE,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,CAgB5D;AAED,uEAAuE;AACvE,wBAAgB,YAAY,CAAC,gBAAgB,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,aAAa,CAmBzF;AAED,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE9C"}
|
package/dist/config.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Config parsing, validation, and namespace resolution.
|
|
3
|
-
*
|
|
4
|
-
* Uses Zod for schema validation — all config errors surface at startup
|
|
5
|
-
* with clear messages instead of failing at runtime.
|
|
6
3
|
*/
|
|
4
|
+
import { createHash } from "node:crypto";
|
|
7
5
|
import { z } from "zod";
|
|
8
6
|
// ============================================================================
|
|
9
7
|
// Schema
|
|
@@ -18,7 +16,12 @@ const ConfigSchema = z.object({
|
|
|
18
16
|
serverUrl: z.string()
|
|
19
17
|
.min(1, "required")
|
|
20
18
|
.url("must be a valid URL"),
|
|
21
|
-
defaultNamespace: z.string()
|
|
19
|
+
defaultNamespace: z.string()
|
|
20
|
+
.min(1, "must not be empty")
|
|
21
|
+
.refine((ns) => !/[\r\n"]/.test(ns), {
|
|
22
|
+
message: 'defaultNamespace must not contain quotes (") or newline characters',
|
|
23
|
+
})
|
|
24
|
+
.default("default"),
|
|
22
25
|
autoRecall: z.boolean().default(true),
|
|
23
26
|
autoCapture: z.boolean().default(true),
|
|
24
27
|
maxRecallResults: z.number().min(1).max(20).default(5),
|
|
@@ -28,7 +31,6 @@ const ConfigSchema = z.object({
|
|
|
28
31
|
// ============================================================================
|
|
29
32
|
// Env Var Resolution
|
|
30
33
|
// ============================================================================
|
|
31
|
-
/** Replace ${ENV_VAR} placeholders with process.env values. */
|
|
32
34
|
function resolveEnvVar(value) {
|
|
33
35
|
return value.replace(/\$\{([^}]+)\}/g, (_, name) => {
|
|
34
36
|
const v = process.env[name];
|
|
@@ -37,10 +39,6 @@ function resolveEnvVar(value) {
|
|
|
37
39
|
return v;
|
|
38
40
|
});
|
|
39
41
|
}
|
|
40
|
-
/**
|
|
41
|
-
* Resolve ${ENV_VAR} placeholders in all string fields of a config object.
|
|
42
|
-
* Non-string fields are passed through unchanged.
|
|
43
|
-
*/
|
|
44
42
|
function resolveEnvVars(raw) {
|
|
45
43
|
const resolved = {};
|
|
46
44
|
for (const [key, value] of Object.entries(raw)) {
|
|
@@ -51,24 +49,11 @@ function resolveEnvVars(raw) {
|
|
|
51
49
|
// ============================================================================
|
|
52
50
|
// Config Parser
|
|
53
51
|
// ============================================================================
|
|
54
|
-
/**
|
|
55
|
-
* Parse and validate raw plugin config from openclaw.json.
|
|
56
|
-
*
|
|
57
|
-
* Resolves ${ENV_VAR} placeholders in string fields, then validates
|
|
58
|
-
* the full config against the Zod schema. Throws with clear field-level
|
|
59
|
-
* error messages on invalid config.
|
|
60
|
-
*
|
|
61
|
-
* @param raw - Raw config object from `api.pluginConfig`
|
|
62
|
-
* @returns Validated config with all defaults applied
|
|
63
|
-
* @throws {Error} If config is missing, or any field fails validation
|
|
64
|
-
*/
|
|
65
52
|
export function parseConfig(raw) {
|
|
66
53
|
if (!raw || typeof raw !== "object") {
|
|
67
54
|
throw new Error("memory-memwal: config is required");
|
|
68
55
|
}
|
|
69
|
-
// Resolve env vars before validation
|
|
70
56
|
const resolved = resolveEnvVars(raw);
|
|
71
|
-
// Validate with Zod — clear error messages per field
|
|
72
57
|
const result = ConfigSchema.safeParse(resolved);
|
|
73
58
|
if (!result.success) {
|
|
74
59
|
const issues = result.error.issues
|
|
@@ -78,29 +63,40 @@ export function parseConfig(raw) {
|
|
|
78
63
|
}
|
|
79
64
|
return result.data;
|
|
80
65
|
}
|
|
81
|
-
/**
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
66
|
+
/** Derive a collision-resistant namespace for an agent name. */
|
|
67
|
+
export function deriveAgentNamespace(rawName) {
|
|
68
|
+
const normalized = rawName.normalize("NFKC").trim();
|
|
69
|
+
const lower = normalized.toLowerCase();
|
|
70
|
+
// Domain 1: Safe standard names (1-64 ASCII alphanumeric/hyphen/underscore)
|
|
71
|
+
if (/^[a-zA-Z0-9_-]{1,64}$/.test(normalized)) {
|
|
72
|
+
return lower;
|
|
73
|
+
}
|
|
74
|
+
// Domain 2: Unsafe names (hash length >= 68 chars prevents domain collision)
|
|
75
|
+
const slug = lower
|
|
76
|
+
.replace(/[^a-z0-9_-]+/g, "-")
|
|
77
|
+
.replace(/^-+|-+$/g, "")
|
|
78
|
+
.slice(0, 32);
|
|
79
|
+
const hash = createHash("sha256").update(lower).digest("hex");
|
|
80
|
+
return slug && slug !== "main" ? `_h_${slug}_${hash}` : `_h_agent_${hash}`;
|
|
81
|
+
}
|
|
82
|
+
/** Resolve agent namespace and human-readable name from sessionKey. */
|
|
92
83
|
export function resolveAgent(defaultNamespace, sessionKey) {
|
|
93
84
|
if (!sessionKey)
|
|
94
85
|
return { namespace: defaultNamespace, agentName: "main" };
|
|
95
86
|
const match = sessionKey.match(/^agent:([^:]+):/);
|
|
96
|
-
const
|
|
97
|
-
if (!
|
|
87
|
+
const rawName = match?.[1];
|
|
88
|
+
if (!rawName || rawName === "main") {
|
|
89
|
+
return { namespace: defaultNamespace, agentName: "main" };
|
|
90
|
+
}
|
|
91
|
+
const trimmed = rawName.trim();
|
|
92
|
+
if (trimmed === "main" || trimmed.toLowerCase() === "main") {
|
|
98
93
|
return { namespace: defaultNamespace, agentName: "main" };
|
|
99
|
-
|
|
94
|
+
}
|
|
95
|
+
const namespace = deriveAgentNamespace(rawName);
|
|
96
|
+
const legacyNamespace = rawName !== namespace && /^[a-zA-Z0-9_-]{1,64}$/.test(trimmed) ? rawName : undefined;
|
|
97
|
+
const agentName = trimmed.normalize("NFKC").slice(0, 64) || namespace;
|
|
98
|
+
return { namespace, legacyNamespace, agentName };
|
|
100
99
|
}
|
|
101
|
-
/**
|
|
102
|
-
* Format key for safe logging (first 4 + last 4 chars).
|
|
103
|
-
*/
|
|
104
100
|
export function keyPreview(key) {
|
|
105
101
|
return key.length > 8 ? `${key.slice(0, 4)}...${key.slice(-4)}` : "****";
|
|
106
102
|
}
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,+EAA+E;AAC/E,SAAS;AACT,+EAA+E;AAE/E,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC;IAC5B,UAAU,EAAE,CAAC,CAAC,MAAM,EAAE;SACnB,GAAG,CAAC,CAAC,EAAE,UAAU,CAAC;SAClB,KAAK,CAAC,mBAAmB,EAAE,kDAAkD,CAAC;IACjF,SAAS,EAAE,CAAC,CAAC,MAAM,EAAE;SAClB,GAAG,CAAC,CAAC,EAAE,UAAU,CAAC;SAClB,KAAK,CAAC,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K,MAAM,IAAI,OAAO,CAAC,WAAW,EAAE,KAAK,MAAM,EAAE,CAAC;QAC3D,OAAO,EAAE,SAAS,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,CAAC;IAC5D,CAAC;IAED,MAAM,SAAS,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAChD,MAAM,eAAe,GACnB,OAAO,KAAK,SAAS,IAAI,uBAAuB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,SAAS,CAAC;IACvF,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,SAAS,CAAC;IACtE,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,SAAS,EAAE,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,GAAW;IACpC,OAAO,GAAG,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,MAAM,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC;AAC3E,CAAC"}
|
package/dist/hooks/recall.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Auto-recall hook — before_prompt_build.
|
|
3
|
-
*
|
|
4
|
-
* Searches Walrus Memory for memories relevant to the user's prompt and injects
|
|
5
|
-
* them into the LLM context. Also injects a namespace instruction so
|
|
6
|
-
* tools scope to the correct agent's memory.
|
|
7
3
|
*/
|
|
8
4
|
import type { MemWal } from "@mysten-incubation/memwal";
|
|
9
5
|
import type { PluginConfig } from "../types.js";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recall.d.ts","sourceRoot":"","sources":["../../src/hooks/recall.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"recall.d.ts","sourceRoot":"","sources":["../../src/hooks/recall.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAC;AAIxD,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,6DAA6D;AAC7D,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,GAAG,IAAI,CA8EvF"}
|
package/dist/hooks/recall.js
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Auto-recall hook — before_prompt_build.
|
|
3
|
-
*
|
|
4
|
-
* Searches Walrus Memory for memories relevant to the user's prompt and injects
|
|
5
|
-
* them into the LLM context. Also injects a namespace instruction so
|
|
6
|
-
* tools scope to the correct agent's memory.
|
|
7
3
|
*/
|
|
8
4
|
import { resolveAgent } from "../config.js";
|
|
9
5
|
import { looksLikeInjection } from "../capture.js";
|
|
@@ -12,28 +8,46 @@ import { MIN_PROMPT_LENGTH } from "../constants.js";
|
|
|
12
8
|
/** Register the before_prompt_build hook for auto-recall. */
|
|
13
9
|
export function registerRecallHook(api, client, config) {
|
|
14
10
|
api.on("before_prompt_build", async (event, ctx) => {
|
|
15
|
-
// Skip trivial prompts ("ok", "y") — not worth a server round-trip
|
|
16
11
|
if (!event.prompt || event.prompt.length < MIN_PROMPT_LENGTH)
|
|
17
12
|
return;
|
|
18
|
-
const { namespace, agentName } = resolveAgent(config.defaultNamespace, ctx?.sessionKey);
|
|
19
|
-
// The LLM doesn't know which agent it is. This instruction guides
|
|
20
|
-
// memory_search/memory_store tool calls to the correct namespace.
|
|
21
|
-
// Returned via appendSystemContext in ALL code paths (including errors)
|
|
22
|
-
// so tools still scope correctly even when recall itself fails.
|
|
13
|
+
const { namespace, legacyNamespace, agentName } = resolveAgent(config.defaultNamespace, ctx?.sessionKey);
|
|
23
14
|
const namespaceInstruction = `When using memory_search or memory_store tools, ` +
|
|
24
|
-
`pass namespace
|
|
15
|
+
`pass namespace=${JSON.stringify(namespace)} to scope operations to the current agent's memory.`;
|
|
25
16
|
try {
|
|
26
|
-
const
|
|
27
|
-
|
|
17
|
+
const recallPromises = [
|
|
18
|
+
client.recall(event.prompt, config.maxRecallResults, namespace),
|
|
19
|
+
];
|
|
20
|
+
if (legacyNamespace && legacyNamespace !== namespace) {
|
|
21
|
+
recallPromises.push(client.recall(event.prompt, config.maxRecallResults, legacyNamespace));
|
|
22
|
+
}
|
|
23
|
+
const resultsList = await Promise.allSettled(recallPromises);
|
|
24
|
+
const primaryResult = resultsList[0];
|
|
25
|
+
if (primaryResult?.status === "rejected") {
|
|
26
|
+
api.logger.warn(`memory-memwal: canonical recall failed: ${String(primaryResult.reason)}`);
|
|
27
|
+
}
|
|
28
|
+
const candidates = [];
|
|
29
|
+
const seen = new Set();
|
|
30
|
+
for (const res of resultsList) {
|
|
31
|
+
if (res.status === "fulfilled" && res.value?.results?.length) {
|
|
32
|
+
for (const item of res.value.results) {
|
|
33
|
+
const key = item.blob_id || item.text;
|
|
34
|
+
if (!seen.has(key)) {
|
|
35
|
+
seen.add(key);
|
|
36
|
+
candidates.push(item);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!candidates.length) {
|
|
28
42
|
return { appendSystemContext: namespaceInstruction };
|
|
29
43
|
}
|
|
30
|
-
|
|
31
|
-
// detection (drop memories containing prompt manipulation attempts)
|
|
32
|
-
const relevant = result.results.filter((r) => (1 - r.distance) >= config.minRelevance &&
|
|
44
|
+
const filtered = candidates.filter((r) => (1 - r.distance) >= config.minRelevance &&
|
|
33
45
|
!looksLikeInjection(r.text));
|
|
34
|
-
if (!
|
|
46
|
+
if (!filtered.length) {
|
|
35
47
|
return { appendSystemContext: namespaceInstruction };
|
|
36
48
|
}
|
|
49
|
+
filtered.sort((a, b) => a.distance - b.distance);
|
|
50
|
+
const relevant = filtered.slice(0, config.maxRecallResults);
|
|
37
51
|
api.logger.info(`memory-memwal: auto-recall injected ${relevant.length} memories ` +
|
|
38
52
|
`(agent: ${agentName}, namespace: ${namespace})`);
|
|
39
53
|
return {
|
package/dist/hooks/recall.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"recall.js","sourceRoot":"","sources":["../../src/hooks/recall.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"recall.js","sourceRoot":"","sources":["../../src/hooks/recall.ts"],"names":[],"mappings":"AAAA;;GAEG;AAGH,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,uBAAuB,EAAE,MAAM,cAAc,CAAC;AAEvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAEpD,6DAA6D;AAC7D,MAAM,UAAU,kBAAkB,CAAC,GAAQ,EAAE,MAAc,EAAE,MAAoB;IAC/E,GAAG,CAAC,EAAE,CAAC,qBAAqB,EAAE,KAAK,EAAE,KAAU,EAAE,GAAQ,EAAE,EAAE;QAC3D,IAAI,CAAC,KAAK,CAAC,MAAM,IAAI,KAAK,CAAC,MAAM,CAAC,MAAM,GAAG,iBAAiB;YAAE,OAAO;QAErE,MAAM,EAAE,SAAS,EAAE,eAAe,EAAE,SAAS,EAAE,GAAG,YAAY,CAAC,MAAM,CAAC,gBAAgB,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC;QACzG,MAAM,oBAAoB,GACxB,kDAAkD;YAClD,kBAAkB,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,qDAAqD,CAAC;QAEnG,IAAI,CAAC;YACH,MAAM,cAAc,GAAG;gBACrB,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,gBAAgB,EAAE,SAAS,CAAC;aAChE,CAAC;YAE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.5-dev.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "NemoClaw/OpenClaw memory plugin — encrypted, decentralized long-term memory via Walrus Memory",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"dependencies": {
|
|
37
37
|
"@sinclair/typebox": "0.34.48",
|
|
38
38
|
"zod": "^3.23.0",
|
|
39
|
-
"@mysten-incubation/memwal": "0.
|
|
39
|
+
"@mysten-incubation/memwal": "0.1.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/node": "^22.0.0",
|
|
@@ -50,6 +50,7 @@
|
|
|
50
50
|
"scripts": {
|
|
51
51
|
"build": "tsc",
|
|
52
52
|
"typecheck": "tsc --noEmit",
|
|
53
|
+
"test": "tsc && node --test \"test/**/*.test.mjs\"",
|
|
53
54
|
"clean": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\""
|
|
54
55
|
}
|
|
55
56
|
}
|