From 197e771a4fcb8a20be67e7ea014bd0290a4faae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Mon, 13 Jul 2026 06:45:54 +0300 Subject: [PATCH] =?UTF-8?q?feat(chat):=20=D0=B6=D1=83=D1=80=D0=BD=D0=B0?= =?UTF-8?q?=D0=BB=20=D1=80=D0=B0=D0=B7=D0=B3=D0=BE=D0=B2=D0=BE=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B5=D0=B7=D0=B6=D0=B0=D0=B5?= =?UTF-8?q?=D1=82=20=D1=81=20Jivo=20=D0=BD=D0=B0=20=D1=81=D0=B2=D0=BE?= =?UTF-8?q?=D0=B9=20=D1=87=D0=B0=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jivo_chat_id -> chat_id + добавлены source/user_id/ip (спека 2026-07-13-own-chat-widget-design §5). Историческая миграция создания таблицы не тронута. Co-Authored-By: Claude Opus 4.8 --- app/app/Jobs/Bot/ProcessJivoMessageJob.php | 10 +++--- .../2026_07_13_100000_chat_widget_columns.php | 34 +++++++++++++++++++ app/tests/Feature/Bot/BotDialogsTableTest.php | 20 +++++++++-- app/tests/Feature/Bot/BotSpeedBudgetTest.php | 2 +- 4 files changed, 57 insertions(+), 9 deletions(-) create mode 100644 app/database/migrations/2026_07_13_100000_chat_widget_columns.php diff --git a/app/app/Jobs/Bot/ProcessJivoMessageJob.php b/app/app/Jobs/Bot/ProcessJivoMessageJob.php index bd776ea2..5a7c960a 100644 --- a/app/app/Jobs/Bot/ProcessJivoMessageJob.php +++ b/app/app/Jobs/Bot/ProcessJivoMessageJob.php @@ -56,7 +56,7 @@ class ProcessJivoMessageJob implements ShouldQueue // Память разговора — ДО записи текущего сообщения (иначе оно попадёт в историю). // Служебные хвосты «👉 Показать на портале: …» из прошлых ответов LLM не нужны. $history = BotDialog::query() - ->where('jivo_chat_id', $this->chatId) + ->where('chat_id', $this->chatId) ->orderByDesc('id') ->limit(self::HISTORY_LIMIT) ->get(['direction', 'message']) @@ -69,7 +69,7 @@ class ProcessJivoMessageJob implements ShouldQueue ->all(); BotDialog::create([ - 'jivo_chat_id' => $this->chatId, + 'chat_id' => $this->chatId, 'direction' => 'in', 'message' => $this->text, 'created_at' => now(), @@ -85,7 +85,7 @@ class ProcessJivoMessageJob implements ShouldQueue app(JivoBotClient::class)->sendMessage($this->chatId, $this->clientId, $answer->text); BotDialog::create([ - 'jivo_chat_id' => $this->chatId, + 'chat_id' => $this->chatId, 'direction' => 'out', 'message' => $answer->text, 'matched_chunks' => $answer->matchedChunkIds, @@ -99,7 +99,7 @@ class ProcessJivoMessageJob implements ShouldQueue private function awaitsContact(): bool { $lastOut = BotDialog::query() - ->where('jivo_chat_id', $this->chatId) + ->where('chat_id', $this->chatId) ->where('direction', 'out') ->latest('id') ->value('message'); @@ -124,7 +124,7 @@ class ProcessJivoMessageJob implements ShouldQueue } $rows = BotDialog::query() - ->where('jivo_chat_id', $this->chatId) + ->where('chat_id', $this->chatId) ->orderByDesc('id') ->limit(8) ->get(['direction', 'message']) diff --git a/app/database/migrations/2026_07_13_100000_chat_widget_columns.php b/app/database/migrations/2026_07_13_100000_chat_widget_columns.php new file mode 100644 index 00000000..74b51b2d --- /dev/null +++ b/app/database/migrations/2026_07_13_100000_chat_widget_columns.php @@ -0,0 +1,34 @@ +insert([ - 'jivo_chat_id' => 'chat-1', + 'chat_id' => 'chat-1', 'direction' => 'in', 'message' => 'что такое проект?', 'created_at' => now(), ]); DB::table('bot_dialogs')->insert([ - 'jivo_chat_id' => 'chat-1', + 'chat_id' => 'chat-1', 'direction' => 'out', 'message' => 'Проект — это…', 'matched_chunks' => json_encode([1, 2]), @@ -24,5 +24,19 @@ it('bot_dialogs принимает запись входа и выхода', fun 'created_at' => now(), ]); - expect(DB::table('bot_dialogs')->where('jivo_chat_id', 'chat-1')->count())->toBe(2); + expect(DB::table('bot_dialogs')->where('chat_id', 'chat-1')->count())->toBe(2); +}); + +it('журнал разговоров хранит номер разговора, откуда пришёл и кто клиент', function () { + $columns = DB::select(" + SELECT column_name FROM information_schema.columns + WHERE table_name = 'bot_dialogs' + "); + $names = array_map(fn ($c) => $c->column_name, $columns); + + expect($names)->toContain('chat_id') + ->and($names)->toContain('source') + ->and($names)->toContain('user_id') + ->and($names)->toContain('ip') + ->and($names)->not->toContain('jivo_chat_id'); }); diff --git a/app/tests/Feature/Bot/BotSpeedBudgetTest.php b/app/tests/Feature/Bot/BotSpeedBudgetTest.php index 6b320e16..dee3f1a2 100644 --- a/app/tests/Feature/Bot/BotSpeedBudgetTest.php +++ b/app/tests/Feature/Bot/BotSpeedBudgetTest.php @@ -33,7 +33,7 @@ it('наша часть тракта (без сети) укладывается $latencies = []; for ($i = 0; $i < 10; $i++) { (new ProcessJivoMessageJob("chat-{$i}", 'c', 'что такое проект?'))->handle(); - $latencies[] = (int) BotDialog::where('jivo_chat_id', "chat-{$i}") + $latencies[] = (int) BotDialog::where('chat_id', "chat-{$i}") ->where('direction', 'out')->value('latency_ms'); }