# raedbari/ai-agent-platform architecture

> Architecture diagram of the GitHub repository raedbari/ai-agent-platform, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/raedbari/ai-agent-platform
- Repository: https://github.com/raedbari/ai-agent-platform

## Overview

#### Purpose and Runtime
This is a Laravel/PHP application for configuring AI agents, clients, and knowledge bases, then serving retrieval-augmented chat responses. Laravel HTTP, Eloquent, queues, Artisan, and Filament provide the runtime surfaces. The primary application is rooted at `artisan`, `bootstrap/app.php`, and `public/index.php`; `ai-agent-rag-v2-patch/` and `rag-phase-backup-20260721-100426/` are bundled patch/backup trees rather than the main runtime source.

#### Entry Points and Interfaces
HTTP routing is defined in `routes/web.php` and `routes/api.php`; agent-facing request handling converges on `app/Http/Controllers/AgentController.php`. A browser-facing playground/chat preview is provided by `resources/views/playground.blade.php` and `resources/views/components/chat-preview.blade.php`. Administrative management is implemented through the Filament panel provider and resources:
- `app/Providers/Filament/NezarPanelProvider.php`
- `app/Filament/Resources/Agents/AgentResource.php`
- `app/Filament/Resources/Clients/ClientResource.php`

#### Domain and Persistence Boundary
Eloquent models define the core domain: agents hold behavior/rules, clients provide tenancy/ownership context, knowledge bases are source documents, and chunks are the retrieval unit.
- `app/Models/Agent.php`
- `app/Models/Client.php`
- `app/Models/KnowledgeBase.php`
- `app/Models/KnowledgeChunk.php`

Database migrations establish these entities, agent-to-knowledge-base evolution, and chunk persistence, notably `database/migrations/2026_07_20_131430_create_knowledge_chunks_table.php` and `database/migrations/2026_02_23_195623_upgrade_knowledge_bases_for_agents.php`. The relational database is the system of record for document metadata, indexing state, chunk text, and embeddings.

#### Knowledge Indexing Pipeline
Knowledge-base lifecycle events are observed and dispatched to background indexing, separating document writes from potentially expensive extraction and embedding work:
- `app/Observers/KnowledgeBaseObserver.php`
- `app/Jobs/IndexKnowledgeBase.php`
- `app/Services/KnowledgePipelineService.php`

The pipeline obtains source content, splits it into bounded chunks, generates embeddings, and persists `KnowledgeChunk` records:
- `app/Services/KnowledgeContentService.php`
- `app/Services/KnowledgeChunker.php`
- `app/Services/EmbeddingService.php`
- `app/Services/KnowledgeIndexingService.php`

Queue infrastructure is configured through `config/queue.php`; the `knowledge:reindex`-style operational entry point is `app/Console/Commands/ReindexKnowledge.php`.

#### Retrieval and RAG Assembly
At chat time, the controller/service layer resolves the target agent and its permitted knowledge bases, embeds the user query, scores stored chunks, and selects relevant context. Retrieval is encapsulated in `app/Services/KnowledgeRetrievalService.php`, with RAG controls in `config/rag.php`. This is an application-managed vector workflow over database-stored embeddings rather than a separately visible vector-database integration.

The selected context, agent instructions/critical rules, and user message are passed to the AI layer. This boundary is centered on:
- `app/Http/Controllers/AgentController.php`
- `app/Services/AIService.php`
- `app/Services/KnowledgeRetrievalService.php`

#### External Services and Configuration
The application has explicit Gemini and embeddings configuration, indicating external model/embedding-provider calls are isolated behind service classes rather than embedded in controllers:
- `config/gemini.php`
- `config/embeddings.php`
- `app/Services/AIService.php`
- `app/Services/EmbeddingService.php`

Credentials and deployment-specific provider settings belong in environment configuration (`.env.example`). Laravel cache, database, filesystem, queue, and Sanctum configuration provide the main infrastructure boundaries in `config/`.

