#!/bin/bash # Get the directory where this script is located SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # Computer Dispatch System # This script intelligently routes prompts to the most appropriate thinking mechanism # or directly to Ollama based on complexity, question type, and user intent. # # APPLICATION LOGIC: # The computer dispatch system implements an intelligent routing mechanism that # analyzes user prompts and determines the optimal response strategy. The system # operates through three distinct phases designed to maximize response quality: # # PHASE 1 - PROMPT ANALYSIS: # - Analyzes prompt complexity, length, and question type # - Identifies user intent and specific keywords # - Determines if direct Ollama response is appropriate # - Classifies prompts into response categories # # PHASE 2 - MECHANISM SELECTION: # - Routes to appropriate thinking mechanism based on classification # - Uses decision tree with keywords for clear cases # - Considers prompt complexity and user intent # - Falls back to direct Ollama for simple cases # # PHASE 3 - RESPONSE EXECUTION: # - Executes the selected mechanism or direct Ollama call # - Maintains transparency about the routing decision # - Provides consistent output format regardless of mechanism # - Logs the decision process for analysis # # DISPATCH MODELING: # The system applies intelligent routing principles to AI response generation: # - Prompt classification helps match complexity to appropriate mechanism # - Keyword analysis identifies specific user needs and intent # - Decision tree provides consistent, predictable routing logic # - Direct Ollama routing handles simple cases efficiently # - Transparency shows users how their prompt was processed # - The system may improve response quality by using specialized mechanisms # # The dispatch process emphasizes efficiency and appropriateness, # ensuring users get the best possible response for their specific needs. # The system balances speed with depth based on prompt characteristics. # --- Model Configuration --- DEFAULT_MODEL="gemma3n:e2b" # --- Defaults --- DEFAULT_ROUNDS=2 # --- Argument Validation --- if [ "$#" -lt 1 ]; then echo -e "\n\tComputer" echo -e "\tThis script intelligently routes prompts to the most appropriate thinking mechanism" echo -e "\tor directly to Ollama based on complexity, question type, and user intent." echo -e "\n\tUsage: $0 [-f ] [-d] \"\" [number_of_rounds]" echo -e "\n\tExample: $0 -f ./input.txt \"Please analyze this text\" 2" echo -e "\n\tIf number_of_rounds is not provided, the program will default to $DEFAULT_ROUNDS rounds." echo -e "\n\t-f (optional): Append the contents of the file to the prompt." echo -e "\n\t-d (optional): Force direct Ollama response (bypass thinking mechanisms)." echo -e "\n" exit 1 fi # --- Argument Parsing --- FILE_PATH="" FORCE_DIRECT=false while getopts "f:d" opt; do case $opt in f) FILE_PATH="$OPTARG" ;; d) FORCE_DIRECT=true ;; *) echo "Invalid option: -$OPTARG" >&2 exit 1 ;; esac done shift $((OPTIND -1)) PROMPT="$1" if [ -z "$2" ]; then ROUNDS=$DEFAULT_ROUNDS else ROUNDS=$2 fi # If file path is provided, append its contents to the prompt if [ -n "$FILE_PATH" ]; then if [ ! -f "$FILE_PATH" ]; then echo "File not found: $FILE_PATH" >&2 exit 1 fi FILE_CONTENTS=$(cat "$FILE_PATH") PROMPT="$PROMPT\n[FILE CONTENTS]\n$FILE_CONTENTS\n[END FILE]" fi # Source the logging system using absolute path source "${SCRIPT_DIR}/logging.sh" # --- File Initialization --- # Create a temporary directory if it doesn't exist mkdir -p ~/tmp # Create a unique file for this session based on the timestamp SESSION_FILE=~/tmp/computer_$(date +%Y%m%d_%H%M%S).txt # Initialize timing SESSION_ID=$(generate_session_id) start_timer "$SESSION_ID" "computer" echo "Computer Dispatch Session Log: ${SESSION_FILE}" echo "---------------------------------" # Store the initial user prompt in the session file echo "USER PROMPT: ${PROMPT}" >> "${SESSION_FILE}" echo "FORCE DIRECT: ${FORCE_DIRECT}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" # --- Prompt Analysis Function --- analyze_prompt() { local prompt="$1" local analysis="" # Check for direct Ollama requests if [[ "$prompt" =~ (direct|simple|quick|fast|straight) ]]; then analysis="DIRECT" return fi # Check prompt length (simple heuristic for complexity) local word_count=$(echo "$prompt" | wc -w) # Very short prompts (likely simple questions) if [ "$word_count" -le 5 ]; then analysis="DIRECT" return fi # Keyword-based classification if [[ "$prompt" =~ (consensus|agree|disagree|vote|multiple|perspectives|opinions) ]]; then analysis="CONSENSUS" elif [[ "$prompt" =~ (synthesize|combine|integrate|unify|merge|consolidate) ]]; then analysis="SYNTHESIS" elif [[ "$prompt" =~ (explore|paths|alternatives|options|compare|strategies|approaches) ]]; then analysis="EXPLORATION" elif [[ "$prompt" =~ (analyze|examine|explore|investigate|deep|thorough|comprehensive) ]]; then analysis="SOCRATIC" elif [[ "$prompt" =~ (improve|refine|edit|revise|better|enhance|polish|fix) ]]; then analysis="CRITIQUE" elif [[ "$prompt" =~ (review|feedback|peer|collaborate|suggest|advice) ]]; then analysis="PEER_REVIEW" else # Default to direct for unclear cases analysis="DIRECT" fi echo "$analysis" } # --- Mechanism Selection --- echo "Analyzing prompt and selecting mechanism..." echo "PROMPT ANALYSIS:" >> "${SESSION_FILE}" if [ "$FORCE_DIRECT" = true ]; then MECHANISM="DIRECT" REASON="User requested direct response with -d flag" else MECHANISM=$(analyze_prompt "$PROMPT") case "$MECHANISM" in "DIRECT") REASON="Simple prompt or direct request" ;; "CONSENSUS") REASON="Multiple perspectives or consensus needed" ;; "SYNTHESIS") REASON="Integration of multiple approaches needed" ;; "EXPLORATION") REASON="Systematic exploration of alternatives needed" ;; "SOCRATIC") REASON="Deep analysis or exploration required" ;; "CRITIQUE") REASON="Improvement or refinement requested" ;; "PEER_REVIEW") REASON="Collaborative review or feedback needed" ;; *) REASON="Default fallback" MECHANISM="DIRECT" ;; esac fi echo "Selected mechanism: ${MECHANISM}" >> "${SESSION_FILE}" echo "Reason: ${REASON}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" echo "Selected mechanism: ${MECHANISM}" echo "Reason: ${REASON}" echo "---------------------------------" # --- Response Execution --- echo "Executing selected mechanism..." echo "RESPONSE EXECUTION:" >> "${SESSION_FILE}" case "$MECHANISM" in "DIRECT") echo "Using direct Ollama response..." echo "DIRECT OLLAMA RESPONSE:" >> "${SESSION_FILE}" DIRECT_PROMPT="You are an expert assistant. You always flag if you don't know something. Please provide a clear, helpful response to the following prompt: ${PROMPT}" RESPONSE=$(ollama run "${DEFAULT_MODEL}" "${DIRECT_PROMPT}") echo "${RESPONSE}" >> "${SESSION_FILE}" echo "" >> "${SESSION_FILE}" echo "---------------------------------" echo "Direct response:" echo "---------------------------------" echo "${RESPONSE}" ;; "CONSENSUS") echo "Delegating to consensus mechanism..." echo "DELEGATING TO CONSENSUS:" >> "${SESSION_FILE}" # Execute consensus script and display output directly "${SCRIPT_DIR}/consensus" "${PROMPT}" "${ROUNDS}" 2>&1 | tee -a "${SESSION_FILE}" ;; "SOCRATIC") echo "Delegating to Socratic mechanism..." echo "DELEGATING TO SOCRATIC:" >> "${SESSION_FILE}" # Execute Socratic script and display output directly "${SCRIPT_DIR}/socratic" "${PROMPT}" "${ROUNDS}" 2>&1 | tee -a "${SESSION_FILE}" ;; "CRITIQUE") echo "Delegating to critique mechanism..." echo "DELEGATING TO CRITIQUE:" >> "${SESSION_FILE}" # Execute critique script and display output directly "${SCRIPT_DIR}/critique" "${PROMPT}" "${ROUNDS}" 2>&1 | tee -a "${SESSION_FILE}" ;; "PEER_REVIEW") echo "Delegating to peer-review mechanism..." echo "DELEGATING TO PEER_REVIEW:" >> "${SESSION_FILE}" # Execute peer-review script and display output directly "${SCRIPT_DIR}/peer-review" "${PROMPT}" "${ROUNDS}" 2>&1 | tee -a "${SESSION_FILE}" ;; "SYNTHESIS") echo "Delegating to synthesis mechanism..." echo "DELEGATING TO SYNTHESIS:" >> "${SESSION_FILE}" # Execute synthesis script and display output directly "${SCRIPT_DIR}/synthesis" "${PROMPT}" "${ROUNDS}" 2>&1 | tee -a "${SESSION_FILE}" ;; "EXPLORATION") echo "Delegating to exploration mechanism..." echo "DELEGATING TO EXPLORATION:" >> "${SESSION_FILE}" # Execute exploration script and display output directly "${SCRIPT_DIR}/exploration" "${PROMPT}" "${ROUNDS}" 2>&1 | tee -a "${SESSION_FILE}" ;; esac # --- Final Summary --- echo "" >> "${SESSION_FILE}" echo "DISPATCH SUMMARY:" >> "${SESSION_FILE}" echo "================" >> "${SESSION_FILE}" echo "Original Prompt: ${PROMPT}" >> "${SESSION_FILE}" echo "Selected Mechanism: ${MECHANISM}" >> "${SESSION_FILE}" echo "Reason: ${REASON}" >> "${SESSION_FILE}" echo "Rounds: ${ROUNDS}" >> "${SESSION_FILE}" # End timing duration=$(end_timer "$SESSION_ID" "computer") echo "" echo "Execution time: ${duration} seconds" echo "Full dispatch log: ${SESSION_FILE}"