about summary refs log tree commit diff stats
path: root/bash/talk-to-computer/computer
blob: 77fffcd5dd24aee74bbaa1f0ad45c635ea81b832 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/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
    show_computer_help
    exit 1
fi

# Help function
show_computer_help() {
    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 [options] \"<your prompt>\" [number_of_rounds]"
    echo -e "\n\tOptions:"
    echo -e "\t  -f <file_path>    Append the contents of the file to the prompt"
    echo -e "\t  -d               Force direct Ollama response (bypass thinking mechanisms)"
    echo -e "\t  -m <mechanism>   Manually select thinking mechanism:"
    echo -e "\t                    direct, socratic, exploration, consensus, critique,"
    echo -e "\t                    synthesis, peer-review, puzzle"
    echo -e "\t  -h, --help       Show this help message"
    echo -e "\n\tExamples:"
    echo -e "\t  $0 \"What is 2+2?\"                                    # Auto-routing"
    echo -e "\t  $0 -f document.txt \"Analyze this\" 3                 # With file, 3 rounds"
    echo -e "\t  $0 -d \"Simple question\"                             # Direct response only"
    echo -e "\t  $0 -m puzzle \"Using Lil, how can I...\"             # Force puzzle mechanism"
    echo -e "\n\tIf number_of_rounds is not provided, defaults to $DEFAULT_ROUNDS rounds."
    echo -e "\n"
}

# Available mechanisms
show_mechanisms() {
    echo -e "\n\tAvailable Thinking Mechanisms:"
    echo -e "\t  direct        - Simple questions, direct answers"
    echo -e "\t  socratic      - Deep questioning and analysis"
    echo -e "\t  exploration   - Multiple solution paths and comparison"
    echo -e "\t  consensus     - Multiple model agreement"
    echo -e "\t  critique      - Improvement suggestions and refinement"
    echo -e "\t  synthesis     - Combining and integrating approaches"
    echo -e "\t  peer-review   - Collaborative feedback and review"
    echo -e "\t  puzzle        - Coding problems and Lil programming"
    echo -e "\n"
}

# --- Argument Parsing ---
FILE_PATH=""
FORCE_DIRECT=false
MANUAL_MECHANISM=""
while getopts "f:dm:h-:" opt; do
  case $opt in
    f)
      FILE_PATH="$OPTARG"
      ;;
    d)
      FORCE_DIRECT=true
      ;;
    m)
      MANUAL_MECHANISM="$OPTARG"
      ;;
    h)
      show_computer_help
      exit 0
      ;;
    -)
      case "${OPTARG}" in
        help)
          show_computer_help
          exit 0
          ;;
        mechanisms)
          show_mechanisms
          exit 0
          ;;
        *)
          echo "Invalid option: --${OPTARG}" >&2
          exit 1
          ;;
      esac
      ;;
    *)
      echo "Invalid option: -$OPTARG" >&2
      show_computer_help
      exit 1
      ;;
  esac
done
shift $((OPTIND -1))

PROMPT="$1"
if [ -z "$2" ]; then
    ROUNDS=$DEFAULT_ROUNDS
else
    ROUNDS=$2
fi

# Store original prompt for validation after sourcing
ORIGINAL_PROMPT="$PROMPT"
ORIGINAL_FILE_PATH="$FILE_PATH"
ORIGINAL_ROUNDS="$ROUNDS"

# Source the logging system using absolute path
source "${SCRIPT_DIR}/logging.sh"

# Ensure validation functions are available
if ! command -v validate_prompt >/dev/null 2>&1; then
    echo "Error: Validation functions not loaded properly" >&2
    exit 1
fi

# Validate and set default model with fallback
DEFAULT_MODEL=$(validate_model "$DEFAULT_MODEL" "llama3:8b-instruct-q4_K_M")
if [ $? -ne 0 ]; then
    log_error "No valid default model available"
    exit 1
fi