#### Administrative Control Flow
Filament resources manage agents and clients, while relation managers attach knowledge bases to those scopes:
- `app/Filament/Resources/Agents/RelationManagers/KnowledgeBasesRelationManager.php`
- `app/Filament/Resources/Clients/RelationManagers/KnowledgeBasesRelationManager.php`

Thus, administrative changes flow from Filament/Eloquent writes through the knowledge-base observer into asynchronous indexing; runtime chat flows independently through API/controller retrieval and AI generation.

Main entry points: [`app/Services/KnowledgePipelineService.php`](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgePipelineService.php) (Knowledge pipeline), [`app/Services/KnowledgeRetrievalService.php`](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgeRetrievalService.php) (Knowledge retrieval), [`app/Models/KnowledgeBase.php`](https://github.com/raedbari/ai-agent-platform/blob/main/app/Models/KnowledgeBase.php) (Agent, client & knowledge models), [`app/Http/Controllers/AgentController.php`](https://github.com/raedbari/ai-agent-platform/blob/main/app/Http/Controllers/AgentController.php) (Agent controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Interfaces"]
  node_laravel{{"Laravel application<br/>runtime<br/>[app.php]"}}
  node_routes["Web &amp; API routes<br/>routing<br/>[api.php]"]
  node_playground["Chat playground<br/>browser view"]
  node_filament["Filament admin panel<br/>admin interface"]
  node_agent_resource["Agent administration<br/>Filament resource<br/>[AgentResource.php]"]
  node_client_resource["Client administration<br/>Filament resource<br/>[ClientResource.php]"]
end

subgraph group_domain["Domain &amp; Storage"]
  node_domain_models["Agent, client &amp; knowledge models<br/>Eloquent models<br/>[KnowledgeBase.php]"]
  node_database[("Relational database<br/>system of record")]
end

subgraph group_indexing["Knowledge Indexing"]
  node_kb_observer["Knowledge base observer<br/>Eloquent observer"]
  node_index_job["Index knowledge-base job<br/>queued job"]
  node_knowledge_pipeline["Knowledge pipeline<br/>indexing orchestrator"]
  node_content_service["Content extraction<br/>source content service"]
  node_chunker["Knowledge chunker<br/>chunking service"]
  node_embedding_service["Embedding service<br/>provider adapter"]
  node_reindex_command["Knowledge reindex command<br/>Artisan command"]
end

subgraph group_rag["Chat &amp; RAG"]
  node_agent_controller["Agent controller<br/>HTTP controller"]
  node_retrieval["Knowledge retrieval<br/>RAG retrieval service"]
  node_ai_service["AI generation service<br/>model provider adapter<br/>[AIService.php]"]
  node_model_provider{{"Gemini &amp; embedding providers<br/>external AI services<br/>[gemini.php]"}}
end

node_laravel -->|"serves"| node_routes
node_routes -->|"chat requests"| node_agent_controller
node_playground -->|"chat preview"| node_agent_controller
node_filament -->|"hosts"| node_agent_resource
node_filament -->|"hosts"| node_client_resource
node_agent_resource -->|"manages"| node_domain_models
node_client_resource -->|"manages"| node_domain_models
node_domain_models -->|"persists"| node_database
node_domain_models -->|"knowledge-base events"| node_kb_observer
node_kb_observer -->|"dispatches"| node_index_job
node_reindex_command -->|"queues reindexing"| node_index_job
node_index_job -->|"runs"| node_knowledge_pipeline
node_knowledge_pipeline -->|"extracts source"| node_content_service
node_knowledge_pipeline -->|"splits content"| node_chunker
node_knowledge_pipeline -->|"embeds chunks"| node_embedding_service
node_knowledge_pipeline -->|"stores chunks"| node_database
node_embedding_service -->|"embedding calls"| node_model_provider
node_agent_controller -->|"retrieves context"| node_retrieval
node_retrieval -->|"embeds query"| node_embedding_service
node_retrieval -->|"scores chunks"| node_database
node_agent_controller -->|"requests response"| node_ai_service
node_retrieval -->|"selected context"| node_ai_service
node_ai_service -->|"generation calls"| node_model_provider

click node_laravel "https://github.com/raedbari/ai-agent-platform/blob/main/bootstrap/app.php"
click node_routes "https://github.com/raedbari/ai-agent-platform/blob/main/routes/api.php"
click node_playground "https://github.com/raedbari/ai-agent-platform/blob/main/resources/views/playground.blade.php"
click node_filament "https://github.com/raedbari/ai-agent-platform/blob/main/app/Providers/Filament/NezarPanelProvider.php"
click node_agent_resource "https://github.com/raedbari/ai-agent-platform/blob/main/app/Filament/Resources/Agents/AgentResource.php"
click node_client_resource "https://github.com/raedbari/ai-agent-platform/blob/main/app/Filament/Resources/Clients/ClientResource.php"
click node_agent_controller "https://github.com/raedbari/ai-agent-platform/blob/main/app/Http/Controllers/AgentController.php"
click node_domain_models "https://github.com/raedbari/ai-agent-platform/blob/main/app/Models/KnowledgeBase.php"
click node_database "https://github.com/raedbari/ai-agent-platform/blob/main/database/migrations/2026_07_20_131430_create_knowledge_chunks_table.php"
click node_kb_observer "https://github.com/raedbari/ai-agent-platform/blob/main/app/Observers/KnowledgeBaseObserver.php"
click node_index_job "https://github.com/raedbari/ai-agent-platform/blob/main/app/Jobs/IndexKnowledgeBase.php"
click node_knowledge_pipeline "https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgePipelineService.php"
click node_content_service "https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgeContentService.php"
click node_chunker "https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgeChunker.php"
click node_embedding_service "https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/EmbeddingService.php"
click node_retrieval "https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgeRetrievalService.php"
click node_ai_service "https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/AIService.php"
click node_model_provider "https://github.com/raedbari/ai-agent-platform/blob/main/config/gemini.php"
click node_reindex_command "https://github.com/raedbari/ai-agent-platform/blob/main/app/Console/Commands/ReindexKnowledge.php"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_laravel,node_routes,node_playground,node_filament,node_agent_resource,node_client_resource toneBlue
class node_domain_models,node_database toneAmber
class node_kb_observer,node_index_job,node_knowledge_pipeline,node_content_service,node_chunker,node_embedding_service,node_reindex_command toneMint
class node_agent_controller,node_retrieval,node_ai_service,node_model_provider toneRose
```

## Components

### Interfaces

- [Laravel application](https://github.com/raedbari/ai-agent-platform/blob/main/bootstrap/app.php): runtime · HTTP, Artisan, queues, and Eloquent runtime. · `bootstrap/app.php`
- [Web & API routes](https://github.com/raedbari/ai-agent-platform/blob/main/routes/api.php): routing · `routes/api.php`
- [Chat playground](https://github.com/raedbari/ai-agent-platform/blob/main/resources/views/playground.blade.php): browser view · `resources/views/playground.blade.php`
- [Filament admin panel](https://github.com/raedbari/ai-agent-platform/blob/main/app/Providers/Filament/NezarPanelProvider.php): admin interface · `app/Providers/Filament/NezarPanelProvider.php`
- [Agent administration](https://github.com/raedbari/ai-agent-platform/blob/main/app/Filament/Resources/Agents/AgentResource.php): Filament resource · `app/Filament/Resources/Agents/AgentResource.php`
- [Client administration](https://github.com/raedbari/ai-agent-platform/blob/main/app/Filament/Resources/Clients/ClientResource.php): Filament resource · `app/Filament/Resources/Clients/ClientResource.php`

### Domain & Storage

- [Agent, client & knowledge models](https://github.com/raedbari/ai-agent-platform/blob/main/app/Models/KnowledgeBase.php): Eloquent models · Defines tenancy, agents, knowledge bases, and chunks. · `app/Models/KnowledgeBase.php`
- [Relational database](https://github.com/raedbari/ai-agent-platform/blob/main/database/migrations/2026_07_20_131430_create_knowledge_chunks_table.php): system of record · Stores metadata, indexing state, chunks, and embeddings. · `database/migrations/2026_07_20_131430_create_knowledge_chunks_table.php`

### Knowledge Indexing

- [Knowledge base observer](https://github.com/raedbari/ai-agent-platform/blob/main/app/Observers/KnowledgeBaseObserver.php): Eloquent observer · `app/Observers/KnowledgeBaseObserver.php`
- [Index knowledge-base job](https://github.com/raedbari/ai-agent-platform/blob/main/app/Jobs/IndexKnowledgeBase.php): queued job · `app/Jobs/IndexKnowledgeBase.php`
- [Knowledge pipeline](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgePipelineService.php): indexing orchestrator · Coordinates extraction, chunking, embedding, and persistence. · `app/Services/KnowledgePipelineService.php`
- [Content extraction](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgeContentService.php): source content service · `app/Services/KnowledgeContentService.php`
- [Knowledge chunker](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgeChunker.php): chunking service · `app/Services/KnowledgeChunker.php`
- [Embedding service](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/EmbeddingService.php): provider adapter · Generates document and query embeddings. · `app/Services/EmbeddingService.php`
- [Knowledge reindex command](https://github.com/raedbari/ai-agent-platform/blob/main/app/Console/Commands/ReindexKnowledge.php): Artisan command · `app/Console/Commands/ReindexKnowledge.php`

### Chat & RAG

- [Agent controller](https://github.com/raedbari/ai-agent-platform/blob/main/app/Http/Controllers/AgentController.php): HTTP controller · Converges agent-facing chat requests. · `app/Http/Controllers/AgentController.php`
- [Knowledge retrieval](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/KnowledgeRetrievalService.php): RAG retrieval service · Scores stored chunks and selects permitted context. · `app/Services/KnowledgeRetrievalService.php`
- [AI generation service](https://github.com/raedbari/ai-agent-platform/blob/main/app/Services/AIService.php): model provider adapter · Builds generation requests from instructions, context, and message. · `app/Services/AIService.php`
- [Gemini & embedding providers](https://github.com/raedbari/ai-agent-platform/blob/main/config/gemini.php): external AI services · `config/gemini.php`

## Connections

- Laravel application → Web & API routes: serves
- Web & API routes → Agent controller: chat requests
- Chat playground → Agent controller: chat preview
- Filament admin panel → Agent administration: hosts
- Filament admin panel → Client administration: hosts
- Agent administration → Agent, client & knowledge models: manages
- Client administration → Agent, client & knowledge models: manages
- Agent, client & knowledge models → Relational database: persists
- Agent, client & knowledge models → Knowledge base observer: knowledge-base events
- Knowledge base observer → Index knowledge-base job: dispatches
- Knowledge reindex command → Index knowledge-base job: queues reindexing
- Index knowledge-base job → Knowledge pipeline: runs
- Knowledge pipeline → Content extraction: extracts source
- Knowledge pipeline → Knowledge chunker: splits content
- Knowledge pipeline → Embedding service: embeds chunks
- Knowledge pipeline → Relational database: stores chunks
- Embedding service → Gemini & embedding providers: embedding calls
- Agent controller → Knowledge retrieval: retrieves context
- Knowledge retrieval → Embedding service: embeds query
- Knowledge retrieval → Relational database: scores chunks
- Agent controller → AI generation service: requests response
- Knowledge retrieval → AI generation service: selected context
- AI generation service → Gemini & embedding providers: generation calls
