Compare commits

...
Sign in to create a new pull request.

14 commits

Author SHA1 Message Date
92be169d45 Update README.md 2025-07-17 02:57:25 +00:00
d96c14cf7f Add new file 2025-07-17 01:53:48 +00:00
73f1a2d7a9 Add new file 2025-07-17 01:51:34 +00:00
998003c3c7 Add new file 2025-07-17 01:51:01 +00:00
6f657e8ac0 Add new file 2025-07-17 01:49:30 +00:00
a9bd9870e6 Add new file 2025-07-17 01:38:45 +00:00
ce8771da51 Add new directory 2025-07-17 01:38:24 +00:00
6486f491b9 Add new file 2025-07-17 01:35:51 +00:00
cb11586b9b Add new file 2025-07-17 01:32:22 +00:00
b01d8c0eb7 Update README.md 2025-07-17 01:29:57 +00:00
2c936b8eb3 Add new file 2025-07-17 01:16:48 +00:00
7d4ef82a24 slop server with terminal 2025-07-17 01:09:21 +00:00
e3c4bc8f23 Add new file 2025-07-17 00:43:27 +00:00
803f32b3a2 Add new directory 2025-07-17 00:42:33 +00:00
12 changed files with 3250 additions and 0 deletions

54
slop-terminal/.gitignore vendored Normal file
View file

@ -0,0 +1,54 @@
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# Virtual environments
venv/
env/
ENV/
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
# OS
.DS_Store
Thumbs.db
# Project specific - data and generated files
data/
!data/.gitkeep
!data/README.md
bin/
firecracker/
downloads/
logs/
*.log
# Generated Scripts directory (created by vars.sh)
Scripts/
# Environment files
.env

0
slop-terminal/.gitkeep Normal file
View file

View file

@ -0,0 +1,72 @@
stages:
- test
- build
- deploy
variables:
DOCKER_IMAGE_NAME: "slop-terminal"
DOCKER_TAG: "$CI_COMMIT_REF_SLUG"
# Test stage
test:
stage: test
image: python:3.9
before_script:
- pip install -r requirements.txt
- pip install pytest pytest-cov
script:
- python -m pytest tests/ --cov=.
coverage: '/TOTAL.+?(\d+\%)$/'
artifacts:
reports:
coverage_report:
coverage_format: cobertura
path: coverage.xml
# Build Docker image
build:
stage: build
image: docker:20.10.16
services:
- docker:20.10.16-dind
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
script:
- docker build -t $CI_REGISTRY_IMAGE:$DOCKER_TAG .
- docker push $CI_REGISTRY_IMAGE:$DOCKER_TAG
- docker tag $CI_REGISTRY_IMAGE:$DOCKER_TAG $CI_REGISTRY_IMAGE:latest
- docker push $CI_REGISTRY_IMAGE:latest
only:
- main
- develop
# Deploy to staging
deploy_staging:
stage: deploy
image: alpine:latest
before_script:
- apk add --no-cache curl
script:
- echo "Deploying to staging environment..."
- curl -X POST "$STAGING_WEBHOOK_URL" -H "Content-Type: application/json" -d '{"image":"'$CI_REGISTRY_IMAGE:$DOCKER_TAG'"}'
only:
- develop
environment:
name: staging
url: https://staging.your-domain.com
# Deploy to production
deploy_production:
stage: deploy
image: alpine:latest
before_script:
- apk add --no-cache curl
script:
- echo "Deploying to production environment..."
- curl -X POST "$PRODUCTION_WEBHOOK_URL" -H "Content-Type: application/json" -d '{"image":"'$CI_REGISTRY_IMAGE:$DOCKER_TAG'"}'
only:
- main
environment:
name: production
url: https://your-domain.com
when: manual

43
slop-terminal/Dockerfile Normal file
View file

