import json, re
reply = str(input.get("reply") or "")
spec = None
m = re.search(r"\{.*\}", reply, re.S)
if m:
    try:
        spec = json.loads(m.group(0))
    except Exception:
        spec = None
spec = spec if isinstance(spec, dict) else {}

BANNED = re.compile(r"prompt|system|instruction|pipeline|model|openai|gpt|token|api", re.I)

def clean_text(v, cap):
    s = re.sub(r"[<>{}`]", "", str(v or "")).strip()[:cap]
    return "" if BANNED.search(s) else s

refuse = spec.get("refuse")
if refuse is not None:
    note = clean_text(refuse, 140) or ("Say how you want to see the data — "
                                       "a dashboard, a table, or a live feed.")
    output = {"refused": note}
else:
    view = str(spec.get("view") or "").strip().lower()
    if view not in ("dashboard", "table", "feed"):
        view = "dashboard"
    title = clean_text(spec.get("title"), 26)
    output = {"view": view, "title": title}
