]> begriffs open source - ai-unix/blob - ai-test
Simpler prompt for fixer
[ai-unix] / ai-test
1 #!/bin/bash
2
3 # AI-Test: Semantic validation tool
4 # Beautiful implementation using composable functions
5
6 SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
7 source "$SCRIPT_DIR/ai-common"
8
9 show_usage() {
10     cat << 'EOF'
11 Usage: ai-test CONDITION [FILE...]
12 Validate text content against semantic criteria.
13
14 Arguments:
15   CONDITION       Semantic condition to test against
16
17 Options:
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
22 EOF
23 }
24
25 build_validation_prompt() {
26     local condition="$1"
27     
28     cat << EOF
29 You are a semantic validation tool. Analyze the following text and determine if it meets this condition: $condition
30
31 CRITICAL: You must respond with EXACTLY one word: either "TRUE" or "FALSE". No other text, explanations, or formatting.
32
33 - If the condition is met, respond: TRUE
34 - If the condition is not met, respond: FALSE
35 EOF
36 }
37
38 apply_condition_logic() {
39     local normalized_result="$1"
40     local invert="$2"
41     
42     local condition_met="false"
43     [[ "$normalized_result" == "TRUE" ]] && condition_met="true"
44     
45     # Apply inversion if requested
46     if [[ "$invert" == "true" ]]; then
47         [[ "$condition_met" == "true" ]] && condition_met="false" || condition_met="true"
48     fi
49     
50     echo "$condition_met"
51 }
52
53 generate_output() {
54     local condition_met="$1"
55     local condition="$2"
56     local quiet="$3"
57     local passthrough="$4"
58     local input="$5"
59     
60     if [[ "$condition_met" == "true" ]]; then
61         if [[ "$passthrough" == "true" ]]; then
62             echo "$input"
63         elif [[ "$quiet" == "false" ]]; then
64             echo "Condition met: $condition"
65         fi
66         return "$EXIT_SUCCESS"
67     else
68         if [[ "$quiet" == "false" && "$passthrough" == "false" ]]; then
69             echo "Condition not met: $condition"
70         fi
71         return "$EXIT_NO_MATCH"
72     fi
73 }
74
75 main() {
76     local quiet="false"
77     local invert="false"
78     local passthrough="false"
79     local condition=""
80     local files=()
81     local input response normalized_result condition_met
82     
83     # Parse options
84     while getopts "qvph" opt; do
85         case $opt in
86             q) quiet="true" ;;
87             v) invert="true" ;;
88             p) passthrough="true" ;;
89             h) handle_help_option show_usage ;;
90             \?) handle_invalid_option "$OPTARG" ;;
91         esac
92     done
93     
94     shift $((OPTIND-1))
95     
96     # Validate environment and arguments
97     ensure_dependencies
98     ensure_argument_provided "Condition" "$1" show_usage
99     
100     condition="$1"
101     shift
102     files=("$@")
103     
104     # Validate files if provided
105     [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}"
106     
107     # Process input
108     if ! input=$(process_input_sources "${files[@]}"); then
109         exit "$EXIT_NO_MATCH"
110     fi
111     
112     # Execute LLM request
113     local prompt
114     prompt=$(build_validation_prompt "$condition")
115     
116     if ! response=$(execute_llm_request "$prompt" "$input"); then
117         case $? in
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)" ;;
121         esac
122         exit $?
123     fi
124     
125     # Process and validate boolean response
126     if ! normalized_result=$(process_boolean_response "$response"); then
127         exit $?
128     fi
129     
130     # Apply condition logic
131     condition_met=$(apply_condition_logic "$normalized_result" "$invert")
132     
133     # Generate output and return appropriate exit code
134     if ! generate_output "$condition_met" "$condition" "$quiet" "$passthrough" "$input"; then
135         exit $?
136     fi
137     
138     exit "$EXIT_SUCCESS"
139 }
140
141 main "$@"