feat: per-agent memory isolation for openclaw-mem0 plugin
Co-authored-by: deshraj <2945708+deshraj@users.noreply.github.com>
This commit is contained in:
co-authored by
deshraj
parent
ce05224bad
commit
eac9b473a9
+29
-7
@@ -616,6 +616,22 @@ const memoryPlugin = {
|
|||||||
// Track current session ID for tool-level session scoping
|
// Track current session ID for tool-level session scoping
|
||||||
let currentSessionId: string | undefined;
|
let currentSessionId: string | undefined;
|
||||||
|
|
||||||
|
// Track current agent ID for per-agent memory isolation
|
||||||
|
let currentAgentId: string | undefined;
|
||||||
|
|
||||||
|
// Helper: extract agentId from session key (pattern: agent:<agentId>:...)
|
||||||
|
// Returns undefined for the "main" agent to preserve default behavior.
|
||||||
|
function extractAgentId(sessionKey: string): string | undefined {
|
||||||
|
const match = sessionKey.match(/^agent:([^:]+):/);
|
||||||
|
return match && match[1] !== "main" ? match[1] : undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Helper: get effective userId for Mem0 operations.
|
||||||
|
// Precedence: explicit override > current agentId (non-main) > configured userId.
|
||||||
|
function effectiveUserId(userIdOverride?: string): string {
|
||||||
|
return userIdOverride || currentAgentId || cfg.userId;
|
||||||
|
}
|
||||||
|
|
||||||
api.logger.info(
|
api.logger.info(
|
||||||
`openclaw-mem0: registered (mode: ${cfg.mode}, user: ${cfg.userId}, graph: ${cfg.enableGraph}, autoRecall: ${cfg.autoRecall}, autoCapture: ${cfg.autoCapture})`,
|
`openclaw-mem0: registered (mode: ${cfg.mode}, user: ${cfg.userId}, graph: ${cfg.enableGraph}, autoRecall: ${cfg.autoRecall}, autoCapture: ${cfg.autoCapture})`,
|
||||||
);
|
);
|
||||||
@@ -623,7 +639,7 @@ const memoryPlugin = {
|
|||||||
// Helper: build add options
|
// Helper: build add options
|
||||||
function buildAddOptions(userIdOverride?: string, runId?: string): AddOptions {
|
function buildAddOptions(userIdOverride?: string, runId?: string): AddOptions {
|
||||||
const opts: AddOptions = {
|
const opts: AddOptions = {
|
||||||
user_id: userIdOverride || cfg.userId,
|
user_id: effectiveUserId(userIdOverride),
|
||||||
source: "OPENCLAW",
|
source: "OPENCLAW",
|
||||||
};
|
};
|
||||||
if (runId) opts.run_id = runId;
|
if (runId) opts.run_id = runId;
|
||||||
@@ -643,7 +659,7 @@ const memoryPlugin = {
|
|||||||
runId?: string,
|
runId?: string,
|
||||||
): SearchOptions {
|
): SearchOptions {
|
||||||
const opts: SearchOptions = {
|
const opts: SearchOptions = {
|
||||||
user_id: userIdOverride || cfg.userId,
|
user_id: effectiveUserId(userIdOverride),
|
||||||
top_k: limit ?? cfg.topK,
|
top_k: limit ?? cfg.topK,
|
||||||
limit: limit ?? cfg.topK,
|
limit: limit ?? cfg.topK,
|
||||||
threshold: cfg.searchThreshold,
|
threshold: cfg.searchThreshold,
|
||||||
@@ -935,7 +951,7 @@ const memoryPlugin = {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
let memories: MemoryItem[] = [];
|
let memories: MemoryItem[] = [];
|
||||||
const uid = userId || cfg.userId;
|
const uid = effectiveUserId(userId);
|
||||||
|
|
||||||
if (scope === "session") {
|
if (scope === "session") {
|
||||||
if (currentSessionId) {
|
if (currentSessionId) {
|
||||||
@@ -1236,9 +1252,12 @@ const memoryPlugin = {
|
|||||||
api.on("before_agent_start", async (event, ctx) => {
|
api.on("before_agent_start", async (event, ctx) => {
|
||||||
if (!event.prompt || event.prompt.length < 5) return;
|
if (!event.prompt || event.prompt.length < 5) return;
|
||||||
|
|
||||||
// Track session ID
|
// Track session ID and agent ID
|
||||||
const sessionId = (ctx as any)?.sessionKey ?? undefined;
|
const sessionId = (ctx as any)?.sessionKey ?? undefined;
|
||||||
if (sessionId) currentSessionId = sessionId;
|
if (sessionId) {
|
||||||
|
currentSessionId = sessionId;
|
||||||
|
currentAgentId = extractAgentId(sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Search long-term memories (user-scoped)
|
// Search long-term memories (user-scoped)
|
||||||
@@ -1303,9 +1322,12 @@ const memoryPlugin = {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Track session ID
|
// Track session ID and agent ID
|
||||||
const sessionId = (ctx as any)?.sessionKey ?? undefined;
|
const sessionId = (ctx as any)?.sessionKey ?? undefined;
|
||||||
if (sessionId) currentSessionId = sessionId;
|
if (sessionId) {
|
||||||
|
currentSessionId = sessionId;
|
||||||
|
currentAgentId = extractAgentId(sessionId);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Extract messages, limiting to last 10
|
// Extract messages, limiting to last 10
|
||||||
|
|||||||
Reference in New Issue
Block a user