#!/bin/bash # AI-Test: Semantic validation tool # Beautiful implementation using composable functions SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$SCRIPT_DIR/ai-common" show_usage() { cat << 'EOF' Usage: ai-test CONDITION [FILE...] Validate text content against semantic criteria. Arguments: CONDITION Semantic condition to test against Options: -q Suppress all output (exit code only) -v Invert match (exit 0 when condition is false) -p Pass-through mode (output input unchanged when condition is true) -h Show this help message EOF } build_validation_prompt() { local condition="$1" cat << EOF You are a semantic validation tool. Analyze the following text and determine if it meets this condition: $condition CRITICAL: You must respond with EXACTLY one word: either "TRUE" or "FALSE". No other text, explanations, or formatting. - If the condition is met, respond: TRUE - If the condition is not met, respond: FALSE EOF } apply_condition_logic() { local normalized_result="$1" local invert="$2" local condition_met="false" [[ "$normalized_result" == "TRUE" ]] && condition_met="true" # Apply inversion if requested if [[ "$invert" == "true" ]]; then [[ "$condition_met" == "true" ]] && condition_met="false" || condition_met="true" fi echo "$condition_met" } generate_output() { local condition_met="$1" local condition="$2" local quiet="$3" local passthrough="$4" local input="$5" if [[ "$condition_met" == "true" ]]; then if [[ "$passthrough" == "true" ]]; then echo "$input" elif [[ "$quiet" == "false" ]]; then echo "Condition met: $condition" fi return "$EXIT_SUCCESS" else if [[ "$quiet" == "false" && "$passthrough" == "false" ]]; then echo "Condition not met: $condition" fi return "$EXIT_NO_MATCH" fi } main() { local quiet="false" local invert="false" local passthrough="false" local condition="" local files=() local input response normalized_result condition_met # Parse options while getopts "qvph" opt; do case $opt in q) quiet="true" ;; v) invert="true" ;; p) passthrough="true" ;; h) handle_help_option show_usage ;; \?) handle_invalid_option "$OPTARG" ;; esac done shift $((OPTIND-1)) # Validate environment and arguments ensure_dependencies ensure_argument_provided "Condition" "$1" show_usage condition="$1" shift files=("$@") # Validate files if provided [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}" # Process input if ! input=$(process_input_sources "${files[@]}"); then exit "$EXIT_NO_MATCH" fi # Execute LLM request local prompt prompt=$(build_validation_prompt "$condition") if ! response=$(execute_llm_request "$prompt" "$input"); then case $? in "$EXIT_NO_MATCH") print_error "$(error_llm_no_response)" ;; "$EXIT_API_ERROR") print_error "$(error_llm_api_error)" ;; *) print_error "$(error_llm_command_failed)" ;; esac exit $? fi # Process and validate boolean response if ! normalized_result=$(process_boolean_response "$response"); then exit $? fi # Apply condition logic condition_met=$(apply_condition_logic "$normalized_result" "$invert") # Generate output and return appropriate exit code if ! generate_output "$condition_met" "$condition" "$quiet" "$passthrough" "$input"; then exit $? fi exit "$EXIT_SUCCESS" } main "$@"