diff --git a/app/ia.py b/app/ia.py new file mode 100644 index 0000000..170c95b --- /dev/null +++ b/app/ia.py @@ -0,0 +1,164 @@ +"""Couche IA de Courrier (tt-2026-11-072) : suggestion de categorie. + +Regles fermes : +- interrupteur desactive par defaut (reglage en base, pas de variable qui l'allume en douce) ; +- le texte d'un mail ne part que vers PrivAiTe (alias courrier-main), jamais directement vers un modele ; +- PrivAiTe injoignable ou en erreur : refus net (503), aucun contournement ; +- la suggestion est proposee, jamais appliquee : aucun classement n'est ecrit ; +- aucun contenu de mail dans les journaux (seulement des comptes et des codes). +""" +import logging +import os +import re +from typing import Any, Dict, Optional + +import httpx +from fastapi import APIRouter, HTTPException, Query +from pydantic import BaseModel + +from app.config import get_tunis_now_iso +from app import db as _db + +log = logging.getLogger("courrier.ia") + +PRIVAITE_URL = os.getenv("PRIVAITE_URL", "http://100.94.90.119:8401/v1/chat/completions") +PRIVAITE_KEY_PATH = os.getenv("PRIVAITE_KEY_PATH", "/app/secrets/privaite.key") +IA_MODELE = os.getenv("IA_MODELE", "courrier-main") +IA_DELAI_S = float(os.getenv("IA_DELAI_S", "40")) +MAX_TEXTE = 3000 +A_CLASSER = "À classer" + + +class IAIndisponible(Exception): + pass + + +def _pret(conn) -> None: + conn.execute("CREATE TABLE IF NOT EXISTS reglages (cle TEXT PRIMARY KEY, valeur TEXT NOT NULL, le TEXT NOT NULL)") + conn.execute("CREATE TABLE IF NOT EXISTS suggestions_ia (message_id TEXT PRIMARY KEY, categorie TEXT NOT NULL, le TEXT NOT NULL)") + + +def ia_active(db_path: Optional[str] = None) -> bool: + conn = _db.get_connection(db_path) + try: + _pret(conn) + r = conn.execute("SELECT valeur FROM reglages WHERE cle='ia_actif'").fetchone() + return bool(r and r[0] == "1") + finally: + conn.close() + + +def regler_ia(actif: bool, db_path: Optional[str] = None) -> None: + conn = _db.get_connection(db_path) + try: + _pret(conn) + with conn: + conn.execute("INSERT INTO reglages (cle, valeur, le) VALUES ('ia_actif', ?, ?) " + "ON CONFLICT(cle) DO UPDATE SET valeur=excluded.valeur, le=excluded.le", + ("1" if actif else "0", get_tunis_now_iso())) + finally: + conn.close() + + +def _cle() -> str: + try: + with open(PRIVAITE_KEY_PATH, "r", encoding="utf-8") as f: + k = f.read().strip() + except OSError: + raise IAIndisponible("cle PrivAiTe absente") + if not k: + raise IAIndisponible("cle PrivAiTe vide") + return k + + +def appeler_privaite(messages, max_tokens: int = 30) -> str: + """Seul point de sortie vers un modele : PrivAiTe. Toute erreur = IAIndisponible, sans repli.""" + try: + r = httpx.post(PRIVAITE_URL, timeout=IA_DELAI_S, + headers={"Authorization": "Bearer " + _cle()}, + json={"model": IA_MODELE, "messages": messages, "max_tokens": max_tokens, "temperature": 0}) + except httpx.HTTPError as e: + log.warning("PrivAiTe injoignable (%s)", type(e).__name__) + raise IAIndisponible("PrivAiTe injoignable") + if r.status_code != 200: + log.warning("PrivAiTe a repondu %s", r.status_code) + raise IAIndisponible("PrivAiTe a refuse la requete") + try: + return str(r.json()["choices"][0]["message"]["content"] or "") + except (KeyError, IndexError, ValueError, TypeError): + raise IAIndisponible("reponse PrivAiTe illisible") + + +def construire_messages(objet: str, corps: str, categories) -> list: + liste = ", ".join(categories) + consigne = ("Tu classes un mail dans UNE categorie parmi : " + liste + ". " + "Reponds uniquement par le nom exact de la categorie, sans autre mot. " + "Si aucune ne convient clairement, reponds : " + A_CLASSER + ".") + texte = "Objet : " + (objet or "(vide)") + "\n\n" + (corps or "")[:MAX_TEXTE] + return [{"role": "system", "content": consigne}, {"role": "user", "content": texte}] + + +def lire_categorie(reponse: str, categories) -> str: + """Valide la reponse du modele contre la liste : tout le reste devient « À classer ».""" + rep = re.sub(r"[\s\"'.*`]+", " ", reponse or "").strip().lower() + for c in categories: + if rep == c.lower(): + return c + return A_CLASSER + + +def suggerer(objet: str, corps: str, db_path: Optional[str] = None) -> Dict[str, Any]: + if not ia_active(db_path): + raise HTTPException(status_code=409, detail="L'IA est désactivée.") + cats = [c["nom"] if "nom" in c else c.get("name") for c in _db.list_categories(db_path)] + cats = [c for c in cats if c and c != A_CLASSER] + try: + rep = appeler_privaite(construire_messages(objet, corps, cats)) + except IAIndisponible as e: + raise HTTPException(status_code=503, detail="IA indisponible, aucune suggestion : " + str(e) + ".") + return {"categorie": lire_categorie(rep, cats), "source": "ia", "appliquee": False} + + +class IAEtat(BaseModel): + actif: bool + + +def creer_routeur(mirror_client) -> APIRouter: + router = APIRouter(prefix="/ia") + + @router.get("/etat") + def etat(): + return {"actif": ia_active(), "modele": IA_MODELE} + + @router.put("/etat") + def modifier_etat(e: IAEtat): + regler_ia(e.actif) + return {"actif": ia_active(), "modele": IA_MODELE} + + @router.get("/suggestion") + def suggestion(id: str = Query(..., min_length=1, max_length=400)): + """Propose une categorie pour un mail. N'ecrit aucun classement.""" + if not ia_active(): + raise HTTPException(status_code=409, detail="L'IA est désactivée.") + try: + res = mirror_client.get_mail(id) + except Exception: + raise HTTPException(status_code=502, detail="Lecture du mail impossible : miroir indisponible.") + d = res.get("donnees") if isinstance(res, dict) else None + if not isinstance(d, dict) or not d: + raise HTTPException(status_code=404, detail="Mail introuvable.") + eh = d.get("en_tetes") or {} + objet = str(eh.get("Subject") or eh.get("subject") or "") + out = suggerer(objet, str(d.get("corps_texte") or "")) + conn = _db.get_connection() + try: + _pret(conn) + with conn: + conn.execute("INSERT INTO suggestions_ia (message_id, categorie, le) VALUES (?, ?, ?) " + "ON CONFLICT(message_id) DO UPDATE SET categorie=excluded.categorie, le=excluded.le", + (id, out["categorie"], get_tunis_now_iso())) + finally: + conn.close() + return out + + return router diff --git a/app/main.py b/app/main.py index 27eb1bb..097b540 100644 --- a/app/main.py +++ b/app/main.py @@ -45,6 +45,7 @@ from app.classifier import ( reclassify_today_mails ) from app.mirror_client import MirrorClient +from app.ia import creer_routeur from app.auth import verify_cf_access_jwt # --- Schémas Pydantic --- @@ -626,4 +627,5 @@ def confirm_rule_endpoint(regle_id: int): "message": "Règle confirmée et appliquée aux classements automatiques." } +api.include_router(creer_routeur(mirror_client)) app.include_router(api) diff --git a/tests/test_ia_072.py b/tests/test_ia_072.py new file mode 100644 index 0000000..bef5868 --- /dev/null +++ b/tests/test_ia_072.py @@ -0,0 +1,87 @@ +import httpx +import pytest +from fastapi.testclient import TestClient + +from app import ia +from app.main import app, mirror_client + +DONNEES = {"message_id": "m-1", "en_tetes": {"Subject": "Colis pour Tunis"}, + "corps_texte": "Merci de rapporter le colis. Ali Test 12 345,678 DT"} + + +@pytest.fixture +def mail(monkeypatch): + monkeypatch.setattr(mirror_client, "get_mail", lambda mid: {"donnees": DONNEES}) + + +def test_ia_desactivee_par_defaut(client): + assert client.get("/api/v1/ia/etat").json()["actif"] is False + + +def test_suggestion_refusee_si_ia_desactivee(client, mail, monkeypatch): + appels = [] + monkeypatch.setattr(ia, "appeler_privaite", lambda *a, **k: appels.append(1) or "Tunis") + assert client.get("/api/v1/ia/suggestion?id=m-1").status_code == 409 + assert appels == [] + + +def test_interrupteur_se_bascule(client): + assert client.put("/api/v1/ia/etat", json={"actif": True}).json()["actif"] is True + assert client.put("/api/v1/ia/etat", json={"actif": False}).json()["actif"] is False + + +def test_suggestion_proposee_jamais_appliquee(client, mail, monkeypatch): + client.put("/api/v1/ia/etat", json={"actif": True}) + monkeypatch.setattr(ia, "appeler_privaite", lambda *a, **k: "Tunis") + j = client.get("/api/v1/ia/suggestion?id=m-1").json() + assert j == {"categorie": "Tunis", "source": "ia", "appliquee": False} + assert client.get("/api/v1/classements/m-1").status_code == 404 + + +def test_reponse_hors_liste_devient_a_classer(client, mail, monkeypatch): + client.put("/api/v1/ia/etat", json={"actif": True}) + monkeypatch.setattr(ia, "appeler_privaite", lambda *a, **k: "Je pense que c'est urgent") + assert client.get("/api/v1/ia/suggestion?id=m-1").json()["categorie"] == "À classer" + + +def test_refus_net_si_privaite_injoignable(client, mail, monkeypatch): + client.put("/api/v1/ia/etat", json={"actif": True}) + def boom(*a, **k): + raise httpx.ConnectError("x") + monkeypatch.setattr(ia.httpx, "post", boom) + monkeypatch.setattr(ia, "_cle", lambda: "k") + r = client.get("/api/v1/ia/suggestion?id=m-1") + assert r.status_code == 503 + + +def test_aucun_appel_direct_hors_privaite(client, mail, monkeypatch): + client.put("/api/v1/ia/etat", json={"actif": True}) + urls = [] + def faux(url, **k): + urls.append(url) + raise httpx.ConnectError("x") + monkeypatch.setattr(ia.httpx, "post", faux) + monkeypatch.setattr(ia, "_cle", lambda: "k") + client.get("/api/v1/ia/suggestion?id=m-1") + assert urls == [ia.PRIVAITE_URL] and "8401" in urls[0] + + +def test_aucun_contenu_de_mail_dans_les_journaux(client, mail, monkeypatch, caplog): + client.put("/api/v1/ia/etat", json={"actif": True}) + class R: + status_code = 500 + monkeypatch.setattr(ia.httpx, "post", lambda *a, **k: R()) + monkeypatch.setattr(ia, "_cle", lambda: "k") + with caplog.at_level("DEBUG"): + client.get("/api/v1/ia/suggestion?id=m-1") + assert "Ali Test" not in caplog.text and "12 345" not in caplog.text and "Colis" not in caplog.text + + +def test_suggestion_exige_le_jeton_access(): + assert TestClient(app).get("/api/v1/ia/suggestion?id=m-1").status_code == 401 + + +def test_cle_absente_refus_net(client, mail, monkeypatch): + client.put("/api/v1/ia/etat", json={"actif": True}) + monkeypatch.setattr(ia, "PRIVAITE_KEY_PATH", "/nonexistent/k") + assert client.get("/api/v1/ia/suggestion?id=m-1").status_code == 503