Skip to content

Latest commit

Β 

History

History
541 lines (440 loc) Β· 18.1 KB

File metadata and controls

541 lines (440 loc) Β· 18.1 KB

Project Architecture β€” Behflow

🎯 System Goals

  • AI-Powered: Intelligent task management using LangGraph and LLM orchestration
  • Self-Hosted: Complete data ownership and control
  • Lightweight: Minimal dependencies, fast startup, low resource usage
  • Scalable: Horizontal scaling support via Docker Swarm/Kubernetes
  • Maintainable: Clean separation of concerns, modular architecture
  • Developer-Friendly: Comprehensive documentation, clear structure

πŸ— High-Level Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                         Frontend                            β”‚
β”‚         (Vanilla JS + HTML5 + CSS3 + Nginx)                β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
β”‚  β”‚   Task UI    β”‚  β”‚   Chat UI    β”‚  β”‚    Auth UI   β”‚     β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚                  β”‚                  β”‚
          β”‚     REST API     β”‚     REST API     β”‚  REST API
          β”‚                  β”‚                  β”‚
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      Backend (FastAPI)                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚  Auth      β”‚  β”‚  Task      β”‚  β”‚  Automated         β”‚    β”‚
β”‚  β”‚  Router    β”‚  β”‚  Router    β”‚  β”‚  Process Scheduler β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚        β”‚               β”‚                     β”‚               β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚            Database Service Layer                     β”‚   β”‚
β”‚  β”‚  (Auth, Task, Chat, Process Services)                 β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                         β”‚
            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
            β”‚            β”‚            β”‚
    β”Œβ”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”   β”Œβ”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
    β”‚  PostgreSQL  β”‚   β”‚   Agent   β”‚ β”‚
    β”‚   Database   β”‚   β”‚  Service  β”‚ β”‚
    β”‚              β”‚   β”‚(LangGraph)β”‚ β”‚
    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
                           β”‚         β”‚
                      β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”   β”‚
                      β”‚   LLM    β”‚   β”‚
                      β”‚Providers β”‚   β”‚
                      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚

🧩 Component Details

1. Frontend Layer

Technology: Vanilla JavaScript, HTML5, CSS3, Nginx

Responsibilities:

  • User interface for task management
  • AI chat interface
  • Authentication flows
  • Theme management (dark/light)
  • State management via LocalStorage

Key Files:

  • index.html - Main application
  • app.js - Task and chat logic
  • login.js / register.js - Authentication
  • theme.js - Theme switching
  • styles.css - Atom One Dark Pro theme

Communication:

  • REST API calls to backend
  • Token-based authentication
  • Real-time updates (planned: WebSocket)

2. Backend Layer (FastAPI)

Technology: FastAPI, Python 3.11+, Uvicorn

Responsibilities:

  • RESTful API endpoints
  • Business logic orchestration
  • Authentication and authorization
  • Database operations
  • Background task scheduling
  • Integration with AI agent

API Routers:

/register                 - User registration
/login                    - User authentication
/me                       - Current user info
/api/v1/tasks             - Task CRUD operations
/api/v1/chat              - AI chat interactions
/api/v1/processes         - Automated processes
/health                   - Health check

Services:

  • AuthService - User management
  • TaskService - Task operations
  • ChatService - Chat sessions
  • AutomatedProcessService - Process management

Middleware:

  • CORS handling
  • Error handling
  • Request logging
  • Rate limiting (optional)

3. Database Layer

Technology: PostgreSQL 15+, SQLAlchemy ORM

Schema:

  • users - User accounts
  • tasks - Task storage
  • chat_sessions - Chat sessions
  • chat_messages - Message history
  • automated_processes - Process definitions
  • automated_process_executions - Execution logs

Features:

  • Connection pooling
  • Transaction management
  • Migration support (Alembic)
  • Timezone-aware timestamps
  • JSONB for flexible data

4. Agent Service (LangGraph)

Technology: LangGraph, LangChain, OpenAI/Anthropic

Responsibilities:

  • Natural language understanding
  • Intent classification
  • Tool execution
  • Response generation
  • Context management

Graph Nodes:

  1. Entry Node - Initialize conversation
  2. Intent Classification - Understand user request
  3. Tool Execution - Execute actions
  4. Response Generation - Generate natural language response

