-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·1589 lines (1416 loc) · 75.3 KB
/
setup.sh
File metadata and controls
executable file
·1589 lines (1416 loc) · 75.3 KB
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Exit immediately if a command exits with a non-zero status.
set -e
# --- UI & Colors ---
# Define ANSI color codes
RED=$'\033[0;31m'
GREEN=$'\033[0;32m'
YELLOW=$'\033[0;33m'
BLUE=$'\033[0;34m'
MAGENTA=$'\033[0;35m'
CYAN=$'\033[0;36m'
GRAY=$'\033[0;37m'
BOLD=$'\033[1m'
UNDERLINE=$'\033[4m'
RESET=$'\033[0m'
# Color support check
if [[ -t 1 ]]; then
# Check if terminal supports colors
ncolors=$(tput colors 2>/dev/null || echo 0)
if [[ -n "$ncolors" && $ncolors -ge 8 ]]; then
HAS_COLORS=true
else
HAS_COLORS=false
# Reset color variables to empty strings if colors aren't supported
RED='' GREEN='' YELLOW='' BLUE='' MAGENTA='' CYAN='' GRAY='' BOLD='' UNDERLINE='' RESET=''
fi
else
HAS_COLORS=false
RED='' GREEN='' YELLOW='' BLUE='' MAGENTA='' CYAN='' GRAY='' BOLD='' UNDERLINE='' RESET=''
fi
# --- Configuration Variables ---
PYTHON_CMD="python3"
VENV_DIR="venv"
MCP_JSON_FILE="mcp.json"
MODELS_DIR="models"
HUGGINGFACE_CLI_CMD="huggingface-cli" # Use variable for potential path issues
CONFIG_YAML="config.yaml"
# --- Helper Function Definitions ---
print_section_header() {
local title="$1"
local width=70
local title_len=${#title}
local padding_total=$((width - title_len - 2))
local padding_left=$((padding_total / 2))
local padding_right=$((padding_total - padding_left))
if [ $padding_left -lt 0 ]; then padding_left=0; fi
if [ $padding_right -lt 0 ]; then padding_right=0; fi
echo ""
echo "${BLUE}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓${RESET}"
printf "${BLUE}┃%${padding_left}s${RESET}${BOLD}%s${RESET}%${padding_right}s${BLUE}┃${RESET}\\n" "" "$title" ""
echo "${BLUE}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛${RESET}"
echo ""
}
print_info() { echo "${GREEN}[INFO]${RESET} $1"; }
print_warning() { echo "${YELLOW}[WARNING]${RESET} $1"; }
print_error() { echo "${RED}[ERROR]${RESET} $1" >&2; }
print_progress() {
local percent=$1
local width=50
local completed=$((percent * width / 100))
local remaining=$((width - completed))
printf "${BLUE}[${GREEN}"
printf "%0.s█" $(seq 1 $completed)
printf "%0.s░" $(seq 1 $remaining)
printf "${BLUE}] ${YELLOW}%d%%${RESET}\\r" $percent
}
print_model_choice() {
local number=$1
local name=$2
local description=$3
local recommended=$4
if [ "$recommended" = "true" ]; then
echo "${GREEN}${BOLD}[$number]${RESET} ${CYAN}${BOLD}$name${RESET} ${GREEN}(Recommended)${RESET}"
else
echo "${GREEN}${BOLD}[$number]${RESET} ${CYAN}${BOLD}$name${RESET}"
fi
echo " ${GRAY}$description${RESET}"
}
print_summary_box() {
local title="$1"
shift
local lines=("$@")
local width=70
echo "${YELLOW}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓${RESET}"
printf "${YELLOW}┃${RESET}${BOLD}%$(( (width - ${#title}) / 2 ))s%s%$(( (width - ${#title} + 1) / 2 ))s${RESET}${YELLOW}┃${RESET}\\n" "" "$title" ""
echo "${YELLOW}┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫${RESET}"
for line in "${lines[@]}"; do
printf "${YELLOW}┃${RESET} %-$((width-2))s ${YELLOW}┃${RESET}\\n" "$line"
done
echo "${YELLOW}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛${RESET}"
}
get_system_ram() {
if [[ "$(uname)" == "Darwin" ]]; then
local ram_bytes=$(sysctl -n hw.memsize)
echo $((ram_bytes / 1024 / 1024 / 1024))
elif [[ "$(uname)" == "Linux" ]]; then
local ram_kb=$(grep MemTotal /proc/meminfo | awk '{print $2}')
echo $((ram_kb / 1024 / 1024))
else
echo 8 # Default fallback
fi
}
detect_apple_silicon_model() {
if [[ "$(uname)" == "Darwin" ]]; then
local model=$(sysctl -n machdep.cpu.brand_string 2>/dev/null || echo "")
if [[ "$model" == *"Apple"* ]]; then
local chip_info=""
local grep_exit_status=1
if system_profiler SPHardwareDataType 2>/dev/null | grep -q "Chip"; then
grep_exit_status=0
chip_info=$(system_profiler SPHardwareDataType 2>/dev/null | grep "Chip" | awk -F': ' '{print $2}')
fi
if [ $grep_exit_status -eq 0 ]; then echo "$chip_info"; else echo "Apple Silicon (unknown)"; fi
else echo "Not Apple Silicon"; fi
else echo "Not macOS"; fi
}
has_nvidia_gpu() {
if command -v nvidia-smi &> /dev/null; then return 0; else return 1; fi
}
get_nvidia_vram() {
if command -v nvidia-smi &> /dev/null; then
local vram_mb=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | head -n 1 | tr -d '[:space:]')
echo $((vram_mb / 1024))
else echo 0; fi
}
get_apple_unified_memory() {
if [[ "$(uname)" == "Darwin" ]]; then
if system_profiler SPHardwareDataType 2>/dev/null | grep -q "Memory"; then
local memory=$(system_profiler SPHardwareDataType 2>/dev/null | grep "Memory" | awk -F': ' '{print $2}' | sed 's/ GB//')
echo "$memory"
else echo "$(get_system_ram)"; fi
else echo 0; fi
}
# GGUF Model Download Helper
_huggingface_download_gguf() {
local repo_id=$1
local gguf_filename=$2
local target_dir=$3
local model_url=$4 # Optional URL for error messages
local target_path="${target_dir}/${gguf_filename}"
local marker_file="${target_dir}/.${gguf_filename}.download_complete"
print_info "Attempting download: Repo=${repo_id}, File=${gguf_filename}, Target=${target_dir}"
mkdir -p "$target_dir"
if [ ! -f "$marker_file" ] && [ -f "$target_path" ]; then
print_warning "Found potentially incomplete GGUF file: ${target_path}. Removing."
rm -f "$target_path"
fi
if [ -f "$marker_file" ]; then
print_info "GGUF file ${gguf_filename} already downloaded, skipping."
return 0
fi
# Ensure VENV_DIR and HUGGINGFACE_CLI_CMD are accessible
if $VENV_DIR/bin/$HUGGINGFACE_CLI_CMD download "$repo_id" "$gguf_filename" --local-dir "$target_dir" --local-dir-use-symlinks False; then
if [ -f "$target_path" ]; then
print_info "GGUF file downloaded successfully: ${target_path}"
touch "$marker_file"
return 0
else
print_error "huggingface-cli download succeeded but file not found: ${target_path}"
rm -f "$target_path"
return 1
fi
else
local exit_code=$?
print_error "huggingface-cli download failed (exit code ${exit_code}) for ${repo_id}/${gguf_filename}."
if [ $exit_code -eq 1 ]; then
print_warning "May need to accept terms or request access on Hugging Face."
if [ -n "$model_url" ]; then
echo "Visit: ${model_url}"
echo "Log in, accept terms, ensure access, then re-run setup."
if [[ "$(uname)" == "Darwin" ]]; then open "${model_url}"; fi
else echo "Check Hugging Face repository ${repo_id} for access requirements."; fi
fi
rm -f "$target_path"
return 1
fi
}
# Function to determine if local models should be offered (based only on NVIDIA)
should_offer_local_models() {
if has_nvidia_gpu; then echo true; else echo false; fi
}
# Simplified function to ask user about model preference based on hardware
# Sets global _ask_model_preference_result
ask_model_preference() {
echo ""
echo "${YELLOW}${BOLD}Checking Hardware Compatibility...${RESET}"
if ! has_nvidia_gpu; then
print_warning "NVIDIA GPU not detected."
if [[ "$(uname)" == "Darwin" ]]; then print_warning "Local models not recommended on macOS."; fi
print_info "Only Cloud Models will be configured."
_ask_model_preference_result=false # Set global var
return
fi
print_info "NVIDIA GPU detected ✓"
echo ""
echo "${CYAN}Cloud Models (Claude/Gemini) are always available.${RESET}"
echo "${GRAY}Compatible Local Models (70B+, requires >=24GB VRAM) can also be set up.${RESET}"
echo ""
# Check for quick install mode
if [ "$QUICK_INSTALL" = true ]; then
local vram_gb=$(get_nvidia_vram)
if [ "$vram_gb" -ge 24 ]; then
print_info "${GREEN}Auto-enabling setup for Local Models (${vram_gb}GB VRAM detected).${RESET}"
_ask_model_preference_result=true # Set global var
else
print_info "${YELLOW}Auto-disabling setup for Local Models (insufficient VRAM: ${vram_gb}GB).${RESET}"
_ask_model_preference_result=false # Set global var
fi
return
fi
# Interactive mode
local user_choice
read -p "${BLUE}Enable setup for Local Models? (Requires login, downloads) [y/${GREEN}N${BLUE}]: ${RESET}" user_choice
if [[ "$user_choice" == "y" || "$user_choice" == "Y" ]]; then
echo ""; print_info "${GREEN}Local Model setup enabled.${RESET}";
_ask_model_preference_result=true # Set global var
else
echo ""; print_info "${YELLOW}Local Model setup disabled. Only Cloud Models configured.${RESET}";
_ask_model_preference_result=false # Set global var
fi
}
# Summarizer Model Selection (Simplified - Always Flash)
# Sets global _selected_model_result
select_summarizer_model() {
# Assumes CONFIG_YAML accessible
print_section_header "SUMMARIZER MODEL CONFIGURATION"
local MODEL_GEMINI_FLASH="gemini-2.0-flash" # Change to gemini-2.5-flash-preview-04-17 when ready
# Always auto-select Gemini Flash regardless of quick/interactive mode
print_info "Defaulting Summarizer Model to: ${CYAN}${MODEL_GEMINI_FLASH}${RESET} (Cloud)"
print_info "Updating config.yaml..."
local update_ok=true
# Always use yq now
yq -i ".SUMMARIZER_MODEL = \"$MODEL_GEMINI_FLASH\" | .CHUNK_SIZE = 0 | .USE_LOCAL_SUMMARIZER_MODEL = false" "$CONFIG_YAML" || update_ok=false
if [ "$update_ok" = true ]; then
print_info "Summarizer configured in config.yaml ✓"
else
print_error "Failed to update config.yaml for summarizer!"
fi
# Set a global-like variable instead of echoing
_selected_model_result="$MODEL_GEMINI_FLASH"
}
# Primary Model Selection (Simplified - No Associative Arrays)
# Sets global _selected_model_result
select_primary_model() {
# Assumes USE_LOCAL_MODELS, CONFIG_YAML accessible
print_section_header "PRIMARY REASONING MODEL SELECTION"
# Define model names and descriptions in parallel arrays
local model_names=()
local model_descriptions=()
local default_model_name=""
# Add Cloud Models
local model_claude="claude-3.7-sonnet-cloud"
model_names+=("$model_claude")
model_descriptions+=("Cloud: Claude 3.7 Sonnet - 200K context, strong reasoning (API Key)")
local model_gemini="gemini-2.5-pro-cloud"
model_names+=("$model_gemini")
model_descriptions+=("Cloud: Gemini 2.5 Pro - 1M+ context, multimodal (API Key)")
default_model_name="$model_gemini" # Default to Gemini Cloud
# Define 70B+ Local Models from table
local model_llama33_70b="Llama-3.3-70B-Instruct-Q4_K_M.gguf"
local model_qwen_72b="Qwen2.5-72B-Instruct-Q4_K_M.gguf"
local model_deepseek_70b="DeepSeek-R1-Distill-Llama-70B-IQ4_NL.gguf"
local model_xwin_70b="xwin-lm-70b-v0.1.Q4_K_M.gguf"
local local_models_added=false
# Offer 70B+ Local Models only if local models enabled AND sufficient VRAM (>= 24GB)
if [ "$USE_LOCAL_MODELS" = true ]; then
NVIDIA_VRAM=$(get_nvidia_vram)
print_info "Checking VRAM for 70B+ Local Models (Detected: ${NVIDIA_VRAM}GB)"
# Check for 70B+ models (>= 24GB VRAM)
if [ "$NVIDIA_VRAM" -ge 24 ]; then
print_info "Offering 70B+ Local Models (>=24GB VRAM required)."
# Add the models from the table with updated descriptions
model_names+=("$model_llama33_70b")
model_descriptions+=("Local: Llama-3.3 70B (128k ctx) - Best all-round reasoning (~5 tok/s Q4)")
model_names+=("$model_qwen_72b")
model_descriptions+=("Local: Qwen2.5 72B (128k ctx) - Balanced, multilingual (~6 tok/s Q4)")
model_names+=("$model_deepseek_70b")
model_descriptions+=("Local: DeepSeek-R1 70B (128k ctx) - Science-grade CoT (IQ4, slow ~3 tok/s)")
model_names+=("$model_xwin_70b")
model_descriptions+=("Local: Xwin-LM 70B v0.1 (8k ctx) - Alignment champ, instruction following")
local_models_added=true
# For 70B+ models, set Llama 3.3 as default if available
if [ "$USE_LOCAL_MODELS" = true ] && [ "$NVIDIA_VRAM" -ge 24 ]; then
default_model_name="$model_llama33_70b"
fi
else
print_warning "Insufficient VRAM (${NVIDIA_VRAM}GB) for 70B+ Local Models. Only Cloud models offered."
fi
else
print_info "Local models disabled, only offering cloud models."
fi
# Find index of default model
local default_index=1 # Start assuming first model is default
for i in "${!model_names[@]}"; do
if [[ "${model_names[$i]}" = "$default_model_name" ]]; then
default_index=$((i + 1))
break
fi
done
# Quick install mode - auto-select the default model
if [ "$QUICK_INSTALL" = true ]; then
local selected_primary_model="${model_names[$((default_index - 1))]}"
print_info "Auto-selecting primary model: ${CYAN}${selected_primary_model}${RESET}"
else
# Interactive mode - Print selection header and menu
echo ""
echo "${CYAN}${BOLD}Choose a primary reasoning model:${RESET}"
echo "${GRAY}Plans research, uses tools, writes reports${RESET}"
echo ""
for i in "${!model_names[@]}"; do
idx=$((i + 1))
model="${model_names[$i]}"
description="${model_descriptions[$i]}"
is_recommended=false
if [ "$idx" -eq $default_index ]; then is_recommended=true; fi
print_model_choice "$idx" "$model" "$description" "$is_recommended"
done
echo ""
# Get user choice
local primary_user_choice
# Use printf for consistent prompting
printf "%s [%s%s%s]: %s" "${BLUE}Enter selection" "${GREEN}" "${default_index}" "${BLUE}" "${RESET}"
read primary_user_choice # Use plain read to wait for input
echo "" # Add newline after input
primary_user_choice=${primary_user_choice:-$default_index} # Default if empty
# Validate choice
if ! [[ "$primary_user_choice" =~ ^[0-9]+$ ]] || [ "$primary_user_choice" -lt 1 ] || [ "$primary_user_choice" -gt ${#model_names[@]} ]; then
print_warning "Invalid selection. Using default: ${default_model_name}"
primary_user_choice=$default_index
fi
# Get selected model name using the chosen index (0-based for array)
local selected_primary_model="${model_names[$((primary_user_choice - 1))]}"
print_info "Selected primary model: ${CYAN}${selected_primary_model}${RESET}"
fi
# Rest of the function is the same for both modes
# Determine final config values based on selection
local final_primary_model_type=""
local final_local_model_name=""
local final_local_quant_level=""
if [[ "$selected_primary_model" == *"cloud"* ]]; then
final_local_model_name="" # Clear local name for cloud
final_local_quant_level="" # Clear quant level for cloud
if [[ "$selected_primary_model" == *"claude"* ]]; then final_primary_model_type="claude";
elif [[ "$selected_primary_model" == *"gemini"* ]]; then final_primary_model_type="gemini";
else final_primary_model_type="unknown_cloud"; fi
elif [[ "$selected_primary_model" == *"$model_llama33_70b"* ]]; then
# Known local Q4_K_M model
final_primary_model_type="local"
final_local_model_name="$selected_primary_model"
final_local_quant_level="Q4_K_M"
elif [[ "$selected_primary_model" == *"$model_qwen_72b"* ]]; then
# Known local Q4_K_M model
final_primary_model_type="local"
final_local_model_name="$selected_primary_model"
final_local_quant_level="Q4_K_M"
elif [[ "$selected_primary_model" == *"$model_deepseek_70b"* ]]; then
# Known local IQ4_NL model
final_primary_model_type="local"
final_local_model_name="$selected_primary_model"
final_local_quant_level="IQ4_NL"
elif [[ "$selected_primary_model" == *"$model_xwin_70b"* ]]; then
# Known local Q4_K_M model
final_primary_model_type="local"
final_local_model_name="$selected_primary_model"
final_local_quant_level="Q4_K_M"
else
# Fallback for unexpected cases
print_error "Error: Unknown model type selected: ${selected_primary_model}"
final_primary_model_type="unknown"
final_local_model_name="$selected_primary_model"
final_local_quant_level=""
fi
print_info "Updating config.yaml for primary model..."
# Assume CONFIG_YAML is accessible
local update_ok=true
# Always use yq now
# Use yq: set local fields to null if cloud, otherwise use final values
if [[ "$final_primary_model_type" == "claude" || "$final_primary_model_type" == "gemini" ]]; then
yq -i ".PRIMARY_MODEL_TYPE = \"$final_primary_model_type\" | .LOCAL_MODEL_NAME = null | .LOCAL_MODEL_QUANT_LEVEL = null" "$CONFIG_YAML" || update_ok=false
else # Local model
yq -i ".PRIMARY_MODEL_TYPE = \"$final_primary_model_type\" | .LOCAL_MODEL_NAME = \"$final_local_model_name\" | .LOCAL_MODEL_QUANT_LEVEL = \"$final_local_quant_level\"" "$CONFIG_YAML" || update_ok=false
fi
if [ "$update_ok" = true ]; then
print_info "Primary model configured in config.yaml ✓"
else
print_error "Failed to update config.yaml for primary model!"
fi
# Set the global variable instead of echoing
_selected_model_result="$selected_primary_model"
}
# Configure Additional Options
configure_additional_options() {
local SELECTED_PRIMARY_MODEL="$1" # Passed from main
# Assumes CONFIG_YAML accessible
print_section_header "ADDITIONAL CONFIGURATION OPTIONS"
# 1. Memory Capacity
echo "${CYAN}${BOLD}Memory Capacity Setting${RESET}"
local CURRENT_MEMORY_TOKENS=$(grep -E "^CONVERSATION_MEMORY_MAX_TOKENS:" "$CONFIG_YAML" | awk '{print $2}' || echo "900000")
# Note: Suggested values leave a buffer within the model's max context window to ensure space for the generated output.
local SUGGESTED_MEMORY_TOKENS="900000"; local MEMORY_EXPLANATION="Gemini default (1M+ context)"
if [[ "$SELECTED_PRIMARY_MODEL" == *"claude"* ]]; then SUGGESTED_MEMORY_TOKENS="120000"; MEMORY_EXPLANATION="Claude (~200K context, 120K leaves buffer for output)";
elif [[ "$SELECTED_PRIMARY_MODEL" == *"llama-3.3-70b"* ]]; then SUGGESTED_MEMORY_TOKENS="110000"; MEMORY_EXPLANATION="Llama 3 70B (128K context, 110K leaves buffer for output)"; fi
echo ""; echo "Current: ${YELLOW}${CURRENT_MEMORY_TOKENS}${RESET}, Suggested: ${GREEN}${SUGGESTED_MEMORY_TOKENS}${RESET} (${MEMORY_EXPLANATION})"; echo ""
local UPDATE_MEMORY
if [ "$QUICK_INSTALL" = true ]; then
print_info "Auto-updating memory capacity to recommended value: ${SUGGESTED_MEMORY_TOKENS}"
UPDATE_MEMORY="Y"
else
printf "%s [%s%s%s/%s] %s" "${BLUE}Update memory capacity?" "${GREEN}" "Y" "${BLUE}" "n" "${RESET}" # Use printf for consistent prompting
read UPDATE_MEMORY # Use plain read to wait for input
echo "" # Add newline after input
UPDATE_MEMORY=${UPDATE_MEMORY:-Y} # Default to Y if empty
fi
if [[ "$UPDATE_MEMORY" == "y" || "$UPDATE_MEMORY" == "Y" ]]; then
# Always use yq now
yq -i ".CONVERSATION_MEMORY_MAX_TOKENS = $SUGGESTED_MEMORY_TOKENS" "$CONFIG_YAML" || print_error "yq update failed for memory capacity"
print_info "Memory capacity updated."
else print_info "Keeping current memory capacity."; fi
# 2. Enhanced Reasoning
echo ""; echo "${CYAN}${BOLD}Enhanced Reasoning Setting${RESET}"; echo "${GRAY}Enables explicit 'thinking' steps${RESET}"
local CURRENT_THINKING=$(grep -E "^ENABLE_THINKING:" "$CONFIG_YAML" | awk '{print $2}' || echo "false")
local SUGGESTED_THINKING="true"; local THINKING_EXPLANATION="Recommended for capable models"
echo ""; echo "Current: ${YELLOW}${CURRENT_THINKING}${RESET}, Suggested: ${GREEN}${SUGGESTED_THINKING}${RESET} (${THINKING_EXPLANATION})"; echo ""
local UPDATE_THINKING
if [ "$QUICK_INSTALL" = true ]; then
print_info "Auto-enabling enhanced reasoning for better performance."
UPDATE_THINKING="Y"
else
printf "%s [%s%s%s/%s] %s" "${BLUE}Update enhanced reasoning?" "${GREEN}" "Y" "${BLUE}" "n" "${RESET}" # Use printf for consistent prompting
read UPDATE_THINKING # Use plain read to wait for input
echo "" # Add newline after input
UPDATE_THINKING=${UPDATE_THINKING:-Y} # Default to Y if empty
fi
if [[ "$UPDATE_THINKING" == "y" || "$UPDATE_THINKING" == "Y" ]]; then
# Always use yq now
yq -i ".ENABLE_THINKING = $SUGGESTED_THINKING" "$CONFIG_YAML" || print_error "yq update failed for enhanced reasoning"
print_info "Enhanced reasoning updated."
else
yq -i ".ENABLE_THINKING = false" "$CONFIG_YAML" || print_error "yq update failed for enhanced reasoning"
print_info "Enhanced reasoning set to false."
fi
# Research Depth settings are managed directly in config.yaml
print_info "Research depth settings (MIN/MAX_REGULAR_WEB_PAGES) are kept as defined in config.yaml."
}
# Optimal GPU Layers Calculation (Simplified)
get_optimal_gpu_layers() {
local model="$1"
local has_nvidia="$4" # Only 4th arg needed now
local gpu_layers=0
if [ "$has_nvidia" = true ]; then
NVIDIA_VRAM=$(get_nvidia_vram)
print_info "Calculating optimal GPU layers for $model on NVIDIA (${NVIDIA_VRAM}GB VRAM)"
# Check for 70B / 72B models
if [[ "$model" == *"70b"* || "$model" == *"70B"* || "$model" == *"72b"* || "$model" == *"72B"* ]]; then
# 70B/72B Model Logic
if [ "$NVIDIA_VRAM" -ge 48 ]; then gpu_layers=64
elif [ "$NVIDIA_VRAM" -ge 40 ]; then gpu_layers=50
elif [ "$NVIDIA_VRAM" -ge 32 ]; then gpu_layers=40
elif [ "$NVIDIA_VRAM" -ge 24 ]; then gpu_layers=32
else gpu_layers=10; print_warning "Low VRAM ($NVIDIA_VRAM GB) for 70B+ model."; fi
print_info "Recommended GPU layers for 70B+: $gpu_layers"
else
# Fallback for unknown local model
gpu_layers=10; print_warning "Unknown local model size ($model), using default GPU layers: $gpu_layers";
fi
else
# Non-NVIDIA or Cloud
gpu_layers=0
print_info "No NVIDIA GPU detected or Cloud model selected, setting GPU layers to 0."
fi
echo "$gpu_layers"
}
# Function to update N_GPU_LAYERS in config.yaml
update_gpu_layers() {
local gpu_layers=$1
# Assumes CONFIG_YAML accessible
print_info "Setting N_GPU_LAYERS to $gpu_layers in config.yaml"
# Always use yq now
yq -i ".N_GPU_LAYERS = $gpu_layers" "$CONFIG_YAML" || print_error "yq update failed for N_GPU_LAYERS"
}
# Check and Set Environment Variables
check_and_set_env_var() {
local key_name="$1"
local description="$2"
local key_url="$3"
local is_required="$4" # New parameter to indicate if key is mandatory
local env_file=".env"
local current_value
local key_exists=false
local key_has_value=false
local is_placeholder=false
# Ensure .env file exists before trying to read/write
if [ ! -f "$env_file" ]; then
# Try creating from example if possible
if [ -f ".env.example" ]; then
print_info "Creating $env_file from .env.example for key check..."
cp .env.example "$env_file"
else
print_warning "$env_file not found, cannot check/set ${key_name}."
return
fi
fi
if grep -qE "^${key_name}=" "$env_file"; then
key_exists=true
current_value=$(grep -E "^${key_name}=" "$env_file" | head -n 1 | cut -d '=' -f 2- | sed -e 's/^[\\"\\ \\"]*//' -e 's/[\\"\\ \\"]*$//')
if [ -n "$current_value" ]; then
key_has_value=true
# Check if it's a placeholder value
if [[ "$current_value" == "api_key_here" ]]; then
is_placeholder=true
fi
fi
fi
if [ "$key_has_value" = true ] && [ "$is_placeholder" = false ]; then
# Mask the key for display (show first 4 and last 4 chars)
local masked_value
local length=${#current_value}
if [ $length -le 8 ]; then
# If key is too short, just show all asterisks
masked_value=$(printf '%*s' "$length" | tr ' ' '*')
else
# Show first 4 and last 4 chars with asterisks in between
local first_part="${current_value:0:4}"
local last_part="${current_value: -4}"
local middle_length=$((length - 8))
local middle_part=$(printf '%*s' "$middle_length" | tr ' ' '*')
masked_value="${first_part}${middle_part}${last_part}"
fi
print_info "${key_name} found: ${CYAN}${masked_value}${RESET}"
echo "Current ${key_name} appears to be set."
local update_key="N"
# In quick install mode, keep existing key values
if [ "$QUICK_INSTALL" = true ]; then
print_info "Keeping existing ${key_name} in quick install mode ✓"
else
# Interactive mode: Ask to update
printf "%s [%s%s%s/%s] %s" "${YELLOW}Update this key?" "${YELLOW}" "y" "${GREEN}" "N" "${RESET}" # Note: Default is N
read update_key
echo "" # Add newline
update_key=${update_key:-N} # Default to N if empty
fi
if [[ "$update_key" == "y" || "$update_key" == "Y" ]]; then
read -s -p "${YELLOW}Enter new value for ${key_name}: ${RESET}" user_key; echo
if [ -n "$user_key" ]; then
# Escape potential special characters in key for sed/echo
escaped_user_key=$(echo "$user_key" | sed 's/[&/\\#]/\\\\&/g') # Escape &, /, \, #
# Ensure value is quoted in .env
quoted_key_value="\"${escaped_user_key}\""
print_info "Updating ${key_name} in $env_file..."
# Use # as separator for sed to avoid issues with / in keys/paths
sed -i.bak "s#^${key_name}=.*#${key_name}=${quoted_key_value}#" "$env_file" && rm -f "${env_file}.bak" || print_error "sed update failed for ${key_name}"
print_info "${key_name} has been updated in $env_file ✓"
else
print_warning "Empty key provided, keeping existing value."
fi
else
# Only print this if we didn't update (i.e., quick mode or user chose 'N')
if [ "$QUICK_INSTALL" = false ]; then
print_info "Keeping existing ${key_name} ✓"
fi
fi
else
# Key is missing, empty, or contains placeholder
if [ "$key_exists" = true ] && [ "$is_placeholder" = true ]; then
print_warning "${key_name} found in $env_file but contains placeholder value."
elif [ "$key_exists" = true ]; then
print_warning "${key_name} found in $env_file but appears to be empty."
else
print_warning "${key_name} not found in $env_file."
fi
echo "${CYAN}This key is needed for: ${description}${RESET}"
echo "You can obtain one here: ${BLUE}${UNDERLINE}${key_url}${RESET}"
# In quick install mode, just warn if required, don't prompt
if [ "$QUICK_INSTALL" = true ]; then
if [ "$is_required" = true ]; then
print_info "${YELLOW}This key is required. Please add it to the .env file manually later.${RESET}"
else
print_info "${YELLOW}Optional key not set. Can be added to .env file later if needed.${RESET}"
fi
return
fi
# Interactive mode: Prompt for the key
local user_key=""
local valid_key=false
# If required, loop until valid key is provided
while [ "$valid_key" = false ]; do
# Use standard read -s for sensitive input
read -s -p "${YELLOW}Enter value for ${key_name}: ${RESET}" user_key; echo
if [ -n "$user_key" ]; then
valid_key=true
elif [ "$is_required" = true ]; then
print_error "This API key is required for the selected model. Please provide a valid key."
else
print_warning "Empty key provided. Continuing without ${key_name}."
valid_key=true # Allow empty if not required
fi
done
if [ -n "$user_key" ]; then
# Escape potential special characters in key for sed/echo
escaped_user_key=$(echo "$user_key" | sed 's/[&/\\#]/\\\\&/g') # Escape &, /, \, #
# Ensure value is quoted in .env
quoted_key_value="\"${escaped_user_key}\""
if [ "$key_exists" = true ]; then
print_info "Updating ${key_name} in $env_file..."
# Use # as separator for sed to avoid issues with / in keys/paths
sed -i.bak "s#^${key_name}=.*#${key_name}=${quoted_key_value}#" "$env_file" && rm -f "${env_file}.bak" || print_error "sed update failed for ${key_name}"
else
print_info "Adding ${key_name} to $env_file..."
# Append the new key=value pair
echo "${key_name}=${quoted_key_value}" >> "$env_file"
fi
print_info "${key_name} has been set in $env_file ✓"
else
print_warning "Skipped setting ${key_name}. Application might require it later."
fi
fi
}
# Display Configuration Summary (Simplified)
display_configuration_summary() {
# Assumes these are passed or accessible: SELECTED_MODEL_FILENAME, SELECTED_PRIMARY_MODEL, OPTIMAL_GPU_LAYERS, CONFIG_YAML, VENV_DIR
print_section_header "FINAL CONFIGURATION"
local summary_lines=(
"Summarizer Model: ${CYAN}${SELECTED_MODEL_FILENAME}${RESET}" # Always Flash now
"- ${GREEN}Using cloud-based Gemini Flash model${RESET}"
"- ${GREEN}Chunking disabled (CHUNK_SIZE: 0)${RESET}"
"- ${GREEN}USE_LOCAL_SUMMARIZER_MODEL: false${RESET}" ""
"Primary Reasoning Model: ${CYAN}${SELECTED_PRIMARY_MODEL}${RESET}"
)
local FINAL_PRIMARY_MODEL_TYPE=$(grep -E "^PRIMARY_MODEL_TYPE:" "$CONFIG_YAML" | awk '{print $2}' | sed 's/\"//g')
local FINAL_GPU_LAYERS=$(grep -E "^N_GPU_LAYERS:" "$CONFIG_YAML" | awk '{print $2}' || echo "0") # Read final value
local FINAL_QUANT_LEVEL=$(grep -E "^LOCAL_MODEL_QUANT_LEVEL:" "$CONFIG_YAML" | awk '{print $2}' | sed 's/\"//g')
if [[ "$FINAL_PRIMARY_MODEL_TYPE" == "claude" ]]; then
summary_lines+=("- ${GREEN}Using cloud-based Claude model${RESET}")
summary_lines+=("- ${GRAY}Requires ANTHROPIC_API_KEY in .env file${RESET}")
elif [[ "$FINAL_PRIMARY_MODEL_TYPE" == "gemini" ]]; then
summary_lines+=("- ${GREEN}Using cloud-based Gemini model${RESET}")
summary_lines+=("- ${GRAY}Requires GEMINI_API_KEY in .env file${RESET}")
elif [[ "$FINAL_PRIMARY_MODEL_TYPE" == "local" && "$SELECTED_PRIMARY_MODEL" == *"llama-3.3-70b"* ]]; then
summary_lines+=("- ${YELLOW}Using local Llama 3 70B GGUF model${RESET}")
summary_lines+=("- ${GRAY}LOCAL_MODEL_NAME: $SELECTED_PRIMARY_MODEL${RESET}")
summary_lines+=("- ${GRAY}LOCAL_MODEL_QUANT_LEVEL: ${FINAL_QUANT_LEVEL}${RESET}")
summary_lines+=("- ${GRAY}N_GPU_LAYERS: ${FINAL_GPU_LAYERS}${RESET}")
else summary_lines+=("- ${RED}Unknown primary model configuration${RESET}"); fi
# --- Add Next Step Model to Summary ---
local FINAL_NEXT_STEP_MODEL=$(grep -E "^NEXT_STEP_MODEL:" "$CONFIG_YAML" | awk '{print $2}' | sed 's/\"//g')
local FINAL_NEXT_STEP_THINKING=$(grep -E "^NEXT_STEP_THINKING_DEFAULT:" "$CONFIG_YAML" | awk '{print $2}' | sed 's/\"//g')
summary_lines+=("" "Next Step Model: ${CYAN}${FINAL_NEXT_STEP_MODEL}${RESET}")
summary_lines+=("- Default Thinking Mode: ${YELLOW}${FINAL_NEXT_STEP_THINKING}${RESET}")
summary_lines+=("" "--- Additional Settings ---")
local MEMORY_TOKENS=$(grep -E "^CONVERSATION_MEMORY_MAX_TOKENS:" "$CONFIG_YAML" | awk '{print $2}' || echo "Unknown")
local THINKING_ENABLED=$(grep -E "^ENABLE_THINKING:" "$CONFIG_YAML" | awk '{print $2}' || echo "Unknown")
local MIN_PAGES=$(grep -E "^MIN_REGULAR_WEB_PAGES:" "$CONFIG_YAML" | awk '{print $2}' || echo "Unknown")
local MAX_PAGES=$(grep -E "^MAX_REGULAR_WEB_PAGES:" "$CONFIG_YAML" | awk '{print $2}' || echo "Unknown")
summary_lines+=("Memory Capacity: ${GRAY}${MEMORY_TOKENS} tokens${RESET}")
summary_lines+=("Enhanced Reasoning: ${GRAY}${THINKING_ENABLED}${RESET}")
summary_lines+=("Research Depth: ${GRAY}${MIN_PAGES}-${MAX_PAGES} web pages per topic${RESET}")
print_summary_box "FINAL CONFIGURATION" "${summary_lines[@]}"
echo ""
echo "${GREEN}${BOLD}Setup Steps Completed!${RESET}"
echo ""
echo "To activate the virtual environment, run: ${CYAN}source $VENV_DIR/bin/activate${RESET}"
if [[ "$FINAL_PRIMARY_MODEL_TYPE" == "claude" ]] || [[ "$FINAL_PRIMARY_MODEL_TYPE" == "gemini" ]] || [[ "$SELECTED_MODEL_FILENAME" == *"gemini"* ]]; then
echo "Remember to add required API keys to ${CYAN}.env${RESET} file."
fi
echo "Run the application using: ${CYAN}./run.sh${RESET}"
echo ""
}
# --- GGUF Download Functions for 70B+ Models ---
download_llama33_70b_gguf() {
local target_dir=$1
local repo_id="unsloth/Llama-3.3-70B-Instruct-GGUF"
local gguf_filename="Llama-3.3-70B-Instruct-Q4_K_M.gguf"
local model_url="https://huggingface.co/${repo_id}"
print_info "Attempting download from: ${repo_id} / ${gguf_filename}"
if _huggingface_download_gguf "$repo_id" "$gguf_filename" "$target_dir" "$model_url"; then return 0; else print_error "Failed download: ${gguf_filename}"; return 1; fi
}
download_qwen_72b_gguf() {
local target_dir=$1
local repo_id="Qwen/Qwen2.5-72B-Instruct-GGUF"
local gguf_filename="Qwen2.5-72B-Instruct-Q4_K_M.gguf"
local model_url="https://huggingface.co/${repo_id}"
print_info "Attempting download from: ${repo_id} / ${gguf_filename}"
if _huggingface_download_gguf "$repo_id" "$gguf_filename" "$target_dir" "$model_url"; then return 0; else print_error "Failed download: ${gguf_filename}"; return 1; fi
}
download_deepseek_70b_gguf() {
local target_dir=$1
local repo_id="unsloth/DeepSeek-R1-Distill-Llama-70B-GGUF"
local gguf_filename="DeepSeek-R1-Distill-Llama-70B-IQ4_NL.gguf" # Note the IQ4_NL quant
local model_url="https://huggingface.co/${repo_id}"
print_info "Attempting download from: ${repo_id} / ${gguf_filename}"
if _huggingface_download_gguf "$repo_id" "$gguf_filename" "$target_dir" "$model_url"; then return 0; else print_error "Failed download: ${gguf_filename}"; return 1; fi
}
download_xwin_70b_gguf() {
local target_dir=$1
local repo_id="TheBloke/Xwin-LM-70B-V0.1-GGUF"
local gguf_filename="xwin-lm-70b-v0.1.Q4_K_M.gguf"
local model_url="https://huggingface.co/${repo_id}"
print_info "Attempting download from: ${repo_id} / ${gguf_filename}"
if _huggingface_download_gguf "$repo_id" "$gguf_filename" "$target_dir" "$model_url"; then return 0; else print_error "Failed download: ${gguf_filename}"; return 1; fi
}
# --- Main Execution Function ---
main() {
# --- Define variables needed in this scope ---
local SELECTED_MODEL_FILENAME="N/A"
local SELECTED_PRIMARY_MODEL="N/A"
local USE_LOCAL_MODELS=false
local LLAMA_CPP_PYTHON_FLAGS="N/A"
local OPTIMAL_GPU_LAYERS=0
local CURRENT_GPU_LAYERS="N/A"
local UPDATE_GPU_LAYERS="n"
local PRIMARY_QUANT_LEVEL="N/A"
local IS_MAC=$([ "$(uname)" == "Darwin" ] && echo true || echo false)
local HAS_NVIDIA=$(has_nvidia_gpu && echo true || echo false) # Check GPU early
local MODEL_LLAMA3_70B="llama-3.3-70b-instruct.Q4_K_M.gguf" # Define needed model const
local _selected_model_result="" # Variable to capture function results
local _ask_model_preference_result="" # Variable to capture ask_model_preference result
# --- New setup mode variable: quick vs interactive ---
local QUICK_INSTALL=true
# --- Print setup header ---
echo ""
echo "${BLUE}${BOLD}==================================================================${RESET}"
echo "${BLUE}${BOLD} Python AI Research Agent - Setup Script ${RESET}"
echo "${BLUE}${BOLD}==================================================================${RESET}"
echo ""
# --- Welcome message and setup mode selection ---
echo "${CYAN}${BOLD}Welcome to the Deep Researcher Setup Script${RESET}"
echo ""
echo "This script will set up your environment with the following components:"
echo " • ${GREEN}Python virtual environment${RESET}"
echo " • ${GREEN}Required Python packages${RESET}"
echo " • ${GREEN}Patchright web browser automation${RESET}"
echo " • ${GREEN}MCP API tools configuration${RESET}"
echo " • ${GREEN}API keys for AI services${RESET}"
if [ "$HAS_NVIDIA" = true ]; then
local vram_gb=$(get_nvidia_vram)
if [ "$vram_gb" -ge 24 ]; then
echo " • ${GREEN}Local LLM models (NVIDIA GPU ${vram_gb}GB detected)${RESET}"
else
echo " • ${YELLOW}Cloud models only (NVIDIA GPU has insufficient VRAM: ${vram_gb}GB)${RESET}"
fi
elif [ "$IS_MAC" = true ]; then
local mac_model=$(detect_apple_silicon_model)
if [[ "$mac_model" == *"Apple"* ]]; then
echo " • ${YELLOW}Cloud models only (Apple Silicon: ${mac_model})${RESET}"
else
echo " • ${YELLOW}Cloud models only (Intel Mac)${RESET}"
fi
else
echo " • ${YELLOW}Cloud models only (No NVIDIA GPU detected)${RESET}"
fi
echo ""
echo "The script can run in one of two modes:"
echo " 1. ${CYAN}Quick Install:${RESET} Automatically install with defaults for your hardware"
echo " 2. ${CYAN}Interactive:${RESET} Ask for confirmation at each step"
echo ""
local setup_mode
printf "%s [%s%s%s/%s] %s" "${BLUE}Choose setup mode" "${GREEN}" "1" "${BLUE}" "2" "${RESET}" # Use printf for consistent prompting
read setup_mode # Use plain read to wait for input
echo "" # Add newline after input
setup_mode=${setup_mode:-1} # Default to 1 if empty
if [ "$setup_mode" = "2" ]; then
QUICK_INSTALL=false
echo "${YELLOW}Selected: Interactive installation${RESET}"
else
QUICK_INSTALL=true
echo "${GREEN}Selected: Quick installation with defaults${RESET}"
fi
# --- Comprehensive prerequisite checking ---
print_section_header "CHECKING PREREQUISITES"
local missing_prerequisites=false
# Essential commands
echo "${CYAN}Checking essential commands...${RESET}"
for cmd in "$PYTHON_CMD" "jq" "git"; do
if ! command -v $cmd &> /dev/null; then
print_error "$cmd is required but not found on your system."
missing_prerequisites=true
case $cmd in
"$PYTHON_CMD")
echo "Please install Python 3 from https://www.python.org/downloads/"
;;
"jq")
if [ "$IS_MAC" = true ]; then
echo "On macOS, you can install jq with: brew install jq"
else
echo "On Linux, you can install jq with: sudo apt install jq (Debian/Ubuntu)"
echo "or: sudo dnf install jq (Fedora)"
fi
;;
"git")
if [ "$IS_MAC" = true ]; then
echo "On macOS, you can install git with: brew install git"
else
echo "On Linux, you can install git with: sudo apt install git (Debian/Ubuntu)"
echo "or: sudo dnf install git (Fedora)"
fi
;;
esac
else
print_info "$cmd found ✓"
# Check Python version if the command is Python
if [ "$cmd" = "$PYTHON_CMD" ]; then
local python_version=$($PYTHON_CMD --version 2>&1 | cut -d' ' -f2)
local python_major=$(echo $python_version | cut -d. -f1)
local python_minor=$(echo $python_version | cut -d. -f2)
print_info "Python version: $python_version"
if [ "$python_major" -lt 3 ] || ([ "$python_major" -eq 3 ] && [ "$python_minor" -lt 8 ]); then
print_warning "Python 3.8+ is recommended, you have $python_version"
echo "The script might work with your version, but some features may be unavailable."
fi
fi
fi
done
# Check for yq, which we will attempt to install if missing
if ! command -v yq &> /dev/null; then
print_warning "yq (YAML processor) not found - we'll try to install it later."
else
print_info "yq found ✓"
fi
# Check for required files
echo ""
echo "${CYAN}Checking required files...${RESET}"
for file in "$MCP_JSON_FILE" "$CONFIG_YAML"; do
if [ ! -f "$file" ]; then
print_error "$file not found."
missing_prerequisites=true
if [ "$file" = "$CONFIG_YAML" ]; then
echo "Is config.yaml.example available to copy from?"
if [ -f "${file}.example" ]; then
echo "Attempting to create $file from ${file}.example..."
cp "${file}.example" "$file" && print_info "Created $file from example ✓" || print_error "Failed to create $file"
else
echo "No ${file}.example found. Please create $file before continuing."
fi
fi
else
print_info "$file found ✓"
fi
done
# Hardware detection information
echo ""
echo "${CYAN}Hardware detection...${RESET}"
if [ "$IS_MAC" = true ]; then
local mac_model=$(detect_apple_silicon_model)
print_info "Running on macOS: $mac_model"
if [[ "$mac_model" == *"Apple"* ]]; then
local unified_memory=$(get_apple_unified_memory)
print_info "Apple Silicon with ${unified_memory}GB unified memory detected."
fi
else
print_info "Running on Linux ($(uname -m))"
fi
if [ "$HAS_NVIDIA" = true ]; then
local vram_gb=$(get_nvidia_vram)
print_info "NVIDIA GPU detected with ${vram_gb}GB VRAM"
else
print_info "No NVIDIA GPU detected."
fi
local ram_gb=$(get_system_ram)
print_info "System RAM: ${ram_gb}GB"
# Exit if critical prerequisites are missing
if [ "$missing_prerequisites" = true ]; then
print_error "Some critical prerequisites are missing. Please install them and try again."
exit 1
fi
# Make global config paths accessible if helper functions need them without args
# (Already defined globally, but ensure functions can see them if needed)
# VENV_DIR="venv"; CONFIG_YAML="config.yaml"; MODELS_DIR="models"; MCP_JSON_FILE="mcp.json"
# PYTHON_CMD="python3"
# --- Step 0: Ensure uv (uvx) is installed ---
print_section_header "CHECKING FOR 'uv' (uvx) TOOL"
if ! command -v uvx &> /dev/null; then
print_warning "'uvx' (uv) not found. Installing via official script..."
# Use the official install script for Mac/Linux (ARM64 safe)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Ensure ~/.local/bin is on PATH for this session
export PATH="$HOME/.local/bin:$PATH"
print_info "'uv' (uvx) installed. If you still get 'command not found', add 'export PATH=\"$HOME/.local/bin:$PATH\"' to your shell profile."
else
print_info "'uvx' (uv) found ✓"
fi
# --- Step 1: Prerequisite Checks ---
print_section_header "SYSTEM CHECKS & PREFERENCES"
if ! command -v $PYTHON_CMD &> /dev/null; then print_error "Python 3 not found."; exit 1; else print_info "Python 3 found ✓"; fi
if ! command -v jq &> /dev/null; then print_error "jq not found."; exit 1; else print_info "jq found ✓"; fi
# Check for yq, install if missing
if ! command -v yq &> /dev/null; then
print_warning "yq YAML processor not found."
if [ "$IS_MAC" = true ] && command -v brew &> /dev/null; then
print_info "Attempting to install yq using Homebrew..."
if brew install yq; then
print_info "yq installed successfully ✓"
else