Files
SyspanelMultiAgentsAI/context_manager.py
T
marcocosta10000 67e8df36ce feat: add complete multi-agent AI system with Telegram bot
Initial implementation of MultiAgentsAI featuring:
- Dynamic agent configuration via environment variables
- SQLite database and JSON daily context storage
- OpenRouter API integration for multiple AI models
- Telegram bot with commands for control, history, and interaction
- Autonomous conversation loop with pause/resume and agent silencing
- Model testing and statistics commands
2026-10-17 19:25:05 -03:00

52 lines
1.6 KiB
Python
Executable File

# Gerado por IA
import os
from datetime import datetime
from database import get_today_messages, save_message
from config import settings
def get_daily_context_path() -> str:
today = datetime.utcnow().strftime("%Y-%m-%d")
os.makedirs(settings.CONTEXTS_DIR, exist_ok=True)
return os.path.join(settings.CONTEXTS_DIR, f"conversas_{today}.json")
def load_daily_context() -> list[dict]:
"""Carrega o contexto do dia. SQLite e a fonte da verdade; JSON e um cache sincronizado."""
# Fonte primaria: SQLite
context = get_today_messages()
if context:
return context
# Fallback: JSON (caso SQLite esteja vazio)
path = get_daily_context_path()
if os.path.exists(path):
try:
import json
with open(path, "r", encoding="utf-8") as f:
return json.load(f)
except Exception:
return []
return []
def append_to_context(agent_name: str, message: str) -> None:
path = get_daily_context_path()
context = load_daily_context()
timestamp = datetime.utcnow().isoformat()
entry = {"agent_name": agent_name, "message": message, "created_at": timestamp}
context.append(entry)
import json
with open(path, "w", encoding="utf-8") as f:
json.dump(context, f, ensure_ascii=False, indent=2)
save_message(agent_name, message)
def get_context_summary(max_messages: int = 50) -> str:
context = load_daily_context()
if not context:
return "Nenhuma conversa registrada para hoje ainda."
lines = [f"[{c['created_at']}] {c['agent_name']}: {c['message']}" for c in context[-max_messages:]]
return "\n".join(lines)