mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-01 08:00:22 +00:00
32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
import { stripAssistantInternalScaffolding } from "openclaw/plugin-sdk/text-runtime";
|
|
|
|
/**
|
|
* Patterns that indicate assistant-internal metadata leaked into text.
|
|
* These must never reach a user-facing channel.
|
|
*/
|
|
const INTERNAL_SEPARATOR_RE = /(?:#\+){2,}#?/g;
|
|
const ASSISTANT_ROLE_MARKER_RE = /\bassistant\s+to\s*=\s*\w+/gi;
|
|
const ROLE_TURN_MARKER_RE = /\b(?:user|system|assistant)\s*:\s*$/gm;
|
|
|
|
/**
|
|
* Strip all assistant-internal scaffolding from outbound text before delivery.
|
|
* Applies reasoning/thinking tag removal, memory tag removal, and
|
|
* model-specific internal separator stripping.
|
|
*/
|
|
export function sanitizeOutboundText(text: string): string {
|
|
if (!text) {
|
|
return text;
|
|
}
|
|
|
|
let cleaned = stripAssistantInternalScaffolding(text);
|
|
|
|
cleaned = cleaned.replace(INTERNAL_SEPARATOR_RE, "");
|
|
cleaned = cleaned.replace(ASSISTANT_ROLE_MARKER_RE, "");
|
|
cleaned = cleaned.replace(ROLE_TURN_MARKER_RE, "");
|
|
|
|
// Collapse excessive blank lines left after stripping.
|
|
cleaned = cleaned.replace(/\n{3,}/g, "\n\n").trim();
|
|
|
|
return cleaned;
|
|
}
|