Newer
Older
copilot / bin / agent-exec
#!/usr/bin/env bash
# Local Agent - Auto Shell Execution Wrapper
# Handles command execution with safety checks and logging

set -euo pipefail

AGENT_LOG="/opt/local-agent/logs/agent.log"
DEEPSEEK_BIN="/root/.local/bin/deepseek"
COMMAND="${1:-}"
ARGS=("${@:2}")

log_event() {
    echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" >> "$AGENT_LOG"
}

# Safety check for dangerous commands
check_safety() {
    local cmd="$1"
    local dangerous_patterns=(
        "rm -rf /"
        "mkfs"
        "dd if=/dev/zero"
        ":(){:|:&};:"  # fork bomb
    )
    
    for pattern in "${dangerous_patterns[@]}"; do
        if [[ "$cmd" =~ $pattern ]]; then
            log_event "BLOCKED: $cmd (dangerous pattern detected)"
            echo "❌ Command blocked for safety: dangerous pattern detected"
            return 1
        fi
    done
    return 0
}

# Execute command via DeepSeek with context
execute_with_deepseek() {
    local query="$1"
    log_event "EXEC: $query"
    
    # Use DeepSeek to interpret and execute safely
    $DEEPSEEK_BIN -q "$query" --json 2>/dev/null | jq -r '.response' || echo "Execution completed"
}

# Direct shell execution (with safety checks)
execute_shell() {
    local cmd="$1"
    
    check_safety "$cmd" || return 1
    
    log_event "SHELL: $cmd"
    eval "$cmd"
}

case "${COMMAND}" in
    "")
        echo "Local Agent v1.0"
        echo "Usage: agent-exec <command> [args...]"
        echo "       agent-exec --ask '<question>'"
        ;;
    --ask)
        if [[ -n "${ARGS[0]:-}" ]]; then
            execute_with_deepseek "${ARGS[0]}"
        else
            echo "Error: --ask requires a question"
            exit 1
        fi
        ;;
    *)
        execute_shell "$COMMAND ${ARGS[@]}"
        ;;
esac