Explorer
/proc/123/root/tmp/integrate_mas_identity.py
← Zurück ↓ Download
from pathlib import Path
p=Path('/opt/struktur/social-media-agent/publish_ready.py'); s=p.read_text()
old='''try:
    import yaml
    _editorial_config = yaml.safe_load((RADAR_DIR / "sources_multi.yaml").read_text(encoding="utf-8")).get("editorial_filters", {})
    REJECT_TITLE_TERMS = tuple(_editorial_config.get("reject_title_patterns", REJECT_TITLE_TERMS))
    TRANSFORMABLE_SOURCE_TERMS = tuple(_editorial_config.get("transformable_source_terms", TRANSFORMABLE_SOURCE_TERMS))
except Exception:
    pass'''
new='''MAS_CONTENT_IDENTITY = {}
MAS_CONTENT_POLICY = {}
MAS_CTA_MATRIX = {}
try:
    import yaml
    _mas_config = yaml.safe_load((RADAR_DIR / "sources_multi.yaml").read_text(encoding="utf-8")) or {}
    _editorial_config = _mas_config.get("editorial_filters", {})
    MAS_CONTENT_IDENTITY = _mas_config.get("mas_content_identity", {}) or {}
    MAS_CONTENT_POLICY = _mas_config.get("mas_content_policy", {}) or {}
    MAS_CTA_MATRIX = _mas_config.get("mas_cta_matrix", {}) or {}
    REJECT_TITLE_TERMS = tuple(_editorial_config.get("reject_title_patterns", REJECT_TITLE_TERMS))
    TRANSFORMABLE_SOURCE_TERMS = tuple(_editorial_config.get("transformable_source_terms", TRANSFORMABLE_SOURCE_TERMS))
except Exception:
    pass'''
assert s.count(old)==1; s=s.replace(old,new)
# add policy helper before quality_gate
needle='def quality_gate(signal: dict, facebook_text: str, linkedin_text: str) -> QualityAssessment:'
helper='''def _mas_policy_violation(text: str) -> str:
    low=text.casefold()
    for prohibited in MAS_CONTENT_POLICY.get("prohibited", []):
        phrase=str(prohibited).casefold()
        if phrase == "agentensprache" and any(term in low for term in AGENT_LANGUAGE_TERMS): return "content_policy_agent_language"
        if phrase and phrase != "agentensprache" and phrase in low: return "content_policy_" + re.sub(r"[^a-z0-9]+", "_", phrase).strip("_")
    if NUMERIC_CLAIM_RE.search(text): return "content_policy_unverified_precision"
    return ""


def quality_gate(signal: dict, facebook_text: str, linkedin_text: str) -> QualityAssessment:'''
assert s.count(needle)==1; s=s.replace(needle,helper)
old='''    combined = f"{facebook_text}\\n{linkedin_text}"
    low = combined.casefold()
    if any(term in low for term in AGENT_LANGUAGE_TERMS):'''
new='''    combined = f"{facebook_text}\\n{linkedin_text}"
    low = combined.casefold()
    policy_violation = _mas_policy_violation(combined)
    if policy_violation:
        return QualityAssessment(False, policy_violation)
    if any(term in low for term in AGENT_LANGUAGE_TERMS):'''
assert s.count(old)==1; s=s.replace(old,new)
# CTA matrix controls allowed CTA types; keep existing matrix text source.
old='''def choose_cta(signal: dict) -> str:
    cta_type = _text(signal.get("cta_type") or "beratung")
    variants = CTA_VARIANTS.get(cta_type, CTA_VARIANTS["beratung"])'''
new='''def choose_cta(signal: dict) -> str:
    cta_type = _text(signal.get("cta_type") or "beratung")
    matrix_key = {"beratung": "information", "messung": "messung", "objektpruefung": "diagnose", "lueftung": "lueftung", "monitoring_homeprotect": "sensorik_monitoring"}.get(cta_type, cta_type)
    if MAS_CTA_MATRIX and matrix_key not in MAS_CTA_MATRIX:
        matrix_key = "information"
    variants = CTA_VARIANTS.get(cta_type, CTA_VARIANTS["beratung"])'''
assert s.count(old)==1; s=s.replace(old,new)
p.write_text(s); print('MAS identity and policy loaded')