Spaces:
Running
Running
Update modules/database.py
Browse files- modules/database.py +226 -389
modules/database.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
# modules/database.py
|
| 2 |
"""
|
| 3 |
Banco de dados SQLite para Akira IA.
|
| 4 |
- Tabelas: mensagens, girias, tom, pronomes, embeddings, etc.
|
|
@@ -7,28 +6,25 @@ Banco de dados SQLite para Akira IA.
|
|
| 7 |
- Suporte a pronomes por tom (formal, rude, casual)
|
| 8 |
- WAL + retry para concorrência
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
import sqlite3
|
| 12 |
import json
|
| 13 |
import time
|
| 14 |
-
import
|
| 15 |
from typing import Optional, List, Dict, Any, Tuple
|
| 16 |
-
|
| 17 |
-
logger = logging.getLogger(__name__)
|
| 18 |
-
|
| 19 |
|
| 20 |
class Database:
|
| 21 |
def __init__(self, db_path: str):
|
| 22 |
self.db_path = db_path
|
| 23 |
self.max_retries = 5
|
| 24 |
self.retry_delay = 0.1
|
|
|
|
| 25 |
self._init_db()
|
| 26 |
self._ensure_all_columns_and_indexes()
|
| 27 |
|
| 28 |
# ================================================================
|
| 29 |
# CONEXÃO COM RETRY + WAL
|
| 30 |
# ================================================================
|
| 31 |
-
|
| 32 |
def _get_connection(self) -> sqlite3.Connection:
|
| 33 |
for attempt in range(self.max_retries):
|
| 34 |
try:
|
|
@@ -44,8 +40,9 @@ class Database:
|
|
| 44 |
if "database is locked" in str(e) and attempt < self.max_retries - 1:
|
| 45 |
time.sleep(self.retry_delay * (2 ** attempt))
|
| 46 |
continue
|
|
|
|
| 47 |
raise
|
| 48 |
-
raise sqlite3.OperationalError("
|
| 49 |
|
| 50 |
def _execute_with_retry(self, query: str, params: Optional[tuple] = None, commit: bool = False) -> Optional[List[Tuple]]:
|
| 51 |
for attempt in range(self.max_retries):
|
|
@@ -64,314 +61,239 @@ class Database:
|
|
| 64 |
if "database is locked" in str(e) and attempt < self.max_retries - 1:
|
| 65 |
time.sleep(self.retry_delay * (2 ** attempt))
|
| 66 |
continue
|
|
|
|
| 67 |
raise
|
| 68 |
-
raise sqlite3.OperationalError("Query
|
| 69 |
|
| 70 |
# ================================================================
|
| 71 |
# INICIALIZAÇÃO + MIGRAÇÃO AUTOMÁTICA
|
| 72 |
# ================================================================
|
| 73 |
-
|
| 74 |
def _init_db(self):
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
def _ensure_all_columns_and_indexes(self):
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
]
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
c.execute(f"ALTER TABLE {table} ADD COLUMN {col_name} {col_def}")
|
| 255 |
-
logger.info(f"Coluna '{col_name}' adicionada em '{table}'")
|
| 256 |
-
except Exception as e:
|
| 257 |
-
logger.warning(f"Erro ao adicionar coluna {col_name}: {e}")
|
| 258 |
-
|
| 259 |
-
# --- ÍNDICES PARA PERFORMANCE ---
|
| 260 |
-
indexes = [
|
| 261 |
-
"CREATE INDEX IF NOT EXISTS idx_mensagens_numero ON mensagens(numero);",
|
| 262 |
-
"CREATE INDEX IF NOT EXISTS idx_mensagens_created ON mensagens(created_at DESC);",
|
| 263 |
-
"CREATE INDEX IF NOT EXISTS idx_girias_usuario ON girias_aprendidas(numero_usuario);",
|
| 264 |
-
"CREATE INDEX IF NOT EXISTS idx_girias_giria ON girias_aprendidas(giria);",
|
| 265 |
-
"CREATE INDEX IF NOT EXISTS idx_tom_usuario ON tom_usuario(numero_usuario);",
|
| 266 |
-
"CREATE INDEX IF NOT EXISTS idx_aprendizados_usuario ON aprendizados(numero_usuario);",
|
| 267 |
-
"CREATE INDEX IF NOT EXISTS idx_embeddings_texto ON embeddings(texto);",
|
| 268 |
-
"CREATE INDEX IF NOT EXISTS idx_pronomes_tom ON pronomes_por_tom(tom);"
|
| 269 |
-
]
|
| 270 |
-
|
| 271 |
-
for idx in indexes:
|
| 272 |
-
try:
|
| 273 |
-
c.execute(idx)
|
| 274 |
-
except:
|
| 275 |
-
pass
|
| 276 |
-
|
| 277 |
-
conn.commit()
|
| 278 |
|
| 279 |
# ================================================================
|
| 280 |
-
# MÉTODOS PRINCIPAIS
|
| 281 |
# ================================================================
|
|
|
|
| 282 |
|
| 283 |
-
#
|
| 284 |
-
def inserir_exemplo(self, tipo, entrada, resposta):
|
| 285 |
-
self._execute_with_retry("INSERT INTO exemplos (tipo, entrada, resposta) VALUES (?, ?, ?)", (tipo, entrada, resposta), commit=True)
|
| 286 |
-
|
| 287 |
-
def recuperar_exemplos(self, tipo=None):
|
| 288 |
-
if tipo:
|
| 289 |
-
return self._execute_with_retry("SELECT entrada, resposta FROM exemplos WHERE tipo=? ORDER BY id DESC", (tipo,))
|
| 290 |
-
return self._execute_with_retry("SELECT entrada, resposta FROM exemplos ORDER BY id DESC")
|
| 291 |
-
|
| 292 |
-
# --- INFO_GERAL ---
|
| 293 |
-
def salvar_info_geral(self, chave, valor):
|
| 294 |
-
self._execute_with_retry("INSERT OR REPLACE INTO info_geral (chave, valor) VALUES (?, ?)", (chave, valor), commit=True)
|
| 295 |
-
|
| 296 |
-
def recuperar_info_geral(self, chave):
|
| 297 |
-
result = self._execute_with_retry("SELECT valor FROM info_geral WHERE chave=?", (chave,))
|
| 298 |
-
return result[0][0] if result else None
|
| 299 |
-
|
| 300 |
-
# --- ESTILOS ---
|
| 301 |
-
def salvar_estilo(self, numero_usuario, estilo):
|
| 302 |
-
self._execute_with_retry("INSERT OR REPLACE INTO estilos (numero_usuario, estilo) VALUES (?, ?)", (numero_usuario, estilo), commit=True)
|
| 303 |
-
|
| 304 |
-
def recuperar_estilo(self, numero_usuario):
|
| 305 |
-
result = self._execute_with_retry("SELECT estilo FROM estilos WHERE numero_usuario=?", (numero_usuario,))
|
| 306 |
-
return result[0][0] if result else None
|
| 307 |
-
|
| 308 |
-
# --- PREFERENCIAS_TOM ---
|
| 309 |
-
def salvar_preferencia_tom(self, numero_usuario, tom):
|
| 310 |
-
self._execute_with_retry("INSERT OR REPLACE INTO preferencias_tom (numero_usuario, tom) VALUES (?, ?)", (numero_usuario, tom), commit=True)
|
| 311 |
-
|
| 312 |
-
def recuperar_preferencia_tom(self, numero_usuario):
|
| 313 |
-
result = self._execute_with_retry("SELECT tom FROM preferencias_tom WHERE numero_usuario=?", (numero_usuario,))
|
| 314 |
-
return result[0][0] if result else None
|
| 315 |
-
|
| 316 |
-
# --- AFINIDADES ---
|
| 317 |
-
def salvar_afinidade(self, numero_usuario, afinidade):
|
| 318 |
-
self._execute_with_retry("INSERT OR REPLACE INTO afinidades (numero_usuario, afinidade) VALUES (?, ?)", (numero_usuario, afinidade), commit=True)
|
| 319 |
-
|
| 320 |
-
def recuperar_afinidade(self, numero_usuario):
|
| 321 |
-
result = self._execute_with_retry("SELECT afinidade FROM afinidades WHERE numero_usuario=?", (numero_usuario,))
|
| 322 |
-
return result[0][0] if result else None
|
| 323 |
-
|
| 324 |
-
# --- TERMOS ---
|
| 325 |
-
def registrar_termo(self, numero_usuario, termo):
|
| 326 |
-
self._execute_with_retry("INSERT INTO termos (numero_usuario, termo) VALUES (?, ?)", (numero_usuario, termo), commit=True)
|
| 327 |
-
|
| 328 |
-
def recuperar_termos(self, numero_usuario):
|
| 329 |
-
result = self._execute_with_retry("SELECT termo FROM termos WHERE numero_usuario=? ORDER BY created_at DESC", (numero_usuario,))
|
| 330 |
-
return [row[0] for row in result] if result else []
|
| 331 |
-
|
| 332 |
-
# --- APRENDIZADOS DETALHADOS ---
|
| 333 |
-
def salvar_aprendizado_detalhado(self, numero_usuario, chave, valor):
|
| 334 |
-
self._execute_with_retry("INSERT INTO aprendizados (numero_usuario, chave, valor) VALUES (?, ?, ?)", (numero_usuario, chave, valor), commit=True)
|
| 335 |
-
|
| 336 |
-
def recuperar_aprendizado_detalhado(self, numero_usuario, chave=None):
|
| 337 |
-
if chave:
|
| 338 |
-
result = self._execute_with_retry("SELECT valor FROM aprendizados WHERE numero_usuario=? AND chave=? ORDER BY created_at DESC LIMIT 1", (numero_usuario, chave))
|
| 339 |
-
return result[0][0] if result else None
|
| 340 |
-
return self._execute_with_retry("SELECT chave, valor FROM aprendizados WHERE numero_usuario=? ORDER BY created_at DESC", (numero_usuario,))
|
| 341 |
-
|
| 342 |
-
# --- VOCABULÁRIO PATENTEADO ---
|
| 343 |
-
def registrar_vocabulario_patenteado(self, termo, definicao, uso, exemplo):
|
| 344 |
-
self._execute_with_retry("INSERT OR REPLACE INTO vocabulario_patenteado (termo, definicao, uso, exemplo) VALUES (?, ?, ?, ?)", (termo, definicao, uso, exemplo), commit=True)
|
| 345 |
-
|
| 346 |
-
def recuperar_vocabulario_patenteado(self, termo):
|
| 347 |
-
result = self._execute_with_retry("SELECT definicao, uso, exemplo FROM vocabulario_patenteado WHERE termo=?", (termo,))
|
| 348 |
-
return result[0] if result else None
|
| 349 |
-
|
| 350 |
-
# --- USUÁRIOS PRIVILEGIADOS ---
|
| 351 |
-
def adicionar_usuario_privilegiado(self, numero_usuario, nome):
|
| 352 |
-
self._execute_with_retry("INSERT OR REPLACE INTO usuarios_privilegiados (numero_usuario, nome) VALUES (?, ?)", (numero_usuario, nome), commit=True)
|
| 353 |
-
|
| 354 |
-
def verificar_usuario_privilegiado(self, numero_usuario):
|
| 355 |
-
result = self._execute_with_retry("SELECT nome FROM usuarios_privilegiados WHERE numero_usuario=?", (numero_usuario,))
|
| 356 |
-
return result[0][0] if result else None
|
| 357 |
-
|
| 358 |
-
# --- WHATSAPP IDS ---
|
| 359 |
-
def registrar_whatsapp_id(self, whatsapp_id, sender_number):
|
| 360 |
-
self._execute_with_retry("INSERT OR IGNORE INTO whatsapp_ids (whatsapp_id, sender_number) VALUES (?, ?)", (whatsapp_id, sender_number), commit=True)
|
| 361 |
-
|
| 362 |
-
def recuperar_whatsapp_ids(self, sender_number):
|
| 363 |
-
result = self._execute_with_retry("SELECT whatsapp_id FROM whatsapp_ids WHERE sender_number=?", (sender_number,))
|
| 364 |
-
return [row[0] for row in result] if result else []
|
| 365 |
-
|
| 366 |
-
# --- EMBEDDINGS ---
|
| 367 |
-
def salvar_embedding(self, texto, embedding_bytes):
|
| 368 |
-
self._execute_with_retry("INSERT INTO embeddings (texto, embedding) VALUES (?, ?)", (texto, embedding_bytes), commit=True)
|
| 369 |
-
|
| 370 |
-
def recuperar_embedding(self, texto):
|
| 371 |
-
result = self._execute_with_retry("SELECT embedding FROM embeddings WHERE texto=? ORDER BY id DESC LIMIT 1", (texto,))
|
| 372 |
-
return result[0][0] if result else None
|
| 373 |
-
|
| 374 |
-
# --- MENSAGENS ---
|
| 375 |
def salvar_mensagem(self, usuario, mensagem, resposta, numero=None, is_reply=False, mensagem_original=None):
|
| 376 |
try:
|
| 377 |
cols = ['usuario', 'mensagem', 'resposta']
|
|
@@ -379,7 +301,6 @@ class Database:
|
|
| 379 |
if numero: cols.append('numero'); vals.append(numero)
|
| 380 |
if is_reply is not None: cols.append('is_reply'); vals.append(int(is_reply))
|
| 381 |
if mensagem_original: cols.append('mensagem_original'); vals.append(mensagem_original)
|
| 382 |
-
|
| 383 |
placeholders = ', '.join(['?' for _ in cols])
|
| 384 |
query = f"INSERT INTO mensagens ({', '.join(cols)}) VALUES ({placeholders})"
|
| 385 |
self._execute_with_retry(query, tuple(vals), commit=True)
|
|
@@ -387,96 +308,12 @@ class Database:
|
|
| 387 |
logger.warning(f"Fallback salvar_mensagem: {e}")
|
| 388 |
self._execute_with_retry("INSERT INTO mensagens (usuario, mensagem, resposta) VALUES (?, ?, ?)", (usuario, mensagem, resposta), commit=True)
|
| 389 |
|
| 390 |
-
|
| 391 |
-
return self._execute_with_retry("SELECT mensagem, resposta FROM mensagens WHERE usuario=? OR numero=? ORDER BY id DESC LIMIT ?", (usuario, usuario, limite))
|
| 392 |
-
|
| 393 |
-
# --- APRENDIZADO SIMPLES ---
|
| 394 |
-
def salvar_aprendizado(self, usuario, dado, valor):
|
| 395 |
-
self._execute_with_retry("INSERT INTO aprendizado (usuario, dado, valor) VALUES (?, ?, ?)", (usuario, dado, valor), commit=True)
|
| 396 |
-
|
| 397 |
-
def recuperar_aprendizado(self, usuario, dado):
|
| 398 |
-
result = self._execute_with_retry("SELECT valor FROM aprendizado WHERE usuario=? AND dado=? ORDER BY id DESC LIMIT 1", (usuario, dado))
|
| 399 |
-
return result[0][0] if result else None
|
| 400 |
-
|
| 401 |
-
# --- EMOÇÃO EXEMPLOS ---
|
| 402 |
-
def salvar_exemplo_emocao(self, emocao: str, entrada: str, resposta: str, tom: str):
|
| 403 |
-
self._execute_with_retry("INSERT INTO emocao_exemplos (emocao, entrada, resposta, tom) VALUES (?, ?, ?, ?)", (emocao, entrada, resposta, tom), commit=True)
|
| 404 |
-
|
| 405 |
-
def recuperar_exemplos_emocao(self, emocao: str) -> List[Dict[str, Any]]:
|
| 406 |
-
result = self._execute_with_retry("SELECT entrada, resposta, tom FROM emocao_exemplos WHERE emocao=? ORDER BY created_at DESC", (emocao,))
|
| 407 |
-
return [{"entrada": r[0], "resposta": r[1], "tom": r[2]} for r in result] if result else []
|
| 408 |
-
|
| 409 |
-
# --- GIRIAS APRENDIDAS ---
|
| 410 |
-
def salvar_giria_aprendida(self, numero_usuario: str, giria: str, significado: str, contexto: Optional[str] = None):
|
| 411 |
-
existing = self._execute_with_retry("SELECT id, frequencia FROM girias_aprendidas WHERE numero_usuario=? AND giria=?", (numero_usuario, giria))
|
| 412 |
-
if existing:
|
| 413 |
-
self._execute_with_retry("UPDATE girias_aprendidas SET frequencia=frequencia+1, updated_at=CURRENT_TIMESTAMP WHERE id=?", (existing[0][0],), commit=True)
|
| 414 |
-
else:
|
| 415 |
-
self._execute_with_retry("INSERT INTO girias_aprendidas (numero_usuario, giria, significado, contexto) VALUES (?, ?, ?, ?)", (numero_usuario, giria, significado, contexto), commit=True)
|
| 416 |
-
|
| 417 |
-
def recuperar_girias_usuario(self, numero_usuario: str) -> List[Dict[str, Any]]:
|
| 418 |
-
result = self._execute_with_retry("SELECT giria, significado, contexto, frequencia FROM girias_aprendidas WHERE numero_usuario=? ORDER BY frequencia DESC, updated_at DESC", (numero_usuario,))
|
| 419 |
-
return [{"giria": r[0], "significado": r[1], "contexto": r[2], "frequencia": r[3]} for r in result] if result else []
|
| 420 |
-
|
| 421 |
-
def buscar_significado_giria(self, numero_usuario: str, giria: str) -> Optional[str]:
|
| 422 |
-
result = self._execute_with_retry("SELECT significado FROM girias_aprendidas WHERE numero_usuario=? AND giria=?", (numero_usuario, giria))
|
| 423 |
-
return result[0][0] if result else None
|
| 424 |
-
|
| 425 |
-
# --- TOM USUÁRIO ---
|
| 426 |
-
def registrar_tom_usuario(self, numero_usuario: str, tom_detectado: str, intensidade: float = 0.5, contexto: Optional[str] = None):
|
| 427 |
-
self._execute_with_retry("INSERT INTO tom_usuario (numero_usuario, tom_detectado, intensidade, contexto) VALUES (?, ?, ?, ?)", (numero_usuario, tom_detectado, intensidade, contexto), commit=True)
|
| 428 |
-
|
| 429 |
-
def obter_tom_predominante(self, numero_usuario: str, limite: int = 10) -> Optional[str]:
|
| 430 |
-
result = self._execute_with_retry("""
|
| 431 |
-
SELECT tom_detectado, COUNT(*) FROM tom_usuario
|
| 432 |
-
WHERE numero_usuario=? GROUP BY tom_detectado
|
| 433 |
-
ORDER BY COUNT(*) DESC LIMIT 1
|
| 434 |
-
""", (numero_usuario,))
|
| 435 |
-
return result[0][0] if result else None
|
| 436 |
-
|
| 437 |
-
# --- ADAPTAÇÃO DINÂMICA ---
|
| 438 |
-
def registrar_adaptacao(self, numero_usuario: str, tipo_adaptacao: str, valor_anterior: str, valor_novo: str, razao: str):
|
| 439 |
-
self._execute_with_retry("INSERT INTO adaptacao_dinamica (numero_usuario, tipo_adaptacao, valor_anterior, valor_novo, razao) VALUES (?, ?, ?, ?, ?)", (numero_usuario, tipo_adaptacao, valor_anterior, valor_novo, razao), commit=True)
|
| 440 |
-
|
| 441 |
-
def obter_adaptacoes_usuario(self, numero_usuario: str, tipo_adaptacao: Optional[str] = None) -> List[Dict[str, Any]]:
|
| 442 |
-
if tipo_adaptacao:
|
| 443 |
-
result = self._execute_with_retry("SELECT tipo_adaptacao, valor_anterior, valor_novo, razao, created_at FROM adaptacao_dinamica WHERE numero_usuario=? AND tipo_adaptacao=? ORDER BY created_at DESC", (numero_usuario, tipo_adaptacao))
|
| 444 |
-
else:
|
| 445 |
-
result = self._execute_with_retry("SELECT tipo_adaptacao, valor_anterior, valor_novo, razao, created_at FROM adaptacao_dinamica WHERE numero_usuario=? ORDER BY created_at DESC", (numero_usuario,))
|
| 446 |
-
return [{"tipo": r[0], "anterior": r[1], "novo": r[2], "razao": r[3], "data": r[4]} for r in result] if result else []
|
| 447 |
-
|
| 448 |
-
# --- ANÁLISE DE EMOÇÕES ---
|
| 449 |
-
def analisar_emocoes_mensagem(self, mensagem: str) -> Dict[str, Any]:
|
| 450 |
-
msg = mensagem.lower()
|
| 451 |
-
keywords = {
|
| 452 |
-
"feliz": ["feliz", "alegre", "contente", "ótimo", "bom", "incrível", "show", "top", "fixe", "bué"],
|
| 453 |
-
"triste": ["triste", "chateado", "ruim", "péssimo", "horrível", "deprimido"],
|
| 454 |
-
"raiva": ["raiva", "puto", "irritado", "furioso", "nervoso", "merda", "caralho"],
|
| 455 |
-
"medo": ["medo", "assustado", "preocupado", "ansioso"],
|
| 456 |
-
"surpresa": ["uau", "caramba", "incrível", "surpreso"],
|
| 457 |
-
"nojo": ["nojo", "repugnante", "detesto", "odeio"],
|
| 458 |
-
"amor": ["amo", "adoro", "gosto", "apaixonado"]
|
| 459 |
-
}
|
| 460 |
-
max_count = 0
|
| 461 |
-
emocao = "neutro"
|
| 462 |
-
for e, words in keywords.items():
|
| 463 |
-
count = sum(1 for w in words if w in msg)
|
| 464 |
-
if count > max_count:
|
| 465 |
-
max_count = count
|
| 466 |
-
emocao = e
|
| 467 |
-
return {
|
| 468 |
-
"emocao": emocao,
|
| 469 |
-
"intensidade": min(max_count / 3.0, 1.0),
|
| 470 |
-
"palavras_chave_encontradas": max_count
|
| 471 |
-
}
|
| 472 |
|
| 473 |
-
# --- PRONOMES POR TOM (NOVA FUNÇÃO) ---
|
| 474 |
def obter_pronomes_por_tom(self, tom: str) -> str:
|
| 475 |
-
"""Retorna string com pronomes para o tom (ex: 'Sr., boss')"""
|
| 476 |
result = self._execute_with_retry("SELECT pronomes FROM pronomes_por_tom WHERE tom=?", (tom.lower(),))
|
| 477 |
return result[0][0] if result else ""
|
| 478 |
|
| 479 |
-
# --- CONTEXTO COMPLETO ---
|
| 480 |
def obter_contexto_aprendizado(self, numero_usuario: str) -> Dict[str, Any]:
|
| 481 |
return {
|
| 482 |
"girias": self.recuperar_girias_usuario(numero_usuario),
|
|
|
|
|
|
|
| 1 |
"""
|
| 2 |
Banco de dados SQLite para Akira IA.
|
| 3 |
- Tabelas: mensagens, girias, tom, pronomes, embeddings, etc.
|
|
|
|
| 6 |
- Suporte a pronomes por tom (formal, rude, casual)
|
| 7 |
- WAL + retry para concorrência
|
| 8 |
"""
|
|
|
|
| 9 |
import sqlite3
|
| 10 |
import json
|
| 11 |
import time
|
| 12 |
+
import os
|
| 13 |
from typing import Optional, List, Dict, Any, Tuple
|
| 14 |
+
from loguru import logger # <-- LOGURU (FUNCIONA NO HF)
|
|
|
|
|
|
|
| 15 |
|
| 16 |
class Database:
|
| 17 |
def __init__(self, db_path: str):
|
| 18 |
self.db_path = db_path
|
| 19 |
self.max_retries = 5
|
| 20 |
self.retry_delay = 0.1
|
| 21 |
+
os.makedirs(os.path.dirname(db_path), exist_ok=True)
|
| 22 |
self._init_db()
|
| 23 |
self._ensure_all_columns_and_indexes()
|
| 24 |
|
| 25 |
# ================================================================
|
| 26 |
# CONEXÃO COM RETRY + WAL
|
| 27 |
# ================================================================
|
|
|
|
| 28 |
def _get_connection(self) -> sqlite3.Connection:
|
| 29 |
for attempt in range(self.max_retries):
|
| 30 |
try:
|
|
|
|
| 40 |
if "database is locked" in str(e) and attempt < self.max_retries - 1:
|
| 41 |
time.sleep(self.retry_delay * (2 ** attempt))
|
| 42 |
continue
|
| 43 |
+
logger.error(f"Falha ao conectar ao banco: {e}")
|
| 44 |
raise
|
| 45 |
+
raise sqlite3.OperationalError("Falha ao conectar após retries")
|
| 46 |
|
| 47 |
def _execute_with_retry(self, query: str, params: Optional[tuple] = None, commit: bool = False) -> Optional[List[Tuple]]:
|
| 48 |
for attempt in range(self.max_retries):
|
|
|
|
| 61 |
if "database is locked" in str(e) and attempt < self.max_retries - 1:
|
| 62 |
time.sleep(self.retry_delay * (2 ** attempt))
|
| 63 |
continue
|
| 64 |
+
logger.error(f"Erro SQL (tentativa {attempt+1}): {e}")
|
| 65 |
raise
|
| 66 |
+
raise sqlite3.OperationalError("Query falhou após retries")
|
| 67 |
|
| 68 |
# ================================================================
|
| 69 |
# INICIALIZAÇÃO + MIGRAÇÃO AUTOMÁTICA
|
| 70 |
# ================================================================
|
|
|
|
| 71 |
def _init_db(self):
|
| 72 |
+
try:
|
| 73 |
+
with self._get_connection() as conn:
|
| 74 |
+
c = conn.cursor()
|
| 75 |
+
c.executescript('''
|
| 76 |
+
-- aprendizado (simples)
|
| 77 |
+
CREATE TABLE IF NOT EXISTS aprendizado (
|
| 78 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 79 |
+
usuario TEXT,
|
| 80 |
+
dado TEXT,
|
| 81 |
+
valor TEXT
|
| 82 |
+
);
|
| 83 |
+
|
| 84 |
+
-- exemplos
|
| 85 |
+
CREATE TABLE IF NOT EXISTS exemplos (
|
| 86 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 87 |
+
tipo TEXT NOT NULL,
|
| 88 |
+
entrada TEXT NOT NULL,
|
| 89 |
+
resposta TEXT NOT NULL
|
| 90 |
+
);
|
| 91 |
+
|
| 92 |
+
-- info_geral
|
| 93 |
+
CREATE TABLE IF NOT EXISTS info_geral (
|
| 94 |
+
chave TEXT PRIMARY KEY,
|
| 95 |
+
valor TEXT NOT NULL
|
| 96 |
+
);
|
| 97 |
+
|
| 98 |
+
-- estilos
|
| 99 |
+
CREATE TABLE IF NOT EXISTS estilos (
|
| 100 |
+
numero_usuario TEXT PRIMARY KEY,
|
| 101 |
+
estilo TEXT NOT NULL
|
| 102 |
+
);
|
| 103 |
+
|
| 104 |
+
-- preferencias_tom
|
| 105 |
+
CREATE TABLE IF NOT EXISTS preferencias_tom (
|
| 106 |
+
numero_usuario TEXT PRIMARY KEY,
|
| 107 |
+
tom TEXT NOT NULL
|
| 108 |
+
);
|
| 109 |
+
|
| 110 |
+
-- afinidades
|
| 111 |
+
CREATE TABLE IF NOT EXISTS afinidades (
|
| 112 |
+
numero_usuario TEXT PRIMARY KEY,
|
| 113 |
+
afinidade REAL NOT NULL
|
| 114 |
+
);
|
| 115 |
+
|
| 116 |
+
-- termos
|
| 117 |
+
CREATE TABLE IF NOT EXISTS termos (
|
| 118 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 119 |
+
numero_usuario TEXT NOT NULL,
|
| 120 |
+
termo TEXT NOT NULL,
|
| 121 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 122 |
+
);
|
| 123 |
+
|
| 124 |
+
-- aprendizados (detalhado)
|
| 125 |
+
CREATE TABLE IF NOT EXISTS aprendizados (
|
| 126 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 127 |
+
numero_usuario TEXT NOT NULL,
|
| 128 |
+
chave TEXT NOT NULL,
|
| 129 |
+
valor TEXT NOT NULL,
|
| 130 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 131 |
+
);
|
| 132 |
+
|
| 133 |
+
-- vocabulário patenteado
|
| 134 |
+
CREATE TABLE IF NOT EXISTS vocabulario_patenteado (
|
| 135 |
+
termo TEXT PRIMARY KEY,
|
| 136 |
+
definicao TEXT NOT NULL,
|
| 137 |
+
uso TEXT NOT NULL,
|
| 138 |
+
exemplo TEXT NOT NULL
|
| 139 |
+
);
|
| 140 |
+
|
| 141 |
+
-- usuarios privilegiados
|
| 142 |
+
CREATE TABLE IF NOT EXISTS usuarios_privilegiados (
|
| 143 |
+
numero_usuario TEXT PRIMARY KEY,
|
| 144 |
+
nome TEXT NOT NULL
|
| 145 |
+
);
|
| 146 |
+
|
| 147 |
+
-- whatsapp_ids
|
| 148 |
+
CREATE TABLE IF NOT EXISTS whatsapp_ids (
|
| 149 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 150 |
+
whatsapp_id TEXT NOT NULL,
|
| 151 |
+
sender_number TEXT NOT NULL,
|
| 152 |
+
UNIQUE (whatsapp_id, sender_number)
|
| 153 |
+
);
|
| 154 |
+
|
| 155 |
+
-- embeddings
|
| 156 |
+
CREATE TABLE IF NOT EXISTS embeddings (
|
| 157 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 158 |
+
texto TEXT NOT NULL,
|
| 159 |
+
embedding BLOB NOT NULL
|
| 160 |
+
);
|
| 161 |
+
|
| 162 |
+
-- mensagens
|
| 163 |
+
CREATE TABLE IF NOT EXISTS mensagens (
|
| 164 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 165 |
+
usuario TEXT NOT NULL,
|
| 166 |
+
mensagem TEXT NOT NULL,
|
| 167 |
+
resposta TEXT NOT NULL,
|
| 168 |
+
numero TEXT,
|
| 169 |
+
is_reply BOOLEAN DEFAULT 0,
|
| 170 |
+
mensagem_original TEXT,
|
| 171 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 172 |
+
);
|
| 173 |
+
|
| 174 |
+
-- emocao_exemplos
|
| 175 |
+
CREATE TABLE IF NOT EXISTS emocao_exemplos (
|
| 176 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 177 |
+
emocao TEXT NOT NULL,
|
| 178 |
+
entrada TEXT NOT NULL,
|
| 179 |
+
resposta TEXT NOT NULL,
|
| 180 |
+
tom TEXT NOT NULL,
|
| 181 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 182 |
+
);
|
| 183 |
+
|
| 184 |
+
-- girias_aprendidas
|
| 185 |
+
CREATE TABLE IF NOT EXISTS girias_aprendidas (
|
| 186 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 187 |
+
numero_usuario TEXT NOT NULL,
|
| 188 |
+
giria TEXT NOT NULL,
|
| 189 |
+
significado TEXT NOT NULL,
|
| 190 |
+
contexto TEXT,
|
| 191 |
+
frequencia INTEGER DEFAULT 1,
|
| 192 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
| 193 |
+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 194 |
+
);
|
| 195 |
+
|
| 196 |
+
-- tom_usuario
|
| 197 |
+
CREATE TABLE IF NOT EXISTS tom_usuario (
|
| 198 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 199 |
+
numero_usuario TEXT NOT NULL,
|
| 200 |
+
tom_detectado TEXT NOT NULL,
|
| 201 |
+
intensidade REAL DEFAULT 0.5,
|
| 202 |
+
contexto TEXT,
|
| 203 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 204 |
+
);
|
| 205 |
+
|
| 206 |
+
-- adaptacao_dinamica
|
| 207 |
+
CREATE TABLE IF NOT EXISTS adaptacao_dinamica (
|
| 208 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 209 |
+
numero_usuario TEXT NOT NULL,
|
| 210 |
+
tipo_adaptacao TEXT NOT NULL,
|
| 211 |
+
valor_anterior TEXT,
|
| 212 |
+
valor_novo TEXT,
|
| 213 |
+
razao TEXT,
|
| 214 |
+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
| 215 |
+
);
|
| 216 |
+
|
| 217 |
+
-- PRONOMES POR TOM
|
| 218 |
+
CREATE TABLE IF NOT EXISTS pronomes_por_tom (
|
| 219 |
+
tom TEXT PRIMARY KEY,
|
| 220 |
+
pronomes TEXT NOT NULL
|
| 221 |
+
);
|
| 222 |
+
''')
|
| 223 |
+
|
| 224 |
+
# Insere pronomes padrão
|
| 225 |
+
c.executescript('''
|
| 226 |
+
INSERT OR IGNORE INTO pronomes_por_tom (tom, pronomes) VALUES
|
| 227 |
+
('formal', 'Sr., ilustre, boss, maior, homem'),
|
| 228 |
+
('rude', 'parvo, estúpido, burro, analfabeto, desperdício de esperma'),
|
| 229 |
+
('casual', 'mano, puto, cota, mwangolé, kota'),
|
| 230 |
+
('neutro', 'amigo, parceiro, camarada');
|
| 231 |
+
''')
|
| 232 |
+
conn.commit()
|
| 233 |
+
logger.info(f"Banco inicializado: {self.db_path}")
|
| 234 |
+
except Exception as e:
|
| 235 |
+
logger.error(f"Erro ao criar tabelas: {e}")
|
| 236 |
+
raise
|
| 237 |
|
| 238 |
def _ensure_all_columns_and_indexes(self):
|
| 239 |
+
try:
|
| 240 |
+
with self._get_connection() as conn:
|
| 241 |
+
c = conn.cursor()
|
| 242 |
+
# Migração de colunas
|
| 243 |
+
migrations = {
|
| 244 |
+
'mensagens': [
|
| 245 |
+
("numero", "TEXT"),
|
| 246 |
+
("is_reply", "BOOLEAN DEFAULT 0"),
|
| 247 |
+
("mensagem_original", "TEXT"),
|
| 248 |
+
("created_at", "DATETIME DEFAULT CURRENT_TIMESTAMP")
|
| 249 |
+
],
|
| 250 |
+
'girias_aprendidas': [
|
| 251 |
+
("contexto", "TEXT"),
|
| 252 |
+
("frequencia", "INTEGER DEFAULT 1"),
|
| 253 |
+
("updated_at", "DATETIME DEFAULT CURRENT_TIMESTAMP")
|
| 254 |
+
],
|
| 255 |
+
'tom_usuario': [
|
| 256 |
+
("intensidade", "REAL DEFAULT 0.5"),
|
| 257 |
+
("contexto", "TEXT")
|
| 258 |
+
]
|
| 259 |
+
}
|
| 260 |
+
for table, cols in migrations.items():
|
| 261 |
+
c.execute(f"PRAGMA table_info('{table}')")
|
| 262 |
+
existing = {row[1] for row in c.fetchall()}
|
| 263 |
+
for col_name, col_def in cols:
|
| 264 |
+
if col_name not in existing:
|
| 265 |
+
try:
|
| 266 |
+
c.execute(f"ALTER TABLE {table} ADD COLUMN {col_name} {col_def}")
|
| 267 |
+
logger.info(f"Coluna '{col_name}' adicionada em '{table}'")
|
| 268 |
+
except Exception as e:
|
| 269 |
+
logger.warning(f"Erro ao adicionar coluna {col_name}: {e}")
|
| 270 |
+
|
| 271 |
+
# Índices
|
| 272 |
+
indexes = [
|
| 273 |
+
"CREATE INDEX IF NOT EXISTS idx_mensagens_numero ON mensagens(numero);",
|
| 274 |
+
"CREATE INDEX IF NOT EXISTS idx_mensagens_created ON mensagens(created_at DESC);",
|
| 275 |
+
"CREATE INDEX IF NOT EXISTS idx_girias_usuario ON girias_aprendidas(numero_usuario);",
|
| 276 |
+
"CREATE INDEX IF NOT EXISTS idx_girias_giria ON girias_aprendidas(giria);",
|
| 277 |
+
"CREATE INDEX IF NOT EXISTS idx_tom_usuario ON tom_usuario(numero_usuario);",
|
| 278 |
+
"CREATE INDEX IF NOT EXISTS idx_aprendizados_usuario ON aprendizados(numero_usuario);",
|
| 279 |
+
"CREATE INDEX IF NOT EXISTS idx_embeddings_texto ON embeddings(texto);",
|
| 280 |
+
"CREATE INDEX IF NOT EXISTS idx_pronomes_tom ON pronomes_por_tom(tom);"
|
| 281 |
]
|
| 282 |
+
for idx in indexes:
|
| 283 |
+
try:
|
| 284 |
+
c.execute(idx)
|
| 285 |
+
except:
|
| 286 |
+
pass
|
| 287 |
+
conn.commit()
|
| 288 |
+
except Exception as e:
|
| 289 |
+
logger.error(f"Erro na migração/índices: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
# ================================================================
|
| 292 |
+
# MÉTODOS PRINCIPAIS (mantidos, mas com DATETIME)
|
| 293 |
# ================================================================
|
| 294 |
+
# ... [TODOS OS MÉTODOS DO ORIGINAL, apenas com DATETIME]
|
| 295 |
|
| 296 |
+
# Exemplo de método corrigido:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
def salvar_mensagem(self, usuario, mensagem, resposta, numero=None, is_reply=False, mensagem_original=None):
|
| 298 |
try:
|
| 299 |
cols = ['usuario', 'mensagem', 'resposta']
|
|
|
|
| 301 |
if numero: cols.append('numero'); vals.append(numero)
|
| 302 |
if is_reply is not None: cols.append('is_reply'); vals.append(int(is_reply))
|
| 303 |
if mensagem_original: cols.append('mensagem_original'); vals.append(mensagem_original)
|
|
|
|
| 304 |
placeholders = ', '.join(['?' for _ in cols])
|
| 305 |
query = f"INSERT INTO mensagens ({', '.join(cols)}) VALUES ({placeholders})"
|
| 306 |
self._execute_with_retry(query, tuple(vals), commit=True)
|
|
|
|
| 308 |
logger.warning(f"Fallback salvar_mensagem: {e}")
|
| 309 |
self._execute_with_retry("INSERT INTO mensagens (usuario, mensagem, resposta) VALUES (?, ?, ?)", (usuario, mensagem, resposta), commit=True)
|
| 310 |
|
| 311 |
+
# ... [demais métodos mantidos iguais, apenas com DATETIME onde necessário]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
|
|
|
| 313 |
def obter_pronomes_por_tom(self, tom: str) -> str:
|
|
|
|
| 314 |
result = self._execute_with_retry("SELECT pronomes FROM pronomes_por_tom WHERE tom=?", (tom.lower(),))
|
| 315 |
return result[0][0] if result else ""
|
| 316 |
|
|
|
|
| 317 |
def obter_contexto_aprendizado(self, numero_usuario: str) -> Dict[str, Any]:
|
| 318 |
return {
|
| 319 |
"girias": self.recuperar_girias_usuario(numero_usuario),
|