3 # AI-Cut: Structured field extraction 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-cut -f FIELDS [FILE...]
12 Extract structured fields from unstructured text using natural language field descriptions.
15 -f FIELDS Comma-separated field descriptions (required)
16 -j Output as JSON objects (default: TSV)
17 -h Show this help message
21 build_extraction_prompt() {
23 local json_output="$2"
25 local format_instructions
26 if [[ "$json_output" == "true" ]]; then
27 format_instructions='For each line or record in the input, extract the specified fields and output as a JSON object with the field names as keys. Output one JSON object per line. Output ONLY the raw JSON - do not wrap in markdown code blocks or add any other formatting.
30 {"field1": "value1", "field2": "value2", "field3": "value3"}'
32 format_instructions='For each line or record in the input, extract the specified fields and output as tab-separated values. Output the fields in the same order as specified.
39 You are a structured data extraction tool. Extract the following fields from the unstructured text: $fields
45 process_extraction_response() {
47 local json_output="$2"
49 if [[ "$json_output" == "true" ]]; then
50 process_json_response "$response"
57 validate_and_setup() {
63 ensure_argument_provided "Field specification (-f)" "$fields" show_usage
65 [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}"
70 local json_output="false"
74 while getopts "f:jh" opt; do
76 f) fields="$OPTARG" ;;
77 j) json_output="true" ;;
78 h) handle_help_option show_usage ;;
79 \?) handle_invalid_option "$OPTARG" ;;
86 # Early validation with immediate exit on failure
87 validate_and_setup "$fields" "${files[@]}"
89 # Process input with early exit
91 input=$(process_input_sources "${files[@]}") || exit "$EXIT_NO_MATCH"
93 # Execute LLM request with early exit
95 prompt=$(build_extraction_prompt "$fields" "$json_output")
96 response=$(execute_llm_request "$prompt" "$input") || handle_llm_error $?
98 # Process response with early exit
100 result=$(process_extraction_response "$response" "$json_output") || exit $?