-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·509 lines (428 loc) · 14.1 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·509 lines (428 loc) · 14.1 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
#!/bin/sh
set -e
REPO="autohandai/code-cli"
BINARY_NAME="autohand"
COMPAT_BINARY_NAME="autohand-code"
AGENT_ALIAS_NAME="agent"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
info() {
printf "${BLUE}%s${NC}\n" "$1"
}
success() {
printf "${GREEN}%s${NC}\n" "$1"
}
warn() {
printf "${YELLOW}%s${NC}\n" "$1"
}
install_local_ai_runtime_if_requested() {
if [ "${AUTOHAND_INSTALL_LOCAL_AI:-0}" != "1" ]; then
return 0
fi
if [ "$(uname -s)" != "Darwin" ] || [ "$(uname -m)" != "arm64" ]; then
warn "Skipping Autohand AI Local runtime: MLX requires Apple Silicon macOS."
return 0
fi
info "Installing Autohand AI Local runtime..."
# Keep this pin in sync with MLX_LM_PINNED_VERSION in
# src/providers/autohandAILocalSetup.ts so the installer and the in-app setup
# wizard provision the same MLX runtime.
MLX_LM_SPEC="mlx-lm==0.31.3"
if ! command -v mlx_lm.server >/dev/null 2>&1; then
if command -v uv >/dev/null 2>&1; then
uv tool install "$MLX_LM_SPEC"
elif command -v pipx >/dev/null 2>&1; then
pipx install "$MLX_LM_SPEC"
else
python3 -m pip install --user "$MLX_LM_SPEC"
fi
fi
if ! command -v llmfit >/dev/null 2>&1; then
# --local installs to ~/.local/bin without sudo (no password prompt).
curl -fsSL https://llmfit.axjns.dev/install.sh | sh -s -- --local
fi
success "Autohand AI Local runtime installed."
}
main() {
printf "${BLUE}"
cat << 'EOF'
___ __ __ __
/ | __ __/ /_____ / /_ ____ _____ ____/ /
/ /| |/ / / / __/ __ \/ __ \/ __ `/ __ \/ __ /
/ ___ / /_/ / /_/ /_/ / / / / /_/ / / / / /_/ /
/_/ |_\__,_/\__/\____/_/ /_/\__,_/_/ /_/\__,_/
EOF
printf "${NC}"
echo ""
need_cmd curl
need_cmd uname
need_cmd chmod
need_cmd ln
# Determine channel from flags or environment
local _channel="stable"
for arg in "$@"; do
case "$arg" in
--alpha)
_channel="alpha"
;;
esac
done
# Environment variable override
if [ -n "${AUTOHAND_CHANNEL:-}" ]; then
_channel="$AUTOHAND_CHANNEL"
fi
get_architecture || return 1
local _arch="$RETVAL"
local _version="${AUTOHAND_VERSION:-latest}"
local _asset_name="autohand-${_arch}.tar.gz"
local _url
local _checksum_url
if [ "$_channel" = "alpha" ]; then
# Alpha: fetch the latest prerelease tag from GitHub API
printf "${YELLOW}Fetching latest alpha release...${NC}\n"
local _alpha_tag
_alpha_tag=$(get_latest_alpha_tag)
if [ -z "$_alpha_tag" ]; then
printf "${RED}Error: Failed to find an alpha release${NC}\n"
printf "${YELLOW}Hint: Check available releases at https://github.com/${REPO}/releases${NC}\n"
exit 1
fi
_version=$(echo "$_alpha_tag" | sed 's/^v//')
_url="https://github.com/${REPO}/releases/download/${_alpha_tag}/${_asset_name}"
elif [ "$_version" = "latest" ]; then
_url="https://github.com/${REPO}/releases/latest/download/${_asset_name}"
else
_url="https://github.com/${REPO}/releases/download/v${_version}/${_asset_name}"
fi
_checksum_url="${_url}.sha256"
local _dir
if [ -n "${AUTOHAND_INSTALL_DIR:-}" ]; then
_dir="$AUTOHAND_INSTALL_DIR"
elif [ "$(id -u)" = "0" ]; then
_dir="/usr/local/bin"
elif [ -d "$HOME/.local/bin" ]; then
_dir="$HOME/.local/bin"
else
_dir="$HOME/.local/bin"
mkdir -p "$_dir"
fi
printf "${YELLOW}Downloading Autohand CLI...${NC}\n"
echo " Channel: $_channel"
echo " Platform: $_arch"
echo " Version: $_version"
echo " Target: $_dir/$BINARY_NAME"
echo ""
need_cmd tar
local _tmp_dir
_tmp_dir=$(mktemp -d)
local _archive_path="${_tmp_dir}/${_asset_name}"
local _checksum_path="${_archive_path}.sha256"
if ! curl -fsSL "$_url" -o "$_archive_path" 2>/dev/null; then
printf "${RED}Error: Failed to download from $_url${NC}\n"
printf "${YELLOW}Hint: Check if the version exists at https://github.com/${REPO}/releases${NC}\n"
rm -rf "$_tmp_dir"
exit 1
fi
if ! curl -fsSL "$_checksum_url" -o "$_checksum_path" 2>/dev/null; then
printf "${RED}Error: Failed to download checksum from $_checksum_url${NC}\n"
rm -rf "$_tmp_dir"
exit 1
fi
if [ ! -s "$_archive_path" ]; then
printf "${RED}Error: Downloaded file is empty${NC}\n"
rm -rf "$_tmp_dir"
exit 1
fi
verify_checksum "$_archive_path" "$_checksum_path"
tar -xzf "$_archive_path" -C "$_tmp_dir"
if [ ! -f "${_tmp_dir}/autohand" ]; then
printf "${RED}Error: Bundle does not contain autohand${NC}\n"
rm -rf "$_tmp_dir"
exit 1
fi
chmod +x "${_tmp_dir}/autohand"
local _binary_version
if ! _binary_version=$(probe_binary_version "${_tmp_dir}/autohand" "${_tmp_dir}/version"); then
printf "${RED}Error: Downloaded Autohand CLI failed to start${NC}\n"
printf "${YELLOW}The existing installation was not changed.${NC}\n"
rm -rf "$_tmp_dir"
exit 1
fi
install_file "${_tmp_dir}/autohand" "$_dir/$BINARY_NAME"
install_symlink "$BINARY_NAME" "$_dir/$COMPAT_BINARY_NAME"
install_symlink "$BINARY_NAME" "$_dir/$AGENT_ALIAS_NAME"
claim_agent_alias_path_wide "$_dir/$BINARY_NAME" "$_dir"
rm -rf "$_tmp_dir"
if ! echo "$PATH" | tr ':' '\n' | grep -qx "$_dir"; then
echo ""
printf "${YELLOW}Note: Add $_dir to your PATH:${NC}\n"
echo ""
echo " # For bash (~/.bashrc):"
echo " export PATH=\"\$PATH:$_dir\""
echo ""
echo " # For zsh (~/.zshrc):"
echo " export PATH=\"\$PATH:$_dir\""
echo ""
fi
echo ""
printf "${GREEN}Autohand CLI installed successfully!${NC}\n"
echo ""
echo "Version: $_binary_version"
echo ""
echo "Get started:"
echo " autohand # Start interactive mode"
echo " autohand --help # Show all options"
echo " autohand login # Sign in to your account"
install_local_ai_runtime_if_requested
if [ "$(uname -s)" = "Darwin" ] && [ "$(uname -m)" = "arm64" ]; then
echo ""
echo "Autohand AI Local:"
echo " Run /model, choose Autohand AI, then Local."
echo " To preinstall MLX and llmfit during install, set AUTOHAND_INSTALL_LOCAL_AI=1."
fi
echo ""
}
compute_sha256() {
local _file="$1"
if command -v sha256sum > /dev/null 2>&1; then
sha256sum "$_file" | awk '{print $1}'
return 0
fi
if command -v shasum > /dev/null 2>&1; then
shasum -a 256 "$_file" | awk '{print $1}'
return 0
fi
return 1
}
verify_checksum() {
local _file="$1"
local _checksum_file="$2"
local _expected _actual
_expected=$(awk '{print $1}' "$_checksum_file")
if [ -z "$_expected" ]; then
printf "${RED}Error: Checksum file is empty${NC}\n"
exit 1
fi
if ! _actual=$(compute_sha256 "$_file"); then
printf "${RED}Error: No SHA-256 tool available (need sha256sum or shasum)${NC}\n"
exit 1
fi
if [ "$_expected" != "$_actual" ]; then
printf "${RED}Error: Checksum verification failed${NC}\n"
exit 1
fi
success "Checksum verification passed"
}
probe_binary_version() {
local _binary="$1"
local _output_file="$2"
local _pid _watchdog _status
if command -v timeout > /dev/null 2>&1; then
if timeout 5 "$_binary" --version < /dev/null > "$_output_file" 2>/dev/null; then
_status=0
else
_status=$?
fi
if [ "$_status" -ne 0 ]; then
return "$_status"
fi
else
"$_binary" --version < /dev/null > "$_output_file" 2>/dev/null &
_pid=$!
(
local _sleep_pid=""
trap '[ -z "$_sleep_pid" ] || kill "$_sleep_pid" 2>/dev/null; exit 0' HUP INT TERM
sleep 5 &
_sleep_pid=$!
wait "$_sleep_pid" 2>/dev/null || exit 0
kill "$_pid" 2>/dev/null
) &
_watchdog=$!
if wait "$_pid" 2>/dev/null; then
_status=0
else
_status=$?
fi
kill "$_watchdog" 2>/dev/null || true
wait "$_watchdog" 2>/dev/null || true
if [ "$_status" -ne 0 ]; then
return "$_status"
fi
fi
cat "$_output_file"
}
install_file() {
local _source="$1"
local _dest="$2"
if [ -w "$(dirname "$_dest")" ]; then
cp "$_source" "$_dest"
else
printf "${YELLOW}Elevated permissions required to install to $(dirname "$_dest")${NC}\n"
sudo cp "$_source" "$_dest"
fi
}
install_symlink() {
local _target="$1"
local _dest="$2"
if [ -w "$(dirname "$_dest")" ]; then
ln -sfn "$_target" "$_dest"
else
printf "${YELLOW}Elevated permissions required to create alias in $(dirname "$_dest")${NC}\n"
sudo ln -sfn "$_target" "$_dest"
fi
}
claim_agent_alias_path_wide() {
# agent is a generic name other AI CLIs also claim (e.g. Grok installs its
# own "agent" earlier on PATH). Reclaim it in every writable PATH
# directory we didn't just install into, so "agent" resolves to Autohand
# instead of whichever competing tool happened to win the PATH race.
# User-writable directories only: no sudo/elevation into directories the
# current user can't already write to.
local _canonical="$1"
local _own_dir="$2"
local _seen=""
local _p _candidate _link_target
local _old_ifs="$IFS"
IFS=':'
set -- $PATH
IFS="$_old_ifs"
for _p in "$@"; do
[ -n "$_p" ] || continue
[ -d "$_p" ] || continue
[ "$_p" = "$_own_dir" ] && continue
case " $_seen " in
*" $_p "*) continue ;;
esac
_seen="$_seen $_p"
[ -w "$_p" ] || continue
_candidate="$_p/$AGENT_ALIAS_NAME"
if [ -L "$_candidate" ]; then
_link_target=$(readlink "$_candidate" 2>/dev/null || true)
[ "$_link_target" = "$_canonical" ] && continue
elif [ ! -e "$_candidate" ]; then
continue
fi
rm -f "$_candidate" 2>/dev/null || continue
if ln -sfn "$_canonical" "$_candidate" 2>/dev/null; then
info "Claimed existing 'agent' command in $_p"
fi
done
}
get_latest_alpha_tag() {
# Fetch recent releases and pick the newest prerelease by published timestamp.
# GitHub API list order is not guaranteed chronological for prereleases.
local _releases_json
_releases_json=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases?per_page=100" 2>/dev/null) || return 1
local _found_tag
_found_tag=$(
printf '%s' "$_releases_json" \
| tr '\n' ' ' \
| tr '{' '\n' \
| awk '
BEGIN {
best_tag = "";
best_timestamp = "";
}
/"tag_name"[[:space:]]*:/ && /"prerelease"[[:space:]]*:[[:space:]]*true/ {
tag = "";
published = "";
created = "";
if (match($0, /"tag_name"[[:space:]]*:[[:space:]]*"[^"]+"/)) {
tag = substr($0, RSTART, RLENGTH);
sub(/.*"tag_name"[[:space:]]*:[[:space:]]*"/, "", tag);
sub(/".*/, "", tag);
}
if (match($0, /"published_at"[[:space:]]*:[[:space:]]*"[^"]+"/)) {
published = substr($0, RSTART, RLENGTH);
sub(/.*"published_at"[[:space:]]*:[[:space:]]*"/, "", published);
sub(/".*/, "", published);
}
if (match($0, /"created_at"[[:space:]]*:[[:space:]]*"[^"]+"/)) {
created = substr($0, RSTART, RLENGTH);
sub(/.*"created_at"[[:space:]]*:[[:space:]]*"/, "", created);
sub(/".*/, "", created);
}
timestamp = published;
if (timestamp == "") {
timestamp = created;
}
if (tag != "" && timestamp != "") {
if (best_timestamp == "" || timestamp > best_timestamp) {
best_timestamp = timestamp;
best_tag = tag;
}
}
}
END {
if (best_tag != "") {
print best_tag;
}
}
'
)
if [ -n "$_found_tag" ]; then
printf '%s\n' "$_found_tag"
return 0
fi
return 1
}
get_architecture() {
local _ostype _cputype
_ostype="$(uname -s)"
_cputype="$(uname -m)"
case "$_ostype" in
Linux)
_ostype="linux"
;;
Darwin)
_ostype="macos"
;;
MINGW* | MSYS* | CYGWIN* | Windows_NT)
_ostype="windows"
;;
FreeBSD)
_ostype="linux"
;;
*)
err "Unsupported operating system: $_ostype"
return 1
;;
esac
case "$_cputype" in
x86_64 | amd64)
_cputype="x64"
;;
aarch64 | arm64)
_cputype="arm64"
;;
armv7l)
err "32-bit ARM is not supported. Please use a 64-bit system."
return 1
;;
i686 | i386)
err "32-bit x86 is not supported. Please use a 64-bit system."
return 1
;;
*)
err "Unsupported CPU architecture: $_cputype"
return 1
;;
esac
RETVAL="${_ostype}-${_cputype}"
}
say() {
printf 'autohand: %s\n' "$1"
}
err() {
printf "${RED}Error: %s${NC}\n" "$1" >&2
exit 1
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1; then
err "Required command not found: $1"
fi
}
main "$@" || exit 1