inceptia-io/larabrain
LaraBrain gives your Laravel app “self-awareness” by scanning models, migrations, routes, and controllers to build a context graph, then lets you ask natural-language questions via AI providers (OpenAI, Gemini, Anthropic, DeepSeek) with links to relevant code.
Installation:
composer require inceptia-io/larabrain
php artisan vendor:publish --tag=brain-config
Configure .env with your AI provider key (e.g., OPENAI_API_KEY).
Initial Scan:
php artisan app-brain:scan
This builds the context graph for your codebase (models, routes, controllers).
First Question:
php artisan app-brain:ask "How does user registration work?"
Or via the web UI at /brain (if enabled).
config/app-brain.php (adjust AI provider, caching, UI settings).php artisan app-brain:scan --help (customize paths, dry runs).AppBrain::ask() for programmatic use.Onboarding a New Developer: Ask, "Explain the order processing flow" to get a structured breakdown with clickable links to relevant code/files. Ideal for reducing ramp-up time.
Scanning Workflow:
app/Console/Kernel.php to run nightly:
protected function schedule(Schedule $schedule): void
{
$schedule->command('app-brain:scan')->daily();
}
--path to target specific directories (e.g., app/Features):
php artisan app-brain:scan --path=app/Features
Querying Workflow:
php artisan app-brain:ask "List all API routes" --json > routes.json
$response = AppBrain::ask('What models use the `User` model?');
return response()->json($response->answer);
Context-Driven Development:
'ui' => [
'middleware' => ['web', 'can:access-brain-ui'],
],
public function handle(Deployed $event)
{
Artisan::call('app-brain:scan');
}
BrainInterface in unit tests:
$this->mock(BrainInterface::class, function ($mock) {
$mock->shouldReceive('ask')
->andReturn(new AppBrainResponse('Mock answer'));
});
AppBrainResponse to include metadata:
$response = AppBrain::ask('Explain the checkout flow', ['format' => 'markdown']);
php artisan app-brain:ask "Show routes for the payment model" --keyword=payment
config(['app-brain.ai.default' => 'gemini']);
$response = AppBrain::ask('Describe the user model');
Cold Starts:
BRAIN_CACHE_ENABLED=true) and pre-scan during deployments.Token Limits:
gpt-4o max 128K tokens).--keyword to narrow context or reduce BRAIN_OPENAI_MAX_TOKENS.Dynamic Code:
RouteScanner to include dynamic route resolution.Permission Errors:
config/app-brain.php under 'ui' => ['middleware'].Cache Invalidation:
php artisan cache:clear
php artisan app-brain:scan --force
BRAIN_ASK_LOG_QUERIES=true
BRAIN_LOG_CHANNEL=single
php artisan app-brain:scan --dry-run
IntentMap:
IntentMap::extend(Intent::DescribeModel, ['schema', 'database']);
Cache Prefix Collisions:
BRAIN_CACHE_PREFIX if using other packages with brain-* keys.UI Route Conflicts:
BRAIN_UI_PREFIX (e.g., to dev-brain) to avoid clashes with existing routes.AI Provider Timeouts:
BRAIN_OPENAI_TIMEOUT (default: 60s) for slow networks:
BRAIN_OPENAI_TIMEOUT=120
Custom Scanners:
Arafat\Brain\Contracts\ScannerInterface to add support for:
.env files).config/app-brain.php:
'scan' => [
'scanners' => [
'custom' => \App\Scanners\CustomScanner::class,
],
],
Prompt Customization:
Brain::extend(function ($app) {
$app->singleton('brain.prompt', function () {
return new CustomPromptTemplate();
});
});
Response Post-Processing:
AppBrainResponse to add fields (e.g., confidence scores):
class ExtendedResponse extends AppBrainResponse
{
public function getConfidence(): float
{
return $this->answer->contains('likely') ? 0.7 : 1.0;
}
}
Queue Integration:
BRAIN_QUEUE_ENABLED=true
BrainEvents::asked:
Event::listen(BrainEvents::asked, function ($response) {
// Store response in DB or notify users
});
'scan' => [
'scanners' => [
'model' => false, // Disable model scanning
'route' => true,
],
],
BRAIN_ASK_CACHE_CONTEXT=true
BRAIN_CACHE_CONTEXT_TTL=86400
BRAIN_OPENAI_MODEL=gpt-3.5-turbo
How can I help you explore Laravel packages today?