diff --git a/bash-helper.sh b/bash-helper.sh index a231910..521c5a6 100755 --- a/bash-helper.sh +++ b/bash-helper.sh @@ -39,8 +39,9 @@ show_help() { echo "" echo -e "${BOLD}${YELLOW}MODES${NC}" echo -e " ${CYAN}Natural Language Queries:${NC}" - echo -e " ${GREEN}ask${NC} \"query\" Ask in natural language" - echo -e " ${GREEN}task${NC} \"description\" Describe what you want to do" + echo -e " ${GREEN}ask${NC} \"query\" Ask in natural language (database)" + echo -e " ${GREEN}task${NC} \"description\" Describe what you want to do (database)" + echo -e " ${GREEN}ai${NC} \"complex query\" AI-powered NL2SH model (advanced) 🤖" echo "" echo -e " ${CYAN}Browse & Explore:${NC}" echo -e " ${GREEN}category${NC} NAME Browse by category (files/text/network/system)" @@ -75,14 +76,16 @@ show_help() { echo -e " ${GREEN}✓${NC} jq (for natural language queries)" echo -e " ${GREEN}✓${NC} fzf (for interactive mode only)" echo "" - echo -e "${BOLD}${YELLOW}AI ENHANCEMENT${NC}" - echo " For advanced natural language understanding, install Ollama:" - echo -e " ${DIM}curl -fsSL https://ollama.com/install.sh | sh${NC}" - echo -e " ${DIM}ollama pull codellama:7b${NC}" - echo -e " Then use: ${GREEN}bash-helper ai \"complex query\"${NC} ${DIM}(Phase 3 - coming soon)${NC}" + echo -e "${BOLD}${YELLOW}AI MODE${NC} 🤖" + echo -e " ${GREEN}✓ NL2SH model detected!${NC} Use AI for complex queries:" + echo -e " ${GREEN}bash-helper ai \"find python files modified last week\"${NC}" + echo -e " ${GREEN}bash-helper ai \"count lines in all js files\"${NC}" + echo "" + echo " Other models available: llama3.2, qwen2.5" + echo " To switch models, edit mode_ai() function" echo "" echo -e "${YELLOW}Database:${NC} $DB_FILE" - echo -e "${YELLOW}Version:${NC} 2.0.0 (Phase 1 Complete)" + echo -e "${YELLOW}Version:${NC} 2.1.0 (90+ commands, AI-powered)" echo "" } @@ -304,6 +307,83 @@ mode_category() { done } +# AI mode - use NL2SH model for complex queries +mode_ai() { + local user_query="$1" + + if [ -z "$user_query" ]; then + echo -e "${RED}Error: No query provided${NC}" + echo "Usage: bash-helper ai \"your complex query\"" + echo "" + echo "Example:" + echo " bash-helper ai \"find all python files modified in last week\"" + return 1 + fi + + # Check if ollama is available + if ! command -v ollama &> /dev/null; then + echo -e "${RED}Error: Ollama is not installed${NC}" + echo "" + echo "Install Ollama:" + echo " curl -fsSL https://ollama.com/install.sh | sh" + echo "" + echo "For now, try: bash-helper ask \"$user_query\"" + return 1 + fi + + # Check if NL2SH model is available + if ! ollama list | grep -q "westenfelder/NL2SH"; then + echo -e "${YELLOW}NL2SH model not found. Available models:${NC}" + ollama list + echo "" + echo "Pull NL2SH model with:" + echo " ollama pull westenfelder/NL2SH" + return 1 + fi + + echo "" + echo -e "${CYAN}🤖 AI Assistant - Using NL2SH Model${NC}" + echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}" + echo -e "${YELLOW}Query:${NC} $user_query" + echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}" + echo "" + echo -e "${DIM}Generating bash command...${NC}" + echo "" + + # Query the NL2SH model + local response=$(ollama run westenfelder/NL2SH "$user_query" 2>/dev/null) + + if [ -z "$response" ]; then + echo -e "${RED}Error: No response from AI model${NC}" + echo "" + echo "Try a simpler query or use: bash-helper ask \"$user_query\"" + return 1 + fi + + # Display the generated command + echo -e "${GREEN}Generated Command:${NC}" + echo -e " ${MAGENTA}$response${NC}" + echo "" + + # Extract the base command for explanation + local base_cmd=$(echo "$response" | awk '{print $1}') + + # Try to provide explanation + if command -v "$base_cmd" &> /dev/null || type "$base_cmd" 2>/dev/null | grep -q "shell builtin"; then + echo -e "${YELLOW}Command Info:${NC}" + if type "$base_cmd" 2>/dev/null | grep -q "shell builtin"; then + help "$base_cmd" 2>/dev/null | head -3 + else + man "$base_cmd" 2>/dev/null | col -b | grep -A 2 "^NAME" | tail -2 + fi + echo "" + fi + + echo -e "${DIM}Note: Always review AI-generated commands before running them!${NC}" + echo "" + echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}" +} + # Explain mode - parse and explain a command mode_explain() { local cmd_line="$1" @@ -381,19 +461,36 @@ show_flags() { echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" echo "" - if type "$CMD_NAME" | grep -q "shell builtin"; then + # Try to get flags from man page + if type "$CMD_NAME" 2>/dev/null | grep -q "shell builtin"; then + # Shell builtin - use help command if [ -n "$FILTER" ]; then - help "$CMD_NAME" | grep -E ' -' | grep -i "$FILTER" + help "$CMD_NAME" 2>/dev/null | grep -E '^\s*-' | grep -i "$FILTER" else - help "$CMD_NAME" | grep -E ' -' + help "$CMD_NAME" 2>/dev/null | grep -E '^\s*-' fi elif command -v "$CMD_NAME" &> /dev/null; then + # External command - use man page + # Extract the OPTIONS section from man page + local man_output=$(man "$CMD_NAME" 2>/dev/null | col -b) + + if [ -z "$man_output" ]; then + echo -e "${RED}No manual page available for: $CMD_NAME${NC}" + return 1 + fi + + # Try to extract just the OPTIONS/FLAGS section + local options_section=$(echo "$man_output" | sed -n '/^OPTIONS/,/^[A-Z][A-Z]/p' | head -100) + + if [ -z "$options_section" ]; then + # Fallback: get all lines that look like flags + options_section=$(echo "$man_output" | grep -E '^\s*-' | head -50) + fi + if [ -n "$FILTER" ]; then - man "$CMD_NAME" 2>/dev/null | col -b | grep -E '^\s*-' | grep -i "$FILTER" || \ - man "$CMD_NAME" 2>/dev/null | col -b | grep -E ' -' | grep -i "$FILTER" + echo "$options_section" | grep -i "$FILTER" | head -30 else - man "$CMD_NAME" 2>/dev/null | col -b | grep -E '^\s*-' || \ - man "$CMD_NAME" 2>/dev/null | col -b | grep -E ' -' + echo "$options_section" | head -30 fi else echo -e "${RED}Could not find help for: $CMD_NAME${NC}" @@ -429,8 +526,9 @@ get_command_description() { echo "Command help" } -# Command definitions +# Command definitions for interactive mode COMMANDS=( + # File Operations "ls:List directory contents" "cd:Change the current directory" "pwd:Print the name of the current working directory" @@ -439,9 +537,102 @@ COMMANDS=( "rm:Remove files or directories" "mkdir:Create a new directory" "rmdir:Remove an empty directory" - "grep:Print lines matching a pattern" - "find:Search for files in a directory hierarchy" + "touch:Create empty file or update timestamp" + "cat:Concatenate and display files" + "less:View file contents with pagination" + "head:Display first lines of a file" + "tail:Display last lines of a file" + "chmod:Change file permissions" + "chown:Change file owner" + "ln:Create links between files" + "file:Determine file type" + "stat:Display file status" + + # Text Processing + "grep:Search for patterns in files" + "sed:Stream editor for text manipulation" + "awk:Pattern scanning and text processing" + "cut:Remove sections from lines of files" + "sort:Sort lines of text" + "uniq:Report or filter out repeated lines" + "tr:Translate or delete characters" + "wc:Count lines, words, and characters" + "diff:Compare files line by line" + + # File Search + "find:Search for files in directory hierarchy" + "locate:Find files by name quickly" + "which:Show full path of commands" + "whereis:Locate binary, source, and manual pages" + + # Compression + "tar:Archive files" + "gzip:Compress files" + "gunzip:Decompress gzip files" + "zip:Package and compress files" + "unzip:Extract compressed zip files" + + # Network + "ping:Test network connectivity" + "curl:Transfer data from URLs" + "wget:Download files from the web" + "ssh:Secure shell remote login" + "scp:Secure copy files between hosts" + "netstat:Network statistics" + "ss:Socket statistics" + "ip:Show/manipulate routing and devices" + "ifconfig:Configure network interfaces" + + # System Info + "top:Display system processes" + "htop:Interactive process viewer" + "ps:Report process status" + "df:Show disk space usage" + "du:Estimate file space usage" + "free:Display memory usage" + "uname:Print system information" + "uptime:Show how long system has been running" + + # Process Management + "kill:Send signal to a process" + "pkill:Kill processes by name" + "killall:Kill processes by name" + "bg:Put job in background" + "fg:Bring job to foreground" + "jobs:List active jobs" + + # System Control + "sudo:Execute command as another user" + "systemctl:Control systemd services" + "service:Control system services" + "reboot:Reboot the system" + "shutdown:Shut down the system" + + # Package Management + "apt:Package manager (Debian/Ubuntu)" + "apt-get:APT package handling utility" + "dpkg:Debian package manager" + "snap:Snap package manager" + + # User Management + "useradd:Create new user" + "userdel:Delete user account" + "passwd:Change user password" + "su:Switch user" + "whoami:Print current username" + "w:Show who is logged in" + "who:Show who is logged in" + + # Misc Utilities "echo:Display a line of text" + "date:Display or set system date and time" + "cal:Display calendar" + "history:Show command history" + "alias:Create command aliases" + "export:Set environment variables" + "env:Display environment variables" + "man:Display manual pages" + "watch:Execute command periodically" ) # Parse arguments @@ -482,6 +673,10 @@ else MODE="category" QUERY="$2" ;; + ai) + MODE="ai" + QUERY="$2" + ;; -*) echo "Unknown option: $1" echo "Use --help for usage information" @@ -509,6 +704,10 @@ if [ -n "$MODE" ]; then mode_category "$QUERY" exit $? ;; + ai) + mode_ai "$QUERY" + exit $? + ;; esac fi