Blitz

System Architecture

Legion CLI is structured as a monorepo with the core engine in packages/opencode/. The system follows a modular architecture with clear separation of concerns.

High-Level Flow

User Input → Session → LLM Request → Provider API → Response → Tool Execution → Output
     ↑                                    ↓
     └──────────── Memory System ─────────┘

Core Components

1. Session Layer (src/session/)

Manages conversation state, message history, and LLM interactions.

2. Tool System (src/tool/)

Provides capabilities to the AI agent.

3. Provider System (src/provider/)

Handles communication with AI model providers.

4. Plugin System (src/plugin/)

Extends functionality through plugins.

5. Memory System (src/kilocode/memory/)

Persistent project knowledge.

Data Flow

┌─────────────┐     ┌──────────────┐     ┌─────────────┐
│   TUI/CLI   │────▶│   Session    │────▶│  LLM Layer  │
└─────────────┘     └──────────────┘     └─────────────┘
                           │                    │
                           ▼                    ▼
                    ┌──────────────┐     ┌─────────────┐
                    │    Tools     │     │  Providers  │
                    └──────────────┘     └─────────────┘
                           │                    │
                           ▼                    ▼
                    ┌──────────────┐     ┌─────────────┐
                    │   Filesystem │     │  API calls  │
                    └──────────────┘     └─────────────┘

Key Design Decisions

  1. Effect-based Services - Core services use Effect for dependency injection and error handling
  2. Singleton State - Per-project state managed via InstanceState
  3. Tool Registration - Tools are registered declaratively and auto-discovered
  4. Prompt Assembly - System prompts are composed from multiple sources (soul, brain, provider, memory)
  5. Plugin Architecture - Extensible via hooks and MCP servers