π Manus AI-Style Terminal Integration
This document explains the real-time terminal streaming functionality added to the Data Extractor application.
π Overview
The terminal integration provides a Manus AI-style terminal interface with real-time command execution and streaming output, seamlessly integrated into the existing Gradio application.
ποΈ Architecture
Components
WebSocket Server (
terminal_stream.py
)- Handles real-time communication between frontend and backend
- Manages command execution with streaming output
- Supports multiple concurrent connections
- Runs on port 8765
Frontend Terminal (
static/terminal.html
)- Beautiful Manus AI-inspired terminal interface
- Real-time output streaming via WebSocket
- Command history navigation
- Keyboard shortcuts and controls
Gradio Integration (Modified
app.py
)- Added terminal tab to existing interface
- Embedded terminal as iframe component
- Auto-starts WebSocket server on application launch
π¨ Features
Terminal Interface
- Real-time Streaming: Live command output as it happens
- Command History: Navigate with β/β arrow keys
- Interrupt Support: Ctrl+C to stop running commands
- Auto-reconnect: Automatically reconnects on connection loss
- Status Indicators: Visual connection and execution status
- Responsive Design: Works on desktop and mobile
Security
- Command Sanitization: Uses
shlex.split()
for safe command parsing - Process Isolation: Commands run in separate processes
- Error Handling: Robust error handling and logging
π Usage
Starting the Application
python app.py
The terminal WebSocket server automatically starts on port 8765.
Accessing the Terminal
- Open the Gradio interface (usually http://localhost:7860)
- Click on the "π» Terminal" tab
- Start typing commands in the terminal interface
Keyboard Shortcuts
- Enter: Execute command
- β/β: Navigate command history
- Ctrl+C: Interrupt running command
- Ctrl+L: Clear terminal screen
- Tab: Command completion (planned feature)
π§ Configuration
WebSocket Server Settings
# In terminal_stream.py
WEBSOCKET_HOST = 'localhost'
WEBSOCKET_PORT = 8765
Terminal Appearance
Customize the terminal appearance by modifying the CSS in static/terminal.html
:
/* Main terminal colors */
.terminal-container {
background: linear-gradient(135deg, #0d1117 0%, #161b22 100%);
}
/* Command prompt */
.prompt {
color: #58a6ff;
}
π‘ WebSocket API
Client β Server Messages
Execute Command
{
"type": "command",
"command": "ls -la"
}
Interrupt Command
{
"type": "interrupt"
}
Server β Client Messages
Command Output
{
"type": "output",
"data": "file1.txt\nfile2.txt",
"stream": "stdout",
"timestamp": "2024-01-01T12:00:00.000Z"
}
Command Completion
{
"type": "command_complete",
"exit_code": 0,
"message": "Process exited with code 0",
"timestamp": "2024-01-01T12:00:00.000Z"
}
π οΈ Development
Adding New Features
- Server-side: Modify
terminal_stream.py
- Client-side: Update
static/terminal.html
- Integration: Adjust
app.py
if needed
Testing
# Test WebSocket server independently
python -c "from terminal_stream import run_websocket_server; run_websocket_server()"
# Test terminal interface
# Open static/terminal.html in browser
π Troubleshooting
Common Issues
WebSocket Connection Failed
- Check if port 8765 is available
- Verify firewall settings
- Check server logs for errors
Commands Not Executing
- Verify WebSocket connection status
- Check terminal logs for errors
- Ensure proper command syntax
Terminal Not Loading
- Check if
static/terminal.html
exists - Verify Gradio file serving configuration
- Check browser console for errors
- Check if
Debug Mode
Enable debug logging:
import logging
logging.getLogger('terminal_stream').setLevel(logging.DEBUG)
π Advanced Usage
Custom Commands
Add custom command handlers in terminal_stream.py
:
async def handle_custom_command(self, command):
if command.startswith('custom:'):
# Handle custom command
await self.broadcast({
'type': 'output',
'data': 'Custom command executed',
'stream': 'stdout'
})
return True
return False
Integration with Workflow
Stream workflow logs to terminal:
# In workflow code
from terminal_stream import terminal_manager
async def log_to_terminal(message):
await terminal_manager.broadcast({
'type': 'output',
'data': message,
'stream': 'workflow'
})
π Dependencies
websockets
: WebSocket server implementationasyncio
: Async programming supportsubprocess
: Command executionshlex
: Safe command parsing
π― Future Enhancements
- Command auto-completion
- File upload/download via terminal
- Terminal themes and customization
- Multi-session support
- Terminal recording and playback
- Integration with workflow logging
- SSH/remote terminal support
π License
This terminal implementation is part of the Data Extractor project and follows the same license terms.