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

Function stripInboundMetadata

packages/memos-core/src/capture/index.ts:139–186  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

137 * Also strips the envelope timestamp prefix like "[Tue 2026-03-03 21:58 GMT+8] "
138 */
139export function stripInboundMetadata(text: string): string {
140 let cleaned = stripMemoryInjection(text);
141 cleaned = stripEnvelopePrefix(cleaned);
142
143 // Strip OpenClaw envelope tags: [message_id: ...], [[reply_to_current]], etc.
144 cleaned = cleaned.replace(/\[message_id:\s*[a-f0-9-]+\]/gi, "");
145 cleaned = cleaned.replace(/\[\[reply_to_current\]\]/gi, "");
146
147 if (!SENTINEL_FAST_RE.test(cleaned)) {
148 return stripEnvelopePrefix(cleaned).trim();
149 }
150
151 const lines = cleaned.split("\n");
152 const result: string[] = [];
153 let inMetaBlock = false;
154 let inFencedJson = false;
155
156 for (let i = 0; i < lines.length; i++) {
157 const line = lines[i];
158 const trimmed = line.trim();
159
160 if (!inMetaBlock && INBOUND_META_SENTINELS.some((s) => s === trimmed)) {
161 if (lines[i + 1]?.trim() === "```json") {
162 inMetaBlock = true;
163 inFencedJson = false;
164 continue;
165 }
166 continue;
167 }
168
169 if (inMetaBlock) {
170 if (!inFencedJson && trimmed === "```json") {
171 inFencedJson = true;
172 continue;
173 }
174 if (inFencedJson && trimmed === "```") {
175 inMetaBlock = false;
176 inFencedJson = false;
177 continue;
178 }
179 continue;
180 }
181
182 result.push(line);
183 }
184
185 return stripEnvelopePrefix(result.join("\n")).trim();
186}
187
188/** Strip <think…>…</think> blocks emitted by DeepSeek-style reasoning models. */
189const THINKING_TAG_RE = /<think[\s>][\s\S]*?<\/think>\s*/gi;

Callers 7

serveMemoriesMethod · 0.90
serveTaskDetailMethod · 0.90
serveMemoryDetailMethod · 0.90
handleMigrateScanMethod · 0.90
runMigrationMethod · 0.90
importAgentMethod · 0.90
captureMessagesFunction · 0.70

Calls 3

stripMemoryInjectionFunction · 0.70
stripEnvelopePrefixFunction · 0.70
joinMethod · 0.45

Tested by

no test coverage detected