#!/bin/bash # AI-Class: Semantic categorization 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-class CATEGORIES [FILE...] Categorize input text into predefined buckets based on semantic content. Arguments: CATEGORIES Comma-separated list of category labels Options: -h Show this help message EOF } build_categorization_prompt() { local categories="$1" cat << EOF You are a semantic categorization tool. Categorize each line of the input text into one of these categories: $categories For each line of input, output the line prefixed with the most appropriate category label followed by a colon and space. Example format: category1: original line content category2: another line content Categories to choose from: $categories EOF } validate_and_setup() { local categories="$1" shift local files=("$@") ensure_dependencies ensure_argument_provided "Categories" "$categories" show_usage [[ ${#files[@]} -gt 0 ]] && ensure_files_exist "${files[@]}" } main() { local categories="" local files=() # Parse options while getopts "h" opt; do case $opt in h) handle_help_option show_usage ;; \?) handle_invalid_option "$OPTARG" ;; esac done shift $((OPTIND-1)) categories="$1" shift files=("$@") # Early validation with immediate exit on failure validate_and_setup "$categories" "${files[@]}" # Process input with early exit local input input=$(process_input_sources "${files[@]}") || exit "$EXIT_NO_MATCH" # Execute LLM request with early exit local prompt response prompt=$(build_categorization_prompt "$categories") response=$(execute_llm_request "$prompt" "$input") || handle_llm_error $? # Process response with early exit local result result=$(process_categorization_response "$response") || exit $? echo "$result" } main "$@"