@ -0,0 +1,43 @@
FROM ubuntu:20.04
# Avoid interactive prompts
ENV DEBIAN_FRONTEND=noninteractive
# Install system dependencies
RUN apt-get update && apt-get install -y \
python3 \
python3-pip \
curl \
wget \
tar \
gzip \
qemu-kvm \
&& rm -rf /var/lib/apt/lists/*
# Create app directory
WORKDIR /app
# Copy requirements first for better caching
COPY requirements.txt .
RUN pip3 install --no-cache-dir -r requirements.txt
# Copy application files
COPY slop_with_terminal.py .
COPY vars.sh .
COPY static/ ./static/
COPY config/ ./config/
# Create necessary directories
RUN mkdir -p data bin firecracker downloads Scripts
# Make scripts executable
RUN chmod +x vars.sh
# Set up environment
ENV SLOP_DATA_DIR=/app/data
# Expose port
EXPOSE 31337
# Run setup and start server
CMD ["bash", "-c", "./vars.sh && python3 slop_with_terminal.py"]

39
slop-terminal/Makefile Normal file
View file

@ -0,0 +1,39 @@
.PHONY: help setup install test run clean docker-build docker-run
help:
@echo "Available commands:"
@echo " setup - Run initial setup (vars.sh)"
@echo " install - Install Python dependencies"
@echo " test - Run tests"
@echo " run - Start the server (slop_with_terminal.py)"
@echo " clean - Clean up generated files"
@echo " docker-build - Build Docker image"
@echo " docker-run - Run with Docker Compose"
setup:
@echo "Setting up SLOP Terminal..."
chmod +x vars.sh
./vars.sh
install:
pip3 install -r requirements.txt
test:
python3 -m pytest tests/ -v
run:
python3 slop_with_terminal.py
clean:
rm -rf __pycache__ .pytest_cache
rm -rf data/
rm -rf bin/
rm -rf firecracker/
rm -rf downloads/
rm -rf Scripts/
docker-build:
docker build -t slop-terminal .
docker-run:
docker-compose up -d

388
slop-terminal/README.md Normal file
View file

@ -0,0 +1,388 @@
# SLOP Terminal Server
A comprehensive SLOP (Streamlined Language Operations Protocol) server implementation with AI model integration, sandboxed code execution, and gaming capabilities.
## Features
### 🤖 AI Integration
- **Multi-Model Support**: Dynamic discovery of OpenAI-compatible endpoints (vLLM, Ollama, etc.)
- **Tool Calling**: AI can automatically invoke tools to assist with tasks
- **Chat Interface**: Full conversation support with context management
### 🛡️ Security & Sandboxing
- **Firecracker Sandboxes**: Secure microVM-based code execution
- **User Isolation**: Per-user sandbox environments with persistent state
- **Permission Controls**: Configurable security policies and access controls
### 🔧 Terminal Tools
- **File Operations**: Read, write, and manage files safely
- **Code Execution**: Python and shell command execution in sandboxes
- **System Information**: Access to system stats and environment details
- **Directory Management**: Navigate and explore filesystem
### 🎮 Gaming Features
- **Casino Games**: Slots and Blackjack with persistent wallets
- **Agent Support**: Multi-agent gaming with individual statistics
- **Persistent State**: Game history and balances stored across sessions
### 💾 Data Management
- **Memory System**: Key-value storage with query capabilities
- **Resource Management**: Hierarchical resource storage and retrieval
- **Persistent Storage**: File-based data persistence with locking
## Quick Start
### Prerequisites
- **Linux/WSL2** (Ubuntu 18.04+ recommended)
- **Python 3.8+**
- **KVM support** (for Firecracker sandboxes)
- **Root/sudo access** (for initial setup)
### Installation
1. **Clone the repository**:
```bash
git clone <repository-url>
cd slop-terminal
```
2. **Run the setup**:
```bash
make setup
```
This will automatically:
- Install system dependencies
- Download and configure Firecracker
- Set up KVM permissions
- Install Python packages
- Configure environment variables
3. **Test the installation**:
```bash
./Scripts/test_firecracker.sh # Auto-generated by setup
```
4. **Start the server**:
```bash
make run
```
## Configuration
### Environment Variables
The server supports extensive configuration through environment variables. Copy the template and customize:
```bash
cp config/.env.example .env
nano .env
```
#### Model Endpoints
```bash
export MODEL_ENDPOINT_0="https://your-model-endpoint.com/v1"
export MODEL_API_KEY_0="your-api-key-or-not-needed"
# Add more endpoints with incrementing numbers
```
#### Server Settings
```bash
export SLOP_PORT="31337" # Server port
export SLOP_DATA_DIR="./data" # Data directory
export SLOP_LOG_LEVEL="INFO" # Logging level
export FLASK_DEBUG="true" # Debug mode
```
#### Security Settings
```bash
export ENABLE_FIRECRACKER="true" # Enable sandbox
export ALLOW_DANGEROUS_TOOLS="false" # Allow unsafe tools
export REQUIRE_USER_ID="true" # Require user identification
export DEFAULT_TIMEOUT="60" # Default execution timeout
```
#### Firecracker Settings
```bash
export FIRECRACKER_KERNEL="./firecracker/kernel/vmlinux.bin"
export FIRECRACKER_ROOTFS="./firecracker/rootfs/rootfs.ext4"
export FIRECRACKER_MEMORY="512" # Memory in MB
export FIRECRACKER_VCPU="1" # Virtual CPU count
```
## API Endpoints
### Chat & AI
- `POST /chat` - Chat with AI models with tool calling support
- `GET /models` - List available AI models
### Tools
- `GET /tools` - List all available tools
- `GET /tools/{tool_id}` - Get tool details
- `POST /tools/{tool_id}` - Execute a specific tool
### Memory Management
- `GET /memory` - List all memory keys
- `GET /memory/{key}` - Retrieve memory value
- `POST /memory` - Store key-value pair
- `DELETE /memory/{key}` - Delete memory key
- `POST /memory/query` - Query memory with filters
### Resource Management
- `GET /resources` - List all resources
- `GET /resources/{resource_id}` - Get specific resource
- `POST /resources` - Create new resource
- `PUT /resources/{resource_id}` - Update resource
- `DELETE /resources/{resource_id}` - Delete resource
- `GET /resources/search` - Search resources
### Gaming
- `POST /tools/slots` - Play slot machine
- `POST /tools/blackjack` - Play blackjack
### System
- `GET /info` - Server information and capabilities
- `POST /pay` - Mock payment processing
## Available Tools
### Terminal Tools
- **`sandbox_python`** - Execute Python code in secure sandbox
- **`sandbox_exec`** - Execute shell commands in secure sandbox
- **`file_read`** - Read file contents safely
- **`directory_list`** - List directory contents
- **`sysinfo`** - Get system information
- **`pwd`** - Get current directory
- **`sandbox_manage`** - Manage sandbox instances
### Utility Tools
- **`calculator`** - Basic math calculations
- **`greet`** - Simple greeting tool
### Gaming Tools
- **`slots`** - Slot machine game
- **`blackjack`** - Blackjack card game
## Security Features
### Firecracker Sandboxes
- **Isolated Execution**: Each user gets their own microVM
- **Resource Limits**: Configurable CPU and memory limits
- **Network Isolation**: No network access from sandboxes
- **Persistent Environments**: Python virtual environments persist across calls
### Access Controls
- **User Identification**: Optional user ID requirement
- **Tool Restrictions**: Dangerous tools only available in sandboxes
- **Timeout Controls**: Configurable execution timeouts
- **Permission Levels**: Different access levels for different tools
## Usage Examples
### Chat with AI
```bash
curl -X POST http://localhost:31337/chat \
-H "Content-Type: application/json" \
-d '{
"messages": [{"role": "user", "content": "Calculate 15 * 23"}],
"model": "your-model-name",
"enable_tool_calling": true,
"user_id": "user123"
}'
```
### Execute Python Code
```bash
curl -X POST http://localhost:31337/tools/sandbox_python \
-H "Content-Type: application/json" \
-d '{
"code": "print(\"Hello, World!\")",
"user_id": "user123",
"packages": ["requests", "numpy"]
}'
```
### Store and Retrieve Memory
```bash
# Store
curl -X POST http://localhost:31337/memory \
-H "Content-Type: application/json" \
-d '{"key": "user_pref", "value": "dark_mode"}'
# Retrieve
curl http://localhost:31337/memory/user_pref
```
### Play Casino Games
```bash
curl -X POST http://localhost:31337/tools/slots \
-H "Content-Type: application/json" \
-d '{"bet": 10, "agent_id": "player1"}'
```
## Development
### Available Make Commands
```bash
make help # Show all available commands
make setup # Run vars.sh setup
make install # Install Python dependencies
make run # Start the server (python3 slop_with_terminal.py)
make test # Run tests
make clean # Clean up generated files
make docker-build # Build Docker image
make docker-run # Run with Docker Compose
```
### Project Structure
```
slop-terminal/
├── slop_with_terminal.py # Main Flask server
├── vars.sh # Setup and configuration script
├── README.md # This file
├── requirements.txt # Python dependencies
├── Makefile # Build automation
├── Dockerfile # Container configuration
├── docker-compose.yml # Docker setup
├── .gitlab-ci.yml # CI/CD pipeline
├── .gitignore # Git ignore rules
├── config/ # Configuration files
│ └── .env.example # Environment template
├── docs/ # Documentation
├── tests/ # Test files
├── static/ # Static files
├── Scripts/ # Generated scripts (auto-generated by vars.sh)
│ ├── set_env.sh # Environment setup
│ ├── test_firecracker.sh # Test script
│ └── start_firecracker.sh # Firecracker wrapper
├── data/ # Data directory
│ ├── memory.json # Memory storage
│ ├── resources.json # Resource storage
│ ├── terminal/ # Terminal data
│ └── slop_monolith.log # Server logs
├── firecracker/ # Firecracker components
│ ├── kernel/ # Kernel images
│ └── rootfs/ # Root filesystems
├── downloads/ # Downloaded files
└── bin/ # Binary files
```
### Adding New Tools
1. Create a class inheriting from `TerminalTool`
2. Implement required methods: `tool_id`, `description`, `arguments`, `execute`
3. Register the tool in `initialize_terminal_tools()`
### Adding New Models
Simply add new environment variables:
```bash
export MODEL_ENDPOINT_X="https://your-endpoint.com/v1"
export MODEL_API_KEY_X="your-key"
```
## Troubleshooting
### Common Issues
**KVM Permission Errors**:
```bash
# Fix KVM permissions
sudo usermod -a -G kvm $USER
# Restart terminal or re-login
```
**Firecracker Not Working**:
```bash
# Test Firecracker setup
./Scripts/test_firecracker.sh # Auto-generated by setup
# Check KVM availability
ls -la /dev/kvm
```
**Model Connection Issues**:
```bash
# Test endpoint manually
curl https://your-endpoint.com/v1/models
```
**Memory/Resource Lock Timeouts**:
- Check disk space in data directory
- Ensure proper file permissions
- Restart server if persistent
### Debug Mode
Enable detailed logging:
```bash
# Edit .env file
SLOP_LOG_LEVEL=DEBUG
FLASK_DEBUG=true
# Then start server
make run
# or directly: python3 slop_with_terminal.py
```
## Performance Tuning
### Firecracker Settings
Edit your `.env` file:
```bash
FIRECRACKER_MEMORY=1024 # Increase memory
FIRECRACKER_VCPU=2 # More CPU cores
```
### Timeout Settings
Edit your `.env` file:
```bash
DEFAULT_TIMEOUT=120 # Longer timeouts
```
## License
This project is released under the Public Domain. See the original SLOP specification for more details.
## Contributing
1. Fork the repository
2. Create a feature branch
3. Make your changes
4. Add tests if applicable
5. Submit a pull request
## Support
For issues and questions:
- Check the troubleshooting section
- Review server logs in `data/slop_monolith.log`
- Test with `./Scripts/test_firecracker.sh` (auto-generated by setup)
- Use `make help` to see all available commands
- Verify endpoints with curl
## Related Projects
- [SLOP Specification](https://slop.unturf.com/)
- [Original SLOP Examples](https://github.com/agnt-gg/slop)
## Quick Start Summary
```bash
# Clone and navigate to project
git clone <repository-url>
cd slop-terminal
# Setup environment
cp config/.env.example .env
make setup
# Start server
make run
# Test
curl http://localhost:31337/info
```
---
Built with ❤️ for the SLOP community

View file

@ -0,0 +1,23 @@
# Server Configuration
SLOP_PORT=31337
SLOP_DATA_DIR=./data
SLOP_LOG_LEVEL=INFO
FLASK_DEBUG=false
# Security Settings
ENABLE_FIRECRACKER=true
ALLOW_DANGEROUS_TOOLS=false
REQUIRE_USER_ID=true
DEFAULT_TIMEOUT=60
# Firecracker Configuration
FIRECRACKER_KERNEL=./firecracker/kernel/vmlinux.bin
FIRECRACKER_ROOTFS=./firecracker/rootfs/rootfs.ext4
FIRECRACKER_MEMORY=512
FIRECRACKER_VCPU=1
# Model Endpoints (add as many as needed)
MODEL_ENDPOINT_0=https://hermes.ai.unturf.com/v1
MODEL_API_KEY_0=not-needed
# MODEL_ENDPOINT_1=https://another-endpoint.com/v1
# MODEL_API_KEY_1=your-api-key-here

View file

View file

@ -0,0 +1,24 @@
version: '3.8'
services:
slop-terminal:
build: .
ports:
- "31337:31337"
volumes:
- ./data:/app/data
- ./config:/app/config
environment:
- SLOP_PORT=31337
- SLOP_DATA_DIR=/app/data
- FLASK_DEBUG=false
- ENABLE_FIRECRACKER=true
privileged: true # Required for KVM/Firecracker
devices:
- /dev/kvm:/dev/kvm
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:31337/info"]
interval: 30s
timeout: 10s
retries: 3

View file

@ -0,0 +1,18 @@
# Core dependencies
flask==2.3.3
flask-swagger-ui==4.11.1
openai==1.3.0
requests==2.31.0
# Math and expression evaluation
expr.py==0.1.0
# System monitoring
psutil==5.9.5
# Development dependencies (optional)
pytest==7.4.3
pytest-cov==4.1.0
black==23.9.1
flake8==6.1.0
mypy==1.6.1

File diff suppressed because it is too large Load diff

407
slop-terminal/vars.sh Normal file
View file

@ -0,0 +1,407 @@
#!/bin/bash
# vars.sh - SLOP Monolith Configuration for Linux/WSL2
# Usage: source vars.sh
echo "SLOP Monolith Configuration"
echo "============================"
CURRENT_DIR=$(pwd)
# OS Detection
if [[ -n "$WSL_DISTRO_NAME" ]]; then
IS_WSL=true
IS_LINUX=true
OS_NAME="WSL2 ($WSL_DISTRO_NAME)"
elif [[ "$(uname -s)" == "Linux" ]]; then
IS_WSL=false
IS_LINUX=true
OS_NAME="Linux"
else
IS_WSL=false
IS_LINUX=false
OS_NAME="Unknown"
fi
# Architecture Detection
DETECTED_ARCH=$(uname -m)
case $DETECTED_ARCH in
"x86_64")
ARCH="x86_64"
;;
"aarch64"|"arm64")
ARCH="aarch64"
;;
*)
ARCH="x86_64"
;;
esac
echo "System Info:"
echo " OS: $OS_NAME"
echo " Architecture: $DETECTED_ARCH -> $ARCH"
# Configuration
FIRECRACKER_VERSION="v1.12.1"
CAN_USE_FIRECRACKER=true
# URLs
FIRECRACKER_URL="https://github.com/firecracker-microvm/firecracker/releases/download/$FIRECRACKER_VERSION/firecracker-$FIRECRACKER_VERSION-$ARCH.tgz"
KERNEL_URL="https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/$ARCH/kernels/vmlinux.bin"
ROOTFS_URL="https://s3.amazonaws.com/spec.ccfc.min/img/quickstart_guide/$ARCH/rootfs/bionic.rootfs.ext4"
echo "Firecracker URL: $FIRECRACKER_URL"
# Create directories
echo ""
echo "Creating directories..."
DIRS=("firecracker" "firecracker/kernel" "firecracker/rootfs" "data" "data/terminal" "downloads" "bin" "Scripts")
for dir in "${DIRS[@]}"; do
mkdir -p "$dir"
echo " Created: $dir"
done
# Install system dependencies
echo ""
echo "Installing system dependencies..."
if command -v apt-get &> /dev/null; then
sudo apt-get update -qq
sudo apt-get install -y curl wget tar gzip python3 python3-pip
echo " System packages installed (apt)"
elif command -v yum &> /dev/null; then
sudo yum install -y curl wget tar gzip python3 python3-pip
echo " System packages installed (yum)"
elif command -v dnf &> /dev/null; then
sudo dnf install -y curl wget tar gzip python3 python3-pip
echo " System packages installed (dnf)"
elif command -v pacman &> /dev/null; then
sudo pacman -S --noconfirm curl wget tar gzip python python-pip
echo " System packages installed (pacman)"
else
echo " Package manager not detected - ensure curl, wget, tar, python3 are installed"
fi
# Download function
download_file() {
local url="$1"
local output="$2"
local description="$3"
echo "Downloading $description..."
echo " URL: $url"
if command -v curl &> /dev/null; then
if curl -L -o "$output" "$url" --connect-timeout 30 --max-time 300 --silent --fail; then
local size=$(stat -c%s "$output" 2>/dev/null || stat -f%z "$output" 2>/dev/null || echo "0")
echo " Downloaded: $((size / 1024 / 1024)) MB"
return 0
else
echo " Download failed with curl"
return 1
fi
elif command -v wget &> /dev/null; then
if wget -O "$output" "$url" --timeout=300 --quiet; then
local size=$(stat -c%s "$output" 2>/dev/null || stat -f%z "$output" 2>/dev/null || echo "0")
echo " Downloaded: $((size / 1024 / 1024)) MB"
return 0
else
echo " Download failed with wget"
return 1
fi
else
echo " Neither curl nor wget available"
return 1
fi
}
# Setup Firecracker
echo ""
echo "Setting up Firecracker..."
# Download binary
if [[ ! -f "bin/firecracker" ]]; then
if download_file "$FIRECRACKER_URL" "downloads/firecracker.tgz" "Firecracker Binary"; then
echo "Extracting Firecracker..."
tar -xzf "downloads/firecracker.tgz" -C "downloads"
# Find and copy binary
BINARY_PATH=$(find downloads -name "firecracker-*" -type f | grep -v "\.tgz$" | head -1)
if [[ -n "$BINARY_PATH" ]]; then
cp "$BINARY_PATH" "bin/firecracker"
chmod +x "bin/firecracker"
echo " Firecracker binary ready"
else
echo " Failed to find firecracker binary in archive"
fi
else
echo " Failed to download Firecracker binary"
fi
else
echo " Firecracker binary already exists"
chmod +x "bin/firecracker"
fi
# Download kernel
if [[ ! -f "firecracker/kernel/vmlinux.bin" ]] || [[ $(stat -c%s "firecracker/kernel/vmlinux.bin" 2>/dev/null || echo "0") -lt 1024 ]]; then
if ! download_file "$KERNEL_URL" "firecracker/kernel/vmlinux.bin" "Kernel"; then
echo " Download failed, creating placeholder..."
dd if=/dev/zero of="firecracker/kernel/vmlinux.bin" bs=1M count=10 2>/dev/null
echo " Kernel placeholder created"
fi
else
echo " Kernel already exists"
fi
# Download rootfs
if [[ ! -f "firecracker/rootfs/rootfs.ext4" ]] || [[ $(stat -c%s "firecracker/rootfs/rootfs.ext4" 2>/dev/null || echo "0") -lt 1024 ]]; then
if ! download_file "$ROOTFS_URL" "firecracker/rootfs/rootfs.ext4" "Root Filesystem"; then
echo " Download failed, creating placeholder..."
dd if=/dev/zero of="firecracker/rootfs/rootfs.ext4" bs=1M count=100 2>/dev/null
echo " Rootfs placeholder created"
fi
else
echo " Rootfs already exists"
fi
# Check and fix KVM
echo "Checking KVM support..."
if [[ -c "/dev/kvm" ]]; then
if [[ -w "/dev/kvm" ]]; then
echo " KVM available and writable"
KVM_AVAILABLE=true
else
echo " KVM exists but not writable - fixing automatically..."
# Check if user is already in kvm group
if groups $USER | grep -q '\bkvm\b'; then
echo " User already in kvm group - permissions should work after restart"
KVM_AVAILABLE=true
else
echo " Adding user to kvm group..."
if sudo usermod -a -G kvm $USER; then
echo " User added to kvm group successfully"
echo " KVM permissions fixed!"
echo " Note: You may need to restart your terminal for full effect"
KVM_AVAILABLE=true
else
echo " Failed to add user to kvm group"
KVM_AVAILABLE=false
fi
fi
fi
else
echo " KVM device not found - installing..."
if sudo apt-get update && sudo apt-get install -y qemu-kvm; then
echo " KVM installed successfully"
# After installation, add user to kvm group
if sudo usermod -a -G kvm $USER; then
echo " User added to kvm group"
KVM_AVAILABLE=true
echo " KVM setup complete!"
echo " Note: You may need to restart your terminal for full effect"
else
echo " Failed to add user to kvm group"
KVM_AVAILABLE=false
fi
else
echo " Failed to install KVM"
KVM_AVAILABLE=false
fi
fi
# Test Firecracker and final verification
echo "Testing Firecracker setup..."
FIRECRACKER_WORKS=false
if [[ -f "bin/firecracker" ]]; then
if ./bin/firecracker --version &>/dev/null; then
echo " Firecracker binary works"
FIRECRACKER_WORKS=true
else
echo " Firecracker binary failed to run"
fi
fi
# Final KVM test after potential fixes
echo "Final KVM verification..."
if [[ -c "/dev/kvm" ]] && [[ -w "/dev/kvm" ]]; then
echo " KVM fully functional"
KVM_AVAILABLE=true
elif [[ -c "/dev/kvm" ]]; then
echo " KVM device exists - permissions may need terminal restart"
KVM_AVAILABLE=true # Mark as available since we fixed the group membership
else
echo " KVM not available"
KVM_AVAILABLE=false
fi
# Determine Firecracker status
if [[ "$FIRECRACKER_WORKS" == true ]] && [[ -f "firecracker/kernel/vmlinux.bin" ]] && [[ -f "firecracker/rootfs/rootfs.ext4" ]]; then
FIRECRACKER_READY=true
else
FIRECRACKER_READY=false
fi
# Install Python dependencies
echo ""
echo "Installing Python dependencies..."
if command -v pip3 &> /dev/null; then
pip3 install flask flask-swagger-ui openai expr.py psutil
echo " Python packages installed"
elif command -v pip &> /dev/null; then
pip install flask flask-swagger-ui openai expr.py psutil
echo " Python packages installed"
else
echo " pip not found - install manually"
fi
# Set Environment Variables
echo ""
echo "Setting environment variables..."
export FIRECRACKER_KERNEL="$CURRENT_DIR/firecracker/kernel/vmlinux.bin"
export FIRECRACKER_ROOTFS="$CURRENT_DIR/firecracker/rootfs/rootfs.ext4"
export FIRECRACKER_MEMORY="512"
export FIRECRACKER_VCPU="1"
export MODEL_ENDPOINT_0="https://hermes.ai.unturf.com/v1"
export MODEL_API_KEY_0="not-needed"
export SLOP_PORT="31337"
export SLOP_DATA_DIR="$CURRENT_DIR/data"
export SLOP_LOG_LEVEL="INFO"
export FLASK_DEBUG="true"
export REQUIRE_USER_ID="true"
export DEFAULT_TIMEOUT="30"
if [[ "$FIRECRACKER_READY" == true ]]; then
export ENABLE_FIRECRACKER="true"
export ALLOW_DANGEROUS_TOOLS="false"
STATUS="ENABLED"
else
export ENABLE_FIRECRACKER="false"
export ALLOW_DANGEROUS_TOOLS="true"
STATUS="DISABLED"
fi
# Create helper scripts
echo "Creating helper scripts..."
# Create firecracker wrapper
cat > Scripts/start_firecracker.sh << 'EOF'
#!/bin/bash
export PATH="$(pwd)/bin:$PATH"
cd "$(dirname "$0")"
./bin/firecracker "$@"
EOF
chmod +x Scripts/start_firecracker.sh
echo " Created start_firecracker.sh"
# Create test script
cat > Scripts/test_firecracker.sh << 'EOF'
#!/bin/bash
echo "Testing Firecracker setup..."
echo "Binary: $(pwd)/bin/firecracker"
echo "Kernel: $(pwd)/firecracker/kernel/vmlinux.bin"
echo "Rootfs: $(pwd)/firecracker/rootfs/rootfs.ext4"
echo ""
if [[ -f "bin/firecracker" ]]; then
echo "✓ Binary exists"
if [[ -x "bin/firecracker" ]]; then
echo "✓ Binary is executable"
if ./bin/firecracker --version 2>/dev/null; then
echo "✓ Binary works correctly"
else
echo "✗ Binary failed to run"
fi
else
echo "✗ Binary not executable"
fi
else
echo "✗ Binary missing"
fi
if [[ -f "firecracker/kernel/vmlinux.bin" ]]; then
size=$(stat -c%s firecracker/kernel/vmlinux.bin 2>/dev/null || stat -f%z firecracker/kernel/vmlinux.bin 2>/dev/null || echo 0)
echo "✓ Kernel exists ($((size / 1024 / 1024)) MB)"
else
echo "✗ Kernel missing"
fi
if [[ -f "firecracker/rootfs/rootfs.ext4" ]]; then
size=$(stat -c%s firecracker/rootfs/rootfs.ext4 2>/dev/null || stat -f%z firecracker/rootfs/rootfs.ext4 2>/dev/null || echo 0)
echo "✓ Rootfs exists ($((size / 1024 / 1024)) MB)"
else
echo "✗ Rootfs missing"
fi
if [[ -c "/dev/kvm" ]]; then
if [[ -w "/dev/kvm" ]]; then
echo "✓ KVM available and writable"
else
echo "⚠ KVM exists but not writable"
fi
else
echo "✗ KVM not available"
fi
EOF
chmod +x Scripts/test_firecracker.sh
echo " Created test_firecracker.sh"
# Create permanent environment file
cat > Scripts/set_env.sh << EOF
#!/bin/bash
# SLOP Environment Variables
export FIRECRACKER_KERNEL="$CURRENT_DIR/firecracker/kernel/vmlinux.bin"
export FIRECRACKER_ROOTFS="$CURRENT_DIR/firecracker/rootfs/rootfs.ext4"
export FIRECRACKER_MEMORY="512"
export FIRECRACKER_VCPU="1"
export MODEL_ENDPOINT_0="https://hermes.ai.unturf.com/v1"
export MODEL_API_KEY_0="not-needed"
export SLOP_PORT="31337"
export SLOP_DATA_DIR="$CURRENT_DIR/data"
export SLOP_LOG_LEVEL="INFO"
export FLASK_DEBUG="true"
export REQUIRE_USER_ID="true"
export DEFAULT_TIMEOUT="30"
export ENABLE_FIRECRACKER="$ENABLE_FIRECRACKER"
export ALLOW_DANGEROUS_TOOLS="$ALLOW_DANGEROUS_TOOLS"
EOF
chmod +x Scripts/set_env.sh
echo " Created set_env.sh"
# Final Status
echo ""
echo "Configuration Summary:"
echo "======================"
echo " System: $OS_NAME ($ARCH)"
echo " Firecracker: $STATUS"
echo " Port: $SLOP_PORT"
echo " Data Dir: $SLOP_DATA_DIR"
echo ""
echo "Files Status:"
echo " bin/firecracker: $(test -f 'bin/firecracker' && echo 'OK' || echo 'MISSING')"
echo " kernel: $(test -f 'firecracker/kernel/vmlinux.bin' && echo 'OK' || echo 'MISSING')"
echo " rootfs: $(test -f 'firecracker/rootfs/rootfs.ext4' && echo 'OK' || echo 'MISSING')"
echo " KVM: $(test -c '/dev/kvm' && test -w '/dev/kvm' && echo 'OK' || echo 'MISSING')"
echo ""
echo "Next Steps:"
echo " 1. Test setup: ./Scripts/test_firecracker.sh"
echo " 2. Start server: python3 server.py"
if [[ "$KVM_AVAILABLE" == true ]] && [[ ! -w "/dev/kvm" ]]; then
echo " 3. If KVM errors occur, restart your terminal"
fi
echo " 4. Test endpoints:"
echo " curl http://localhost:31337/models"
echo " curl http://localhost:31337/tools"
echo ""
echo "Configuration complete!"
if [[ "$KVM_AVAILABLE" == true ]] && [[ ! -w "/dev/kvm" ]]; then
echo "KVM permissions were fixed - you may need to restart your terminal"
echo "Then run: python3 server.py"
else
echo "Run: python3 server.py"
fi