Tools:

  • add_task - Create new tasks
  • update_task - Modify existing tasks
  • list_tasks - Query tasks
  • delete_task - Remove tasks

State Management:

  • Conversation history
  • User context
  • Active tasks
  • Pending actions

5. Scheduler Service

Technology: APScheduler

Responsibilities:

  • Time-based process execution
  • Cron-like scheduling
  • Job monitoring
  • Error handling and retries

Processes:

  • Task rescheduling
  • Session cleanup
  • Reminder notifications
  • Analytics generation

πŸ”„ Request Flow Diagrams

User Registration Flow

User β†’ Frontend β†’ Backend β†’ Database
  β”‚       β”‚         β”‚          β”‚
  β”‚       β”‚         β”‚  Create User
  β”‚       β”‚         │◄──────────
  β”‚       β”‚  Return Token       β”‚
  β”‚       │◄─────────           β”‚
  β”‚  Display Successβ”‚           β”‚
  │◄───────         β”‚           β”‚

Task Creation via Chat

User β†’ Frontend β†’ Backend β†’ Agent β†’ LLM Provider
  β”‚       β”‚         β”‚        β”‚         β”‚
  β”‚  "Create task" β”‚         β”‚         β”‚
  │──────►│         β”‚         β”‚         β”‚
  β”‚       β”‚  POST /chat       β”‚         β”‚
  β”‚       │────────►│         β”‚         β”‚
  β”‚       β”‚         β”‚ Invoke  β”‚         β”‚
  β”‚       β”‚         │────────►│         β”‚
  β”‚       β”‚         β”‚         β”‚  Analyze
  β”‚       β”‚         β”‚         │────────►│
  β”‚       β”‚         β”‚         │◄────────│
  β”‚       β”‚         β”‚         β”‚         β”‚
  β”‚       β”‚         β”‚    add_task()     β”‚
  β”‚       β”‚         │◄────────│         β”‚
  β”‚       β”‚         β”‚                   β”‚
  β”‚       β”‚         β”‚  Save to DB       β”‚
  β”‚       β”‚         β”œβ”€β”€β”€β”€β”€β”€β–Ί Database   β”‚
  β”‚       β”‚  Response with task         β”‚
  β”‚       │◄─────────                   β”‚
  β”‚  Show task card β”‚                   β”‚
  │◄───────         β”‚                   β”‚

Automated Process Execution

Scheduler β†’ AutomatedProcessService β†’ Process Executor β†’ Database
    β”‚              β”‚                        β”‚               β”‚
  Trigger          β”‚                        β”‚               β”‚
    │──────────────►│                        β”‚               β”‚
    β”‚              β”‚  Get Process Config    β”‚               β”‚
    β”‚              β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚               β”‚
    β”‚              β”‚                        β”‚               β”‚
    β”‚              β”‚       Execute          β”‚               β”‚
    β”‚              │◄───────────────────────│               β”‚
    β”‚              β”‚                        β”‚               β”‚
    β”‚              β”‚                   Update Tasks         β”‚
    β”‚              β”‚                        β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚
    β”‚              β”‚  Log Execution         β”‚               β”‚
    β”‚              β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β–Ίβ”‚

πŸ—‚ Repository Structure (implemented)

