|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Repository Overview |
| 6 | + |
| 7 | +Agentex is a comprehensive platform for building and deploying intelligent agents. This repository contains: |
| 8 | + |
| 9 | +- **agentex/** - Backend services (FastAPI + Temporal workflows) |
| 10 | +- **agentex-sgp-app/** - Developer UI for interacting with agents |
| 11 | + |
| 12 | +The platform integrates with the separate [agentex-python SDK](https://github.com/scaleapi/scale-agentex-python) for creating and running agents. |
| 13 | + |
| 14 | +## Development Environment Setup |
| 15 | + |
| 16 | +### Prerequisites |
| 17 | + |
| 18 | +- Python 3.12+ (required for agentex-sdk) |
| 19 | +- Docker and Docker Compose |
| 20 | +- Node.js (for frontend) |
| 21 | +- uv (Python package manager) |
| 22 | + |
| 23 | +### Quick Start (3-Terminal Setup) |
| 24 | + |
| 25 | +**Terminal 1 - Backend:** |
| 26 | +```bash |
| 27 | +cd agentex/ |
| 28 | +make dev # Starts Docker services and backend |
| 29 | +``` |
| 30 | + |
| 31 | +**Terminal 2 - Frontend:** |
| 32 | +```bash |
| 33 | +cd agentex-sgp-app/ |
| 34 | +npm install |
| 35 | +npm run dev # Starts Next.js dev server |
| 36 | +``` |
| 37 | + |
| 38 | +**Terminal 3 - Agent Development:** |
| 39 | +```bash |
| 40 | +export ENVIRONMENT=development |
| 41 | +agentex init # Create a new agent |
| 42 | +cd your-agent-name/ |
| 43 | +agentex agents run --manifest manifest.yaml |
| 44 | +``` |
| 45 | + |
| 46 | +### Backend Services (Docker Compose) |
| 47 | + |
| 48 | +When running `make dev` in agentex/, the following services start: |
| 49 | + |
| 50 | +- **Port 5003**: FastAPI backend server |
| 51 | +- **Port 5432**: PostgreSQL (application database) |
| 52 | +- **Port 5433**: PostgreSQL (Temporal database) |
| 53 | +- **Port 6379**: Redis (streams and caching) |
| 54 | +- **Port 27017**: MongoDB (document storage) |
| 55 | +- **Port 7233**: Temporal server |
| 56 | +- **Port 8080**: Temporal Web UI |
| 57 | + |
| 58 | +All services are networked via `agentex-network` bridge network. |
| 59 | + |
| 60 | +## Common Development Commands |
| 61 | + |
| 62 | +### Backend (agentex/) |
| 63 | + |
| 64 | +```bash |
| 65 | +# Setup and installation |
| 66 | +make install # Install dependencies with uv |
| 67 | +make install-dev # Install with dev dependencies (includes pre-commit) |
| 68 | +make clean # Clean venv and lock files |
| 69 | + |
| 70 | +# Development server |
| 71 | +make dev # Start all Docker services |
| 72 | +make dev-stop # Stop Docker services |
| 73 | +make dev-wipe # Stop services and wipe volumes |
| 74 | + |
| 75 | +# Database migrations |
| 76 | +make migration NAME="description" # Create new Alembic migration |
| 77 | +make apply-migrations # Apply pending migrations |
| 78 | + |
| 79 | +# Testing |
| 80 | +make test # Run all tests |
| 81 | +make test FILE=tests/unit/ # Run unit tests |
| 82 | +make test FILE=tests/unit/test_foo.py # Run specific test file |
| 83 | +make test NAME=crud # Run tests matching pattern |
| 84 | +make test-unit # Unit tests shortcut |
| 85 | +make test-integration # Integration tests shortcut |
| 86 | +make test-cov # Run with coverage report |
| 87 | +make test-docker-check # Verify Docker setup for tests |
| 88 | + |
| 89 | +# Documentation |
| 90 | +make serve-docs # Serve MkDocs on localhost:8001 |
| 91 | +make build-docs # Build documentation |
| 92 | + |
| 93 | +# Deployment |
| 94 | +make docker-build # Build production Docker image |
| 95 | +``` |
| 96 | + |
| 97 | +### Frontend (agentex-sgp-app/) |
| 98 | + |
| 99 | +```bash |
| 100 | +npm install # Install npm dependencies |
| 101 | +npm run dev # Next.js dev server with Turbopack |
| 102 | +npm run build # Build production bundle |
| 103 | +npm run typecheck # TypeScript type checking |
| 104 | +npm run lint # Run ESLint |
| 105 | +npm run format # Run Prettier formatting |
| 106 | +npm test # Run tests |
| 107 | +``` |
| 108 | + |
| 109 | +### Agent Development (agentex-sdk) |
| 110 | + |
| 111 | +```bash |
| 112 | +# Always set this first |
| 113 | +export ENVIRONMENT=development |
| 114 | + |
| 115 | +# Agent management |
| 116 | +agentex init # Create new agent |
| 117 | +agentex agents run --manifest manifest.yaml # Run agent locally (dev) |
| 118 | +agentex agents list # List all agents |
| 119 | +agentex agents build --manifest manifest.yaml --push # Build & push image |
| 120 | +agentex agents deploy --manifest manifest.yaml # Deploy to staging |
| 121 | + |
| 122 | +# Package management (if using uv in agent) |
| 123 | +agentex uv sync # Sync dependencies |
| 124 | +agentex uv add requests # Add new dependency |
| 125 | + |
| 126 | +# Other utilities |
| 127 | +agentex tasks list # View agent tasks |
| 128 | +agentex secrets create # Manage secrets |
| 129 | +``` |
| 130 | + |
| 131 | +## Architecture |
| 132 | + |
| 133 | +### Domain-Driven Design Structure |
| 134 | + |
| 135 | +The backend (`agentex/src/`) follows a clean architecture with strict layer separation: |
| 136 | + |
| 137 | +``` |
| 138 | +src/ |
| 139 | +├── api/ # FastAPI routes, middleware, request/response schemas |
| 140 | +│ ├── routes/ # API endpoints (agents, tasks, messages, spans, etc.) |
| 141 | +│ ├── schemas/ # Pydantic request/response models |
| 142 | +│ ├── authentication_middleware.py |
| 143 | +│ └── app.py # FastAPI application setup |
| 144 | +├── domain/ # Business logic (framework-agnostic) |
| 145 | +│ ├── entities/ # Core domain models |
| 146 | +│ ├── repositories/ # Data access interfaces |
| 147 | +│ ├── services/ # Domain services |
| 148 | +│ └── use_cases/ # Application use cases |
| 149 | +├── adapters/ # External integrations |
| 150 | +│ ├── crud_store/ # Database adapters (PostgreSQL, MongoDB) |
| 151 | +│ ├── streams/ # Redis stream adapter |
| 152 | +│ ├── authentication/ # Auth proxy adapter |
| 153 | +│ └── authorization/ # Authz proxy adapter |
| 154 | +├── config/ # Configuration and dependencies |
| 155 | +│ ├── dependencies.py # Singleton for global dependencies (DB, Temporal, Redis) |
| 156 | +│ └── mongodb_indexes.py |
| 157 | +└── utils/ # Shared utilities |
| 158 | +``` |
| 159 | + |
| 160 | +**Key principles:** |
| 161 | +- Domain layer has no dependencies on frameworks or adapters |
| 162 | +- API layer handles HTTP concerns, delegates to use cases |
| 163 | +- Adapters implement ports defined in domain layer |
| 164 | +- Dependencies flow inward (API → Domain ← Adapters) |
| 165 | + |
| 166 | +### Key Technologies |
| 167 | + |
| 168 | +- **FastAPI**: Web framework with automatic OpenAPI docs at `/swagger` and `/api` |
| 169 | +- **Temporal**: Workflow orchestration for long-running agent tasks |
| 170 | +- **PostgreSQL**: Primary relational database (SQLAlchemy + Alembic migrations) |
| 171 | +- **MongoDB**: Document storage for flexible schemas |
| 172 | +- **Redis**: Streams for real-time communication and caching |
| 173 | +- **Docker**: Containerization and local development |
| 174 | + |
| 175 | +### Dependency Injection |
| 176 | + |
| 177 | +Global dependencies are managed via a Singleton pattern in `src/config/dependencies.py`: |
| 178 | + |
| 179 | +- `GlobalDependencies`: Singleton holding connections to Temporal, databases, Redis, etc. |
| 180 | +- FastAPI dependencies use `Annotated` types (e.g., `DDatabaseAsyncReadWriteEngine`) |
| 181 | +- Connection pools are configured with appropriate sizes for concurrency |
| 182 | +- Startup/shutdown lifecycle managed in `app.py` lifespan context |
| 183 | + |
| 184 | +### Testing Strategy |
| 185 | + |
| 186 | +Tests are organized by type and use different strategies: |
| 187 | + |
| 188 | +**Unit Tests** (`tests/unit/`): |
| 189 | +- Fast, isolated tests using mocks |
| 190 | +- Test domain logic, repositories, services |
| 191 | +- Marked with `@pytest.mark.unit` |
| 192 | + |
| 193 | +**Integration Tests** (`tests/integration/`): |
| 194 | +- Test with real dependencies using testcontainers |
| 195 | +- Postgres, Redis, MongoDB containers spun up automatically |
| 196 | +- Test API endpoints end-to-end |
| 197 | +- Marked with `@pytest.mark.integration` |
| 198 | + |
| 199 | +**Test Runner** (`scripts/run_tests.py`): |
| 200 | +- Automatically detects Docker environment |
| 201 | +- Handles testcontainer setup |
| 202 | +- Smart dependency installation |
| 203 | +- Run via `make test` with various options |
| 204 | + |
| 205 | +### Authentication & Authorization |
| 206 | + |
| 207 | +- **Authentication**: Custom `AgentexAuthMiddleware` verifies requests via external auth service |
| 208 | +- **Authorization**: Domain service (`authorization_service.py`) checks permissions |
| 209 | +- **API Keys**: Agent-specific keys stored in PostgreSQL (`agent_api_keys` table) |
| 210 | +- **Principal Context**: User/agent identity passed through request context |
| 211 | + |
| 212 | +### Key Domain Concepts |
| 213 | + |
| 214 | +- **Agents**: Autonomous entities that execute tasks, managed via ACP protocol |
| 215 | +- **Tasks**: Work units with lifecycle states (pending → running → completed/failed) |
| 216 | +- **Messages**: Communication between system and agents (stored in MongoDB) |
| 217 | +- **Spans**: Execution traces for observability (OpenTelemetry-style) |
| 218 | +- **Events**: Domain events for async communication |
| 219 | +- **States**: Key-value state storage for agents |
| 220 | +- **Deployment History**: Track agent deployment versions and changes |
| 221 | + |
| 222 | +### Frontend Architecture (agentex-sgp-app/) |
| 223 | + |
| 224 | +- **Framework**: Next.js 15 with React 19 and App Router |
| 225 | +- **Styling**: Tailwind CSS with Radix UI components |
| 226 | +- **State**: React hooks and context |
| 227 | +- **Forms**: React Hook Form with Zod validation |
| 228 | +- **UI Components**: Custom components built on Radix primitives |
| 229 | + |
| 230 | +## Important Notes |
| 231 | + |
| 232 | +### Environment Variables |
| 233 | + |
| 234 | +For local development, always set: |
| 235 | +```bash |
| 236 | +export ENVIRONMENT=development |
| 237 | +``` |
| 238 | + |
| 239 | +Backend services read from: |
| 240 | +- `DATABASE_URL`: PostgreSQL connection string |
| 241 | +- `TEMPORAL_ADDRESS`: Temporal server address |
| 242 | +- `REDIS_URL`: Redis connection string |
| 243 | +- `MONGODB_URI`: MongoDB connection string |
| 244 | +- `MONGODB_DATABASE_NAME`: MongoDB database name |
| 245 | + |
| 246 | +Check `agentex/docker-compose.yml` for default values. |
| 247 | + |
| 248 | +### Database Migrations |
| 249 | + |
| 250 | +Always create migrations when changing models: |
| 251 | +1. Modify SQLAlchemy models in `database/models/` |
| 252 | +2. Run `make migration NAME="description"` from `agentex/` |
| 253 | +3. Review generated migration in `database/migrations/versions/` |
| 254 | +4. Apply with `make apply-migrations` |
| 255 | + |
| 256 | +Migrations run automatically during `make dev` startup. |
| 257 | + |
| 258 | +### Redis Port Conflicts |
| 259 | + |
| 260 | +If you have local Redis running, it conflicts with Docker Redis on port 6379: |
| 261 | +```bash |
| 262 | +# macOS |
| 263 | +brew services stop redis |
| 264 | + |
| 265 | +# Linux |
| 266 | +sudo systemctl stop redis-server |
| 267 | +``` |
| 268 | + |
| 269 | +### Working with Temporal |
| 270 | + |
| 271 | +- Access Temporal UI at http://localhost:8080 |
| 272 | +- Workflows are defined using temporalio Python SDK |
| 273 | +- Task queues are used to route work to agents |
| 274 | +- Workflow state persists across service restarts |
| 275 | + |
| 276 | +### API Documentation |
| 277 | + |
| 278 | +- Swagger UI: http://localhost:5003/swagger (interactive) |
| 279 | +- ReDoc: http://localhost:5003/api (readable) |
| 280 | +- OpenAPI spec: http://localhost:5003/openapi.json |
| 281 | + |
| 282 | +## Adding New Features |
| 283 | + |
| 284 | +### Adding a New API Endpoint |
| 285 | + |
| 286 | +1. Define domain entity in `src/domain/entities/` |
| 287 | +2. Create repository interface in `src/domain/repositories/` |
| 288 | +3. Implement repository in `src/adapters/crud_store/` |
| 289 | +4. Create use case in `src/domain/use_cases/` |
| 290 | +5. Define request/response schemas in `src/api/schemas/` |
| 291 | +6. Create route in `src/api/routes/` |
| 292 | +7. Register router in `src/api/app.py` |
| 293 | +8. Write tests in `tests/unit/` and `tests/integration/` |
| 294 | + |
| 295 | +### Adding Database Tables |
| 296 | + |
| 297 | +1. Create SQLAlchemy model in `database/models/` |
| 298 | +2. Generate migration: `make migration NAME="add_table_name"` |
| 299 | +3. Review and edit migration file if needed |
| 300 | +4. Apply migration: `make apply-migrations` |
| 301 | + |
| 302 | +### Adding MongoDB Collections |
| 303 | + |
| 304 | +1. Define indexes in `src/config/mongodb_indexes.py` |
| 305 | +2. Create entity in `src/domain/entities/` |
| 306 | +3. Implement CRUD operations in `src/adapters/crud_store/adapter_mongodb.py` |
| 307 | +4. Indexes are created automatically on startup |
| 308 | + |
| 309 | +## Repository Structure |
| 310 | + |
| 311 | +This repository contains two main components: |
| 312 | +- **Backend**: `agentex/src/`, `agentex/database/`, `agentex/tests/` |
| 313 | +- **Frontend**: `agentex-sgp-app/` |
0 commit comments