3 # AI-Test: Semantic validation tool
4 # Beautiful implementation using composable functions
6 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7 source "$SCRIPT_DIR/ai-common"
11 Usage: ai-test CONDITION [FILE...]
12 Validate text content against semantic criteria.
15 CONDITION Semantic condition to test against
18 -q Suppress all output (exit code only)
19 -v Invert match (exit 0 when condition is false)
20 -p Pass-through mode (output input unchanged when condition is true)
21 -h Show this help message
25 build_validation_prompt() {
29 You are a semantic validation tool. Analyze the following text and determine if it meets this condition: $condition
31 CRITICAL: You must respond with EXACTLY one word: either "TRUE" or "FALSE". No other text, explanations, or formatting.
33 - If the condition is met, respond: TRUE
34 - If the condition is not met, respond: FALSE
38 apply_condition_logic() {
39 local normalized_result="$1"
42 local condition_met="false"
43 [[ "$normalized_result" == "TRUE" ]] && condition_met="true"
45 # Apply inversion if requested
46 if [[ "$invert" == "true" ]]; then
47 [[ "$condition_met" == "true" ]] && condition_met="false" || condition_met="true"
54 local condition_met="$1"
57 local passthrough="$4"
60 if [[ "$condition_met" == "true" ]]; then
61 if [[ "$passthrough" == "true" ]]; then
63 elif [[ "$quiet" == "false" ]]; then
64 echo "Condition met: $condition"
66 return "$EXIT_SUCCESS"
68 if [[ "$quiet" == "false" && "$passthrough" == "false" ]]; then
69 echo "Condition not met: $condition"
71 return "$EXIT_NO_MATCH"
78 local passthrough="false"
81 local input response normalized_result condition_met
84 while getopts "qvph" opt; do
88 p) passthrough="true" ;;
89 h) handle_help_option show_usage ;;
90 \?) handle_invalid_option "$OPTARG" ;;
96 # Validate environment and arguments
98 ensure_argument_provided "Condition" "$1" show_usage
104 # Validate files if provided
105 [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}"
108 if ! input=$(process_input_sources "${files[@]}"); then
109 exit "$EXIT_NO_MATCH"
112 # Execute LLM request
114 prompt=$(build_validation_prompt "$condition")
116 if ! response=$(execute_llm_request "$prompt" "$input"); then
118 "$EXIT_NO_MATCH") print_error "$(error_llm_no_response)" ;;
119 "$EXIT_API_ERROR") print_error "$(error_llm_api_error)" ;;
120 *) print_error "$(error_llm_command_failed)" ;;
125 # Process and validate boolean response
126 if ! normalized_result=$(process_boolean_response "$response"); then
130 # Apply condition logic
131 condition_met=$(apply_condition_logic "$normalized_result" "$invert")
133 # Generate output and return appropriate exit code
134 if ! generate_output "$condition_met" "$condition" "$quiet" "$passthrough" "$input"; then