should_query.sh
1 #!/usr/bin/env bash 2 # Query Decision Logic 3 # Determines whether to query local LLM or skip based on task type 4 # Returns: 0 (should query), 1 (should skip) 5 6 set -euo pipefail 7 8 # Query patterns - these benefit from LLM consultation 9 QUERY_PATTERNS=( 10 # Architecture & Design 11 "architecture" 12 "design" 13 "implement" 14 "approach" 15 "pattern" 16 "structure" 17 "organize" 18 19 # Code Analysis 20 "review" 21 "analyze" 22 "security" 23 "performance" 24 "optimize" 25 "refactor" 26 "improve" 27 28 # Problem Solving 29 "debug" 30 "error" 31 "fix" 32 "issue" 33 "problem" 34 "fail" 35 "wrong" 36 37 # Decision Making 38 "should" 39 "choose" 40 "compare" 41 "better" 42 "vs" 43 "alternative" 44 "trade-off" 45 46 # Understanding 47 "explain" 48 "understand" 49 "how does" 50 "why" 51 "what could" 52 "what if" 53 ) 54 55 # Skip patterns - these don't need LLM 56 SKIP_PATTERNS=( 57 # File operations 58 "list files" 59 "show files" 60 "find file" 61 "ls " 62 "tree" 63 64 # Reading 65 "read file" 66 "cat " 67 "show content" 68 "display" 69 70 # Commands 71 "run " 72 "execute" 73 "start" 74 "stop" 75 "restart" 76 77 # Git operations 78 "git status" 79 "git log" 80 "git diff" 81 "git commit" 82 "git push" 83 84 # Simple queries 85 "what is the path" 86 "where is" 87 "check if running" 88 "is running" 89 90 # Status checks 91 "status" 92 "health" 93 "ping" 94 ) 95 96 # Check if query matches skip patterns 97 should_skip() { 98 local query="$1" 99 local query_lower=$(echo "$query" | tr '[:upper:]' '[:lower:]') 100 101 for pattern in "${SKIP_PATTERNS[@]}"; do 102 if echo "$query_lower" | grep -q "$pattern"; then 103 return 0 # Should skip 104 fi 105 done 106 107 return 1 # Should not skip 108 } 109 110 # Check if query matches query patterns 111 should_query() { 112 local query="$1" 113 local query_lower=$(echo "$query" | tr '[:upper:]' '[:lower:]') 114 115 # First check if we should skip 116 if should_skip "$query"; then 117 return 1 # Don't query 118 fi 119 120 # Check for query patterns 121 for pattern in "${QUERY_PATTERNS[@]}"; do 122 if echo "$query_lower" | grep -q "$pattern"; then 123 return 0 # Should query 124 fi 125 done 126 127 # Default: for ambiguous cases, query 128 # Better to over-query than miss insights 129 return 0 130 } 131 132 # Get recommendation with reasoning 133 get_recommendation() { 134 local query="$1" 135 136 if should_skip "$query"; then 137 echo "SKIP" 138 echo "Reason: This is a tool operation that doesn't benefit from LLM analysis" 139 return 1 140 elif should_query "$query"; then 141 echo "QUERY" 142 echo "Reason: This task benefits from LLM reasoning and analysis" 143 return 0 144 else 145 echo "QUERY" 146 echo "Reason: Ambiguous case - defaulting to query for safety" 147 return 0 148 fi 149 } 150 151 # Usage 152 usage() { 153 cat <<EOF 154 Usage: $0 [options] "user query" 155 156 Options: 157 --verbose, -v Show reasoning 158 --help, -h Show this help 159 160 Returns: 161 0 - Should query LLM 162 1 - Should skip LLM (use tools directly) 163 164 Examples: 165 # Check if should query 166 $0 "How should I implement feature X?" 167 echo \$? # 0 (should query) 168 169 # Check with reasoning 170 $0 -v "List all files in src/" 171 # Output: SKIP 172 # Reason: This is a tool operation 173 174 # In scripts 175 if $0 "Debug this error"; then 176 echo "Querying LLM..." 177 else 178 echo "Using direct tools..." 179 fi 180 EOF 181 exit 0 182 } 183 184 # Main 185 main() { 186 local verbose=false 187 local query="" 188 189 while [[ $# -gt 0 ]]; do 190 case "$1" in 191 -v|--verbose) 192 verbose=true 193 shift 194 ;; 195 -h|--help) 196 usage 197 ;; 198 *) 199 query="$1" 200 shift 201 ;; 202 esac 203 done 204 205 if [[ -z "$query" ]]; then 206 echo "Error: Query is required" >&2 207 usage 208 fi 209 210 if $verbose; then 211 get_recommendation "$query" 212 else 213 if should_query "$query"; then 214 exit 0 215 else 216 exit 1 217 fi 218 fi 219 } 220 221 # Run if called directly 222 if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then 223 main "$@" 224 fi