Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat(openclaw-plugin): support multi-agent memory isolation via hook context agentId (#637)
yingriyanlongafc15b5cf15a
diff --git a/examples/openclaw-memory-plugin/client.ts b/examples/openclaw-memory-plugin/client.tsindex 564899991..c48b2b69a 100644--- a/examples/openclaw-memory-plugin/client.ts+++ b/examples/openclaw-memory-plugin/client.ts@@ -47,16 +47,36 @@ export function isMemoryUri(uri: string): boolean {}export class OpenVikingClient {- private readonly resolvedSpaceByScope: Partial<Record<ScopeName, string>> = {};+ private resolvedSpaceByScope: Partial<Record<ScopeName, string>> = {};private runtimeIdentity: RuntimeIdentity | null = null;constructor(private readonly baseUrl: string,private readonly apiKey: string,- private readonly agentId: string,+ private agentId: string,private readonly timeoutMs: number,) {}+ /**+ * Dynamically switch the agent identity for multi-agent memory isolation.+ * When a shared client serves multiple agents (e.g. in OpenClaw multi-agent+ * gateway), call this before each agent's recall/capture to route memories+ * to the correct agent_space = md5(user_id + agent_id)[:12].+ * Clears cached space resolution so the next request re-derives agent_space.+ */+ setAgentId(newAgentId: string): void {+ if (newAgentId && newAgentId !== this.agentId) {+ this.agentId = newAgentId;+ // Clear cached identity and spaces — they depend on agentId+ this.runtimeIdentity = null;+ this.resolvedSpaceByScope = {};+ }+ }++ getAgentId(): string {+ return this.agentId;+ }+private async request<T>(path: string, init: RequestInit = {}): Promise<T> {const controller = new AbortController();const timer = setTimeout(() => controller.abort(), this.timeoutMs);diff --git a/examples/openclaw-memory-plugin/index.ts b/examples/openclaw-memory-plugin/index.tsindex 4a2a4499e..06551a4a9 100644--- a/examples/openclaw-memory-plugin/index.ts+++ b/examples/openclaw-memory-plugin/index.ts@@ -354,7 +354,16 @@ const memoryPlugin = {);if (cfg.autoRecall || cfg.ingestReplyAssist) {- api.on("before_agent_start", async (event: { messages?: unknown[]; prompt: string }) => {+ api.on("before_agent_start", async (event: { messages?: unknown[]; prompt: string }, ctx?: { agentId?: string }) => {+ // Dynamically switch agent identity for multi-agent memory isolation.+ // In multi-agent gateway deployments, the hook context carries the current+ // agent's ID so we route memory operations to the correct agent_space.+ const hookAgentId = ctx?.agentId;+ if (hookAgentId) {+ const client = await getClient();+ client.setAgentId(hookAgentId);+ api.logger.info?.(`memory-openviking: switched to agentId=${hookAgentId} for recall`);+ }const queryText = extractLatestUserText(event.messages) || event.prompt.trim();if (!queryText) {return;@@ -474,7 +483,14 @@ const memoryPlugin = {if (cfg.autoCapture) {let lastProcessedMsgCount = 0;- api.on("agent_end", async (event: { success?: boolean; messages?: unknown[] }) => {+ api.on("agent_end", async (event: { success?: boolean; messages?: unknown[] }, ctx?: { agentId?: string }) => {+ // Dynamically switch agent identity for multi-agent memory isolation+ const hookAgentId = ctx?.agentId;+ if (hookAgentId) {+ const client = await getClient();+ client.setAgentId(hookAgentId);+ api.logger.info?.(`memory-openviking: switched to agentId=${hookAgentId} for capture`);+ }if (!event.success || !event.messages || event.messages.length === 0) {api.logger.info(`memory-openviking: auto-capture skipped (success=${String(event.success)}, messages=${event.messages?.length ?? 0})`,