# Validate prompt
PROMPT=$(validate_prompt "$ORIGINAL_PROMPT")
if [ $? -ne 0 ]; then
    exit 1
fi

# Validate file path if provided
if [ -n "$ORIGINAL_FILE_PATH" ]; then
    if ! validate_file_path "$ORIGINAL_FILE_PATH"; then
        exit 1
    fi
    FILE_CONTENTS=$(cat "$ORIGINAL_FILE_PATH")
    PROMPT="$PROMPT\n[FILE CONTENTS]\n$FILE_CONTENTS\n[END FILE]"
fi

# Validate rounds
if ! [[ "$ORIGINAL_ROUNDS" =~ ^[1-9][0-9]*$ ]] || [ "$ORIGINAL_ROUNDS" -gt 5 ]; then
    log_error "Invalid number of rounds: $ORIGINAL_ROUNDS (must be 1-5)"
    exit 1
fi

# --- 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}"

# --- Advanced Prompt Analysis Function ---
analyze_prompt() {
    local prompt="$1"
    local use_advanced="${2:-true}"
    
    # Check for direct Ollama requests (explicit user intent)
    if [[ "$prompt" =~ (direct|simple|quick|fast|straight) ]]; then
        echo "DIRECT:1.0"
        return
    fi
    
    # Use advanced classification if available
    if [ "$use_advanced" = "true" ] && [ -f "${SCRIPT_DIR}/classifier.sh" ]; then
        source "${SCRIPT_DIR}/classifier.sh"
        local result=$(classify_prompt "$prompt" true)
        if [[ "$result" =~ ^[A-Z_]+:[0-9.]+$ ]]; then
            echo "$result"
            return
        else
            log_warning "Advanced classifier failed, falling back to simple classification"
        fi
    fi
    
    # Fallback to simple classification
    local analysis=""
    local confidence="0.6"
    
    # 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
        echo "DIRECT:0.8"
        return
    fi
    
    # Keyword-based classification with priority order
    if [[ "$prompt" =~ (consensus|agree|disagree|vote|multiple.*perspectives|multiple.*opinions) ]]; then
        analysis="CONSENSUS"
    elif [[ "$prompt" =~ (synthesize|combine|integrate|unify|merge|consolidate) ]]; then
        analysis="SYNTHESIS"
    elif [[ "$prompt" =~ (explore.*paths|explore.*alternatives|compare.*strategies|compare.*approaches|what.*options) ]]; then
        analysis="EXPLORATION"
    elif [[ "$prompt" =~ (improve|refine|edit|revise|better|enhance|polish|fix|optimize) ]]; then
        analysis="CRITIQUE"
    elif [[ "$prompt" =~ (review|feedback|peer.*review|collaborate|suggest|advice) ]]; then
        analysis="PEER_REVIEW"
    elif [[ "$prompt" =~ (analyze|examine|investigate|deep.*dive|thorough.*analysis|comprehensive) ]]; then
        analysis="SOCRATIC"
    elif [[ "$prompt" =~ (explore|alternatives|options|compare|strategies|approaches) ]]; then
        analysis="EXPLORATION"
        confidence="0.5"  # Lower confidence due to ambiguous keywords
    else
        # Default to direct for unclear cases
        analysis="DIRECT"
        confidence="0.4"
    fi
    
    echo "$analysis:$confidence"
}

# --- Mechanism Selection ---
echo "Analyzing prompt and selecting mechanism..."
echo "PROMPT ANALYSIS:" >> "${SESSION_FILE}"

if [ "$FORCE_DIRECT" = true ]; then
    MECHANISM="DIRECT"
    CONFIDENCE="1.0"
    REASON="User requested direct response with -d flag"
