1010 lines
40 KiB
Python
Executable File
1010 lines
40 KiB
Python
Executable File
# -*- coding: utf-8 -*-
|
|
import os
|
|
import re
|
|
import json
|
|
import hmac
|
|
import uuid
|
|
import secrets
|
|
import logging
|
|
import hashlib
|
|
import datetime
|
|
import subprocess
|
|
import urllib.request
|
|
import urllib.error
|
|
import urllib.parse
|
|
from zoneinfo import ZoneInfo
|
|
from contextlib import asynccontextmanager
|
|
from contextvars import ContextVar
|
|
from typing import Any, Dict, List, Optional
|
|
from starlette.applications import Starlette
|
|
from starlette.responses import JSONResponse
|
|
from starlette.routing import Route, Mount
|
|
from starlette.middleware.base import BaseHTTPMiddleware
|
|
from mcp.server.fastmcp import FastMCP
|
|
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
|
|
|
|
from cap_check import count_today, TUNIS_TZ
|
|
|
|
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s")
|
|
logger = logging.getLogger("treblo-mcp")
|
|
|
|
current_sphere_var: ContextVar[str] = ContextVar("current_sphere", default="")
|
|
|
|
mcp = FastMCP("treblo-mcp")
|
|
|
|
def get_journal_path() -> str:
|
|
path = "/app/data/journal.jsonl"
|
|
if not os.path.exists(os.path.dirname(path)):
|
|
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "journal.jsonl")
|
|
os.makedirs(os.path.dirname(local), exist_ok=True)
|
|
return local
|
|
return path
|
|
|
|
def get_validations_path() -> str:
|
|
path = "/app/data/validations.json"
|
|
if not os.path.exists(os.path.dirname(path)):
|
|
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "validations.json")
|
|
os.makedirs(os.path.dirname(local), exist_ok=True)
|
|
return local
|
|
return path
|
|
|
|
def get_envois_path() -> str:
|
|
path = "/app/data/envois.jsonl"
|
|
if not os.path.exists(os.path.dirname(path)):
|
|
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "envois.jsonl")
|
|
os.makedirs(os.path.dirname(local), exist_ok=True)
|
|
return local
|
|
return path
|
|
|
|
def get_titres_path() -> str:
|
|
path = "/app/data/titres.json"
|
|
if not os.path.exists(os.path.dirname(path)):
|
|
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "titres.json")
|
|
os.makedirs(os.path.dirname(local), exist_ok=True)
|
|
return local
|
|
return path
|
|
|
|
def load_titres() -> Dict[str, str]:
|
|
path = get_titres_path()
|
|
if os.path.exists(path):
|
|
try:
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
return json.load(f)
|
|
except Exception:
|
|
return {}
|
|
return {}
|
|
|
|
def save_titre(task_id: str, titre: str):
|
|
path = get_titres_path()
|
|
titres = load_titres()
|
|
titres[task_id] = titre
|
|
with open(path, "w", encoding="utf-8") as f:
|
|
json.dump(titres, f, indent=2, ensure_ascii=False)
|
|
|
|
def get_titre_for_task(task_id: str, default: Optional[str] = None) -> str:
|
|
titres = load_titres()
|
|
if task_id in titres and titres[task_id]:
|
|
return titres[task_id]
|
|
entry = get_journal_entry(task_id)
|
|
if entry and entry.get("titre"):
|
|
return entry.get("titre")
|
|
for sp in ("nyora", "perso"):
|
|
m_path = os.path.join(get_musique_dir(sp), f"{task_id}.json")
|
|
if os.path.exists(m_path):
|
|
try:
|
|
with open(m_path, "r", encoding="utf-8") as f:
|
|
meta = json.load(f)
|
|
if meta.get("titre"):
|
|
return meta.get("titre")
|
|
except Exception:
|
|
pass
|
|
return default or task_id
|
|
|
|
def validate_titre(titre: str) -> str:
|
|
if not titre or not isinstance(titre, str):
|
|
raise ValueError("Refus : le paramètre titre est obligatoire")
|
|
t = titre.strip()
|
|
if len(t) < 1 or len(t) > 60:
|
|
raise ValueError(f"Refus : le titre doit comporter entre 1 et 60 caractères (reçu: {len(t)})")
|
|
if re.search(r'\bcomme\s+\S+', t, re.IGNORECASE) or re.search(r'\blike\s+\S+', t, re.IGNORECASE):
|
|
raise ValueError("Refus : le titre ne doit contenir ni formule 'comme X' ni référence directe d'artiste")
|
|
return t
|
|
|
|
def nettoyer_titre_fichier(titre: str) -> str:
|
|
res = []
|
|
for c in str(titre or ""):
|
|
if c in '/\\:*?"<>|' or ord(c) < 32 or ord(c) == 127:
|
|
res.append('-')
|
|
else:
|
|
res.append(c)
|
|
s = "".join(res)
|
|
s = re.sub(r'\s+', ' ', s).strip()
|
|
if len(s) > 80:
|
|
s = s[:80].rstrip()
|
|
return s
|
|
|
|
def titre_vers_nom_fichier(titre: str) -> str:
|
|
cleaned = nettoyer_titre_fichier(titre)
|
|
return f"{cleaned}.mp3"
|
|
|
|
def echapper_html(text: str) -> str:
|
|
if not text:
|
|
return ""
|
|
return str(text).replace("&", "&").replace("<", "<").replace(">", ">")
|
|
|
|
def preparer_messages_paroles(titre: str, tags: Any, paroles: Optional[str]) -> List[str]:
|
|
t_esc = echapper_html(titre)
|
|
tags_str = ", ".join(str(t).strip() for t in tags) if isinstance(tags, list) else str(tags or "").strip()
|
|
tags_esc = echapper_html(tags_str)
|
|
|
|
has_paroles = bool(paroles and str(paroles).strip())
|
|
if not has_paroles:
|
|
return [f"<b>🎵 {t_esc}</b>\n<i>{tags_esc}</i>\n\nInstrumental"]
|
|
|
|
raw_strophes = [s.strip() for s in re.split(r'\n\s*\n', str(paroles).strip()) if s.strip()]
|
|
escaped_strophes = [echapper_html(s) for s in raw_strophes]
|
|
|
|
prefix = f"<b>🎵 {t_esc}</b>\n<i>{tags_esc}</i>\n---\n"
|
|
tag_open = "<blockquote>"
|
|
tag_close = "</blockquote>"
|
|
|
|
messages: List[str] = []
|
|
current_strophes: List[str] = []
|
|
|
|
for s in escaped_strophes:
|
|
candidate_strophes = current_strophes + [s]
|
|
candidate_body = "\n\n".join(candidate_strophes)
|
|
current_prefix = prefix if len(messages) == 0 else ""
|
|
candidate_msg = f"{current_prefix}{tag_open}{candidate_body}{tag_close}"
|
|
|
|
if len(candidate_msg) <= 4096:
|
|
current_strophes.append(s)
|
|
else:
|
|
if current_strophes:
|
|
body = "\n\n".join(current_strophes)
|
|
messages.append(f"{current_prefix}{tag_open}{body}{tag_close}")
|
|
current_strophes = [s]
|
|
else:
|
|
lines = s.split("\n")
|
|
chunk_lines = []
|
|
for line in lines:
|
|
cand = "\n".join(chunk_lines + [line])
|
|
cand_msg = f"{current_prefix}{tag_open}{cand}{tag_close}"
|
|
if len(cand_msg) <= 4096:
|
|
chunk_lines.append(line)
|
|
else:
|
|
if chunk_lines:
|
|
messages.append(f"{current_prefix}{tag_open}{'\n'.join(chunk_lines)}{tag_close}")
|
|
current_prefix = ""
|
|
chunk_lines = [line]
|
|
else:
|
|
messages.append(f"{current_prefix}{tag_open}{line[:4000]}{tag_close}")
|
|
current_prefix = ""
|
|
if chunk_lines:
|
|
current_strophes = ["\n".join(chunk_lines)]
|
|
|
|
if current_strophes:
|
|
current_prefix = prefix if len(messages) == 0 else ""
|
|
body = "\n\n".join(current_strophes)
|
|
messages.append(f"{current_prefix}{tag_open}{body}{tag_close}")
|
|
|
|
return messages
|
|
|
|
def get_musique_dir(sphere: str) -> str:
|
|
path = f"/app/musique/{sphere}"
|
|
if not os.path.exists("/app/musique"):
|
|
local = os.path.join(os.path.dirname(os.path.abspath(__file__)), "musique", sphere)
|
|
os.makedirs(local, exist_ok=True)
|
|
return local
|
|
os.makedirs(path, exist_ok=True)
|
|
return path
|
|
|
|
def get_api_key(sphere: str) -> str:
|
|
if sphere == "nyora":
|
|
k = os.environ.get("TREBLO_KEY_NYORA", "").strip()
|
|
elif sphere == "perso":
|
|
k = os.environ.get("TREBLO_KEY_PERSO", "").strip()
|
|
else:
|
|
raise ValueError(f"Sphère inconnue: {sphere}")
|
|
if not k:
|
|
raise ValueError(f"Clé Treblo non configurée pour la sphère {sphere}")
|
|
return k
|
|
|
|
def get_bot_token(sphere: str) -> str:
|
|
if sphere == "nyora":
|
|
return os.environ.get("TELEGRAM_BOT_TOKEN_NYORA", "").strip()
|
|
elif sphere == "perso":
|
|
return os.environ.get("TELEGRAM_BOT_TOKEN_PERSO", "").strip()
|
|
return ""
|
|
|
|
def validate_sphere_access(requested_sphere: str) -> str:
|
|
req = requested_sphere.lower().strip()
|
|
auth = current_sphere_var.get()
|
|
if auth and req != auth:
|
|
raise ValueError(f"Refus explicite : le jeton de la sphère {auth} ne peut pas agir sur la sphère {req}")
|
|
return req
|
|
|
|
def compute_empreinte(sphere: str, tags: Optional[List[str]], prompt: Optional[str], paroles: Optional[str]) -> str:
|
|
s_tags = sorted([str(t).strip() for t in (tags or []) if str(t).strip()])
|
|
p_prompt = (str(prompt).strip() if prompt else "")
|
|
p_paroles = (str(paroles).strip() if paroles else "")
|
|
canonical = f"{sphere.lower().strip()}|{','.join(s_tags)}|{p_prompt}|{p_paroles}"
|
|
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
|
|
|
def treblo_balance(sphere: str) -> Dict[str, Any]:
|
|
key = get_api_key(sphere)
|
|
req = urllib.request.Request("https://api.treblo.com/v1/credits/balance", headers={"Authorization": f"Bearer {key}"})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=20) as resp:
|
|
data = json.loads(resp.read().decode("utf-8"))
|
|
except urllib.error.HTTPError as e:
|
|
err = e.read().decode("utf-8", errors="replace")
|
|
raise RuntimeError(f"Erreur Treblo balance {e.code}: {err}")
|
|
except Exception as e:
|
|
raise RuntimeError(f"Erreur connexion Treblo balance: {e}")
|
|
|
|
num_credits = int(data.get("num_credits", 0))
|
|
num_credits_payg = int(data.get("num_credits_payg", 0))
|
|
total = num_credits + num_credits_payg
|
|
return {
|
|
"sphere": sphere,
|
|
"total": total,
|
|
"num_credits": num_credits,
|
|
"num_credits_payg": num_credits_payg
|
|
}
|
|
|
|
def check_daily_cap(sphere: str):
|
|
daily_cap = int(os.environ.get("TREBLO_DAILY_CAP", "5"))
|
|
journal_path = get_journal_path()
|
|
if count_today(sphere, journal_path) >= daily_cap:
|
|
raise ValueError(f"CAP_REFUSED: plafond quotidien atteint pour la sphère {sphere} ({daily_cap} max par jour)")
|
|
|
|
def append_journal(entry: Dict[str, Any]):
|
|
journal_path = get_journal_path()
|
|
with open(journal_path, "a", encoding="utf-8") as f:
|
|
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
|
|
|
def update_journal_task(task_id: str, new_status: str, credits_apres: Optional[int] = None):
|
|
journal_path = get_journal_path()
|
|
if not os.path.exists(journal_path):
|
|
return
|
|
lines = []
|
|
updated = False
|
|
with open(journal_path, "r", encoding="utf-8") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
try:
|
|
entry = json.loads(line)
|
|
if entry.get("task_id") == task_id:
|
|
entry["statut"] = new_status
|
|
if credits_apres is not None:
|
|
entry["credits_apres"] = credits_apres
|
|
updated = True
|
|
lines.append(json.dumps(entry, ensure_ascii=False))
|
|
except Exception:
|
|
lines.append(line)
|
|
if updated:
|
|
with open(journal_path, "w", encoding="utf-8") as f:
|
|
for l in lines:
|
|
f.write(l + "\n")
|
|
|
|
def get_journal_entry(task_id: str) -> Optional[Dict[str, Any]]:
|
|
journal_path = get_journal_path()
|
|
if not os.path.exists(journal_path):
|
|
return None
|
|
with open(journal_path, "r", encoding="utf-8") as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if not line:
|
|
continue
|
|
try:
|
|
entry = json.loads(line)
|
|
if entry.get("task_id") == task_id:
|
|
return entry
|
|
except Exception:
|
|
pass
|
|
return None
|
|
|
|
def log_envoi(task_id: str, sphere: str, message_id: int, titre: str = "", nom_fichier: str = "", texte_message_id: int = 0):
|
|
path = get_envois_path()
|
|
entry = {
|
|
"type": "apercu_mp3",
|
|
"task_id": task_id,
|
|
"sphere": sphere,
|
|
"titre": titre,
|
|
"nom_fichier": nom_fichier,
|
|
"message_id": int(message_id),
|
|
"texte_message_id": int(texte_message_id),
|
|
"date": datetime.datetime.now(datetime.timezone.utc).isoformat()
|
|
}
|
|
with open(path, "a", encoding="utf-8") as f:
|
|
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
|
|
|
def consume_approved_validation(sphere: str, empreinte: str, val_id: Optional[str] = None) -> bool:
|
|
v_path = get_validations_path()
|
|
if not os.path.exists(v_path):
|
|
return False
|
|
try:
|
|
with open(v_path, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
now_dt = datetime.datetime.now(TUNIS_TZ)
|
|
updated = False
|
|
consumed = False
|
|
for item in data.get("validations", []):
|
|
if item.get("sphere") == sphere and item.get("status") == "APPROVED":
|
|
# L'approbation n'est consommée que si la sphère ET l'empreinte correspondent,
|
|
# avec ou sans validation_id
|
|
if item.get("empreinte") != empreinte:
|
|
continue
|
|
if val_id and item.get("id") != val_id:
|
|
continue
|
|
exp = datetime.datetime.fromisoformat(item.get("expires_at"))
|
|
if exp > now_dt:
|
|
item["status"] = "USED"
|
|
item["used_at"] = now_dt.isoformat()
|
|
consumed = True
|
|
updated = True
|
|
break
|
|
else:
|
|
item["status"] = "EXPIRED"
|
|
updated = True
|
|
if updated:
|
|
with open(v_path, "w", encoding="utf-8") as fw:
|
|
json.dump(data, fw, indent=2, ensure_ascii=False)
|
|
return consumed
|
|
except Exception as e:
|
|
logger.warning("Erreur lecture validations.json: %s", e)
|
|
return False
|
|
|
|
def is_validation_approved(sphere: str, empreinte: str, val_id: Optional[str] = None) -> bool:
|
|
v_path = get_validations_path()
|
|
if not os.path.exists(v_path):
|
|
return False
|
|
try:
|
|
with open(v_path, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
now_dt = datetime.datetime.now(TUNIS_TZ)
|
|
for item in data.get("validations", []):
|
|
if item.get("sphere") == sphere and item.get("status") == "APPROVED":
|
|
if item.get("empreinte") != empreinte:
|
|
continue
|
|
if val_id and item.get("id") != val_id:
|
|
continue
|
|
exp = datetime.datetime.fromisoformat(item.get("expires_at"))
|
|
if exp > now_dt:
|
|
return True
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
def record_pending_validation(sphere: str, val_id: str, empreinte: str, expires_iso: str = "", details: dict = None, code: Optional[str] = None):
|
|
v_path = get_validations_path()
|
|
data = {"validations": []}
|
|
if os.path.exists(v_path):
|
|
try:
|
|
with open(v_path, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
except Exception:
|
|
data = {"validations": []}
|
|
|
|
if not code:
|
|
code = f"{secrets.randbelow(1000000):06d}"
|
|
|
|
data.setdefault("validations", []).append({
|
|
"id": val_id,
|
|
"sphere": sphere,
|
|
"empreinte": empreinte,
|
|
"code": code,
|
|
"status": "PENDING",
|
|
"attempts": 0,
|
|
"created_at": datetime.datetime.now(TUNIS_TZ).isoformat(),
|
|
"expires_at": expires_iso,
|
|
"details": details or {}
|
|
})
|
|
data["validations"] = data["validations"][-50:]
|
|
with open(v_path, "w", encoding="utf-8") as f:
|
|
json.dump(data, f, indent=2, ensure_ascii=False)
|
|
|
|
def send_telegram_validation_request(sphere: str, val_id: str, code: str, tags: list, prompt: str, paroles: str, expires_str: str):
|
|
bot_token = get_bot_token(sphere)
|
|
chat_id = os.environ.get("TELEGRAM_CHAT_ID", "2084513684").strip()
|
|
if not bot_token or not chat_id:
|
|
logger.warning("Telegram bot non configuré pour la sphère %s", sphere)
|
|
return
|
|
|
|
tags_str = ", ".join(tags) if tags else "aucun"
|
|
prompt_str = (prompt[:120] + "...") if prompt and len(prompt) > 120 else (prompt or "aucun")
|
|
paroles_str = (paroles[:120] + "...") if paroles and len(paroles) > 120 else (paroles or "aucune")
|
|
|
|
text = (
|
|
f"🎵 Demande de génération musicale Treblo (validation requise)\n"
|
|
f"• Sphère : {sphere}\n"
|
|
f"• Tags : {tags_str}\n"
|
|
f"• Prompt : {prompt_str}\n"
|
|
f"• Paroles : {paroles_str}\n"
|
|
f"• Réf : #{val_id}\n"
|
|
f"• Code : {code}\n\n"
|
|
f"Transmettre ce code à l'assistant pour autoriser la génération (expiration : {expires_str})"
|
|
)
|
|
|
|
url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
|
payload = json.dumps({"chat_id": chat_id, "text": text}).encode("utf-8")
|
|
req = urllib.request.Request(url, data=payload, headers={"Content-Type": "application/json"})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=10) as resp:
|
|
data = json.loads(resp.read().decode("utf-8"))
|
|
if data.get("ok"):
|
|
logger.info("Notification Telegram envoyée avec succès (message_id=%s)", data.get("result", {}).get("message_id"))
|
|
except Exception as e:
|
|
logger.warning("Échec envoi notification Telegram: %s", e)
|
|
|
|
def send_telegram_preview_mp3(
|
|
sphere: str,
|
|
mp3_path: str,
|
|
task_id: str,
|
|
titre: Optional[str] = None,
|
|
tags: Optional[Any] = None,
|
|
paroles: Optional[str] = None
|
|
) -> Optional[Dict[str, int]]:
|
|
bot_token = get_bot_token(sphere)
|
|
chat_id = os.environ.get("TELEGRAM_CHAT_ID", "2084513684").strip()
|
|
if not bot_token or not chat_id or not os.path.exists(mp3_path):
|
|
logger.warning("send_telegram_preview_mp3: bot_token, chat_id absent ou mp3_path introuvable (%s)", mp3_path)
|
|
return None
|
|
|
|
if not titre:
|
|
titre = get_titre_for_task(task_id)
|
|
|
|
if tags is None or paroles is None:
|
|
meta_path = os.path.join(os.path.dirname(mp3_path), f"{task_id}.json")
|
|
if os.path.exists(meta_path):
|
|
try:
|
|
with open(meta_path, "r", encoding="utf-8") as f:
|
|
meta = json.load(f)
|
|
if tags is None:
|
|
tags = meta.get("tags", [])
|
|
if paroles is None:
|
|
paroles = meta.get("paroles", "")
|
|
except Exception:
|
|
pass
|
|
if tags is None or paroles is None:
|
|
entry = get_journal_entry(task_id)
|
|
if entry:
|
|
if tags is None:
|
|
tags = entry.get("tags", [])
|
|
if paroles is None:
|
|
paroles = entry.get("paroles", "")
|
|
|
|
nom_fichier = titre_vers_nom_fichier(titre)
|
|
performer = "Nyora" if sphere == "nyora" else "Perso"
|
|
|
|
boundary = f"----TrebloBoundary{uuid.uuid4().hex}"
|
|
with open(mp3_path, "rb") as f:
|
|
audio_bytes = f.read()
|
|
|
|
parts = [
|
|
f"--{boundary}\r\nContent-Disposition: form-data; name=\"chat_id\"\r\n\r\n{chat_id}\r\n".encode(),
|
|
f"--{boundary}\r\nContent-Disposition: form-data; name=\"title\"\r\n\r\n{titre}\r\n".encode(),
|
|
f"--{boundary}\r\nContent-Disposition: form-data; name=\"performer\"\r\n\r\n{performer}\r\n".encode(),
|
|
f"--{boundary}\r\nContent-Disposition: form-data; name=\"audio\"; filename=\"{nom_fichier}\"\r\nContent-Type: audio/mpeg\r\n\r\n".encode(),
|
|
audio_bytes,
|
|
f"\r\n--{boundary}--\r\n".encode()
|
|
]
|
|
body = b"".join(parts)
|
|
url_audio = f"https://api.telegram.org/bot{bot_token}/sendAudio"
|
|
req_audio = urllib.request.Request(url_audio, data=body, headers={"Content-Type": f"multipart/form-data; boundary={boundary}"})
|
|
audio_msg_id = None
|
|
try:
|
|
with urllib.request.urlopen(req_audio, timeout=60) as resp:
|
|
data = json.loads(resp.read().decode("utf-8"))
|
|
if data.get("ok"):
|
|
audio_msg_id = data.get("result", {}).get("message_id")
|
|
logger.info("Aperçu MP3 envoyé à Telegram (message_id=%s, nom_fichier=%s)", audio_msg_id, nom_fichier)
|
|
except Exception as e:
|
|
logger.warning("Échec envoi audio MP3 Telegram: %s", e)
|
|
return None
|
|
|
|
# Envoi du/des messages de paroles ou instrumental en HTML
|
|
messages = preparer_messages_paroles(titre, tags, paroles)
|
|
texte_msg_id = None
|
|
url_text = f"https://api.telegram.org/bot{bot_token}/sendMessage"
|
|
|
|
for idx, msg_text in enumerate(messages):
|
|
payload = json.dumps({
|
|
"chat_id": chat_id,
|
|
"text": msg_text,
|
|
"parse_mode": "HTML"
|
|
}).encode("utf-8")
|
|
req_msg = urllib.request.Request(url_text, data=payload, headers={"Content-Type": "application/json"})
|
|
try:
|
|
with urllib.request.urlopen(req_msg, timeout=20) as resp:
|
|
data = json.loads(resp.read().decode("utf-8"))
|
|
if data.get("ok"):
|
|
mid = data.get("result", {}).get("message_id")
|
|
if idx == 0:
|
|
texte_msg_id = mid
|
|
logger.info("Message texte #%d envoyé à Telegram (message_id=%s)", idx + 1, mid)
|
|
except Exception as e:
|
|
logger.warning("Échec envoi message texte Telegram #%d: %s", idx + 1, e)
|
|
|
|
if isinstance(audio_msg_id, int):
|
|
first_text_id = int(texte_msg_id) if isinstance(texte_msg_id, int) else 0
|
|
log_envoi(task_id, sphere, audio_msg_id, titre=titre, nom_fichier=nom_fichier, texte_message_id=first_text_id)
|
|
|
|
return {
|
|
"message_id": audio_msg_id,
|
|
"texte_message_id": texte_msg_id
|
|
}
|
|
|
|
@mcp.tool()
|
|
def solde(sphere: str) -> dict:
|
|
"""Consulte le solde de crédits Treblo pour la sphère indiquée (nyora ou perso)."""
|
|
sp = validate_sphere_access(sphere)
|
|
return treblo_balance(sp)
|
|
|
|
@mcp.tool()
|
|
def valider(val_id: str, code: str) -> dict:
|
|
"""Valide une demande d'approbation préalable à la génération musicale à l'aide du code à 6 chiffres transmis à Nabil.
|
|
Approuve seulement si le code est exact, la demande non expirée (15 minutes) et non utilisée (3 essais max par demande)."""
|
|
val_id_clean = str(val_id).strip().lstrip("#")
|
|
code_input = str(code).strip()
|
|
v_path = get_validations_path()
|
|
if not os.path.exists(v_path):
|
|
raise ValueError("Aucune demande de validation trouvée")
|
|
|
|
with open(v_path, "r", encoding="utf-8") as f:
|
|
data = json.load(f)
|
|
|
|
target = None
|
|
for item in data.get("validations", []):
|
|
if item.get("id") == val_id_clean:
|
|
target = item
|
|
break
|
|
|
|
if not target:
|
|
raise ValueError(f"Demande de validation #{val_id_clean} introuvable")
|
|
|
|
st = target.get("status", "PENDING")
|
|
if st == "USED":
|
|
raise ValueError(f"Demande #{val_id_clean} déjà utilisée (consommée)")
|
|
if st in ("CANCELLED", "REJECTED"):
|
|
raise ValueError(f"Demande #{val_id_clean} déjà annulée ou rejetée")
|
|
if st == "EXPIRED":
|
|
raise ValueError(f"Demande #{val_id_clean} expirée")
|
|
|
|
now_dt = datetime.datetime.now(TUNIS_TZ)
|
|
exp = datetime.datetime.fromisoformat(target.get("expires_at"))
|
|
if now_dt >= exp:
|
|
target["status"] = "EXPIRED"
|
|
with open(v_path, "w", encoding="utf-8") as fw:
|
|
json.dump(data, fw, indent=2, ensure_ascii=False)
|
|
raise ValueError(f"Demande #{val_id_clean} expirée (délai de 15 minutes dépassé)")
|
|
|
|
attempts = target.get("attempts", 0)
|
|
if attempts >= 3:
|
|
target["status"] = "CANCELLED"
|
|
with open(v_path, "w", encoding="utf-8") as fw:
|
|
json.dump(data, fw, indent=2, ensure_ascii=False)
|
|
raise ValueError(f"Limite de 3 essais atteinte : la demande #{val_id_clean} est annulée.")
|
|
|
|
stored_code = str(target.get("code", "")).strip()
|
|
if not hmac.compare_digest(code_input, stored_code):
|
|
attempts += 1
|
|
target["attempts"] = attempts
|
|
if attempts >= 3:
|
|
target["status"] = "CANCELLED"
|
|
with open(v_path, "w", encoding="utf-8") as fw:
|
|
json.dump(data, fw, indent=2, ensure_ascii=False)
|
|
raise ValueError(f"Code incorrect. Limite de 3 essais atteinte : la demande #{val_id_clean} est annulée.")
|
|
else:
|
|
with open(v_path, "w", encoding="utf-8") as fw:
|
|
json.dump(data, fw, indent=2, ensure_ascii=False)
|
|
raise ValueError(f"Code incorrect pour la demande #{val_id_clean} ({3 - attempts} essai(s) restant(s)).")
|
|
|
|
target["status"] = "APPROVED"
|
|
target["approved_at"] = now_dt.isoformat()
|
|
with open(v_path, "w", encoding="utf-8") as fw:
|
|
json.dump(data, fw, indent=2, ensure_ascii=False)
|
|
|
|
return {
|
|
"status": "APPROVED",
|
|
"val_id": val_id_clean,
|
|
"sphere": target.get("sphere"),
|
|
"message": f"Demande #{val_id_clean} approuvée avec succès. Vous pouvez maintenant appeler generer."
|
|
}
|
|
|
|
@mcp.tool()
|
|
def generer(
|
|
sphere: str,
|
|
titre: str,
|
|
tags: Optional[List[str]] = None,
|
|
paroles: Optional[str] = None,
|
|
prompt: Optional[str] = None,
|
|
validation_id: Optional[str] = None
|
|
) -> dict:
|
|
"""Lance une génération musicale auprès de Treblo.
|
|
Refuse si tags + paroles + prompt sont fournis ensemble, ou si plus de 64 tags,
|
|
ou si le plafond journalier est atteint, ou tant que la validation humaine n'a pas été confirmée par Telegram."""
|
|
titre_val = validate_titre(titre)
|
|
sp = validate_sphere_access(sphere)
|
|
check_daily_cap(sp)
|
|
|
|
tag_list = tags
|
|
if isinstance(tag_list, str):
|
|
tag_list = [t.strip() for t in tag_list.split(",") if t.strip()]
|
|
|
|
has_tags = bool(tag_list and len(tag_list) > 0)
|
|
has_paroles = bool(paroles and str(paroles).strip())
|
|
has_prompt = bool(prompt and str(prompt).strip())
|
|
|
|
if has_tags and len(tag_list) > 64:
|
|
raise ValueError("Refus : 64 étiquettes au plus")
|
|
|
|
if has_tags and has_paroles and has_prompt:
|
|
raise ValueError("Refus : impossible de fournir tags, paroles et prompt ensemble (choisir tags + paroles, ou prompt seul)")
|
|
|
|
if not has_tags and not has_prompt:
|
|
raise ValueError("Refus : un prompt ou des étiquettes (tags) doivent être fournis")
|
|
|
|
empreinte = compute_empreinte(sp, tag_list, prompt, paroles)
|
|
|
|
clean_val_id = str(validation_id).strip().lstrip("#") if validation_id else None
|
|
if clean_val_id:
|
|
v_path = get_validations_path()
|
|
if os.path.exists(v_path):
|
|
try:
|
|
with open(v_path, "r", encoding="utf-8") as f:
|
|
v_data = json.load(f)
|
|
for item in v_data.get("validations", []):
|
|
if item.get("id") == clean_val_id and item.get("status") == "APPROVED":
|
|
if item.get("sphere") != sp or item.get("empreinte") != empreinte:
|
|
raise ValueError(
|
|
f"Refus : l'approbation #{clean_val_id} ne correspond pas au contenu demandé "
|
|
f"(sphère ou empreinte différente). L'approbation reste intacte sans débit."
|
|
)
|
|
except ValueError:
|
|
raise
|
|
except Exception:
|
|
pass
|
|
|
|
if not consume_approved_validation(sp, empreinte, clean_val_id):
|
|
val_id = uuid.uuid4().hex[:8]
|
|
code = f"{secrets.randbelow(1000000):06d}"
|
|
now_tunis = datetime.datetime.now(TUNIS_TZ)
|
|
exp_dt = now_tunis + datetime.timedelta(minutes=15)
|
|
expires_iso = exp_dt.isoformat()
|
|
expires_str = exp_dt.strftime("%H:%M:%S")
|
|
|
|
send_telegram_validation_request(
|
|
sphere=sp,
|
|
val_id=val_id,
|
|
code=code,
|
|
tags=tag_list or [],
|
|
prompt=prompt or "",
|
|
paroles=paroles or "",
|
|
expires_str=expires_str
|
|
)
|
|
record_pending_validation(
|
|
sphere=sp,
|
|
val_id=val_id,
|
|
empreinte=empreinte,
|
|
code=code,
|
|
expires_iso=expires_iso,
|
|
details={"tags": tag_list or [], "prompt": prompt or "", "paroles": paroles or "", "titre": titre_val}
|
|
)
|
|
raise ValueError(
|
|
f"Refus : validation humaine requise par Telegram (demande non validée ou code non saisi). "
|
|
f"Une demande d'approbation #{val_id} a été envoyée à Nabil sur Telegram pour la sphère {sp} (expiration à {expires_str}). "
|
|
f"Veuillez demander le code de validation à Nabil et utiliser l'outil valider(val_id='{val_id}', code=...) avant de relancer generer."
|
|
)
|
|
|
|
bal_avant = treblo_balance(sp)
|
|
credits_avant = bal_avant["total"]
|
|
|
|
key = get_api_key(sp)
|
|
body: Dict[str, Any] = {"output_format": "flac"}
|
|
if has_tags:
|
|
body["tags"] = tag_list
|
|
if has_paroles:
|
|
body["lyrics"] = str(paroles).strip()
|
|
if has_prompt:
|
|
body["prompt"] = str(prompt).strip()
|
|
|
|
if not has_paroles and not has_prompt:
|
|
body["instrumental"] = True
|
|
|
|
url = "https://api.treblo.com/v1/generations/v3"
|
|
req = urllib.request.Request(
|
|
url,
|
|
data=json.dumps(body).encode("utf-8"),
|
|
headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"},
|
|
method="POST"
|
|
)
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=30) as resp:
|
|
resp_data = json.loads(resp.read().decode("utf-8"))
|
|
except urllib.error.HTTPError as e:
|
|
err_content = e.read().decode("utf-8", errors="replace")
|
|
raise RuntimeError(f"Erreur Treblo {e.code}: {err_content}")
|
|
except Exception as e:
|
|
raise RuntimeError(f"Erreur appel Treblo: {e}")
|
|
|
|
task_id = resp_data.get("task_id") or resp_data.get("id")
|
|
if not task_id:
|
|
raise RuntimeError(f"Treblo n'a pas renvoyé de task_id: {resp_data}")
|
|
|
|
statut_val = resp_data.get("status", "PENDING")
|
|
|
|
save_titre(task_id, titre_val)
|
|
|
|
entry = {
|
|
"date": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
|
"sphere": sp,
|
|
"titre": titre_val,
|
|
"tags": tag_list or [],
|
|
"prompt": prompt or "",
|
|
"paroles": paroles or "",
|
|
"empreinte": empreinte,
|
|
"task_id": task_id,
|
|
"statut": statut_val,
|
|
"credits_avant": credits_avant,
|
|
"credits_apres": credits_avant
|
|
}
|
|
append_journal(entry)
|
|
|
|
return {
|
|
"task_id": task_id,
|
|
"statut": statut_val,
|
|
"sphere": sp,
|
|
"titre": titre_val,
|
|
"tags": tag_list or [],
|
|
"credits_avant": credits_avant,
|
|
"credits_apres": credits_avant
|
|
}
|
|
|
|
@mcp.tool()
|
|
def statut(task_id: str) -> dict:
|
|
"""Consulte le statut d'une tâche de génération Treblo."""
|
|
auth_sp = current_sphere_var.get() or "nyora"
|
|
key = get_api_key(auth_sp)
|
|
url = f"https://api.treblo.com/v1/generations/status/{task_id}"
|
|
req = urllib.request.Request(url, headers={"Authorization": f"Bearer {key}"})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=20) as resp:
|
|
raw = resp.read().decode("utf-8")
|
|
data = json.loads(raw)
|
|
except urllib.error.HTTPError as e:
|
|
err_content = e.read().decode("utf-8", errors="replace")
|
|
raise RuntimeError(f"Erreur Treblo {e.code}: {err_content}")
|
|
except Exception as e:
|
|
raise RuntimeError(f"Erreur appel Treblo: {e}")
|
|
|
|
if isinstance(data, str):
|
|
st = data
|
|
res = {"status": st, "task_id": task_id}
|
|
elif isinstance(data, dict):
|
|
st = data.get("status")
|
|
res = data
|
|
else:
|
|
st = str(data)
|
|
res = {"status": st, "task_id": task_id}
|
|
|
|
if st == "SUCCESS":
|
|
try:
|
|
bal = treblo_balance(auth_sp)
|
|
update_journal_task(task_id, "SUCCESS", credits_apres=bal["total"])
|
|
except Exception as e:
|
|
logger.warning("Échec actualisation solde dans statut: %s", e)
|
|
update_journal_task(task_id, "SUCCESS")
|
|
elif st == "FAILURE":
|
|
update_journal_task(task_id, "FAILURE")
|
|
|
|
return res
|
|
|
|
@mcp.tool()
|
|
def recuperer(task_id: str, sphere: str) -> dict:
|
|
"""Télécharge les fichiers générés (FLAC ou WAV pour l'archive, MP3 pour l'écoute)
|
|
dans musique/<sphere>/ avec le fichier JSON de métadonnées associé."""
|
|
sp = validate_sphere_access(sphere)
|
|
key = get_api_key(sp)
|
|
url = f"https://api.treblo.com/v1/generations/{task_id}"
|
|
req = urllib.request.Request(url, headers={"Authorization": f"Bearer {key}"})
|
|
try:
|
|
with urllib.request.urlopen(req, timeout=30) as resp:
|
|
data = json.loads(resp.read().decode("utf-8"))
|
|
except urllib.error.HTTPError as e:
|
|
err_content = e.read().decode("utf-8", errors="replace")
|
|
raise RuntimeError(f"Erreur Treblo {e.code}: {err_content}")
|
|
except Exception as e:
|
|
raise RuntimeError(f"Erreur appel Treblo: {e}")
|
|
|
|
dest_dir = get_musique_dir(sp)
|
|
|
|
urls = []
|
|
raw_paths = data.get("song_paths") or data.get("audio_url") or []
|
|
if isinstance(raw_paths, str):
|
|
urls.append(raw_paths)
|
|
elif isinstance(raw_paths, list):
|
|
for item in raw_paths:
|
|
if isinstance(item, str):
|
|
urls.append(item)
|
|
elif isinstance(item, dict):
|
|
for v in item.values():
|
|
if isinstance(v, str) and v.startswith("http"):
|
|
urls.append(v)
|
|
elif isinstance(raw_paths, dict):
|
|
for v in raw_paths.values():
|
|
if isinstance(v, str) and v.startswith("http"):
|
|
urls.append(v)
|
|
|
|
for k, v in data.items():
|
|
if isinstance(v, str) and v.startswith("http") and v not in urls:
|
|
urls.append(v)
|
|
|
|
def detect_format(target_url: str, resp_headers=None) -> str:
|
|
parsed_path = urllib.parse.urlsplit(target_url).path.lower()
|
|
if ".flac" in parsed_path:
|
|
return "flac"
|
|
if ".mp3" in parsed_path:
|
|
return "mp3"
|
|
if ".wav" in parsed_path:
|
|
return "wav"
|
|
if ".ogg" in parsed_path:
|
|
return "ogg"
|
|
if ".m4a" in parsed_path:
|
|
return "m4a"
|
|
if resp_headers:
|
|
ct = resp_headers.get("content-type", "").lower()
|
|
if "flac" in ct:
|
|
return "flac"
|
|
if "mpeg" in ct or "mp3" in ct:
|
|
return "mp3"
|
|
if "wav" in ct:
|
|
return "wav"
|
|
return "bin"
|
|
|
|
downloaded = {}
|
|
for idx, u in enumerate(urls):
|
|
try:
|
|
req_dl = urllib.request.Request(u, headers={"User-Agent": "treblo-mcp/1.0"})
|
|
with urllib.request.urlopen(req_dl, timeout=60) as dl_resp:
|
|
fmt = detect_format(u, dl_resp.headers)
|
|
target_file = os.path.join(dest_dir, f"{task_id}.{fmt}")
|
|
with open(target_file, "wb") as out_f:
|
|
while True:
|
|
chunk = dl_resp.read(65536)
|
|
if not chunk:
|
|
break
|
|
out_f.write(chunk)
|
|
if os.path.exists(target_file) and os.path.getsize(target_file) > 0:
|
|
downloaded[fmt] = target_file
|
|
except Exception as e:
|
|
logger.warning("Échec téléchargement audio: %s", e)
|
|
|
|
flac_file = os.path.join(dest_dir, f"{task_id}.flac")
|
|
wav_file = os.path.join(dest_dir, f"{task_id}.wav")
|
|
mp3_file = os.path.join(dest_dir, f"{task_id}.mp3")
|
|
|
|
has_archive = (os.path.exists(flac_file) and os.path.getsize(flac_file) > 1000) or (os.path.exists(wav_file) and os.path.getsize(wav_file) > 1000)
|
|
has_mp3 = os.path.exists(mp3_file) and os.path.getsize(mp3_file) > 1000
|
|
|
|
if has_mp3 and not has_archive:
|
|
subprocess.run(["ffmpeg", "-y", "-i", mp3_file, flac_file], capture_output=True, check=True)
|
|
elif os.path.exists(flac_file) and not has_mp3:
|
|
subprocess.run(["ffmpeg", "-y", "-i", flac_file, "-b:a", "192k", mp3_file], capture_output=True, check=True)
|
|
elif os.path.exists(wav_file) and not has_mp3:
|
|
subprocess.run(["ffmpeg", "-y", "-i", wav_file, "-b:a", "192k", mp3_file], capture_output=True, check=True)
|
|
elif not has_archive and not has_mp3:
|
|
for ext, path in downloaded.items():
|
|
if os.path.exists(path) and os.path.getsize(path) > 1000:
|
|
subprocess.run(["ffmpeg", "-y", "-i", path, flac_file], capture_output=True, check=True)
|
|
subprocess.run(["ffmpeg", "-y", "-i", path, "-b:a", "192k", mp3_file], capture_output=True, check=True)
|
|
break
|
|
|
|
titre = get_titre_for_task(task_id)
|
|
entry = get_journal_entry(task_id) or {}
|
|
meta = {
|
|
"prompt": entry.get("prompt", data.get("prompt", "")),
|
|
"tags": entry.get("tags", data.get("tags", [])),
|
|
"paroles": entry.get("paroles", data.get("lyrics", "")),
|
|
"titre": entry.get("titre", titre),
|
|
"date": entry.get("date", datetime.datetime.now(datetime.timezone.utc).isoformat()),
|
|
"task_id": task_id
|
|
}
|
|
json_meta_path = os.path.join(dest_dir, f"{task_id}.json")
|
|
if not os.path.exists(json_meta_path):
|
|
with open(json_meta_path, "w", encoding="utf-8") as f:
|
|
json.dump(meta, f, indent=2, ensure_ascii=False)
|
|
|
|
credits_real = None
|
|
try:
|
|
bal = treblo_balance(sp)
|
|
credits_real = bal["total"]
|
|
except Exception as e:
|
|
logger.warning("Échec lecture solde post-tâche: %s", e)
|
|
|
|
update_journal_task(task_id, "SUCCESS", credits_apres=credits_real)
|
|
|
|
if os.path.exists(mp3_file):
|
|
send_telegram_preview_mp3(
|
|
sphere=sp,
|
|
mp3_path=mp3_file,
|
|
task_id=task_id,
|
|
titre=titre,
|
|
tags=meta.get("tags", []),
|
|
paroles=meta.get("paroles", "")
|
|
)
|
|
|
|
archive_target = flac_file if os.path.exists(flac_file) else (wav_file if os.path.exists(wav_file) else None)
|
|
files = [f for f in [json_meta_path, mp3_file, archive_target] if f and os.path.exists(f)]
|
|
|
|
return {
|
|
"status": "ok",
|
|
"task_id": task_id,
|
|
"sphere": sp,
|
|
"fichiers": [os.path.basename(f) for f in files],
|
|
"fichiers_taille": {os.path.basename(f): os.path.getsize(f) for f in files}
|
|
}
|
|
|
|
session_manager = StreamableHTTPSessionManager(mcp._mcp_server, stateless=True, json_response=True)
|
|
|
|
class AuthAndSessionMiddleware(BaseHTTPMiddleware):
|
|
async def dispatch(self, request, call_next):
|
|
if request.url.path == "/health":
|
|
return await call_next(request)
|
|
|
|
auth = request.headers.get("authorization", "").strip()
|
|
if not auth.startswith("Bearer "):
|
|
logger.warning("Auth refusée : header absent ou non-Bearer (chemin=%s)", request.url.path)
|
|
return JSONResponse({"error": "jeton manquant ou invalide"}, status_code=401)
|
|
|
|
token = auth[len("Bearer "):].strip()
|
|
tok_nyora = os.environ.get("TREBLO_MCP_TOKEN_NYORA", "").strip()
|
|
tok_perso = os.environ.get("TREBLO_MCP_TOKEN_PERSO", "").strip()
|
|
|
|
auth_sphere = None
|
|
if tok_nyora and hmac.compare_digest(token, tok_nyora):
|
|
auth_sphere = "nyora"
|
|
elif tok_perso and hmac.compare_digest(token, tok_perso):
|
|
auth_sphere = "perso"
|
|
else:
|
|
logger.warning("Auth refusée : jeton invalide (chemin=%s)", request.url.path)
|
|
return JSONResponse({"error": "jeton invalide"}, status_code=401)
|
|
|
|
token_cv = current_sphere_var.set(auth_sphere)
|
|
try:
|
|
response = await call_next(request)
|
|
if "mcp-session-id" not in response.headers:
|
|
req_sid = request.headers.get("mcp-session-id")
|
|
response.headers["mcp-session-id"] = req_sid or str(uuid.uuid4())
|
|
return response
|
|
finally:
|
|
current_sphere_var.reset(token_cv)
|
|
|
|
async def health(request):
|
|
return JSONResponse({"status": "ok", "service": "treblo-mcp", "tools": 5})
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app_instance):
|
|
global session_manager
|
|
if getattr(session_manager, "_has_started", False):
|
|
session_manager = StreamableHTTPSessionManager(mcp._mcp_server, stateless=True, json_response=True)
|
|
async with session_manager.run():
|
|
yield
|
|
|
|
app = Starlette(
|
|
routes=[
|
|
Route("/health", health),
|
|
Mount("/mcp", app=session_manager.handle_request),
|
|
Mount("/", app=session_manager.handle_request),
|
|
],
|
|
lifespan=lifespan
|
|
)
|
|
app.add_middleware(AuthAndSessionMiddleware)
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(app, host="0.0.0.0", port=8000)
|