]> begriffs open source - ai-unix/blob - ai-fix
Simpler prompt for fixer
[ai-unix] / ai-fix
1 #!/bin/bash
2
3 # AI-Fix: Code review and fixing tool
4 # Two-stage LLM workflow: review code, then apply fixes
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-fix [-r] PROMPT_FILE [FILE...]
12 Review code according to a prompt and optionally apply fixes.
13
14 Arguments:
15   PROMPT_FILE     File containing the review prompt/instructions
16
17 Options:
18   -r, --review    Review only mode (don't apply fixes)
19   -h              Show this help message
20
21 Examples:
22   # Review and fix code (default behavior)
23   ai-fix code-review-prompt.txt mycode.py
24
25   # In vim, review and fix selected text
26   :'<,'>!ai-fix code-review-prompt.txt
27
28   # Review only (no fixes applied)
29   ai-fix -r code-review-prompt.txt mycode.py
30 EOF
31 }
32
33 build_review_prompt() {
34     local review_instructions="$1"
35     
36     echo "$review_instructions"
37 }
38
39 build_fix_prompt() {
40     local review="$1"
41     
42     cat << EOF
43 You are a code fixing tool. Apply straightforward high-priority fixes to the code based on the provided review feedback.
44
45 Review Feedback:
46 $review
47 EOF
48 }
49
50 main() {
51     local review_only=false
52     local prompt_file=""
53     local files=()
54     local input review_instructions review fixed_code
55     
56     # Parse options
57     while getopts "rh-:" opt; do
58         case $opt in
59             r) review_only=true ;;
60             h) handle_help_option show_usage ;;
61             -)
62                 case $OPTARG in
63                     review) review_only=true ;;
64                     *) handle_invalid_option "--$OPTARG" ;;
65                 esac
66                 ;;
67             \?) handle_invalid_option "$OPTARG" ;;
68         esac
69     done
70     
71     shift $((OPTIND-1))
72     
73     # Validate environment and arguments
74     ensure_dependencies
75     ensure_argument_provided "Prompt file" "$1" show_usage
76     
77     prompt_file="$1"
78     shift
79     files=("$@")
80     
81     # Validate prompt file exists
82     ensure_files_exist "$prompt_file"
83     
84     # Validate additional files if provided
85     [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}"
86     
87     # Read review instructions
88     if ! review_instructions=$(cat "$prompt_file"); then
89         print_error "Failed to read prompt file: $prompt_file"
90         exit "$EXIT_USAGE_ERROR"
91     fi
92     
93     # Process input code
94     if ! input=$(process_input_sources "${files[@]}"); then
95         exit "$EXIT_NO_MATCH"
96     fi
97     
98     # Stage 1: Generate code review
99     local review_prompt
100     review_prompt=$(build_review_prompt "$review_instructions")
101     
102     if ! review=$(execute_llm_request "$review_prompt" "$input"); then
103         case $? in
104             "$EXIT_NO_MATCH") print_error "$(error_llm_no_response)" ;;
105             "$EXIT_API_ERROR") print_error "$(error_llm_api_error)" ;;
106             *) print_error "$(error_llm_command_failed)" ;;
107         esac
108         exit $?
109     fi
110     
111     # If review-only mode, output the review and exit
112     if $review_only; then
113         echo "$review"
114         exit "$EXIT_SUCCESS"
115     fi
116     
117     # Stage 2: Apply fixes based on review
118     local fix_prompt
119     fix_prompt=$(build_fix_prompt "$review")
120     
121     if ! fixed_code=$(execute_llm_request "$fix_prompt" "$input"); then
122         case $? in
123             "$EXIT_NO_MATCH") print_error "$(error_llm_no_response)" ;;
124             "$EXIT_API_ERROR") print_error "$(error_llm_api_error)" ;;
125             *) print_error "$(error_llm_command_failed)" ;;
126         esac
127         exit $?
128     fi
129     
130     # Output the fixed code
131     echo "$fixed_code"
132     exit "$EXIT_SUCCESS"
133 }
134
135 main "$@"