-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1178 lines (1061 loc) · 35.9 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1178 lines (1061 loc) · 35.9 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
#!/usr/bin/env bash
set -euo pipefail
# aicx setup — install binaries + configure MCP for supported AI tools
#
# Usage:
# bash install.sh
# bash install.sh --skip-install # MCP config only
# Run from a local checkout, or pass --release to install from GitHub Releases.
#
# Vibecrafted with AI Agents by Vetcoders (c)2026 Vetcoders
SOURCE_PATH="${BASH_SOURCE[0]:-$0}"
if [ -n "$SOURCE_PATH" ] && [ -e "$SOURCE_PATH" ]; then
SCRIPT_DIR=$(cd "$(dirname "$SOURCE_PATH")" && pwd)
else
SCRIPT_DIR="$PWD"
fi
MANIFEST_PATH="$SCRIPT_DIR/Cargo.toml"
HAS_LOCAL_MANIFEST=0
if [ -f "$MANIFEST_PATH" ]; then
HAS_LOCAL_MANIFEST=1
fi
HAS_BUNDLED_BINARIES=0
if [ -x "$SCRIPT_DIR/aicx" ] && [ -x "$SCRIPT_DIR/aicx-mcp" ]; then
HAS_BUNDLED_BINARIES=1
fi
AICX_INSTALL_MODE="${AICX_INSTALL_MODE:-auto}"
AICX_GIT_URL="${AICX_GIT_URL:-https://github.com/Loctree/aicx}"
AICX_BIN_DIR="${AICX_BIN_DIR:-$HOME/.local/bin}"
AICX_RELEASE_REPO="${AICX_RELEASE_REPO:-Loctree/aicx}"
AICX_RELEASE_TAG="${AICX_RELEASE_TAG:-latest}"
AICX_CONFIG_PATH="${AICX_CONFIG_PATH:-$HOME/.aicx/config.toml}"
AICX_HOME_PICKER="${AICX_HOME_PICKER:-auto}"
AICX_STORAGE_HOME="${AICX_STORAGE_HOME:-}"
AICX_EMBEDDER_PICKER="${AICX_EMBEDDER_PICKER:-auto}"
AICX_EMBEDDER_PROFILE="${AICX_EMBEDDER_PROFILE:-}"
AICX_EMBEDDER_FILENAME="${AICX_EMBEDDER_FILENAME:-${AICX_EMBEDDER_FILE:-}}"
AICX_EMBEDDER_CONFIG_PATH="${AICX_EMBEDDER_CONFIG_PATH:-$HOME/.aicx/embedder.toml}"
AICX_INSTALL_FORCE="${AICX_INSTALL_FORCE:-0}"
AICX_INSTALL_DRY_RUN="${AICX_INSTALL_DRY_RUN:-0}"
AICX_CARGO_BIN_DIR="${AICX_CARGO_BIN_DIR:-${CARGO_INSTALL_ROOT:+$CARGO_INSTALL_ROOT/bin}}"
AICX_EMBEDDER_SETUP_DETAIL="No local embedder profile was configured in this run."
if [ -z "$AICX_CARGO_BIN_DIR" ]; then
AICX_CARGO_BIN_DIR="$HOME/.cargo/bin"
fi
SKIP_INSTALL=0
SHADOW_CHECK_ONLY=0
VERIFY_PATH_ONLY=0
for arg in "$@"; do
case "$arg" in
--skip-install) SKIP_INSTALL=1 ;;
--dry-run) AICX_INSTALL_DRY_RUN=1 ;;
--force) AICX_INSTALL_FORCE=1 ;;
--shadow-check-only) SHADOW_CHECK_ONLY=1 ;;
--verify-path-only) VERIFY_PATH_ONLY=1 ;;
--release) AICX_INSTALL_MODE="release" ;;
--release-tag=*) AICX_RELEASE_TAG="${arg#*=}" ;;
--pick-home) AICX_HOME_PICKER="1" ;;
--no-home-prompt) AICX_HOME_PICKER="0" ;;
--aicx-home=*) AICX_STORAGE_HOME="${arg#*=}" ;;
--pick-embedder) AICX_EMBEDDER_PICKER="1" ;;
--no-embedder-prompt) AICX_EMBEDDER_PICKER="0" ;;
--embedder-profile=*) AICX_EMBEDDER_PROFILE="${arg#*=}" ;;
--help|-h)
echo "Usage: install.sh [--skip-install] [--dry-run] [--force]"
echo " Install aicx + aicx-mcp and configure MCP for Claude Code, Codex, and Gemini."
echo " Run from a release bundle or the repo root / local checkout."
echo " --dry-run shows shadow cleanup without installing or rewriting config."
echo " --force skips the multiple-aicx PATH confirmation."
echo ""
echo "Install source is controlled by AICX_INSTALL_MODE:"
echo " auto - prefer bundled binaries, then local checkout, otherwise verified GitHub Release"
echo " release - download an official GitHub Release, verify SHA256, then install its bundle"
echo " bundle - copy bundled binaries into \$AICX_BIN_DIR"
echo " local - cargo install --path <checkout> --locked"
echo " crates - legacy/unsupported: crates.io is not the active AICX distribution path"
echo " git - cargo install --git \$AICX_GIT_URL --locked aicx"
echo ""
echo "Bundle install target:"
echo " AICX_BIN_DIR=\$HOME/.local/bin # default destination for bundled binaries"
echo ""
echo "Release download target:"
echo " AICX_RELEASE_REPO=Loctree/aicx"
echo " AICX_RELEASE_TAG=latest # or vX.Y.Z"
echo ""
echo "Native embedder profile shortcuts:"
echo " default: AICX_EMBEDDER_PROFILE=base # F2LLM 0.6B Q4_K_M GGUF"
echo " dev: AICX_EMBEDDER_PROFILE=dev # F2LLM 1.7B Q4_K_M GGUF"
echo " premium: AICX_EMBEDDER_PROFILE=premium # F2LLM 1.7B Q6_K GGUF"
echo " build: cargo build --release --features native-embedder"
echo ""
echo "Native embedder picker:"
echo " --pick-embedder # interactive config for ~/.aicx/embedder.toml"
echo " --embedder-profile=base|dev|premium"
echo " --no-embedder-prompt # suppress interactive picker"
echo " note: writes only AICX local embedder preferences"
echo ""
echo "AICX storage root picker:"
echo " --pick-home # ask where AICX_HOME should live"
echo " --aicx-home=/absolute/path # persist [storage].home in ~/.aicx/config.toml"
echo " --no-home-prompt # suppress interactive AICX_HOME picker"
echo " default: ~/.aicx # semantic index remains ~/.aicx/indexed/"
exit 0
;;
esac
done
resolve_aicx() {
if [ -x "$AICX_BIN_DIR/aicx" ]; then
AICX_RUN=("$AICX_BIN_DIR/aicx")
return 0
fi
if [ -x "$AICX_CARGO_BIN_DIR/aicx" ]; then
AICX_RUN=("$AICX_CARGO_BIN_DIR/aicx")
return 0
fi
if command -v aicx >/dev/null 2>&1; then
AICX_RUN=("aicx")
return 0
fi
if [ "$HAS_LOCAL_MANIFEST" -eq 1 ] && command -v cargo >/dev/null 2>&1; then
AICX_RUN=("cargo" "run" "--quiet" "--manifest-path" "$MANIFEST_PATH" "--bin" "aicx" "--")
return 0
fi
return 1
}
resolve_aicx_mcp() {
if [ -x "$AICX_BIN_DIR/aicx-mcp" ]; then
AICX_MCP_COMMAND="$AICX_BIN_DIR/aicx-mcp"
AICX_MCP_ARGS_JSON='[]'
return 0
fi
if [ -x "$AICX_CARGO_BIN_DIR/aicx-mcp" ]; then
AICX_MCP_COMMAND="$AICX_CARGO_BIN_DIR/aicx-mcp"
AICX_MCP_ARGS_JSON='[]'
return 0
fi
if command -v aicx-mcp >/dev/null 2>&1; then
AICX_MCP_COMMAND=$(command -v aicx-mcp)
AICX_MCP_ARGS_JSON='[]'
return 0
fi
if [ "$HAS_LOCAL_MANIFEST" -eq 1 ] && command -v cargo >/dev/null 2>&1; then
AICX_MCP_COMMAND="cargo"
AICX_MCP_ARGS_JSON=$(AICX_MANIFEST_PATH="$MANIFEST_PATH" python3 - <<'PY'
import json
import os
print(json.dumps([
"run",
"--quiet",
"--manifest-path",
os.environ["AICX_MANIFEST_PATH"],
"--bin",
"aicx-mcp",
"--",
]))
PY
)
return 0
fi
return 1
}
echo "=== aicx setup ==="
normalise_bool() {
case "${1:-}" in
1|true|TRUE|yes|YES|on|ON) echo "1" ;;
0|false|FALSE|no|NO|off|OFF) echo "0" ;;
*) echo "${1:-}" ;;
esac
}
embedder_repo_for_profile() {
case "${1:-}" in
base) echo "mradermacher/F2LLM-v2-0.6B-GGUF" ;;
dev) echo "mradermacher/F2LLM-v2-1.7B-GGUF" ;;
premium) echo "mradermacher/F2LLM-v2-1.7B-GGUF" ;;
*) return 1 ;;
esac
}
embedder_file_for_profile() {
case "${1:-}" in
base) echo "F2LLM-v2-0.6B.Q4_K_M.gguf" ;;
dev) echo "F2LLM-v2-1.7B.Q4_K_M.gguf" ;;
premium) echo "F2LLM-v2-1.7B.Q6_K.gguf" ;;
*) return 1 ;;
esac
}
write_storage_home_config() {
local configured_home="$1"
local config_path="$AICX_CONFIG_PATH"
mkdir -p "$(dirname "$config_path")"
AICX_CONFIG_PATH_FOR_WRITE="$config_path" \
AICX_STORAGE_HOME_FOR_WRITE="$configured_home" \
python3 - <<'PY'
from pathlib import Path
import json
import os
import re
config_path = Path(os.environ["AICX_CONFIG_PATH_FOR_WRITE"]).expanduser()
storage_home = os.environ["AICX_STORAGE_HOME_FOR_WRITE"].strip()
expanded = Path(storage_home).expanduser()
if not storage_home:
raise SystemExit("empty AICX storage home")
if any(ord(ch) < 32 for ch in storage_home):
raise SystemExit("invalid AICX home: control characters are not allowed")
if ".." in expanded.parts:
raise SystemExit(
"invalid AICX home: parent-directory traversal (`..`) is not allowed"
)
if not (storage_home == "~" or storage_home.startswith("~/") or expanded.is_absolute()):
raise SystemExit(
f"invalid AICX home {storage_home!r}: use an absolute path or ~/..."
)
home_line = f"home = {json.dumps(storage_home)}"
lines = config_path.read_text(encoding="utf-8").splitlines() if config_path.exists() else []
out = []
in_storage = False
storage_seen = False
home_written = False
for line in lines:
stripped = line.strip()
if stripped.startswith("[") and stripped.endswith("]") and not stripped.startswith("[["):
if in_storage and not home_written:
out.append(home_line)
home_written = True
in_storage = stripped == "[storage]"
storage_seen = storage_seen or in_storage
out.append(line)
continue
if in_storage and re.match(r"^\s*home\s*=", line):
if not home_written:
out.append(home_line)
home_written = True
continue
out.append(line)
if in_storage and not home_written:
out.append(home_line)
elif not storage_seen:
if out and out[-1].strip():
out.append("")
out.append("[storage]")
out.append(home_line)
config_path.write_text("\n".join(out).rstrip() + "\n", encoding="utf-8")
print(config_path)
PY
}
maybe_configure_aicx_home() {
local picker selected_home config_written default_home
picker=$(normalise_bool "$AICX_HOME_PICKER")
default_home="$HOME/.aicx"
selected_home="${AICX_STORAGE_HOME:-}"
if [ -z "$selected_home" ] && { [ "$picker" = "1" ] || { [ "$picker" = "auto" ] && [ -t 0 ] && [ -t 1 ] && [ -z "${CI:-}" ]; }; }; then
echo ""
echo "AICX storage root setup"
echo " Default root: $default_home"
echo " Semantic index path: <AICX_HOME>/indexed/_all/embeddings.ndjson"
echo " Press Enter for the default, or enter an absolute path / ~/... for a persistent custom root."
printf "AICX_HOME [%s]: " "$default_home"
read -r selected_home || true
selected_home="${selected_home:-}"
fi
if [ -z "$selected_home" ]; then
echo " AICX_HOME default kept: $default_home"
return 0
fi
config_written=$(write_storage_home_config "$selected_home")
echo " config: $config_written"
echo " storage root: $selected_home"
echo " semantic index path: $selected_home/indexed/_all/embeddings.ndjson"
}
write_embedder_config() {
local profile="$1"
local repo="$2"
local filename="$3"
local path_override="$4"
local config_path="$AICX_EMBEDDER_CONFIG_PATH"
mkdir -p "$(dirname "$config_path")"
EMBEDDER_CONFIG_PATH="$config_path" \
EMBEDDER_PROFILE="$profile" \
EMBEDDER_REPO="$repo" \
EMBEDDER_FILENAME="$filename" \
EMBEDDER_PATH_OVERRIDE="$path_override" \
python3 - <<'PY'
from pathlib import Path
import os
config_path = Path(os.environ["EMBEDDER_CONFIG_PATH"]).expanduser()
profile = os.environ["EMBEDDER_PROFILE"].strip()
repo = os.environ["EMBEDDER_REPO"].strip()
filename = os.environ["EMBEDDER_FILENAME"].strip()
path_override = os.environ["EMBEDDER_PATH_OVERRIDE"].strip()
lines = [
"# aicx native embedder preferences",
"# First-choice local embeddings. Heavy retrieval remains rust-memex/Roost.",
"",
"[native_embedder]",
'backend = "gguf"',
]
if profile:
lines.append(f'profile = "{profile}"')
if repo:
lines.append(f'repo = "{repo}"')
if filename:
lines.append(f'filename = "{filename}"')
if path_override:
lines.append(f'path = "{path_override}"')
lines.append("prefer_embedded = false")
lines.append("max_length = 512")
config_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
print(config_path)
PY
}
maybe_prime_embedder_cache() {
local repo="$1"
local filename="$2"
if [ -z "$repo" ] || ! [ -t 0 ] || ! [ -t 1 ]; then
return 0
fi
if ! command -v hf >/dev/null 2>&1; then
echo " native embedder cache not primed automatically (missing 'hf' CLI)"
if [ -n "$filename" ]; then
echo " later, run: hf download $repo $filename"
else
echo " later, run: hf download <repo> <model.gguf>"
fi
return 0
fi
if [ -z "$filename" ]; then
echo " native embedder cache not primed automatically (custom repo has no GGUF filename)"
echo " later, run: hf download $repo <model.gguf>"
return 0
fi
printf " Download native embedder model into local HF cache now? [y/N] "
read -r reply || true
case "${reply:-}" in
y|Y|yes|YES)
echo " priming HF cache for $repo $filename ..."
if hf download "$repo" "$filename"; then
echo " HF cache primed"
else
echo " warning: hf download failed; config was still saved" >&2
fi
;;
*)
echo " skipping model download"
echo " later, run: hf download $repo $filename"
;;
esac
}
maybe_configure_native_embedder() {
local picker explicit_profile selected_profile selected_repo selected_filename config_written
picker=$(normalise_bool "$AICX_EMBEDDER_PICKER")
explicit_profile="${AICX_EMBEDDER_PROFILE:-}"
selected_profile=""
selected_repo="${AICX_EMBEDDER_REPO:-}"
selected_filename="${AICX_EMBEDDER_FILENAME:-}"
config_written=""
if [ -n "${AICX_EMBEDDER_PATH:-}" ]; then
config_written=$(write_embedder_config "" "" "" "$AICX_EMBEDDER_PATH")
echo " native embedder path pinned in $config_written"
AICX_EMBEDDER_SETUP_DETAIL="Saved explicit local embedder path in $config_written."
return 0
fi
if [ -n "$selected_repo" ]; then
config_written=$(write_embedder_config "" "$selected_repo" "$selected_filename" "")
echo " native embedder repo pinned in $config_written"
AICX_EMBEDDER_SETUP_DETAIL="Saved local embedder repo preference in $config_written."
maybe_prime_embedder_cache "$selected_repo" "$selected_filename"
return 0
fi
if [ -n "$explicit_profile" ]; then
case "$explicit_profile" in
none|off|skip)
echo " native embedder picker: skipped by explicit profile"
AICX_EMBEDDER_SETUP_DETAIL="You skipped native embedder setup."
return 0
;;
base|dev|premium)
selected_profile="$explicit_profile"
;;
*)
echo "Error: unsupported --embedder-profile='$explicit_profile' (expected base, dev, premium, or none)." >&2
exit 1
;;
esac
elif [ "$picker" = "1" ] || { [ "$picker" = "auto" ] && [ -t 0 ] && [ -t 1 ] && [ -z "${CI:-}" ]; }; then
echo ""
echo "Native embedder setup"
echo " This optional step writes $AICX_EMBEDDER_CONFIG_PATH."
echo " Model download is explicit and opt-in."
echo ""
echo " 1) Skip"
echo " Do not configure local embeddings now."
echo ""
echo " 2) Base"
echo " F2LLM 0.6B Q4_K_M GGUF"
echo " Approx. 397 MB"
echo " Recommended default."
echo ""
echo " 3) Dev"
echo " F2LLM 1.7B Q4_K_M GGUF"
echo " Approx. 1.1 GB"
echo " Better quality, requires more resources."
echo ""
echo " 4) Premium"
echo " F2LLM 1.7B Q6_K GGUF"
echo " Approx. 1.4 GB"
echo " Highest local quality, largest download."
echo ""
printf "Select profile [1-4]: "
read -r reply || true
case "${reply:-1}" in
1|"")
echo " native embedder picker: skipped"
AICX_EMBEDDER_SETUP_DETAIL="You skipped native embedder setup."
return 0
;;
2) selected_profile="base" ;;
3) selected_profile="dev" ;;
4) selected_profile="premium" ;;
*)
echo " native embedder picker: invalid choice, skipping"
AICX_EMBEDDER_SETUP_DETAIL="No local embedder profile was configured; invalid picker choice was ignored."
return 0
;;
esac
else
return 0
fi
selected_repo=$(embedder_repo_for_profile "$selected_profile")
selected_filename=$(embedder_file_for_profile "$selected_profile")
config_written=$(write_embedder_config "$selected_profile" "$selected_repo" "$selected_filename" "")
echo " native embedder preference saved to $config_written"
AICX_EMBEDDER_SETUP_DETAIL="Saved the '$selected_profile' local embedder profile in $config_written."
maybe_prime_embedder_cache "$selected_repo" "$selected_filename"
}
cleanup_old_binaries() {
local path="$1"
if [ -L "$path" ] || [ -f "$path" ]; then
if [ "$AICX_INSTALL_DRY_RUN" = "1" ]; then
echo " would remove stale $(basename "$path") from $(dirname "$path")"
return 0
fi
rm -f "$path"
echo " removed stale $(basename "$path") from $(dirname "$path")"
fi
}
strip_trailing_slash() {
local path="$1"
while [ "${#path}" -gt 1 ] && [ "${path%/}" != "$path" ]; do
path="${path%/}"
done
echo "$path"
}
same_path() {
[ "$(strip_trailing_slash "$1")" = "$(strip_trailing_slash "$2")" ]
}
extract_semver() {
local text="${1:-}"
if [[ "$text" =~ ([0-9]+)\.([0-9]+)\.([0-9]+)([-+][0-9A-Za-z.-]+)? ]]; then
echo "${BASH_REMATCH[1]}.${BASH_REMATCH[2]}.${BASH_REMATCH[3]}"
return 0
fi
return 1
}
semver_le() {
local left="$1"
local right="$2"
local la lb lc ra rb rc part
IFS=. read -r la lb lc <<< "$left"
IFS=. read -r ra rb rc <<< "$right"
for part in "$la" "$lb" "$lc" "$ra" "$rb" "$rc"; do
case "$part" in
''|*[!0-9]*) return 1 ;;
esac
done
if [ "$la" -lt "$ra" ]; then return 0; fi
if [ "$la" -gt "$ra" ]; then return 1; fi
if [ "$lb" -lt "$rb" ]; then return 0; fi
if [ "$lb" -gt "$rb" ]; then return 1; fi
[ "$lc" -le "$rc" ]
}
binary_semver() {
local path="$1"
local output
if ! [ -x "$path" ]; then
return 1
fi
output=$("$path" --version 2>/dev/null || true)
extract_semver "$output"
}
manifest_version() {
local line value
if [ ! -f "$MANIFEST_PATH" ]; then
return 1
fi
while IFS= read -r line; do
case "$line" in
version\ =\ \"*\")
value="${line#version = \"}"
value="${value%%\"*}"
echo "$value"
return 0
;;
esac
done < "$MANIFEST_PATH"
return 1
}
npm_global_bin_dir() {
local bin_dir prefix
if ! command -v npm >/dev/null 2>&1; then
return 1
fi
bin_dir=$(npm bin -g 2>/dev/null || true)
if [ -n "$bin_dir" ] && [ -d "$bin_dir" ]; then
echo "$bin_dir"
return 0
fi
prefix=$(npm prefix -g 2>/dev/null || true)
if [ -n "$prefix" ]; then
echo "$prefix/bin"
return 0
fi
return 1
}
install_target_bin_dir() {
case "$1" in
bundle|release) echo "$AICX_BIN_DIR" ;;
local|git|crates) echo "$AICX_CARGO_BIN_DIR" ;;
*) echo "$AICX_BIN_DIR" ;;
esac
}
target_install_version() {
local mode="$1"
case "$mode" in
bundle)
binary_semver "$SCRIPT_DIR/aicx" || true
;;
release)
if [ "$AICX_RELEASE_TAG" != "latest" ]; then
extract_semver "$AICX_RELEASE_TAG" || true
fi
;;
local)
manifest_version || true
;;
*)
echo ""
;;
esac
}
scan_binary_shadows() {
local binary_name="$1"
local target_path="$2"
local shadow_paths count path version resolved
echo "Scanning current $binary_name installation surface..."
shadow_paths=$(which -a "$binary_name" 2>/dev/null | sort -u || true)
if [ -z "$shadow_paths" ]; then
echo " no existing $binary_name on PATH"
return 0
fi
echo "Found existing $binary_name installations:"
count=0
while IFS= read -r path; do
[ -n "$path" ] || continue
count=$((count + 1))
version=$("$path" --version 2>/dev/null || echo "unknown")
echo " $path -> $version"
done <<< "$shadow_paths"
resolved=$(command -v "$binary_name" 2>/dev/null || true)
if [ "$count" -gt 1 ] || { [ -n "$resolved" ] && ! same_path "$resolved" "$target_path"; }; then
echo ""
echo "WARNING: Multiple or shadowing $binary_name binaries detected."
echo " target install path: $target_path"
if [ -n "$resolved" ]; then
echo " PATH currently resolves to: $resolved"
fi
if [ "$AICX_INSTALL_FORCE" != "1" ] && [ "$AICX_INSTALL_DRY_RUN" != "1" ]; then
if [ -t 0 ]; then
printf "Continue install? [y/N] "
read -r confirm
case "${confirm:-}" in
y|Y|yes|YES) ;;
*)
echo "Aborted. Set AICX_INSTALL_FORCE=1 or pass --force to skip this check."
exit 1
;;
esac
else
echo "Aborted in non-interactive mode. Set AICX_INSTALL_FORCE=1 to skip this check."
exit 1
fi
fi
fi
}
scan_aicx_shadows() {
local target_aicx="$1"
local target_mcp="$2"
scan_binary_shadows "aicx" "$target_aicx"
scan_binary_shadows "aicx-mcp" "$target_mcp"
}
cleanup_shadow_pair() {
local dir="$1"
local target_dir="$2"
local target_version="$3"
local candidate="$dir/aicx"
local candidate_version=""
if same_path "$dir" "$target_dir"; then
return 0
fi
if ! { [ -f "$candidate" ] || [ -L "$candidate" ]; }; then
return 0
fi
candidate_version=$(binary_semver "$candidate" || true)
if [ -z "$target_version" ] || [ -z "$candidate_version" ]; then
echo " shadow retained at $candidate (version unknown; cleanup requires an older/equal version)"
return 0
fi
if semver_le "$candidate_version" "$target_version"; then
if [ "$AICX_INSTALL_DRY_RUN" = "1" ]; then
echo " would remove shadow aicx $candidate_version from $dir (target $target_version)"
else
echo " removing shadow aicx $candidate_version from $dir (target $target_version)"
fi
cleanup_old_binaries "$dir/aicx"
cleanup_old_binaries "$dir/aicx-mcp"
else
echo " newer aicx $candidate_version retained at $dir (target $target_version)"
fi
}
cleanup_shadow_aicx_binaries() {
local target_dir="$1"
local target_version="$2"
local npm_bin
echo "Checking canonical shadow install paths..."
cleanup_shadow_pair "$HOME/.local/bin" "$target_dir" "$target_version"
cleanup_shadow_pair "$AICX_CARGO_BIN_DIR" "$target_dir" "$target_version"
if npm_bin=$(npm_global_bin_dir); then
cleanup_shadow_pair "$npm_bin" "$target_dir" "$target_version"
fi
}
verify_install_path_resolution() {
local installed_path="$1"
local binary_name="${2:-aicx}"
local installed_version path_resolved path_resolved_version
if ! [ -x "$installed_path" ]; then
return 0
fi
installed_version=$("$installed_path" --version 2>/dev/null || echo "unknown")
path_resolved=$(command -v "$binary_name" 2>/dev/null || true)
if [ -z "$path_resolved" ]; then
echo ""
echo "=========================================="
echo "WARNING: installed $binary_name is not on PATH"
echo " Installed to: $installed_path -> $installed_version"
echo " Add $(dirname "$installed_path") to PATH before running $binary_name."
echo "=========================================="
return 0
fi
path_resolved_version=$("$binary_name" --version 2>/dev/null || echo "unknown")
if [ -n "$path_resolved" ] && { ! same_path "$path_resolved" "$installed_path" || [ "$installed_version" != "$path_resolved_version" ]; }; then
echo ""
echo "=========================================="
echo "WARNING: PATH version mismatch detected"
echo " Installed to: $installed_path -> $installed_version"
echo " PATH resolves to: $path_resolved -> $path_resolved_version"
echo ""
echo "Other $binary_name binaries in PATH:"
which -a "$binary_name" 2>/dev/null | sort -u | while IFS= read -r path; do
if [ -n "$path" ] && ! same_path "$path" "$installed_path"; then
echo " $path"
fi
done
echo ""
echo "To fix: ensure $(dirname "$installed_path") is before other $binary_name locations in your PATH,"
echo "or remove the older binary shown above."
echo "=========================================="
fi
}
path_has_dir() {
case ":$PATH:" in
*":$1:"*) return 0 ;;
*) return 1 ;;
esac
}
install_bundle_binaries() {
local install_dir="$AICX_BIN_DIR"
local source_aicx="$SCRIPT_DIR/aicx"
local source_mcp="$SCRIPT_DIR/aicx-mcp"
if [ "$HAS_BUNDLED_BINARIES" -ne 1 ]; then
echo "Error: bundle install requested, but prebuilt aicx binaries are not present next to install.sh." >&2
exit 1
fi
mkdir -p "$install_dir"
echo " target bin dir: $install_dir"
echo " pruning stale files in target bin dir..."
cleanup_old_binaries "$install_dir/aicx"
cleanup_old_binaries "$install_dir/aicx-mcp"
cleanup_old_binaries "$install_dir/ai-contexters"
cleanup_old_binaries "$install_dir/ai-contexters-mcp"
cleanup_old_binaries "$HOME/.cargo/bin/ai-contexters"
cleanup_old_binaries "$HOME/.cargo/bin/ai-contexters-mcp"
install -m 755 "$source_aicx" "$install_dir/aicx"
install -m 755 "$source_mcp" "$install_dir/aicx-mcp"
echo " installed bundled binaries into $install_dir"
}
detect_release_target() {
local os arch
os=$(uname -s)
arch=$(uname -m)
case "${os}:${arch}" in
Darwin:arm64) echo "aarch64-apple-darwin" ;;
Darwin:x86_64)
echo "Error: x86_64-apple-darwin release assets are not published in the current AICX release asset set." >&2
echo " Use a local bundle install instead, or install from source with cargo." >&2
exit 1
;;
Linux:x86_64) echo "x86_64-linux-gnu" ;;
Linux:aarch64|Linux:arm64) echo "aarch64-linux-gnu" ;;
*)
echo "Error: unsupported platform for release installer: ${os}/${arch}" >&2
echo " Use a local bundle install instead, or install from source with cargo." >&2
exit 1
;;
esac
}
resolve_release_tag() {
if [ "$AICX_RELEASE_TAG" != "latest" ]; then
echo "$AICX_RELEASE_TAG"
return 0
fi
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required to resolve the latest GitHub release tag." >&2
exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
echo "Error: python3 is required to resolve the latest GitHub release tag." >&2
exit 1
fi
curl -fsSL "https://api.github.com/repos/${AICX_RELEASE_REPO}/releases/latest" | python3 -c '
import json
import sys
data = json.load(sys.stdin)
tag = data.get("tag_name")
if not tag:
raise SystemExit("GitHub API response did not include tag_name")
print(tag)
'
}
download_release_bundle() {
local release_tag target archive_ext archive_name bundle_name base_url tmp_dir archive_path checksum_path bundle_dir
if ! command -v curl >/dev/null 2>&1; then
echo "Error: curl is required for release installer mode." >&2
exit 1
fi
if ! command -v python3 >/dev/null 2>&1; then
echo "Error: python3 is required for release installer mode." >&2
exit 1
fi
release_tag=$(resolve_release_tag)
target=$(detect_release_target)
case "$target" in
*apple-darwin|*windows*) archive_ext="zip" ;;
*) archive_ext="tar.gz" ;;
esac
bundle_name="aicx-${release_tag}-${target}-slim"
archive_name="${bundle_name}.${archive_ext}"
base_url="https://github.com/${AICX_RELEASE_REPO}/releases/download/${release_tag}"
tmp_dir=$(mktemp -d "${TMPDIR:-/tmp}/aicx-release-install.XXXXXX")
archive_path="$tmp_dir/$archive_name"
checksum_path="$tmp_dir/${archive_name}.sha256"
cleanup_release_tmp() {
rm -rf "$tmp_dir"
}
trap cleanup_release_tmp EXIT
echo "[1/4] Downloading verified release bundle..."
echo " release tag: $release_tag"
echo " target: $target"
echo " asset: $archive_name"
curl -fsSL "$base_url/$archive_name" -o "$archive_path"
curl -fsSL "$base_url/${archive_name}.sha256" -o "$checksum_path"
echo "[2/4] Verifying SHA256..."
ARCHIVE_PATH="$archive_path" CHECKSUM_PATH="$checksum_path" python3 - <<'PY'
import hashlib
import os
from pathlib import Path
archive = Path(os.environ["ARCHIVE_PATH"])
checksum = Path(os.environ["CHECKSUM_PATH"]).read_text(encoding="utf-8").strip().split()[0]
digest = hashlib.sha256(archive.read_bytes()).hexdigest()
if digest != checksum:
raise SystemExit(
f"SHA256 mismatch for {archive.name}: expected {checksum}, got {digest}"
)
print(f" checksum ok: {archive.name}")
PY
echo "[3/4] Extracting release bundle..."
ARCHIVE_PATH="$archive_path" DEST_DIR="$tmp_dir" python3 - <<'PY'
import os
import tarfile
import zipfile
from pathlib import Path
archive_path = Path(os.environ["ARCHIVE_PATH"])
dest_dir = Path(os.environ["DEST_DIR"])
if archive_path.name.endswith(".zip"):
with zipfile.ZipFile(archive_path) as archive:
archive.extractall(dest_dir)
elif archive_path.name.endswith(".tar.gz"):
with tarfile.open(archive_path, "r:gz") as archive:
archive.extractall(dest_dir)
else:
raise SystemExit(f"Unsupported archive format: {archive_path.name}")
PY
bundle_dir="$tmp_dir/$bundle_name"
if [ ! -f "$bundle_dir/install.sh" ]; then
echo "Error: release bundle does not contain install.sh: $bundle_dir" >&2
exit 1
fi
echo "[4/4] Delegating to bundled installer..."
AICX_INSTALL_MODE="bundle" \
AICX_BIN_DIR="$AICX_BIN_DIR" \
AICX_INSTALL_FORCE="$AICX_INSTALL_FORCE" \
AICX_INSTALL_DRY_RUN="$AICX_INSTALL_DRY_RUN" \
AICX_CONFIG_PATH="$AICX_CONFIG_PATH" \
AICX_HOME_PICKER="$AICX_HOME_PICKER" \
AICX_STORAGE_HOME="$AICX_STORAGE_HOME" \
AICX_EMBEDDER_PICKER="$AICX_EMBEDDER_PICKER" \
AICX_EMBEDDER_PROFILE="$AICX_EMBEDDER_PROFILE" \
bash "$bundle_dir/install.sh"
exit 0
}
resolve_install_mode() {
case "$AICX_INSTALL_MODE" in
auto)
if [ "$HAS_BUNDLED_BINARIES" -eq 1 ]; then
echo "bundle"
elif [ "$HAS_LOCAL_MANIFEST" -eq 1 ]; then
echo "local"
else
echo "release"
fi
;;
release|bundle|local|crates|git)
echo "$AICX_INSTALL_MODE"
;;
*)
echo "Error: unsupported AICX_INSTALL_MODE='$AICX_INSTALL_MODE' (expected auto, release, bundle, local, crates, or git)." >&2
exit 1
;;
esac
}
INSTALL_MODE=$(resolve_install_mode)
INSTALL_TARGET_BIN_DIR=$(install_target_bin_dir "$INSTALL_MODE")
INSTALL_TARGET_AICX="$INSTALL_TARGET_BIN_DIR/aicx"
INSTALL_TARGET_AICX_MCP="$INSTALL_TARGET_BIN_DIR/aicx-mcp"
INSTALL_TARGET_VERSION=$(target_install_version "$INSTALL_MODE")
if [ "$VERIFY_PATH_ONLY" -eq 1 ]; then
verify_install_path_resolution "$INSTALL_TARGET_AICX" "aicx"
verify_install_path_resolution "$INSTALL_TARGET_AICX_MCP" "aicx-mcp"
exit 0
fi
if [ "$SKIP_INSTALL" -eq 0 ]; then
scan_aicx_shadows "$INSTALL_TARGET_AICX" "$INSTALL_TARGET_AICX_MCP"
cleanup_shadow_aicx_binaries "$INSTALL_TARGET_BIN_DIR" "$INSTALL_TARGET_VERSION"
fi
if [ "$SHADOW_CHECK_ONLY" -eq 1 ]; then
exit 0
fi
if [ "$AICX_INSTALL_DRY_RUN" = "1" ]; then
echo "Dry run complete; no binaries installed and no config files changed."
exit 0
fi
if [ "$INSTALL_MODE" = "release" ]; then
if [ "$SKIP_INSTALL" -eq 1 ]; then
echo "Error: --skip-install cannot be combined with release download mode." >&2
exit 1
fi
download_release_bundle
fi
# --- Step 1: Install binaries ---
if [ "$SKIP_INSTALL" -eq 0 ]; then
if [ "$INSTALL_MODE" != "bundle" ] && ! command -v cargo >/dev/null 2>&1; then
echo "Error: cargo not found. Install Rust first: https://rustup.rs"
exit 1
fi
# Show live compilation progress: count Compiling lines → [1/4] Compiling... (N crates)
cargo_install_with_progress() {
local total=0
"$@" 2>&1 | while IFS= read -r line; do
case "$line" in
*Compiling*)
total=$((total + 1))
printf '\r Compiling... (%d crates)' "$total" >&2
;;
*Finished*|*Installing*|*Installed*|*Replacing*)
printf '\r %s\n' "$line" >&2
;;
esac
done
printf '\n' >&2
}
if [ "$INSTALL_MODE" = "bundle" ]; then
echo "[1/4] Installing bundled aicx + aicx-mcp into $AICX_BIN_DIR..."
install_bundle_binaries
elif [ "$INSTALL_MODE" = "local" ]; then
echo "[1/4] Installing aicx + aicx-mcp from this checkout..."
cargo_install_with_progress cargo install --path "$SCRIPT_DIR" --locked --force --bin aicx --bin aicx-mcp
elif [ "$INSTALL_MODE" = "crates" ]; then