laravel-ai-assistant maintained by mohamedfayez
Laravel AI Assistant — Business Operation Discovery Engine
MVP. This first release does one thing: it statically analyzes an existing Laravel application and discovers candidate business operations — with source-cited evidence — that could later become AI tools. It reads only. It never executes your business logic, never calls an LLM, and never modifies your source code.
This is not a chatbot, an agent runtime, or production-ready. It is the foundation: prove we can understand a real Laravel app's operations before building anything on top.
Why this exists
To let a user operate a Laravel app in natural language, an AI needs to know what operations the app actually has, what they require, and who may run them. The hardest, least-certain part of that vision is automatically understanding a real (often legacy) codebase. So the MVP tackles exactly that — and nothing else.
The guiding principle of the whole system:
The LLM (later) decides intent. Laravel decides permission and execution. Discovery produces candidates with evidence; it is deliberately not a security boundary. No operation is ever auto-exposed.
What it does
php artisan ai:discover walks your app and produces, per operation:
- Entry points (routes → grouped into one operation when they share a handler).
- Parameters from route segments, FormRequest rules and method signatures.
- Evidence, each item classified by strength and linked to a file/line:
Verified fact·Strong inference·Weak inference·Unknown·Conflict
- Read/Write/Destructive classification.
- Authentication, Authorization and Ownership signals — kept strictly separate.
- A 6-box completeness score and advisory read/write eligibility with reasons.
- Conflicts (e.g. a
GETroute that writes) and honest Unknowns (it says "I don't know" rather than guessing — e.g. it flags a possible IDOR when an authenticated route has no ownership scoping).
Output: a structured JSON report, a human-readable Markdown report, and a CLI summary table (exceptions first).
What it does not do (yet)
Chat, AI agent runtime, LLM tool-calling, tool execution, knowledge base, RAG, embeddings, vector DB, conversation management, write/delete execution, confirmation workflow, multi-agent, admin dashboard. These are planned for later phases and intentionally excluded here.
Installation
composer require mohamedfayez/laravel-ai-assistant
The service provider is auto-discovered. Optionally publish the config:
php artisan vendor:publish --tag=ai-assistant-config
Usage
php artisan ai:discover
Options:
| Option | Default | Description |
|---|---|---|
--path= |
host app base path | App to analyze. |
--source= |
runtime |
runtime (live route table — most reliable) or static (parse route files with php-parser). |
--format= |
all |
cli, json, markdown, or all. |
--output= |
config | Directory for report files. |
--fresh |
off | Ignore the analysis cache and re-run. |
--no-write |
off | Print to console only. |
Reports are written to storage/ai-assistant/reports/ by default (configurable). An incremental
cache under storage/ai-assistant/cache/ makes repeat runs instant until code changes.
Example output (excerpt)
### `order.get` — Order Get (✅ read-eligible)
- Kind: Read
- Completeness: 1 (6/6)
- Read eligible: yes — Read, ownership-scoped, no conflicts.
Evidence:
- [Verified fact] Authorization: Authorization enforced in code: authorize('view').
↳ app/Http/Controllers/OrderController.php::show() L26
- [Strong inference] Ownership: Data access is scoped to the authenticated user (1 site).
↳ app/Http/Controllers/OrderController.php::show() L28
And an honest gap in a legacy app:
### `legacy_order.get` — Legacy Order Get
- Read eligible: no — No ownership scoping evidence.
- [Unknown] Ownership: Authenticated, but no ownership scoping or authorization check found
on this path. Possible IDOR — must be verified before any exposure.
How it works
Deterministic, offline, multi-pass pipeline (no LLM):
- Route catalog — the live Laravel route table (
runtime) or a bounded static parse (static). - Codebase index — every class/enum under the scan paths is parsed once with
nikic/php-parser. - Clustering — routes that share a controller handler become one operation.
- Analyzers — parameters, middleware (auth/authz), controller body (ownership scoping, authorization calls, write side-effects, one hop into a delegated service), read/write classification, and existing-test coverage.
- Service-method merge — operations proven to delegate to the same service method are merged; operations on the same service class but a different method are flagged related and kept separate (never merged on names alone).
- Scoring — completeness and advisory eligibility; conflicts and unknowns surfaced.
Evidence & trust model
Strengths are categorical and evidence-linked — not invented confidence percentages:
| Strength | Meaning |
|---|---|
| Verified fact | Present deterministically in the AST (route→method, can: middleware, a validation rule). |
| Strong inference | ≥2 independent deterministic signals agree. |
| Weak inference | A single soft signal. |
| Unknown | No evidence found — stated, not guessed. |
| Conflict | Contradictory evidence across sources. |
Key safety rules enforced in code: authentication is never treated as ownership; a Policy
class merely existing is never treated as authorization being applied — authorization requires
an actual authorize()/Gate/can() call or can: middleware on the path.
Compatibility
| PHP | ^8.1 (requires ext-tokenizer, ext-json) |
| Laravel | 10, 11, 12 (illuminate/* ^10|^11|^12) |
| Parser | nikic/php-parser ^5.0 |
| Tested on | PHP 8.2 + Laravel 12 (via orchestra/testbench) |
Declared support for Laravel 10/11 is by constraint; CI across all three is planned. We do not claim support for versions we have not run.
Known limitations
- Static analysis cannot see purely dynamic dispatch, magic methods, or runtime-built routes.
- Delegation is followed one hop into services; deeper chains are not traversed in the MVP.
- The
staticroute parser handles common forms (verbs,resource/apiResource, groups with prefix/middleware/controller); exotic registrations need theruntimesource. - Ownership/authorization detection is deliberately conservative — it prefers to say
Unknownand flag a gap over asserting a protection that may not exist. - Authorization/auth applied in a controller constructor (
$this->middleware('auth'),HasMiddleware,authorizeResource) is not yet read — this errs safe (a falseUnknown, never a false "protected"). - The
staticparser is bounded: it does not resolve routes registered on a non-Routereceiver, file-based group callbacks,view/redirect/fallback/singletonroutes, or routes built dynamically, and a route file with a parse error yields an empty set silently. Use--source=runtime(the default) for completeness. - Route- and middleware-level evidence cites the route table, not a specific file/line; in-code evidence (ownership, authorization, writes) carries real file/line citations.
- Test-coverage detection is a substring match (a coverage signal, not rule proof); it can only add to write eligibility, which independently requires strong ownership + authorization.
Roadmap (target architecture; built incrementally)
Execution gateway (reuse the host's own enforcement) · read tools over a minimal chat loop ·
review API with developer overrides/versioning · dynamic corroboration · Knowledge Base + RAG
(pluggable vector store) · write operations with payload-bound confirmation · guardrails.
See ARCHITECTURE-DESIGN-v2.md.
Testing
composer test # phpunit
composer lint # pint --test
23 tests, 116 assertions. Accuracy is measured as operation-level precision/recall against
hand-labelled ground truth for two deliberately different fixtures — CleanShop (modern) and
LegacyShop (messy, with an IDOR gap and a GET-that-writes conflict). Current fixtures:
precision = recall = 1.0. These numbers are measured, not claimed, and prove the approach on
these fixtures only — not on every Laravel app.
License
MIT.
Built with ❤️ by Mohamed Fayez