lvlup-dev/laravel-agent-editable-prompts
Installation:
composer require lvlup-dev/laravel-agent-editable-prompts
php artisan migrate
The package auto-discovers the service provider and runs migrations.
First Use Case:
Define prompts for an agent in the database via the agent_prompts table:
// Create a prompt via Tinker or a seeder
\LvlupDev\AgentEditablePrompts\Models\AgentPrompt::create([
'agent_slug' => 'customer-support-agent',
'priority' => 1,
'content' => 'Greet the user warmly.',
]);
Resolve Prompts: Fetch a concatenated string or ordered segments in your code:
$instructions = app(\LvlupDev\AgentEditablePrompts\Services\AgentPromptService::class)
->resolve('customer-support-agent');
$segments = app(\LvlupDev\AgentEditablePrompts\Services\AgentPromptService::class)
->segments('customer-support-agent');
Admin UI (Optional): If using Inertia/Vue, publish the admin UI assets:
php artisan vendor:publish --provider="LvlupDev\AgentEditablePrompts\AgentEditablePromptsServiceProvider" --tag="admin-views"
Then register the CRUD route in routes/web.php:
Route::inertia('/agent-prompts', 'AgentPrompts/Index');
Prompt Management:
AgentPromptService to dynamically fetch prompts by agent_slug.foreach ($segments as $segment) {
Blade::render($segment->content);
}
\n\n):
$fullPrompt = $service->resolve('agent-slug', separator: ' ');
Database Structure:
agent_slug: Group prompts by agent (e.g., support-bot, marketing-agent).priority: Controls order (lower = higher priority). Use 0 for default.content: Store raw prompt text (Markdown/Blade supported).Integration with Agents:
$response = app('llm')->run($instructions);
$cacheKey = "agent_prompt_{$agentSlug}";
$instructions = Cache::remember($cacheKey, now()->addHours(1), fn() =>
$service->resolve($agentSlug)
);
Admin UI (Inertia/Vue):
php artisan vendor:publish --tag="agent-prompts-views"
Testing:
AgentPromptService in tests:
$service = Mockery::mock(AgentPromptService::class);
$service->shouldReceive('resolve')->andReturn('Mocked prompt');
$this->app->instance(AgentPromptService::class, $service);
Priority Collisions:
priority, the database order becomes undefined.priority values or add a created_at fallback in queries.Separator Quirks:
\n\n) may cause issues with multi-line prompts.$service->resolve('agent-slug', separator: PHP_EOL);
Admin UI Dependencies:
inertiajs/inertia-laravel and a frontend setup.Performance with Large Prompts:
segments() for chunked processing or paginate the database query.Migration Conflicts:
agent_prompts table exists, the package’s migration will fail.database/migrations/ for duplicates before running migrate.Query Issues:
agent_slug and priority sorting:
\DB::enableQueryLog();
$service->resolve('agent-slug');
\DB::getQueryLog();
Missing Prompts:
agent_slug matches exactly (case-sensitive) between database and code.where('agent_slug', $slug) debug query to verify records exist.Admin UI Not Loading:
php artisan view:clear
npm run dev
Custom Content Types:
AgentPrompt model to support additional fields (e.g., metadata):
class AgentPrompt extends \LvlupDev\AgentEditablePrompts\Models\AgentPrompt {
protected $casts = [
'metadata' => 'json',
];
}
Dynamic Separators:
resolve() method in a service binding:
$this->app->bind(AgentPromptService::class, function ($app) {
$service = new AgentPromptService();
$service->setSeparatorResolver(fn($slug) => $slug === 'special-agent' ? ' | ' : "\n\n");
return $service;
});
Event Hooks:
\LvlupDev\AgentEditablePrompts\Models\AgentPrompt::updated(fn($prompt) => Cache::forget("agent_prompt_{$prompt->agent_slug}"));
Blade Directives:
Blade::directive('agentPrompt', function ($slug) {
return "<?php echo app(\\LvlupDev\\AgentEditablePrompts\\Services\\AgentPromptService::class)->resolve({$slug}); ?>";
});
Usage:
@agentPrompt('customer-support-agent')
How can I help you explore Laravel packages today?