/ (repo root)
β”œβ”€ docs/                        # πŸ“š Comprehensive documentation
β”‚  β”œβ”€ architecture.md           # System architecture (this file)
β”‚  β”œβ”€ API.md                    # REST API documentation
β”‚  β”œβ”€ AGENT.md                  # AI agent system guide
β”‚  β”œβ”€ DATABASE.md               # Database schema & services
β”‚  β”œβ”€ FRONTEND.md               # Frontend architecture
β”‚  β”œβ”€ INTEGRATION.md            # Integration guide
β”‚  β”œβ”€ DEPLOYMENT.md             # Deployment strategies
β”‚  β”œβ”€ CONFIGURATION.md          # Configuration options
β”‚  β”œβ”€ TESTING.md                # Testing guide
β”‚  β”œβ”€ automated_processes.md    # Process automation
β”‚  └─ README.md                 # Documentation index
β”œβ”€ src/
β”‚  β”œβ”€ frontend/                 # 🌐 Static frontend
β”‚  β”‚  β”œβ”€ index.html            # Main app page
β”‚  β”‚  β”œβ”€ login.html            # Authentication
β”‚  β”‚  β”œβ”€ app.js                # Task & chat logic
β”‚  β”‚  β”œβ”€ theme.js              # Theme management
β”‚  β”‚  β”œβ”€ styles.css            # Styling
β”‚  β”‚  β”œβ”€ nginx.conf            # Server config
β”‚  β”‚  └─ Dockerfile            # Container definition
β”‚  β”œβ”€ backend/                  # πŸ–₯ FastAPI backend
β”‚  β”‚  β”œβ”€ app/
β”‚  β”‚  β”‚  β”œβ”€ api/
β”‚  β”‚  β”‚  β”‚  β”œβ”€ routers/        # API endpoints
β”‚  β”‚  β”‚  β”‚  β”‚  β”œβ”€ auth.py      # Authentication routes
β”‚  β”‚  β”‚  β”‚  β”‚  └─ chat.py      # Chat routes
β”‚  β”‚  β”‚  β”‚  └─ models/         # Request/response models
β”‚  β”‚  β”‚  β”œβ”€ database/
β”‚  β”‚  β”‚  β”‚  β”œβ”€ models.py       # SQLAlchemy models
β”‚  β”‚  β”‚  β”‚  β”œβ”€ database.py     # Connection management
β”‚  β”‚  β”‚  β”‚  β”œβ”€ auth_service.py # User operations
β”‚  β”‚  β”‚  β”‚  β”œβ”€ task_service.py # Task operations
β”‚  β”‚  β”‚  β”‚  β”œβ”€ chat_service.py # Chat operations
β”‚  β”‚  β”‚  β”‚  └─ automated_process_service.py
β”‚  β”‚  β”‚  β”œβ”€ main.py            # FastAPI app
β”‚  β”‚  β”‚  └─ scheduler.py       # Background scheduler
β”‚  β”‚  β”œβ”€ requirements.txt      # Python dependencies
β”‚  β”‚  └─ Dockerfile
β”‚  β”œβ”€ behflow_agent/            # πŸ€– AI Agent service
β”‚  β”‚  β”œβ”€ nodes/
β”‚  β”‚  β”‚  └─ graph_nodes.py     # LangGraph nodes
β”‚  β”‚  β”œβ”€ models/
β”‚  β”‚  β”‚  β”œβ”€ task.py            # Task model
β”‚  β”‚  β”‚  β”œβ”€ automated_process.py
β”‚  β”‚  β”‚  └─ models.py          # Agent state
β”‚  β”‚  β”œβ”€ agent.py              # Main agent class
β”‚  β”‚  β”œβ”€ builder.py            # Agent factory
β”‚  β”‚  β”œβ”€ tools.py              # LangChain tools
β”‚  β”‚  β”œβ”€ llm_config.py         # LLM configuration
β”‚  β”‚  β”œβ”€ users.py              # User context
β”‚  β”‚  └─ utils.py              # Utilities
β”‚  └─ shared/
β”‚     └─ logger.py             # Logging utilities
β”œβ”€ tests/                       # πŸ§ͺ Test suites
β”‚  β”œβ”€ backend/                 # Backend unit tests
β”‚  β”œβ”€ frontend/                # Frontend tests
β”‚  └─ agent/                   # Agent tests
β”œβ”€ infra/
β”‚  β”œβ”€ docker-compose.yml       # Local development
β”‚  └─ migrations/              # Database migrations
β”œβ”€ .github/
β”‚  └─ workflows/               # CI/CD pipelines
β”œβ”€ .env.example                # Environment template
β”œβ”€ README.md                   # Project overview
└─ LICENSE                     # MIT License

οΏ½ Security Architecture

Authentication Flow

1. User provides credentials
2. Backend validates against database
3. Generate JWT token with expiration
4. Client stores token in LocalStorage
5. Include token in Authorization header for API requests
6. Backend validates token on each request

