diff --git a/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php b/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php index 27feb04..9bd2c6d 100644 --- a/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php +++ b/app/Modules/CyberBuddy/Http/Controllers/CyberBuddyController.php @@ -6,7 +6,6 @@ use App\Modules\AdversaryMeter\Http\Controllers\Controller; use App\Modules\CyberBuddy\Events\IngestFile; use App\Modules\CyberBuddy\Helpers\ApiUtilsFacade as ApiUtils; -use App\Modules\CyberBuddy\Http\Conversations\Exchange; use App\Modules\CyberBuddy\Http\Requests\DownloadOneFileRequest; use App\Modules\CyberBuddy\Http\Requests\StreamOneFileRequest; use App\Modules\CyberBuddy\Http\Requests\UploadManyFilesRequest; @@ -15,6 +14,9 @@ use App\Modules\CyberBuddy\Rules\IsValidCollectionName; use App\User; use BotMan\BotMan\BotMan; +use BotMan\BotMan\Messages\Incoming\Answer; +use BotMan\BotMan\Messages\Outgoing\Actions\Button; +use BotMan\BotMan\Messages\Outgoing\Question; use Illuminate\Http\UploadedFile; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; @@ -304,7 +306,47 @@ public function handle(): void $botman->reply($answer); } })->skipsConversation(); - $botman->hears('/conversation', fn(BotMan $botman) => $botman->startConversation(new Exchange())); + $botman->hears('{message}', function (BotMan $botman, string $message) { + $user = $this->user($botman); + if (!$user) { + $botman->reply('Connectez-vous pour accéder à cette commande.
Pour ce faire, vous pouvez utiliser la commande /login {username} {password}'); + } else { + $historyKey = $this->historyKey($botman); + $collection = $botman->userStorage()->get('collection'); + if ($collection) { + $response = ApiUtils::chat_manual_demo($historyKey, $collection, $message); + if ($response['error']) { + $botman->reply('Une erreur s\'est produite. Veuillez réessayer ultérieurement.'); + } else { + $answer = CyberBuddyController::enhanceAnswerWithSources($response['response'], collect($response['context'] ?? [])); + $botman->reply($answer); + } + } else { + $collections = \App\Modules\CyberBuddy\Models\Collection::where('is_deleted', false) + ->get() + ->map(fn(\App\Modules\CyberBuddy\Models\Collection $collection) => Button::create($collection->name)->value($collection->id)) + ->toArray(); + $question = Question::create('Pour commencer, quel corpus de documents souhaitez-vous utiliser?') + ->fallback('Le corpus sélectionné est inconnue.') + ->callbackId('collection') + ->addButtons($collections); + $botman->ask($question, function (Answer $answer) use ($botman, $historyKey, $message) { + if ($answer->isInteractiveMessageReply()) { + $collection = \App\Modules\CyberBuddy\Models\Collection::find($answer->getValue()); + $botman->reply("Le corpus selectionné est {$collection->name}. Je vais maintenant pouvoir vous répondre!"); + $botman->userStorage()->save(['collection' => $collection->name]); + $response = ApiUtils::chat_manual_demo($historyKey, $collection, $message); + if ($response['error']) { + $botman->reply('Une erreur s\'est produite. Veuillez réessayer ultérieurement.'); + } else { + $answer = CyberBuddyController::enhanceAnswerWithSources($response['response'], collect($response['context'] ?? [])); + $botman->reply($answer); + } + } + }); + } + } + }); $botman->fallback(fn(BotMan $botman) => $botman->reply('Désolé, je n\'ai pas compris votre commande.')); $botman->listen(); } @@ -324,6 +366,17 @@ private function user(BotMan $botman): ?User return $user; } + private function historyKey(BotMan $botman): string + { + /** @var string $historyKey */ + $historyKey = $botman->userStorage()->get('history_key'); + if (!$historyKey) { + $historyKey = Str::uuid()->toString(); + $botman->userStorage()->save(['history_key' => $historyKey]); + } + return $historyKey; + } + private function saveOneFile(\App\Modules\CyberBuddy\Models\Collection $collection, UploadedFile $file): ?string { // Extract file metadata diff --git a/app/Modules/CyberBuddy/Http/Conversations/Exchange.php b/app/Modules/CyberBuddy/Http/Conversations/Exchange.php deleted file mode 100644 index e3c07f1..0000000 --- a/app/Modules/CyberBuddy/Http/Conversations/Exchange.php +++ /dev/null @@ -1,76 +0,0 @@ -askForFramework(); - } - - public function stopsConversation(IncomingMessage $message): bool - { - return $message->getText() === 'stop'; - } - - private function askForFramework(): void - { - $collections = Collection::where('is_deleted', false) - ->get() - ->map(fn(Collection $collection) => Button::create($collection->name)->value($collection->id)) - ->toArray(); - - $question = Question::create('Quelle collection de documents souhaitez-vous utiliser?') - ->fallback('La collection sélectionnée est inconnue.') - ->callbackId('collection') - ->addButtons($collections); - - $this->ask($question, function (Answer $answer) { - if ($answer->isInteractiveMessageReply()) { - $collectionId = $answer->getValue(); - $collection = Collection::find($collectionId); - $this->say("La collection selectionnée est {$collection->name}."); - $this->askQuestion(Str::random(), $collection); - } - }); - } - - private function askQuestion(string $historyKey, Collection $collection): void - { - $this->ask('Posez-moi une question!', function (Answer $answer) use ($historyKey, $collection) { - $response = ApiUtils::chat_manual_demo($historyKey, $collection->name, $answer->getText()); - if ($response['error']) { - $this->say('Une erreur s\'est produite. Veuillez réessayer ultérieurement.'); - } else { - $answer = CyberBuddyController::enhanceAnswerWithSources($response['response'], collect($response['context'] ?? [])); - $this->say($answer); - $this->askAnotherQuestion($historyKey, $collection); - } - }); - } - - private function askAnotherQuestion(string $historyKey, Collection $collection): void - { - $this->ask('Une autre question?', function (Answer $answer) use ($historyKey, $collection) { - $response = ApiUtils::chat_manual_demo($historyKey, $collection->name, $answer->getText()); - if ($response['error']) { - $this->say('Une erreur s\'est produite. Veuillez réessayer ultérieurement.'); - } else { - $answer = CyberBuddyController::enhanceAnswerWithSources($response['response'], collect($response['context'] ?? [])); - $this->say($answer); - $this->askAnotherQuestion($historyKey, $collection); - } - }); - } -}