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.
User Input → Session → LLM Request → Provider API → Response → Tool Execution → Output
↑ ↓
└──────────── Memory System ─────────┘
src/session/)Manages conversation state, message history, and LLM interactions.
system.ts - System prompt assembly (soul, brain, provider, memory)llm/request.ts - LLM request preparation and tool injectionprompt.ts - Prompt resolution and template handlinginstruction.ts - Loads instruction files from project and global configsrc/tool/)Provides capabilities to the AI agent.
registry.ts - Central tool registration and lookupbuiltin/ - Core tools (read, write, edit, bash, glob, grep, etc.)kilocode/tool/ - Extended tools (test runner, git, agent manager)src/provider/)Handles communication with AI model providers.
provider.ts - Provider definitions and model catalogstransform.ts - Request/response transformations per providererror.ts - Provider-specific error handlingsrc/plugin/)Extends functionality through plugins.
src/kilocode/memory/)Persistent project knowledge.
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ TUI/CLI │────▶│ Session │────▶│ LLM Layer │
└─────────────┘ └──────────────┘ └─────────────┘
│ │
▼ ▼
┌──────────────┐ ┌─────────────┐
│ Tools │ │ Providers │
└──────────────┘ └─────────────┘
│ │
▼ ▼
┌──────────────┐ ┌─────────────┐
│ Filesystem │ │ API calls │
└──────────────┘ └─────────────┘
InstanceState