diff --git a/app/app/Console/Commands/HelpRebuildKnowledgeCommand.php b/app/app/Console/Commands/HelpRebuildKnowledgeCommand.php new file mode 100644 index 00000000..14024364 --- /dev/null +++ b/app/app/Console/Commands/HelpRebuildKnowledgeCommand.php @@ -0,0 +1,58 @@ +error("В {$dir} нет статей *.md — база знаний осталась прежней."); + + return self::FAILURE; + } + + $articles = []; + foreach ($files as $file) { + $articles[] = $parser->parse('help/'.basename($file), (string) file_get_contents($file)); + } + + DB::transaction(function () use ($articles): void { + KnowledgeChunk::query()->delete(); + foreach ($articles as $article) { + foreach ($article->chunks as $i => $chunk) { + KnowledgeChunk::create([ + 'source_path' => $article->sourcePath, + 'title' => $article->title, + 'tour' => $article->tour, + 'topics' => $article->topics, + 'chunk_index' => $i, + 'content' => $chunk, + ]); + } + } + }); + + $this->info(sprintf('Проиндексировано статей: %d.', count($articles))); + + return self::SUCCESS; + } +} diff --git a/app/app/Models/KnowledgeChunk.php b/app/app/Models/KnowledgeChunk.php new file mode 100644 index 00000000..dcad4ce4 --- /dev/null +++ b/app/app/Models/KnowledgeChunk.php @@ -0,0 +1,13 @@ +dailyAt('00:05') ->timezone('Europe/Moscow') ->onSuccess(fn () => $hb->recordRunResult('App\Jobs\Supplier\FlushDeferredOnlineSyncJob', true, null, null)) @@ -185,3 +186,9 @@ Schedule::command('scheduler:check-heartbeats') ->timezone('Europe/Moscow') ->onSuccess(fn () => $hb->recordRunResult('scheduler:check-heartbeats', true, null, null)) ->onFailure(fn () => $hb->recordRunResult('scheduler:check-heartbeats', false, 'Command failed', null)); + +// База знаний ИИ-бота: ночная переиндексация статей resources/help +// (спека 2026-07-02-jivo-ai-support-bot-design §3). Изменил статью — ночью бот знает. +Schedule::command('help:rebuild-knowledge') + ->dailyAt('04:30') + ->onOneServer(); diff --git a/app/tests/Feature/Bot/HelpRebuildKnowledgeCommandTest.php b/app/tests/Feature/Bot/HelpRebuildKnowledgeCommandTest.php new file mode 100644 index 00000000..ad1670cd --- /dev/null +++ b/app/tests/Feature/Bot/HelpRebuildKnowledgeCommandTest.php @@ -0,0 +1,22 @@ +insert([ + 'source_path' => 'help/deleted-article.md', 'title' => 'Старая', 'topics' => '', + 'chunk_index' => 0, 'content' => 'мусор', 'created_at' => now(), 'updated_at' => now(), + ]); + + $this->artisan('help:rebuild-knowledge')->assertExitCode(0); + + expect(DB::table('knowledge_chunks')->where('source_path', 'help/deleted-article.md')->count())->toBe(0) + ->and(DB::table('knowledge_chunks')->where('title', 'Что такое проект')->count())->toBeGreaterThan(0) + ->and(DB::table('knowledge_chunks')->where('title', 'Тарифы и списания')->count())->toBeGreaterThan(0); +});