const CONTROL_CHARS_EXCEPT_COMMON_WHITESPACE = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/g; const PROTOCOL_MARKUP_TAG = /<\/?pi_tool_(?:call_history|call|result)\b(?:[^<>\r\n]*>|[^<>\r\n]*)/gi; const INTERNAL_HISTORY_MARKER = /(?:<\/?previous_pi_tool_call\b(?:[^<>\r\n]*>|[^<>\r\n]*)|\[\/?previous_pi_tool_call\])/gi; export function truncateText(value: string, maxChars: number): string { if (maxChars === Number.POSITIVE_INFINITY) return value; const limit = Number.isFinite(maxChars) ? Math.max(0, Math.floor(maxChars)) : 0; if (value.length <= limit) return value; const notice = `[truncated ${value.length - limit} chars by xtalpi-pi-tools]`; return limit === 0 ? notice : `${value.slice(0, limit)}\n\n${notice}`; } function protocolMarkerLabel(tag: string): string { const match = tag.match(/<\/?(pi_tool_(?:call_history|call|result))\b/i); const name = (match?.[1] || "pi_tool_protocol").toLowerCase(); const direction = tag.startsWith("