Telegram: Plain-Text-Fallback bei Parse-Fehler, tag-sichere Kürzung auf 4096. Matrix: bytebewusste Kürzung. Downtime: lesender Abgleich nach dem POST, generische Fehlermeldung im Chat. Agent: Platzhalter für nicht ausgeführte Tool-Calls, Text im letzten Schritt erzwingen, Zeitreserve, Kompaktierung, Limit pro Antwort. CLAUDE.md: Fallbacks für Phase 6d. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
30 lines
872 B
Python
30 lines
872 B
Python
import re
|
|
|
|
from .constants import DATA_CLOSE, DATA_OPEN, TOOL_RESULT_MAX
|
|
|
|
CONTROL_RE = re.compile(r"[\x00-\x08\x0b-\x1f\x7f-\x9f]")
|
|
DELIMITER_RE = re.compile(r"<\s*/?\s*daten\s*>", re.IGNORECASE)
|
|
WHITESPACE_RE = re.compile(r"\s+")
|
|
TRUNCATION_SUFFIX = " …[gekürzt]"
|
|
|
|
|
|
def sanitize(text: object, limit: int = TOOL_RESULT_MAX, single_line: bool = False) -> str:
|
|
text = CONTROL_RE.sub("", str(text))
|
|
previous = None
|
|
while previous != text:
|
|
previous = text
|
|
text = DELIMITER_RE.sub("", text)
|
|
if single_line:
|
|
text = WHITESPACE_RE.sub(" ", text)
|
|
text = text.strip()
|
|
if len(text) <= limit:
|
|
return text
|
|
cut = limit - len(TRUNCATION_SUFFIX)
|
|
if cut <= 0:
|
|
return text[:limit]
|
|
return text[:cut].rstrip() + TRUNCATION_SUFFIX
|
|
|
|
|
|
def wrap_data(text: str) -> str:
|
|
return f"{DATA_OPEN}\n{text}\n{DATA_CLOSE}"
|