(text: string)
| 137 | * Also strips the envelope timestamp prefix like "[Tue 2026-03-03 21:58 GMT+8] " |
| 138 | */ |
| 139 | export 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. */ |
| 189 | const THINKING_TAG_RE = /<think[\s>][\s\S]*?<\/think>\s*/gi; |
no test coverage detected