This repository has been archived on 2026-03-09. You can view files and clone it, but cannot push or open issues or pull requests.
php-agentic/Boot.php
Snider ad0ee04b83
Some checks failed
CI / PHP 8.3 (push) Failing after 2s
CI / PHP 8.4 (push) Failing after 2s
chore: make TLS skip detect any non-public TLD, not just .lan
Prepares for lthn.lan → lthn.sh migration. Once real certs are
deployed, verifySsl will always be true and this logic becomes
a no-op safety net for .lan/.lab/.local/.test domains.

Co-Authored-By: Virgil <virgil@lethean.io>
2026-03-03 13:56:06 +00:00

159 lines
5.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Core\Mod\Agentic;
use Core\Events\AdminPanelBooting;
use Core\Events\ConsoleBooting;
use Core\Events\McpToolsRegistering;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
class Boot extends ServiceProvider
{
protected string $moduleName = 'agentic';
/**
* Events this module listens to for lazy loading.
*
* @var array<class-string, string>
*/
public static array $listens = [
AdminPanelBooting::class => 'onAdminPanel',
ConsoleBooting::class => 'onConsole',
McpToolsRegistering::class => 'onMcpTools',
];
public function boot(): void
{
$this->loadMigrationsFrom(__DIR__.'/Migrations');
$this->loadTranslationsFrom(__DIR__.'/Lang', 'agentic');
$this->configureRateLimiting();
$this->scheduleRetentionCleanup();
}
/**
* Register the daily retention cleanup schedule.
*/
protected function scheduleRetentionCleanup(): void
{
$this->app->booted(function (): void {
$schedule = $this->app->make(Schedule::class);
$schedule->command('agentic:plan-cleanup')->daily();
});
}
/**
* Configure rate limiting for agentic endpoints.
*/
protected function configureRateLimiting(): void
{
// Rate limit for the for-agents.json endpoint
// Allow 60 requests per minute per IP
RateLimiter::for('agentic-api', function (Request $request) {
return Limit::perMinute(60)->by($request->ip());
});
}
public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/config.php',
'mcp'
);
$this->mergeConfigFrom(
__DIR__.'/agentic.php',
'agentic'
);
$this->app->singleton(\Core\Mod\Agentic\Services\AgenticManager::class);
$this->app->singleton(\Core\Mod\Agentic\Services\BrainService::class, function ($app) {
$ollamaUrl = config('mcp.brain.ollama_url', 'http://localhost:11434');
$qdrantUrl = config('mcp.brain.qdrant_url', 'http://localhost:6334');
// Skip TLS verification for non-public TLDs (self-signed certs behind Traefik)
$hasLocalTld = static fn (string $url): bool => (bool) preg_match(
'/\.(lan|lab|local|test)(?:[:\/]|$)/',
parse_url($url, PHP_URL_HOST) ?? ''
);
$verifySsl = ! ($hasLocalTld($ollamaUrl) || $hasLocalTld($qdrantUrl));
return new \Core\Mod\Agentic\Services\BrainService(
ollamaUrl: $ollamaUrl,
qdrantUrl: $qdrantUrl,
collection: config('mcp.brain.collection', 'openbrain'),
embeddingModel: config('mcp.brain.embedding_model', 'nomic-embed-text'),
verifySsl: $verifySsl,
);
});
}
// -------------------------------------------------------------------------
// Event-driven handlers (for lazy loading once event system is integrated)
// -------------------------------------------------------------------------
/**
* Handle admin panel booting event.
*/
public function onAdminPanel(AdminPanelBooting $event): void
{
$event->views($this->moduleName, __DIR__.'/View/Blade');
// Register admin routes
if (file_exists(__DIR__.'/Routes/admin.php')) {
$event->routes(fn () => require __DIR__.'/Routes/admin.php');
}
// Register Livewire components
$event->livewire('agentic.admin.dashboard', View\Modal\Admin\Dashboard::class);
$event->livewire('agentic.admin.plans', View\Modal\Admin\Plans::class);
$event->livewire('agentic.admin.plan-detail', View\Modal\Admin\PlanDetail::class);
$event->livewire('agentic.admin.sessions', View\Modal\Admin\Sessions::class);
$event->livewire('agentic.admin.session-detail', View\Modal\Admin\SessionDetail::class);
$event->livewire('agentic.admin.tool-analytics', View\Modal\Admin\ToolAnalytics::class);
$event->livewire('agentic.admin.tool-calls', View\Modal\Admin\ToolCalls::class);
$event->livewire('agentic.admin.api-keys', View\Modal\Admin\ApiKeys::class);
$event->livewire('agentic.admin.templates', View\Modal\Admin\Templates::class);
// Note: Navigation is registered via AdminMenuProvider interface
// in the existing boot() method until we migrate to pure event-driven nav.
}
/**
* Handle console booting event.
*/
public function onConsole(ConsoleBooting $event): void
{
$event->command(Console\Commands\TaskCommand::class);
$event->command(Console\Commands\PlanCommand::class);
$event->command(Console\Commands\GenerateCommand::class);
$event->command(Console\Commands\PlanRetentionCommand::class);
$event->command(Console\Commands\BrainSeedMemoryCommand::class);
$event->command(Console\Commands\BrainIngestCommand::class);
}
/**
* Handle MCP tools registration event.
*
* Note: Agent tools (plan_create, session_start, etc.) are implemented in
* the Mcp module at Mod\Mcp\Tools\Agent\* and registered via AgentToolRegistry.
* Brain tools are registered here as they belong to the Agentic module.
*/
public function onMcpTools(McpToolsRegistering $event): void
{
$registry = $this->app->make(Services\AgentToolRegistry::class);
$registry->registerMany([
new Mcp\Tools\Agent\Brain\BrainRemember(),
new Mcp\Tools\Agent\Brain\BrainRecall(),
new Mcp\Tools\Agent\Brain\BrainForget(),
new Mcp\Tools\Agent\Brain\BrainList(),
]);
}
}