New artisan command brain:reindex {--all} {--chunk=100} that dispatches
the EmbedMemory job for brain memories needing (re)indexing. Without
--all, only memories where indexed_at IS NULL are dispatched; --all
re-embeds every memory (useful after a Qdrant collection wipe or
embedding model change). Uses chunkById for memory-safe iteration at
scale.
php/tests/Feature/Console/BrainReindexCommandTest.php covers Good
(unindexed-only default), Bad (invalid chunk), Ugly (--all flag).
Co-authored-by: Codex <noreply@openai.com>
Closes tasks.lthn.sh/view.php?id=60
68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
|
|
|
// SPDX-License-Identifier: EUPL-1.2
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Core\Mod\Agentic\Console\Commands;
|
|
|
|
use Core\Mod\Agentic\Jobs\EmbedMemory;
|
|
use Core\Mod\Agentic\Models\BrainMemory;
|
|
use Illuminate\Console\Command;
|
|
|
|
class BrainReindexCommand extends Command
|
|
{
|
|
protected $signature = 'brain:reindex {--all} {--chunk=100}';
|
|
|
|
protected $description = 'Dispatch embedding jobs for OpenBrain memories that need indexing';
|
|
|
|
public function handle(): int
|
|
{
|
|
$chunkSize = $this->chunkSize();
|
|
|
|
if ($chunkSize === null) {
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$isReindexingAll = (bool) $this->option('all');
|
|
$query = BrainMemory::query();
|
|
|
|
if (! $isReindexingAll) {
|
|
$query->whereNull('indexed_at');
|
|
}
|
|
|
|
$dispatched = 0;
|
|
|
|
$query->chunkById($chunkSize, static function ($memories) use (&$dispatched): void {
|
|
foreach ($memories as $memory) {
|
|
EmbedMemory::dispatch($memory->id);
|
|
$dispatched++;
|
|
}
|
|
});
|
|
|
|
if ($dispatched === 0) {
|
|
$this->info('No brain memories need reindexing.');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$scope = $isReindexingAll ? 'all' : 'unindexed';
|
|
$this->info("Dispatched {$dispatched} brain memory embedding job(s) for {$scope} memories.");
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
private function chunkSize(): ?int
|
|
{
|
|
$option = $this->option('chunk');
|
|
$chunkSize = filter_var($option, FILTER_VALIDATE_INT);
|
|
|
|
if ($chunkSize === false || $chunkSize < 1) {
|
|
$this->error('--chunk must be a positive integer.');
|
|
|
|
return null;
|
|
}
|
|
|
|
return $chunkSize;
|
|
}
|
|
}
|