"""Content delimiter wrapper for safe LLM prompt integration.""" from typing import Union import json # OpenClaw-style delimiters START_DELIMITER = "<<>>" END_DELIMITER = "<<>>" def wrap_untrusted_content(content: Union[str, dict, list]) -> str: """Wrap external content with safety delimiters. This creates a semantic boundary between system instructions and untrusted external data, helping prevent prompt injection. Args: content: Raw content (string, dict, or list) Returns: Delimited string for LLM consumption """ if isinstance(content, (dict, list)): text = json.dumps(content, indent=2, ensure_ascii=False) else: text = str(content) return f"{START_DELIMITER}\n{text}\n{END_DELIMITER}" def unwrap_trusted_content(delimited: str) -> str: """Extract content from delimiters (for testing/debugging). Args: delimited: Content wrapped in delimiters Returns: Raw content string """ lines = delimited.split('\n') if lines[0] == START_DELIMITER and lines[-1] == END_DELIMITER: return '\n'.join(lines[1:-1]) return delimited