Security Measures

  • Password Hashing: BCrypt with salt
  • JWT Tokens: Short expiration (24h default)
  • HTTPS Only: TLS 1.2+ in production
  • CORS Protection: Whitelisted origins
  • SQL Injection Prevention: Parameterized queries
  • Rate Limiting: Per-IP and per-user limits
  • Input Validation: Pydantic models
  • Secret Management: Environment variables, never hardcoded

πŸ“Š Data Flow Patterns

Task Lifecycle

1. CREATE: User creates task via chat or form
   ↓
2. STORE: Save to database with NOT_STARTED status
   ↓
3. UPDATE: Status changes (IN_PROGRESS, COMPLETED, BLOCKED)
   ↓
4. NOTIFY: Trigger webhooks/notifications
   ↓
5. ARCHIVE/DELETE: Cleanup or permanent removal

Chat Session Lifecycle

1. INITIATE: User sends first message
   ↓
2. CREATE SESSION: Generate session_id
   ↓
3. PROCESS: Agent analyzes and responds
   ↓
4. STORE: Save messages to database
   ↓
5. CONTINUE: Maintain context across messages
   ↓
6. EXPIRE: Auto-cleanup after inactivity

πŸš€ Scalability Strategy

Horizontal Scaling

Backend:

  • Stateless design
  • Load balancing with Nginx/Traefik
  • Auto-scaling based on CPU/memory

Database:

  • Read replicas for queries
  • Connection pooling
  • Query optimization

Agent Service:

  • Async/await for concurrency
  • LLM request batching
  • Response caching

Vertical Scaling

  • Increase container resources
  • Optimize database indices
  • Cache frequently accessed data

πŸ”§ Technology Stack Summary

Component Technology Purpose
Frontend Vanilla JS + HTML5 + CSS3 Lightweight UI
Backend FastAPI + Python 3.11 REST API
Database PostgreSQL 15 Data persistence
Agent LangGraph + LangChain AI orchestration
LLM OpenAI/Anthropic Natural language
Scheduler APScheduler Background jobs
Server Nginx Static files + proxy
Container Docker + Docker Compose Deployment
Orchestration K8s / Docker Swarm Scaling

🎯 Design Principles

  1. Separation of Concerns: Clear boundaries between layers
  2. Single Responsibility: Each component has one job
  3. Dependency Injection: Loose coupling via interfaces
  4. Configuration Over Code: Environment-based settings
  5. Fail Fast: Validate early, fail gracefully
  6. Idempotency: Safe to retry operations
  7. Observability: Comprehensive logging and monitoring

πŸ”„ Development Workflow

Local Development

# 1. Start infrastructure
docker-compose up -d db

# 2. Run backend
cd src/backend
python -m uvicorn app.main:app --reload

# 3. Serve frontend
cd src/frontend
python -m http.server 8080

# 4. Access application
open http://localhost:8080

Testing Workflow

# Run unit tests
pytest tests/

# Run with coverage
pytest --cov=src tests/

# Run integration tests
pytest tests/integration/

# Run end-to-end tests
pytest tests/e2e/

Deployment Workflow

# 1. Build images
docker build -t behflow/backend:latest ./src/backend
docker build -t behflow/frontend:latest ./src/frontend

# 2. Push to registry
docker push behflow/backend:latest
docker push behflow/frontend:latest

# 3. Deploy to production
kubectl apply -f k8s/
# OR
docker stack deploy -c docker-stack.yml behflow

πŸ“ˆ Future Enhancements

Planned Features

  • WebSocket support for real-time updates
  • Voice interface integration
  • Multi-agent collaboration
  • Vector database for long-term memory
  • Mobile app (React Native/Flutter)
  • Browser extension
  • Offline mode with sync
  • Analytics dashboard
  • Custom LLM integration
  • Plugin system

Technical Improvements

  • GraphQL API alongside REST
  • Event sourcing for audit trail
  • CQRS pattern for scalability
  • Redis caching layer
  • Elasticsearch for full-text search
  • Prometheus metrics
  • Distributed tracing (Jaeger)
  • API versioning
  • Rate limiting per user
  • Multi-tenancy support

πŸ“š Architecture Documentation

For detailed information about each component, see:


This architecture is designed to be simple, scalable, and maintainable. It prioritizes developer experience while maintaining production-readiness.