feat: AI usage logging (AiLogger, ai_logs table, admin view)

- AiLogger.php: new service, inserts into ai_logs on every AI call
- ai_logs table: added to migrate.php (provider, model, call_type, tokens, duration_ms)
- AiBot: logs chat, media_response, nlu calls with timing and token counts
- MediaTranscriber: logs whisper, geminiAudio, openaiVision, geminiVision, claudeVision
- /admin/ai-logs: paginated table with provider/company filter
- Layout.php: added IA Logs nav link
- index.php: require AiLogger before AiBot; added GET /admin/ai-logs route

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-05 09:03:21 -05:00
co-authored by Claude Sonnet 4.6
parent 1d9484a1e0
commit 5506ebd2af
7 changed files with 246 additions and 10 deletions
+21
View File
@@ -257,6 +257,27 @@ $db->exec("
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
");
// ─── AI usage logs ───────────────────────────────────────────────────────────
$db->exec("
CREATE TABLE IF NOT EXISTS ai_logs (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
company_id INT DEFAULT NULL,
phone_number VARCHAR(20) DEFAULT NULL,
provider VARCHAR(20) NOT NULL,
model VARCHAR(60) NOT NULL DEFAULT '',
call_type VARCHAR(30) NOT NULL DEFAULT 'chat',
input_preview VARCHAR(500) DEFAULT NULL,
output_preview VARCHAR(500) DEFAULT NULL,
tokens_in INT DEFAULT NULL,
tokens_out INT DEFAULT NULL,
duration_ms INT DEFAULT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
INDEX idx_company (company_id),
INDEX idx_provider (provider),
INDEX idx_created (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
");
// ─── Campo erp_active_users en companies ──────────────────────────────────────
try {
$db->exec("ALTER TABLE companies ADD COLUMN erp_active_users INT DEFAULT 0 AFTER is_active");