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
fix(openclaw-plugin): sanitize mapped OpenViking session ids (#998)
Qin Haojie7b2b9100821d
diff --git a/examples/openclaw-plugin/context-engine.ts b/examples/openclaw-plugin/context-engine.tsindex 738dd2281..e9d0e7a8f 100644--- a/examples/openclaw-plugin/context-engine.ts+++ b/examples/openclaw-plugin/context-engine.ts@@ -1,3 +1,5 @@+import { createHash } from "node:crypto";+import type { OpenVikingClient } from "./client.js";import type { MemoryOpenVikingConfig } from "./config.js";import {@@ -73,7 +75,7 @@ type ContextEngine = {};export type ContextEngineWithSessionMapping = ContextEngine & {- /** Return the OV session ID for an OpenClaw sessionKey (identity: sessionKey IS the OV session ID). */+ /** Return the OV session ID for an OpenClaw sessionKey using a stable cross-platform-safe mapping. */getOVSessionForKey: (sessionKey: string) => string;/** Ensure an OV session exists on the server for the given OpenClaw sessionKey (auto-created by getSession if absent). */resolveOVSession: (sessionKey: string) => Promise<string>;@@ -134,6 +136,30 @@ function warnOrInfo(logger: Logger, message: string): void {logger.info(message);}+function md5Short(input: string): string {+ return createHash("md5").update(input).digest("hex").slice(0, 12);+}++const SAFE_SESSION_KEY_RE = /^[A-Za-z0-9_-]+$/;++export function mapSessionKeyToOVSessionId(sessionKey: string): string {+ const normalized = sessionKey.trim();+ if (!normalized) {+ return "openclaw_session";+ }+ if (SAFE_SESSION_KEY_RE.test(normalized)) {+ return normalized;+ }++ const readable = normalized+ .replace(/[^A-Za-z0-9_-]+/g, "-")+ .replace(/-+/g, "-")+ .replace(/^-|-$/g, "")+ .slice(0, 48);+ const digest = md5Short(normalized);+ return readable ? `openclaw_${readable}_${digest}` : `openclaw_session_${digest}`;+}+export function createMemoryOpenVikingContextEngine(params: {id: string;name: string;@@ -157,11 +183,12 @@ export function createMemoryOpenVikingContextEngine(params: {try {const client = await getClient();const agentId = resolveAgentId(sessionKey);- const commitResult = await client.commitSession(sessionKey, { wait: true, agentId });+ const ovSessionId = mapSessionKeyToOVSessionId(sessionKey);+ const commitResult = await client.commitSession(ovSessionId, { wait: true, agentId });logger.info(- `openviking: committed OV session for sessionKey=${sessionKey}, archived=${commitResult.archived ?? false}, memories=${commitResult.memories_extracted ?? 0}, task_id=${commitResult.task_id ?? "none"}`,+ `openviking: committed OV session for sessionKey=${sessionKey}, ovSessionId=${ovSessionId}, archived=${commitResult.archived ?? false}, memories=${commitResult.memories_extracted ?? 0}, task_id=${commitResult.task_id ?? "none"}`,);- await client.deleteSession(sessionKey, agentId).catch(() => {});+ await client.deleteSession(ovSessionId, agentId).catch(() => {});} catch (err) {warnOrInfo(logger, `openviking: commit failed for sessionKey=${sessionKey}: ${String(err)}`);}@@ -184,10 +211,10 @@ export function createMemoryOpenVikingContextEngine(params: {// --- session-mapping extensions ---- getOVSessionForKey: (sessionKey: string) => sessionKey,+ getOVSessionForKey: (sessionKey: string) => mapSessionKeyToOVSessionId(sessionKey),async resolveOVSession(sessionKey: string): Promise<string> {- return sessionKey;+ return mapSessionKeyToOVSessionId(sessionKey);},commitOVSession: doCommitOVSession,@@ -252,7 +279,9 @@ export function createMemoryOpenVikingContextEngine(params: {}const client = await getClient();- const OVSessionId = sessionKey ?? afterTurnParams.sessionId;+ const OVSessionId = sessionKey+ ? mapSessionKeyToOVSessionId(sessionKey)+ : afterTurnParams.sessionId;await client.addSessionMessage(OVSessionId, "user", decision.normalizedText, agentId);const commitResult = await client.commitSession(OVSessionId, { wait: true, agentId });logger.info(diff --git a/examples/openclaw-plugin/index.ts b/examples/openclaw-plugin/index.tsindex 401102b34..7374d1902 100644--- a/examples/openclaw-plugin/index.ts+++ b/examples/openclaw-plugin/index.ts@@ -239,7 +239,7 @@ const contextEnginePlugin = {text: Type.String({ description: "Information to store as memory source text" }),role: Type.Optional(Type.String({ description: "Session role, default user" })),sessionId: Type.Optional(Type.String({ description: "Existing OpenViking session ID" })),- sessionKey: Type.Optional(Type.String({ description: "OpenClaw sessionKey — uses the persistent 1:1 mapped OV session" })),+ sessionKey: Type.Optional(Type.String({ description: "OpenClaw sessionKey — uses the persistent mapped OV session" })),}),async execute(_toolCallId: string, params: Record<string, unknown>) {const { text } = params as { text: string };@@ -589,7 +589,7 @@ const contextEnginePlugin = {return contextEngineRef;});api.logger.info(- "openviking: registered context-engine (before_prompt_build=auto-recall, afterTurn=auto-capture, sessionKey=1:1 mapping)",+ "openviking: registered context-engine (before_prompt_build=auto-recall, afterTurn=auto-capture, sessionKey=stable mapped session)",);} else {api.logger.warn(