diff --git a/apps/memos-local-openclaw/index.ts b/apps/memos-local-openclaw/index.ts index 769b8b55a..c5e0226ea 100644 --- a/apps/memos-local-openclaw/index.ts +++ b/apps/memos-local-openclaw/index.ts @@ -53,6 +53,45 @@ function deduplicateHits(hits: T[]): T[] { return kept; } +const NEW_SESSION_PROMPT_RE = /A new session was started via \/new or \/reset\./i; +const INTERNAL_CONTEXT_RE = /OpenClaw runtime context \(internal\):[\s\S]*/i; +const CONTINUE_PROMPT_RE = /^Continue where you left off\.[\s\S]*/i; + +function normalizeAutoRecallQuery(rawPrompt: string): string { + let query = rawPrompt.trim(); + + const senderTag = "Sender (untrusted metadata):"; + const senderPos = query.indexOf(senderTag); + if (senderPos !== -1) { + const afterSender = query.slice(senderPos); + const fenceStart = afterSender.indexOf("```json"); + const fenceEnd = fenceStart >= 0 ? afterSender.indexOf("```\n", fenceStart + 7) : -1; + if (fenceEnd > 0) { + query = afterSender.slice(fenceEnd + 4).replace(/^\s*\n/, "").trim(); + } else { + const firstDblNl = afterSender.indexOf("\n\n"); + if (firstDblNl > 0) { + query = afterSender.slice(firstDblNl + 2).trim(); + } + } + } + + query = stripInboundMetadata(query); + query = query.replace(/<[^>]+>/g, "").trim(); + + if (NEW_SESSION_PROMPT_RE.test(query)) { + query = query.replace(NEW_SESSION_PROMPT_RE, "").trim(); + query = query.replace(/^(Execute|Run) your Session Startup sequence[^\n]*\n?/im, "").trim(); + query = query.replace(/^Current time:[^\n]*(\n|$)/im, "").trim(); + } + + query = query.replace(INTERNAL_CONTEXT_RE, "").trim(); + query = query.replace(CONTINUE_PROMPT_RE, "").trim(); + + return query; +} + + const pluginConfigSchema = { type: "object" as const, additionalProperties: true, @@ -1790,24 +1829,7 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`, const rawPrompt = event.prompt; ctx.log.debug(`auto-recall: rawPrompt="${rawPrompt.slice(0, 300)}"`); - let query = rawPrompt; - const senderTag = "Sender (untrusted metadata):"; - const senderPos = rawPrompt.indexOf(senderTag); - if (senderPos !== -1) { - const afterSender = rawPrompt.slice(senderPos); - const fenceStart = afterSender.indexOf("```json"); - const fenceEnd = fenceStart >= 0 ? afterSender.indexOf("```\n", fenceStart + 7) : -1; - if (fenceEnd > 0) { - query = afterSender.slice(fenceEnd + 4).replace(/^\s*\n/, "").trim(); - } else { - const firstDblNl = afterSender.indexOf("\n\n"); - if (firstDblNl > 0) { - query = afterSender.slice(firstDblNl + 2).trim(); - } - } - } - query = stripInboundMetadata(query); - query = query.replace(/<[^>]+>/g, "").trim(); + const query = normalizeAutoRecallQuery(rawPrompt); recallQuery = query; if (query.length < 2) {