shipfastlabs/agent-detector
Lightweight Laravel/PHP utility to detect when your code is running inside an AI agent or automated dev environment. Supports multiple known agents (e.g., Cursor, Gemini, Codex, Claude) via environment-variable detection. Requires PHP 8.2+.
AI_AGENT env var, allowing future-proofing for emerging AI tools.KnownAgent for type safety) but remains framework-agnostic.composer require laravel/agent-detector) and usage via a single function call (detectAgent()) or class (AgentDetector::detect()).KnownAgent enum) are optional.AI_AGENT env var.KnownAgent) and Laravel’s env var ecosystem. Works seamlessly with:
AgentDetectorMiddleware to block AI requests).AgentDetector as a singleton).detectAgent() function. No framework-specific dependencies beyond PHP 8.2+.composer require laravel/agent-detector --dev.AI_AGENT=github-copilot).use Laravel\AgentDetector\detectAgent;
$result = detectAgent();
logger()->info("AI Agent Detected", ["agent" => $result->name]);
// app/Http/Middleware/DetectAgent.php
public function handle(Request $request, Closure $next) {
$result = detectAgent();
$request->merge(['is_ai_agent' => $result->isAgent]);
return $next($request);
}
AgentDetector to the container for dependency injection.
// app/Providers/AppServiceProvider.php
public function register() {
$this->app->singleton(AgentDetector::class);
}
AiAgentDetected).COPILOT_MODEL). Ensure these are set correctly in deployment pipelines (e.g., GitHub Actions, Docker).AgentDetector or using the AGENT_ENV_VARS constant to add custom env vars.AgentDetector::AGENT_ENV_VARS to inspect detected env vars.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| False positive detection | Legitimate requests blocked | Whitelist known good env vars; test thoroughly. |
| False negative detection | AI requests slip through | Monitor logs/metrics; extend detection rules. |
| Env var injection attack | Malicious env vars trigger detection | Validate env vars against a allowlist (e.g., KnownAgent enum). |
| Package update breaks compatibility | New major version introduces issues | Pin to a stable minor version (e.g., ^2.0). |
| Custom agent misconfiguration | Undetected or misclassified agents | Document custom agent setup; use AI_AGENT for clarity. |
detectAgent() vs. AgentDetector::detect()).KnownAgent::Claude).jobs:
test:
env:
AI_AGENT:
How can I help you explore Laravel packages today?