Files
checkbot/app/prompts/validation.py
T
patrickandClaude Sonnet 5.5 9c4f7366eb
test / pytest (3.11) (push) Waiting to run
test / pytest (3.13) (push) Waiting to run
feat: Phase 6 prompt versioning with test area, retention
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-06 02:17:01 +01:00

33 lines
989 B
Python
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import re
import unicodedata
from .constants import NOTE_MAX, TEXT_MAX
CONTROL_RE = re.compile("[\x00-\x08\x0b-\x1f\x7f-\x9f​-‏
-‮⁠-⁤]")
DELIMITER_RE = re.compile(r"<\s*/?\s*daten\s*>", re.IGNORECASE)
WHITESPACE_RE = re.compile(r"\s+")
class PromptError(Exception):
pass
def _normalize(text: object) -> str:
text = unicodedata.normalize("NFKC", str(text)).replace("\r\n", "\n").replace("\r", "\n")
return CONTROL_RE.sub("", text).strip()
def clean_text(text: object) -> str:
value = _normalize(text)
if not value:
raise PromptError("Der Prompt darf nicht leer sein.")
if len(value) > TEXT_MAX:
raise PromptError(f"Der Prompt ist zu lang (höchstens {TEXT_MAX} Zeichen).")
if DELIMITER_RE.search(value):
raise PromptError("Die Begrenzer <daten> und </daten> sind im Prompt nicht erlaubt.")
return value
def clean_note(note: object) -> str:
return WHITESPACE_RE.sub(" ", _normalize(note or ""))[:NOTE_MAX]