File size: 855 Bytes
9a0a30d f470ae3 9a0a30d f470ae3 9a0a30d f470ae3 9a0a30d f470ae3 9a0a30d f470ae3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# utils.py
import re
from typing import Optional
def safe_truncate(text: str, max_chars: int = 12000) -> str:
if not text:
return ""
return text if len(text) <= max_chars else text[:max_chars] + "\n\n[[TRUNCATED]]"
def sanitize_text(text: Optional[str]) -> str:
if not text:
return ""
t = re.sub(r"<[^>]+>", " ", text)
t = re.sub(r"\s+", " ", t).strip()
return t
def _extract_json_from_text(text: str) -> Optional[str]:
if not text:
return None
start = None
depth = 0
for i, ch in enumerate(text):
if ch == "{":
if start is None:
start = i
depth += 1
elif ch == "}":
if depth > 0:
depth -= 1
if depth == 0 and start is not None:
return text[start:i+1]
return None
|