MCPcopy Create free account
hub / github.com/MemTensor/MemOS / stripMemoryInjection

Function stripMemoryInjection

packages/memos-core/src/capture/index.ts:223–327  ·  view source on GitHub ↗

* Strip memory-system injections that get prepended to user messages: * - ... * - === MemOS LONG-TERM MEMORY ... ===\n...MANDATORY... * - [MemOS Auto-Recall] Found N relevant memories:... * - ## Memory system\n\nNo memories were automatically recalled...

(text: string)

Source from the content-addressed store, hash-verified

221 * - ## Memory system\n\nNo memories were automatically recalled...
222 */
223function stripMemoryInjection(text: string): string {
224 let cleaned = text;
225
226 // <memory_context>...</memory_context>
227 const mcStart = cleaned.indexOf("<memory_context>");
228 if (mcStart !== -1) {
229 const mcEnd = cleaned.indexOf("</memory_context>");
230 if (mcEnd !== -1) {
231 cleaned = cleaned.slice(0, mcStart) + cleaned.slice(mcEnd + "</memory_context>".length);
232 } else {
233 cleaned = cleaned.slice(0, mcStart);
234 }
235 cleaned = cleaned.trim();
236 }
237
238 // === MemOS LONG-TERM MEMORY (retrieved from past conversations) ===\n...\nMANDATORY...
239 cleaned = cleaned.replace(
240 /=== MemOS LONG-TERM MEMORY[\s\S]*?(?:MANDATORY[^\n]*\n?|(?=\n{2,}))/gi,
241 "",
242 ).trim();
243
244 // [MemOS Auto-Recall] Found N relevant memories:\n...
245 cleaned = cleaned.replace(
246 /\[MemOS Auto-Recall\][^\n]*\n(?:(?:\d+\.\s+\[(?:USER|ASSISTANT)[^\n]*\n?)*)/gi,
247 "",
248 ).trim();
249
250 // ## Memory system\n\nNo memories were automatically recalled...
251 cleaned = cleaned.replace(
252 /## Memory system\n+No memories were automatically recalled[^\n]*(?:\n[^\n]*memory_search[^\n]*)*/gi,
253 "",
254 ).trim();
255
256 // ## Memory system — ACTION REQUIRED\n...
257 cleaned = cleaned.replace(
258 /## Memory system — ACTION REQUIRED[\s\S]*?(?=\n\[(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+\d{4}-\d{2}-\d{2}|\n\[Subagent)/,
259 "",
260 ).trim();
261
262 // You are running a boot check. Follow BOOT.md instructions exactly.\n...
263 cleaned = cleaned.replace(
264 /^You are running a boot check[\s\S]*?(?=\n\[(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+\d{4}-\d{2}-\d{2}|\n\[Subagent)/m,
265 "",
266 ).trim();
267
268 // Standalone NO_REPLY / HEARTBEAT_OK that leaked into user messages
269 cleaned = cleaned.replace(/^\s*(?:NO_REPLY|HEARTBEAT_OK|HEARTBEAT_CHECK)\s*$/gm, "").trim();
270
271 // Old format: ## Retrieved memories from past conversations\n\nCRITICAL INSTRUCTION:...
272 const recallIdx = cleaned.indexOf("## Retrieved memories from past conversations");
273 if (recallIdx !== -1) {
274 const before = cleaned.slice(0, recallIdx);
275 const after = cleaned.slice(recallIdx);
276 const tsMatch = after.match(/\n\[(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)\s+\d{4}-\d{2}-\d{2}/);
277 if (tsMatch && tsMatch.index != null) {
278 cleaned = (before + after.slice(tsMatch.index)).trim();
279 } else {
280 cleaned = before.trim();

Callers 1

stripInboundMetadataFunction · 0.70

Calls 1

matchMethod · 0.80

Tested by

no test coverage detected