first commit
This commit is contained in:
116
src/Command/KnowledgeIngestCommand.php
Normal file
116
src/Command/KnowledgeIngestCommand.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?php
|
||||
// src/Command/KnowledgeIngestCommand.php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use App\Knowledge\Ingest\KnowledgeIngestService;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
|
||||
#[AsCommand(
|
||||
name: 'mto:agent:knowledge:ingest',
|
||||
description: 'Ingest one or multiple markdown/text documents into file-based knowledge chunks'
|
||||
)]
|
||||
final class KnowledgeIngestCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private readonly KnowledgeIngestService $ingest,
|
||||
private readonly string $uploadsDir,
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
protected function configure(): void
|
||||
{
|
||||
$this
|
||||
->addArgument(
|
||||
'file',
|
||||
InputArgument::OPTIONAL,
|
||||
'Path to a single .txt/.md file'
|
||||
)
|
||||
->addOption(
|
||||
'all',
|
||||
null,
|
||||
InputOption::VALUE_NONE,
|
||||
'Ingest all .md files from the uploads directory'
|
||||
)
|
||||
->addOption(
|
||||
'optimize',
|
||||
'o',
|
||||
InputOption::VALUE_NONE,
|
||||
'Optimize chunks for retrieval quality'
|
||||
);
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$files = [];
|
||||
$optimize = (bool) $input->getOption('optimize');
|
||||
|
||||
if ($input->getOption('all')) {
|
||||
if (!is_dir($this->uploadsDir)) {
|
||||
$output->writeln('<error>❌ uploads directory not found</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$finder = new Finder();
|
||||
$finder
|
||||
->files()
|
||||
->in($this->uploadsDir)
|
||||
->name('*.md');
|
||||
|
||||
if (!$finder->hasResults()) {
|
||||
$output->writeln('<comment>ℹ️ No .md files found in uploads/</comment>');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
foreach ($finder as $file) {
|
||||
$files[] = $file->getRealPath();
|
||||
}
|
||||
|
||||
$output->writeln(sprintf(
|
||||
'📂 Ingesting %d markdown files from uploads (%s)',
|
||||
count($files),
|
||||
$optimize ? 'optimized' : 'standard'
|
||||
));
|
||||
} else {
|
||||
$file = $input->getArgument('file');
|
||||
|
||||
if (!$file) {
|
||||
$output->writeln('<error>❌ Either provide a file or use --all</error>');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$files[] = (string) $file;
|
||||
}
|
||||
|
||||
$totalWritten = 0;
|
||||
|
||||
foreach ($files as $filePath) {
|
||||
$output->writeln('➡️ Ingesting: ' . $filePath);
|
||||
|
||||
$written = $this->ingest->ingestFile(
|
||||
$filePath,
|
||||
optimize: $optimize
|
||||
);
|
||||
|
||||
$totalWritten += count($written);
|
||||
|
||||
foreach ($written as $chunk) {
|
||||
$output->writeln(' - ' . $chunk);
|
||||
}
|
||||
}
|
||||
|
||||
$output->writeln('');
|
||||
$output->writeln('✅ Total written chunks: ' . $totalWritten);
|
||||
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user