mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-21 12:04:59 +00:00
refactor: reuse canonical inbound metadata stripper
This commit is contained in:
@@ -1,8 +1,5 @@
|
||||
import {
|
||||
stripEnvelope,
|
||||
stripInboundMetadataBlocks,
|
||||
stripMessageIdHints,
|
||||
} from "../shared/chat-envelope.js";
|
||||
import { stripInboundMetadata } from "../auto-reply/reply/strip-inbound-meta.js";
|
||||
import { stripEnvelope, stripMessageIdHints } from "../shared/chat-envelope.js";
|
||||
|
||||
export { stripEnvelope };
|
||||
|
||||
@@ -16,7 +13,7 @@ function stripEnvelopeFromContent(content: unknown[]): { content: unknown[]; cha
|
||||
if (entry.type !== "text" || typeof entry.text !== "string") {
|
||||
return item;
|
||||
}
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadataBlocks(entry.text)));
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadata(entry.text)));
|
||||
if (stripped === entry.text) {
|
||||
return item;
|
||||
}
|
||||
@@ -43,7 +40,7 @@ export function stripEnvelopeFromMessage(message: unknown): unknown {
|
||||
const next: Record<string, unknown> = { ...entry };
|
||||
|
||||
if (typeof entry.content === "string") {
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadataBlocks(entry.content)));
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadata(entry.content)));
|
||||
if (stripped !== entry.content) {
|
||||
next.content = stripped;
|
||||
changed = true;
|
||||
@@ -55,7 +52,7 @@ export function stripEnvelopeFromMessage(message: unknown): unknown {
|
||||
changed = true;
|
||||
}
|
||||
} else if (typeof entry.text === "string") {
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadataBlocks(entry.text)));
|
||||
const stripped = stripMessageIdHints(stripEnvelope(stripInboundMetadata(entry.text)));
|
||||
if (stripped !== entry.text) {
|
||||
next.text = stripped;
|
||||
changed = true;
|
||||
|
||||
@@ -16,20 +16,6 @@ const ENVELOPE_CHANNELS = [
|
||||
];
|
||||
|
||||
const MESSAGE_ID_LINE = /^\s*\[message_id:\s*[^\]]+\]\s*$/i;
|
||||
const INBOUND_METADATA_HEADERS = [
|
||||
"Conversation info (untrusted metadata):",
|
||||
"Sender (untrusted metadata):",
|
||||
"Thread starter (untrusted, for context):",
|
||||
"Replied message (untrusted, for context):",
|
||||
"Forwarded message context (untrusted metadata):",
|
||||
"Chat history since last reply (untrusted, for context):",
|
||||
];
|
||||
const REGEX_ESCAPE_RE = /[.*+?^${}()|[\]\\-]/g;
|
||||
const INBOUND_METADATA_PREFIX_RE = new RegExp(
|
||||
"^\\s*(?:" +
|
||||
INBOUND_METADATA_HEADERS.map((header) => header.replace(REGEX_ESCAPE_RE, "\\$&")).join("|") +
|
||||
")\\r?\\n```json\\r?\\n[\\s\\S]*?\\r?\\n```(?:\\r?\\n)*",
|
||||
);
|
||||
|
||||
function looksLikeEnvelopeHeader(header: string): boolean {
|
||||
if (/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}Z\b/.test(header)) {
|
||||
@@ -61,15 +47,3 @@ export function stripMessageIdHints(text: string): string {
|
||||
const filtered = lines.filter((line) => !MESSAGE_ID_LINE.test(line));
|
||||
return filtered.length === lines.length ? text : filtered.join("\n");
|
||||
}
|
||||
|
||||
export function stripInboundMetadataBlocks(text: string): string {
|
||||
let remaining = text;
|
||||
for (;;) {
|
||||
const match = INBOUND_METADATA_PREFIX_RE.exec(remaining);
|
||||
if (!match) {
|
||||
break;
|
||||
}
|
||||
remaining = remaining.slice(match[0].length).replace(/^\r?\n+/, "");
|
||||
}
|
||||
return remaining.trim();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { formatRawAssistantErrorForUi } from "../agents/pi-embedded-helpers.js";
|
||||
import { stripInboundMetadataBlocks } from "../shared/chat-envelope.js";
|
||||
import { stripInboundMetadata } from "../auto-reply/reply/strip-inbound-meta.js";
|
||||
import { stripAnsi } from "../terminal/ansi.js";
|
||||
import { formatTokenCount } from "../utils/usage-format.js";
|
||||
|
||||
@@ -275,7 +275,7 @@ export function extractTextFromMessage(
|
||||
const text = extractTextBlocks(record.content, opts);
|
||||
if (text) {
|
||||
if (record.role === "user") {
|
||||
return stripInboundMetadataBlocks(text);
|
||||
return stripInboundMetadata(text);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user