UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

60 lines (59 loc) • 2.72 kB
import { r as truncateUtf16Safe } from "./utf16-slice-CuDyEXgS.mjs"; //#region src/infra/exec-approval-text-sanitize.ts const EXEC_APPROVAL_INVISIBLE_CHAR_REGEX = /[\p{Cc}\p{Cf}\p{Cs}\p{Zl}\p{Zp}\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000\u115F\u1160\u3164\uFFA0]/gu; const EXEC_APPROVAL_MAX_INPUT = 262144; const EXEC_APPROVAL_MAX_OUTPUT = 16384; const EXEC_APPROVAL_TRUNCATION_MARKER = "…[truncated]"; const EXEC_APPROVAL_OVERSIZED_MARKER = "[exec approval command exceeds display size limit; full text suppressed]"; const EXEC_APPROVAL_WARNING_OVERSIZED_MARKER = "[exec approval warning exceeds display size limit; full text suppressed]"; function sanitizeExecApprovalDisplayTextInternal(e, t) { if (e.length > EXEC_APPROVAL_MAX_INPUT) return { text: t?.oversizedMarker ?? EXEC_APPROVAL_OVERSIZED_MARKER, truncated: false, oversized: true }; const n = e.replace(EXEC_APPROVAL_INVISIBLE_CHAR_REGEX, (e) => t?.preserveLineBreaks && e === "\n" ? "\n" : `\\u{${e.codePointAt(0)?.toString(16).toUpperCase() ?? "FFFD"}}`); if (n.length <= EXEC_APPROVAL_MAX_OUTPUT) return { text: n, truncated: false, oversized: false }; return { text: truncateUtf16Safe(n, EXEC_APPROVAL_MAX_OUTPUT) + EXEC_APPROVAL_TRUNCATION_MARKER, truncated: true, oversized: false }; } /** Sanitizes exec command text for approval UI without exposing status metadata. */ function sanitizeExecApprovalDisplayText(e) { return sanitizeExecApprovalDisplayTextInternal(e).text; } /** * Sanitizes exec command text for approval UI and reports whether size caps changed it. */ function sanitizeExecApprovalDisplayTextWithStatus(e) { return sanitizeExecApprovalDisplayTextInternal(e); } /** * Sanitizes warning prose for approval UI while preserving real line boundaries. */ function sanitizeExecApprovalWarningText(e) { return sanitizeExecApprovalWarningTextWithStatus(e).text; } /** Sanitizes warning prose and reports whether display bounds suppressed any content. */ function sanitizeExecApprovalWarningTextWithStatus(e) { return sanitizeExecApprovalDisplayTextInternal(e.replace(/\r\n?/g, "\n").replace(/[\u2028\u2029]/g, "\n"), { preserveLineBreaks: true, oversizedMarker: EXEC_APPROVAL_WARNING_OVERSIZED_MARKER }); } /** Checks the existing approval code-point cap without materializing every character. */ function exceedsApprovalTextLimit(e, t) { if (e.length <= t) return false; if (e.length > t * 2) return true; let n = t; for (const t of e) if (--n < 0) return true; return false; } //#endregion export { sanitizeExecApprovalWarningTextWithStatus as a, sanitizeExecApprovalWarningText as i, sanitizeExecApprovalDisplayText as n, sanitizeExecApprovalDisplayTextWithStatus as r, exceedsApprovalTextLimit as t };