else
    # Check for manual mechanism selection
    if [ -n "$MANUAL_MECHANISM" ]; then
        # Validate manual mechanism selection
        case "$MANUAL_MECHANISM" in
            direct|DIRECT)
                MECHANISM="DIRECT"
                CONFIDENCE="1.0"
                REASON="User manually selected direct mechanism"
                ;;
            socratic|SOCRATIC)
                MECHANISM="SOCRATIC"
                CONFIDENCE="1.0"
                REASON="User manually selected socratic mechanism"
                ;;
            exploration|EXPLORATION)
                MECHANISM="EXPLORATION"
                CONFIDENCE="1.0"
                REASON="User manually selected exploration mechanism"
                ;;
            consensus|CONSENSUS)
                MECHANISM="CONSENSUS"
                CONFIDENCE="1.0"
                REASON="User manually selected consensus mechanism"
                ;;
            critique|CRITIQUE)
                MECHANISM="CRITIQUE"
                CONFIDENCE="1.0"
                REASON="User manually selected critique mechanism"
                ;;
            synthesis|SYNTHESIS)
                MECHANISM="SYNTHESIS"
                CONFIDENCE="1.0"
                REASON="User manually selected synthesis mechanism"
                ;;
            peer-review|peer_review|PEER_REVIEW|PEER-REVIEW)
                MECHANISM="PEER_REVIEW"
                CONFIDENCE="1.0"
                REASON="User manually selected peer-review mechanism"
                ;;
            puzzle|PUZZLE)
                MECHANISM="PUZZLE"
                CONFIDENCE="1.0"
                REASON="User manually selected puzzle mechanism"
                ;;
            *)
                echo "Error: Invalid mechanism '$MANUAL_MECHANISM'" >&2
                echo "Use --mechanisms to see available options." >&2
                exit 1
                ;;
        esac
    else
        ANALYSIS_RESULT=$(analyze_prompt "$PROMPT")
        MECHANISM=$(echo "$ANALYSIS_RESULT" | cut -d':' -f1)
        CONFIDENCE=$(echo "$ANALYSIS_RESULT" | cut -d':' -f2)

        # Validate confidence score
        if [[ ! "$CONFIDENCE" =~ ^[0-9.]+$ ]]; then
            CONFIDENCE="0.5"
            log_warning "Invalid confidence score, defaulting to 0.5"
        fi
    fi
    
    case "$MECHANISM" in
        "DIRECT")
            REASON="Simple prompt or direct request (confidence: $CONFIDENCE)"
            ;;
        "CONSENSUS")
            REASON="Multiple perspectives or consensus needed (confidence: $CONFIDENCE)"
            ;;
        "SYNTHESIS")
            REASON="Integration of multiple approaches needed (confidence: $CONFIDENCE)"
            ;;
        "EXPLORATION")
            REASON="Systematic exploration of alternatives needed (confidence: $CONFIDENCE)"
            ;;
        "SOCRATIC")
            REASON="Deep analysis or exploration required (confidence: $CONFIDENCE)"
            ;;
        "CRITIQUE")
            REASON="Improvement or refinement requested (confidence: $CONFIDENCE)"
            ;;
        "PEER_REVIEW")
            REASON="Collaborative review or feedback needed (confidence: $CONFIDENCE)"
            ;;
        "PUZZLE")
            REASON="Puzzle solving or coding challenge (confidence: $CONFIDENCE)"
            ;;
        *)
            REASON="Default fallback (confidence: $CONFIDENCE)"
            MECHANISM="DIRECT"
            ;;
    esac
    
    # Low confidence warning
    if (( $(echo "$CONFIDENCE < 0.6" | bc -l 2>/dev/null || echo "0") )); then
        log_warning "Low classification confidence ($CONFIDENCE) for prompt: $PROMPT"
        echo "Note: Classification confidence is low ($CONFIDENCE). Consider using -d for direct response." >&2
    fi
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}"
        ;;
        
        "PUZZLE")
            echo "Delegating to puzzle mechanism..."
            echo "DELEGATING TO PUZZLE:" >> "${SESSION_FILE}"
            
            # Execute puzzle script and display output directly
            "${SCRIPT_DIR}/puzzle" "${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}" 
echo "Full dispatch log: ${SESSION_FILE}"