2026-01-08 11:46:34 +08:00
# Command Capability Design
## Context
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
This design document describes the technical implementation of the command capability for the BLS Project Console, which allows users to send console commands to Redis queues for other programs to read and execute.
## Goals / Non-Goals
### Goals
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- Implement command sending to Redis queues
- Provide command validation and error handling
- Maintain a history of sent commands
- Handle command responses from Redis
- Ensure high performance and reliability
### Non-Goals
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- Command execution or processing
- Complex command syntax highlighting
- Advanced command editing capabilities
## Decisions
### Decision: Redis Queue Implementation
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **What**: Use Redis List as the queue data structure
- **Why**: Redis Lists provide efficient push/pop operations with O(1) time complexity, making them ideal for message queues
2026-01-13 19:45:05 +08:00
- **Alternatives considered**:
2026-01-08 11:46:34 +08:00
- Redis Streams: More advanced but overkill for our use case
- Redis Pub/Sub: No persistence, so commands would be lost if the receiving program is down
### Decision: Command History Storage
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **What**: Store command history in memory with a configurable maximum size
- **Why**: In-memory storage provides fast access times and avoids the complexity of database management
2026-01-13 19:45:05 +08:00
- **Alternatives considered**:
2026-01-08 11:46:34 +08:00
- Database storage: Adds complexity and latency
- File system: Not suitable for real-time access
### Decision: Command Validation
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **What**: Implement basic command validation on both frontend and backend
- **Why**: Frontend validation provides immediate feedback to users, while backend validation ensures data integrity
2026-01-13 19:45:05 +08:00
- **Alternatives considered**:
2026-01-08 11:46:34 +08:00
- Only frontend validation: Less secure, as users could bypass it
- Only backend validation: Less responsive, as users would have to wait for server response
## Architecture
### Frontend Architecture
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
```
CommandView Component
├── CommandForm Component
├── CommandHistory Component
└── CommandService
```
### Backend Architecture
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
```
Command Routes
├── Command Service
│ ├── Redis Client
│ └── Command Manager
└── Command History Manager
```
## Implementation Details
### Redis Connection
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- Use the `redis` npm package to connect to Redis
- Implement automatic reconnection with exponential backoff
- Handle connection errors gracefully
### Command Sending
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
1. User enters a command in the frontend form
2. Frontend validates the command (not empty, no invalid characters)
3. Frontend sends a POST request to `/api/commands` with the command content
4. Backend validates the command again
5. Backend generates a unique command ID
6. Backend adds the command to the Redis queue using `LPUSH`
7. Backend stores the command in the in-memory command history
8. Backend sends a success response to the frontend
### Command Validation
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **Frontend validation**:
- Check that the command is not empty
- Check that the command does not contain invalid characters (e.g., null bytes)
- Limit command length to 1024 characters
- **Backend validation**:
- Same checks as frontend
- Additional server-side validation if needed
### Command History
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- Store command history in an array in memory
- Implement a circular buffer to limit memory usage
- Default maximum command count: 1000
- Configurable via environment variable
- Include command ID, content, timestamp, and status
### Command Response Handling
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
1. Receiving program reads the command from the Redis queue
2. Receiving program executes the command
3. Receiving program writes the response to a separate Redis queue
4. Backend listens for responses on the response queue using `BLPOP`
5. When a response is received, backend updates the command status in the history
6. Backend notifies the frontend of the response via Server-Sent Events (SSE)
## Risks / Trade-offs
### Risk: Redis Connection Failure
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **Risk**: If Redis connection is lost, commands won't be sent
- **Mitigation**: Implement automatic reconnection with exponential backoff, and notify users when connection is lost
### Risk: Command Loss
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **Risk**: Commands could be lost if Redis goes down
- **Mitigation**: Implement Redis persistence (RDB or AOF) to ensure commands are not lost
### Risk: Command Response Timeout
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- **Risk**: Commands could take too long to execute, causing the UI to hang
- **Mitigation**: Implement a timeout mechanism for command responses, and show a loading indicator to users
## Migration Plan
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
No migration is required as this is a new feature.
## Open Questions
2026-01-13 19:45:05 +08:00
2026-01-08 11:46:34 +08:00
- What is the expected maximum command frequency per minute?
- Should we add support for command templates or macros?
2026-01-13 19:45:05 +08:00
- Should we implement command scheduling for future execution?