Layered Design
NikCLI employs a sophisticated layered architecture that promotes separation of concerns, modularity, and maintainability. This document provides an in-depth analysis of each architectural layer, their responsibilities, and interaction patterns.Architectural Layers Overview
The system is organized into six primary layers, each with distinct responsibilities:Layer 1: Presentation Layer
Responsibilities
The presentation layer handles all user interactions and output rendering.Components
Terminal Interface (/src/cli/nik-cli.ts)
Core Functions:
- Command-line argument parsing
- Interactive chat interface
- Real-time streaming output
- User input handling
- Vim mode support
- Paste detection and handling
Advanced CLI UI (/src/cli/ui/advanced-cli-ui.ts)
Capabilities:
- Colored, formatted output
- Progress indicators (spinners, progress bars, dots)
- Tables and structured data display
- Syntax highlighting
- Interactive prompts
- Status panels
Terminal Output Manager (/src/cli/ui/terminal-output-manager.ts)
Responsibilities:
- Stream management
- Panel-based layout
- Real-time updates
- Buffer management
- Terminal capability detection
Diff Manager (/src/cli/ui/diff-manager.ts)
Features:
- Side-by-side diffs
- Unified diffs
- Syntax-highlighted diffs
- Interactive approval
- Batch operations
Web Dashboard (Optional) (/web)
Features:
- Real-time agent status
- Job queue visualization
- Metrics and analytics
- GitHub integration
- Subscription management
Layer Boundaries
Upward Communication: None (top layer) Downward Communication:- Sends commands to Orchestration Layer
- Receives events from Event System
- Subscribes to streaming updates
Layer 2: Orchestration Layer
Responsibilities
Coordinates system-wide operations and manages execution flow.Components
Main Orchestrator (/src/cli/main-orchestrator.ts)
Lifecycle Management:
Streaming Orchestrator (/src/cli/streaming-orchestrator.ts)
Message Queue System:
VM Orchestrator (/src/cli/virtualized-agents/vm-orchestrator.ts)
Container Management:
Layer Boundaries
Upward Communication:- Emits events to Presentation Layer
- Streams real-time updates
- Delegates to Agent Layer
- Uses Service Layer for business logic
- Coordinates multiple agents
Layer 3: Agent Layer
Responsibilities
Executes AI-driven tasks with domain-specific intelligence.Components
Universal Agent
Multi-Purpose Execution:Specialized Agents
Domain Expertise:Virtualized Agents
Isolated Execution:Agent Communication Patterns
Agent-to-Agent Communication:Layer Boundaries
Upward Communication:- Reports progress to Orchestration Layer
- Requests resources and permissions
- Uses Service Layer for operations
- Invokes tools via Tool Service
- Accesses context via Context Service
Layer 4: Service Layer
Responsibilities
Provides reusable business logic and coordinates complex operations.Components
Agent Service (/src/cli/services/agent-service.ts)
Agent Lifecycle:
Planning Service (/src/cli/services/planning-service.ts)
Plan Generation:
Memory Service (/src/cli/services/memory-service.ts)
Conversation Management:
LSP Service (/src/cli/services/lsp-service.ts)
Language Intelligence:
Tool Service (/src/cli/services/tool-service.ts)
Tool Orchestration:
Layer Boundaries
Upward Communication:- Provides APIs to Agent Layer
- Emits service-level events
- Uses Infrastructure Layer for providers
- Accesses Data Layer for persistence
Layer 5: Infrastructure Layer
Responsibilities
Provides foundational services and integrations.Components
AI Provider System
Model Routing:Context & RAG System
Workspace Indexing:Event System
Event Bus:Monitoring System
Telemetry:Layer Boundaries
Upward Communication:- Provides APIs to Service Layer
- Emits infrastructure events
- Uses Data Layer for persistence
- Makes external API calls
Layer 6: Data Layer
Responsibilities
Handles data persistence and retrieval.Components
Configuration Storage
Config Manager:State Persistence
Snapshot Service:Cache Storage
Multi-Tier Cache:Layer Boundaries
Upward Communication:- Provides data APIs to Infrastructure Layer
- None (bottom layer)
- File system operations
- Database connections
- External storage APIs
Cross-Cutting Concerns
Middleware Pipeline
The middleware system operates across all layers:Error Propagation
Errors flow upward through layers:Event Flow
Events can flow both up and down:Design Patterns
Dependency Injection
Factory Pattern
Strategy Pattern
Observer Pattern
Conclusion
NikCLI’s layered architecture provides:- Clear separation of concerns
- Testability at each layer
- Flexibility to swap implementations
- Scalability through horizontal and vertical scaling
- Maintainability with well-defined boundaries