Files
ChatbotAGGLO/app/Livewire/AdminDashboard.php
T
2026-06-22 16:40:24 +02:00

184 lines
6.5 KiB
PHP

<?php
namespace App\Livewire;
use App\Models\Chatbot;
use App\Models\ChatbotHistory;
use App\Services\DocumentIngestionService;
use App\Services\WeaviateService;
use Livewire\Component;
use Livewire\WithFileUploads;
class AdminDashboard extends Component
{
use WithFileUploads;
public Chatbot $chatbot;
public string $url = '';
public $pdfFile;
public string $statusMessage = '';
public string $statusType = ''; // 'success', 'error', 'info'
public string $systemPrompt = '';
public string $chatbotName = '';
public array $history = [];
public bool $enableVision = true;
public bool $crawlEntireSite = false;
public int $maxPages = 30;
public function mount(Chatbot $chatbot)
{
$this->chatbot = $chatbot;
$this->systemPrompt = $chatbot->system_prompt ?? '';
$this->chatbotName = $chatbot->name;
$this->loadHistory();
}
protected function loadHistory()
{
$this->history = $this->chatbot->histories()
->latest()
->limit(15)
->get()
->map(function ($item) {
return [
'action' => $item->action,
'target' => $item->target,
'status' => $item->status,
'timestamp' => $item->created_at->format('d/m/Y H:i')
];
})->toArray();
}
public function saveSystemPrompt()
{
$this->validate([
'systemPrompt' => 'required|string|max:5000',
'chatbotName' => 'required|string|max:100',
]);
$this->chatbot->update([
'name' => $this->chatbotName,
'system_prompt' => $this->systemPrompt
]);
$this->logAction("Configuration Chatbot", "Nom modifié en '{$this->chatbotName}'", "success");
$this->statusMessage = "Les paramètres du chatbot ont été mis à jour avec succès !";
$this->statusType = 'success';
$this->dispatch('settings-updated');
}
protected function logAction(string $action, string $target, string $status)
{
$this->chatbot->histories()->create([
'action' => $action,
'target' => $target,
'status' => $status
]);
$this->loadHistory();
}
public function ingestUrl(DocumentIngestionService $ingestionService)
{
$this->validate([
'url' => 'required|url',
'maxPages' => 'required|integer|min:1|max:100',
]);
if ($this->crawlEntireSite) {
$this->statusMessage = "Exploration et indexation de l'ensemble du site en cours (cela peut prendre quelques minutes)...";
$this->statusType = 'info';
// Increase time limit for crawler execution
@set_time_limit(300);
$indexedCount = $ingestionService->crawlAndIngestSite($this->url, (string) $this->chatbot->id, $this->maxPages);
if ($indexedCount > 0) {
$this->logAction("Crawler Site Web", "{$this->url} ({$indexedCount} pages indexées)", "success");
$this->statusMessage = "Le site a été exploré et {$indexedCount} pages ont été indexées avec succès !";
$this->statusType = 'success';
$this->url = '';
} else {
$this->logAction("Crawler Site Web", $this->url, "failed");
$this->statusMessage = "L'exploration du site a échoué. Veuillez vérifier les logs et l'URL.";
$this->statusType = 'error';
}
} else {
$this->statusMessage = "Indexation de l'URL en cours...";
$this->statusType = 'info';
$success = $ingestionService->ingestUrl($this->url, (string) $this->chatbot->id);
if ($success) {
$this->logAction("Indexation URL", $this->url, "success");
$this->statusMessage = "L'URL a été indexée avec succès dans Weaviate !";
$this->statusType = 'success';
$this->url = '';
} else {
$this->logAction("Indexation URL", $this->url, "failed");
$this->statusMessage = "Impossible d'indexer l'URL. Veuillez vérifier les logs et l'état de Weaviate.";
$this->statusType = 'error';
}
}
}
public function ingestPdf(DocumentIngestionService $ingestionService)
{
$this->validate([
'pdfFile' => 'required|file|mimes:pdf|max:10240', // Max 10MB
]);
$this->statusMessage = "Indexation du fichier PDF en cours...";
$this->statusType = 'info';
$originalName = $this->pdfFile->getClientOriginalName();
$storedPath = $this->pdfFile->storeAs('temp', $originalName);
$absolutePath = storage_path('app/private/' . $storedPath);
if (!file_exists($absolutePath)) {
$absolutePath = storage_path('app/' . $storedPath);
}
$success = $ingestionService->ingestPdf($absolutePath, $originalName, (string) $this->chatbot->id, $this->enableVision);
// Cleanup
if (file_exists($absolutePath)) {
@unlink($absolutePath);
}
if ($success) {
$this->logAction("Indexation PDF", $originalName, "success");
$this->statusMessage = "Le fichier PDF '{$originalName}' a été indexé avec succès !";
$this->statusType = 'success';
$this->pdfFile = null;
} else {
$this->logAction("Indexation PDF", $originalName, "failed");
$this->statusMessage = "Une erreur s'est produite lors de l'indexation du PDF.";
$this->statusType = 'error';
}
}
public function clearDatabase(WeaviateService $weaviateService)
{
$success = $weaviateService->clearChatbotData((string) $this->chatbot->id);
if ($success) {
$this->logAction("Réinitialisation Index", "Suppression de toutes les données vectorielles", "success");
$this->statusMessage = "La base de données vectorielle du chatbot a été réinitialisée avec succès.";
$this->statusType = 'success';
} else {
$this->logAction("Réinitialisation Index", "Suppression de toutes les données vectorielles", "failed");
$this->statusMessage = "Impossible de vider les données Weaviate.";
$this->statusType = 'error';
}
}
public function render()
{
return view('livewire.admin-dashboard');
}
}