from __future__ import annotations import re from typing import Optional from models import SolverResult FORBIDDEN_PATTERNS = [ r"\bthe answer is\b", r"\bcorrect answer\b", r"\bchoice\s+[A-E]\b", r"\boption\s+[A-E]\b", r"\bit'?s\s+[A-E]\b", ] SAFE_FALLBACK = ( "I can help you solve it, but I should not give the final answer directly. " "I’ll guide you to the method and the check you need." ) def contains_forbidden_answering(text: str) -> bool: lower = (text or "").lower() return any(re.search(pattern, lower) for pattern in FORBIDDEN_PATTERNS) def enforce_study_guardrails(reply: str, result: Optional[SolverResult] = None) -> str: if contains_forbidden_answering(reply): if result and result.steps: bullet_steps = "\n".join(f"- {step}" for step in result.steps[:4]) return SAFE_FALLBACK + "\n\nTry this:\n" + bullet_steps return SAFE_FALLBACK return reply