# a16z-infra/ai-town architecture

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

- Interactive diagram: https://gitdiagram.com/a16z-infra/ai-town
- Repository: https://github.com/a16z-infra/ai-town

## Overview

##### Main Components Identification

a) Frontend Layer:
- React-based UI components (src/components)
- Game rendering engine (PixiJS)
- Client-side state management and hooks (src/hooks)

b) Backend Layer (Convex):
- Game Engine (convex/engine)
- AI Agent System (convex/agent)
- AI Town Core (convex/aiTown)
- Database and State Management
- Utility Services (convex/util)

c) External Services:
- LLM Integration (Ollama/Cloud Providers)
- Music Generation (Replicate)
- Authentication (Optional - Clerk)

**┌─────────────────────────────────────────────┐**
- Client Layer               │
- ┌─────────────┐ ┌─────────────┐           │
- React UI  │ │ Game Engine │           │
- ┘ └─────────────┘           │
- ┬─┘

**┌──────────────────────▼────────────────────▼─┐**
- Convex Backend                 │
- ┌─────────────┐ ┌─────────────┐           │
- Game Engine │ │  AI Agents  │           │
- ┘ └─────────────┘           │
- ┌─────────────┐ ┌─────────────┐           │
- Database   │ │  Utilities  │           │
- ┘ └─────────────┘           │
- ┬─────────────┬──────────────────┘

**┌───────────▼─┐ ┌────────▼────┐**
- LLM      │ │   Music     │
- Provider   │ │  Generator  │
- ┘ └─────────────┘

##### Component Details

a) Frontend (React + PixiJS):
- Relationship between React components and game rendering
- Real-time data flow with WebSocket connections
- User interaction points

b) Convex Backend:
- Game engine's core components
- AI agent management system
- Database interactions
- Utility services

c) External Services:
- API connections to LLM providers
- Music generation integration
- Authentication flow (if implemented)

##### Data Flow Indicators

- Real-time updates between frontend and backend
- AI agent interactions with the game world
- Database read/write operations
- External API calls

##### Additional Elements

- Critical paths
- System boundaries

##### Technical Details

- WebSocket connections for real-time updates
- Database schema relationships
- API endpoints
- Caching mechanisms
- State management flow

This system design should provide a clear understanding of how AI Town's components interact while maintaining the appropriate level of abstraction for a principal software engineer to understand the system's architecture and make informed decisions about future development or modifications.

Remember to include notes about scalability considerations, especially regarding the AI agent system and real-time game state management, as these are crucial aspects of the system's architecture.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        UI["React UI Components"]
        GE["Game Rendering Engine"]
        CSM["Client-side State Management"]
        RU["Real-time Updates"]
        HSM["Historical State Management"]
        
        UI --> GE
        UI --> CSM
        CSM --> RU
        CSM --> HSM
    end

    subgraph Backend
        subgraph GameCore
            GEng["Game Engine"]
            WM["World Map System"]
            PM["Player Management"]
            MS["Movement System"]
            CS["Conversation System"]
            IH["Input Handler"]
            GSM["Game State Management"]
            WLD["Game World Management"]
        end

        subgraph AISystem
            AIS["AI Agent System"]
            MEM["Agent Memory System"]
        end

        subgraph Utils
            US["Utility Services"]
            LLM["LLM Integration"]
            MUS["Music System"]
        end
    end

    subgraph Assets
        CA["Character Assets"]
        ANI["Animation System"]
    end

    Client --> GameCore
    GameCore --> AISystem
    AISystem --> Utils
    GameCore --> Utils
    Assets --> Client
    
    %% Style Definitions
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef backend fill:#2ecc71,stroke:#27ae60,color:white
    classDef ai fill:#e74c3c,stroke:#c0392b,color:white
    classDef utils fill:#f1c40f,stroke:#f39c12,color:black
    classDef assets fill:#9b59b6,stroke:#8e44ad,color:white

    %% Apply Styles
    class UI,GE,CSM,RU,HSM frontend
    class GEng,WM,PM,MS,CS,IH,GSM,WLD backend
    class AIS,MEM ai
    class US,LLM,MUS utils
    class CA,ANI assets

    %% Click Events
    click UI "https://github.com/a16z-infra/ai-town/tree/main/src/components/"
    click GE "https://github.com/a16z-infra/ai-town/blob/main/src/components/PixiGame.tsx"
    click CSM "https://github.com/a16z-infra/ai-town/tree/main/src/hooks/"
    click GEng "https://github.com/a16z-infra/ai-town/tree/main/convex/engine/"
    click AIS "https://github.com/a16z-infra/ai-town/tree/main/convex/agent/"
    click US "https://github.com/a16z-infra/ai-town/tree/main/convex/util/"
    click LLM "https://github.com/a16z-infra/ai-town/blob/main/convex/util/llm.ts"
    click MUS "https://github.com/a16z-infra/ai-town/blob/main/convex/music.ts"
    click WLD "https://github.com/a16z-infra/ai-town/blob/main/convex/aiTown/world.ts"
    click PM "https://github.com/a16z-infra/ai-town/blob/main/convex/aiTown/player.ts"
    click MS "https://github.com/a16z-infra/ai-town/blob/main/convex/aiTown/movement.ts"
    click CS "https://github.com/a16z-infra/ai-town/blob/main/convex/aiTown/conversation.ts"
    click MEM "https://github.com/a16z-infra/ai-town/blob/main/convex/agent/memory.ts"
    click IH "https://github.com/a16z-infra/ai-town/blob/main/convex/aiTown/inputHandler.ts"
    click WM "https://github.com/a16z-infra/ai-town/blob/main/convex/aiTown/worldMap.ts"
    click GSM "https://github.com/a16z-infra/ai-town/blob/main/convex/aiTown/game.ts"
    click CA "https://github.com/a16z-infra/ai-town/blob/main/data/characters.ts"
    click ANI "https://github.com/a16z-infra/ai-town/tree/main/data/animations/"
    click RU "https://github.com/a16z-infra/ai-town/blob/main/src/hooks/useWorldHeartbeat.ts"
    click HSM "https://github.com/a16z-infra/ai-town/blob/main/src/hooks/useHistoricalValue.ts"
```
