forked from miaoxworld/OpenClawInstaller
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·3583 lines (3177 loc) · 120 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·3583 lines (3177 loc) · 120 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
#
# ╔═══════════════════════════════════════════════════════════════════════════╗
# ║ ║
# ║ 🦞 OpenClaw 一键部署脚本 v1.0.0 ║
# ║ 智能 AI 助手部署工具 - 支持多平台多模型 ║
# ║ ║
# ║ GitHub: https://github.com/cwj526/OpenClawInstaller ║
# ║ 官方文档: https://clawd.bot/docs ║
# ║ ║
# ╚═══════════════════════════════════════════════════════════════════════════╝
#
# 使用方法:
# curl -fsSL https://raw.githubusercontent.com/cwj526/OpenClawInstaller/main/install.sh | bash
# 或本地执行: chmod +x install.sh && ./install.sh
#
set -e
# ================================ TTY 检测 ================================
# 当通过 curl | bash 运行时,stdin 是管道,需要从 /dev/tty 读取用户输入
if [ -t 0 ]; then
# stdin 是终端
TTY_INPUT="/dev/stdin"
RUN_FROM_PIPE="false"
else
# stdin 是管道,使用 /dev/tty
TTY_INPUT="/dev/tty"
RUN_FROM_PIPE="true"
fi
# ================================ 颜色定义 ================================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
GRAY='\033[0;90m'
NC='\033[0m' # 无颜色
# ================================ 配置变量 ================================
OPENCLAW_VERSION="latest"
CONFIG_DIR="$HOME/.openclaw"
CONFIG_MENU_PATH="$CONFIG_DIR/config-menu.sh"
TUZI_CACHE_DIR="$CONFIG_DIR/cache"
MIN_NODE_VERSION=22
GITHUB_REPO="cwj526/OpenClawInstaller"
GITHUB_RAW_URL="https://raw.githubusercontent.com/$GITHUB_REPO/main"
INSTALL_MODE=""
FORCE_REINSTALL="false"
# ================================ 工具函数 ================================
print_banner() {
echo -e "${CYAN}"
cat << 'EOF'
██████╗ ██████╗ ███████╗███╗ ██╗ ██████╗██╗ █████╗ ██╗ ██╗
██╔═══██╗██╔══██╗██╔════╝████╗ ██║██╔════╝██║ ██╔══██╗██║ ██║
██║ ██║██████╔╝█████╗ ██╔██╗ ██║██║ ██║ ███████║██║ █╗ ██║
██║ ██║██╔═══╝ ██╔══╝ ██║╚██╗██║██║ ██║ ██╔══██║██║███╗██║
╚██████╔╝██║ ███████╗██║ ╚████║╚██████╗███████╗██║ ██║╚███╔███╔╝
╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═══╝ ╚═════╝╚══════╝╚═╝ ╚═╝ ╚══╝╚══╝
🦞 智能 AI 助手一键部署工具 v1.0.0 🦞
EOF
echo -e "${NC}"
}
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
log_step() {
echo -e "${BLUE}[STEP]${NC} $1"
}
run_with_timeout() {
local timeout_seconds="$1"
shift
if command -v timeout >/dev/null 2>&1; then
timeout "$timeout_seconds" "$@"
return $?
fi
if command -v python3 >/dev/null 2>&1; then
python3 - "$timeout_seconds" "$@" <<'PY'
import subprocess
import sys
timeout_seconds = int(sys.argv[1])
cmd = sys.argv[2:]
try:
completed = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
timeout=timeout_seconds,
)
sys.stdout.write(completed.stdout or "")
sys.exit(completed.returncode)
except subprocess.TimeoutExpired as exc:
output = exc.stdout or ""
if isinstance(output, bytes):
output = output.decode(errors="replace")
sys.stdout.write(output)
sys.exit(124)
PY
return $?
fi
"$@"
}
filter_openclaw_test_output() {
echo "$1" | grep -v "ExperimentalWarning" \
| grep -v "at emitExperimentalWarning" \
| grep -v "at ModuleLoader" \
| grep -v "at callTranslator" \
| grep -v "Cannot read properties of undefined" \
| grep -v "TypeError:" \
| grep -v "ReferenceError:" \
| grep -v "\[plugins\]" \
| grep -v "Doctor warnings" \
| grep -v "Registered.*tools" \
| grep -v "State dir migration" \
| grep -v "\[skills\] Skipping skill path that resolves outside its configured root\." \
| grep -v "^│" \
| grep -v "^◇"
}
extract_openclaw_test_error() {
local output="$1"
echo "$output" | grep -iE "HTTP 401|HTTP 403|authentication_error|authentication failed|Invalid bearer token|Incorrect API|Unknown model|API key|超时" | head -5
}
get_expected_tuzi_provider_prefix() {
local tuzi_group="$1"
case "$tuzi_group" in
codex) printf '%s' "tuzi-codex" ;;
claude-code) printf '%s' "tuzi-claude-code" ;;
gaccode) printf '%s' "gac" ;;
*) printf '%s' "" ;;
esac
}
get_expected_tuzi_provider_hint() {
local tuzi_group="$1"
case "$tuzi_group" in
gaccode) printf '%s' "gac-claude/... 或 gac-codex/..." ;;
*)
local prefix
prefix=$(get_expected_tuzi_provider_prefix "$tuzi_group")
[ -n "$prefix" ] && printf '%s' "${prefix}/..."
;;
esac
}
is_expected_tuzi_model_ref() {
local tuzi_group="$1"
local model_ref="$2"
case "$tuzi_group" in
gaccode)
[[ "$model_ref" == gac-claude/* || "$model_ref" == gac-codex/* ]]
;;
*)
local prefix
prefix=$(get_expected_tuzi_provider_prefix "$tuzi_group")
[ -n "$prefix" ] && [[ "$model_ref" == "$prefix/"* ]]
;;
esac
}
get_openclaw_default_model_from_status() {
local status_output="$1"
echo "$status_output" | sed -n 's/^Default[[:space:]]*:[[:space:]]*//p' | head -1
}
run_openclaw_precheck() {
local expected_tuzi_group="$1"
local expected_prefix
local status_output=""
local doctor_output=""
local doctor_exit=0
local combined_output=""
local current_default=""
local blockers=""
local warnings=""
local status_summary=""
expected_prefix=$(get_expected_tuzi_provider_prefix "$expected_tuzi_group")
set +e
status_output=$(run_with_timeout 15 openclaw models status 2>&1)
local status_exit=$?
doctor_output=$(run_with_timeout 12 openclaw doctor 2>&1)
doctor_exit=$?
set -e
combined_output="${status_output}"$'\n'"${doctor_output}"
current_default=$(get_openclaw_primary_model_file "$HOME/.openclaw/openclaw.json")
if [ -z "$current_default" ]; then
current_default=$(get_openclaw_default_model_from_status "$status_output")
fi
status_summary=$(printf '%s\n' "$status_output" \
| grep -E "^Default|^Auth store|^Shell env|^Providers w/ OAuth/tokens|^- " \
| head -8)
if [ $status_exit -ne 0 ] && [ $status_exit -ne 124 ]; then
blockers="${blockers}${blockers:+$'\n'}无法读取 openclaw models status,请检查 OpenClaw 安装或配置。"
fi
if echo "$combined_output" | grep -qiE "Token refresh failed|401|403|authentication_error|authentication failed|Invalid bearer token"; then
blockers="${blockers}${blockers:+$'\n'}检测到鉴权失败或 token 已失效,请重新配置对应 Provider 的凭据。"
fi
if echo "$combined_output" | grep -qi "Unknown model"; then
blockers="${blockers}${blockers:+$'\n'}检测到模型配置错误,OpenClaw 当前无法识别所选模型。"
fi
if [ -n "$expected_prefix" ] && [ -n "$current_default" ] && ! is_expected_tuzi_model_ref "$expected_tuzi_group" "$current_default"; then
blockers="${blockers}${blockers:+$'\n'}默认模型仍是 $(sanitize_model_display "$current_default"),预期应切换到 $(get_expected_tuzi_provider_hint "$expected_tuzi_group")。"
fi
if echo "$combined_output" | grep -q "plugins.allow is empty"; then
warnings="${warnings}${warnings:+$'\n'}检测到插件自动加载提示,可稍后通过 plugins.allow 显式收敛。"
fi
if echo "$combined_output" | grep -q "\[skills\] Skipping skill path that resolves outside its configured root\."; then
warnings="${warnings}${warnings:+$'\n'}检测到 skills 根目录外的符号链接告警,不影响本次 AI 联通性判断。"
fi
if echo "$doctor_output" | grep -q "gateway.mode is unset"; then
warnings="${warnings}${warnings:+$'\n'}doctor 提示 gateway.mode 未设置,但这不阻止本次 local agent 实测。"
fi
if [ $doctor_exit -eq 124 ]; then
warnings="${warnings}${warnings:+$'\n'}openclaw doctor 超时,已按当前输出继续预检。"
fi
OPENCLAW_PRECHECK_DEFAULT_MODEL="$current_default"
OPENCLAW_PRECHECK_STATUS_SUMMARY="$status_summary"
OPENCLAW_PRECHECK_BLOCKERS="$blockers"
OPENCLAW_PRECHECK_WARNINGS="$warnings"
OPENCLAW_PRECHECK_STATUS_OUTPUT="$status_output"
OPENCLAW_PRECHECK_DOCTOR_OUTPUT="$doctor_output"
}
print_exit_hint() {
echo -e "${GRAY}输入 q 可安全退出脚本${NC}"
}
safe_exit() {
echo ""
echo -e "${CYAN}已安全退出脚本。${NC}"
exit 0
}
should_exit_input() {
local value="$1"
case "$value" in
[qQ]|[qQ][uU][iI][tT]|[eE][xX][iI][tT]) return 0 ;;
*) return 1 ;;
esac
}
shell_quote_value() {
local value="$1"
printf '%q' "$value"
}
append_env_kv() {
local env_file="$1"
local key="$2"
local value="$3"
local escaped_value
escaped_value=$(shell_quote_value "$value")
printf 'export %s=%s\n' "$key" "$escaped_value" >> "$env_file"
}
write_env_header() {
local env_file="$1"
local source_label="$2"
cat > "$env_file" << EOF
# OpenClaw 环境变量配置
# 由${source_label}自动生成: $(date '+%Y-%m-%d %H:%M:%S')
EOF
}
get_env_file_value() {
local env_file="$1"
local key="$2"
if [ ! -f "$env_file" ]; then
return 0
fi
local env_line
env_line=$(grep "^export ${key}=" "$env_file" 2>/dev/null | tail -1)
if [ -z "$env_line" ]; then
return 0
fi
local env_value="${env_line#*=}"
bash -c '
eval "value=$1"
printf "%s" "$value"
' _ "$env_value" 2>/dev/null
}
get_openclaw_primary_model_file() {
local config_file="${1:-$HOME/.openclaw/openclaw.json}"
if [ ! -f "$config_file" ]; then
return 0
fi
if command -v node &> /dev/null; then
node -e "
try {
const config = JSON.parse(require('fs').readFileSync('$config_file', 'utf8'));
console.log(config?.agents?.defaults?.model?.primary || '');
} catch (e) {
console.log('');
}
" 2>/dev/null
return 0
fi
if command -v python3 &> /dev/null; then
python3 -c "
import json
try:
with open('$config_file', 'r') as f:
config = json.load(f)
print(config.get('agents', {}).get('defaults', {}).get('model', {}).get('primary', ''))
except Exception:
print('')
" 2>/dev/null
fi
}
get_tuzi_group_from_model_ref() {
local model_ref="$1"
case "$model_ref" in
tuzi-codex/*) printf '%s' "codex" ;;
tuzi-claude-code/*) printf '%s' "claude-code" ;;
gac-claude/*|gac-codex/*) printf '%s' "gaccode" ;;
*) printf '%s' "" ;;
esac
}
get_tuzi_model_name_from_ref() {
local model_ref="$1"
case "$model_ref" in
*/*) printf '%s' "${model_ref#*/}" ;;
*) printf '%s' "$model_ref" ;;
esac
}
sanitize_model_display() {
local value="$1"
local sanitized=""
sanitized=$(printf '%s' "$value" \
| LC_ALL=C tr -cd '[:alnum:]._:/ -' 2>/dev/null \
| sed 's/[[:space:]]\+/ /g; s/^ //; s/ $//')
if [ -n "$sanitized" ]; then
printf '%s' "$sanitized"
else
printf '%s' "(无法识别的模型名)"
fi
}
get_current_tuzi_group() {
local config_file="${1:-$HOME/.openclaw/openclaw.json}"
local primary_model=""
primary_model=$(get_openclaw_primary_model_file "$config_file")
get_tuzi_group_from_model_ref "$primary_model"
}
download_latest_config_menu() {
local target_path="$1"
mkdir -p "$(dirname "$target_path")"
if curl --connect-timeout 10 --max-time 30 -fsSL "$GITHUB_RAW_URL/config-menu.sh" -o "$target_path.tmp"; then
mv "$target_path.tmp" "$target_path"
chmod +x "$target_path"
return 0
fi
rm -f "$target_path.tmp" 2>/dev/null
return 1
}
install_local_config_menu() {
local script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
local local_config_menu="$script_dir/config-menu.sh"
if [ ! -f "$local_config_menu" ]; then
return 1
fi
mkdir -p "$(dirname "$CONFIG_MENU_PATH")"
cp "$local_config_menu" "$CONFIG_MENU_PATH"
chmod +x "$CONFIG_MENU_PATH"
}
ensure_config_menu_available() {
if [ -f "$CONFIG_MENU_PATH" ]; then
chmod +x "$CONFIG_MENU_PATH" 2>/dev/null || true
return 0
fi
if install_local_config_menu; then
return 0
fi
download_latest_config_menu "$CONFIG_MENU_PATH"
}
spinner() {
local pid=$1
local delay=0.1
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
# 从 TTY 读取用户输入(支持 curl | bash 模式)
read_input() {
local prompt="$1"
local var_name="$2"
local value=""
print_exit_hint
echo -en "$prompt"
read value < "$TTY_INPUT"
if should_exit_input "$value"; then
safe_exit
fi
printf -v "$var_name" '%s' "$value"
}
confirm() {
local message="$1"
local default="${2:-y}"
if [ "$default" = "y" ]; then
local prompt="[Y/n]"
else
local prompt="[y/N]"
fi
print_exit_hint
echo -en "${YELLOW}$message $prompt: ${NC}"
read response < "$TTY_INPUT"
response=${response:-$default}
case "$response" in
[qQ]|[qQ][uU][iI][tT]|[eE][xX][iI][tT]) safe_exit ;;
[yY][eE][sS]|[yY]) return 0 ;;
*) return 1 ;;
esac
}
# ================================ 系统检测 ================================
detect_os() {
log_step "检测操作系统..."
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
OS_VERSION=$VERSION_ID
fi
PACKAGE_MANAGER=""
if command -v apt-get &> /dev/null; then
PACKAGE_MANAGER="apt"
elif command -v yum &> /dev/null; then
PACKAGE_MANAGER="yum"
elif command -v dnf &> /dev/null; then
PACKAGE_MANAGER="dnf"
elif command -v pacman &> /dev/null; then
PACKAGE_MANAGER="pacman"
fi
log_info "检测到 Linux 系统: $OS $OS_VERSION (包管理器: $PACKAGE_MANAGER)"
elif [[ "$OSTYPE" == "darwin"* ]]; then
OS="macos"
OS_VERSION=$(sw_vers -productVersion)
PACKAGE_MANAGER="brew"
log_info "检测到 macOS 系统: $OS_VERSION"
elif [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]]; then
OS="windows"
log_info "检测到 Windows 系统 (Git Bash/Cygwin)"
else
log_error "不支持的操作系统: $OSTYPE"
exit 1
fi
}
check_root() {
if [[ $EUID -eq 0 ]]; then
log_warn "检测到以 root 用户运行"
if ! confirm "建议使用普通用户运行,是否继续?" "n"; then
exit 1
fi
fi
}
# ================================ 依赖检查与安装 ================================
check_command() {
command -v "$1" &> /dev/null
}
read_valid_number_choice() {
local prompt="$1"
local min_value="$2"
local max_value="$3"
local default_value="$4"
local result_var="$5"
local choice=""
while true; do
print_exit_hint
echo -en "$prompt"
read choice < "$TTY_INPUT"
choice=${choice:-$default_value}
case "$choice" in
[qQ]|[qQ][uU][iI][tT]|[eE][xX][iI][tT])
safe_exit
;;
esac
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge "$min_value" ] && [ "$choice" -le "$max_value" ]; then
printf -v "$result_var" '%s' "$choice"
return 0
fi
log_error "输入无效,请输入 $min_value-$max_value 之间的数字,或输入 q 退出"
done
}
read_nonempty_value() {
local prompt="$1"
local result_var="$2"
local value=""
while true; do
print_exit_hint
echo -en "$prompt"
read value < "$TTY_INPUT"
if should_exit_input "$value"; then
safe_exit
fi
if [ -n "$value" ]; then
printf -v "$result_var" '%s' "$value"
return 0
fi
log_error "输入不能为空,请重新输入,或输入 q 退出"
done
}
read_value_allow_empty() {
local prompt="$1"
local result_var="$2"
local value=""
print_exit_hint
echo -en "$prompt"
read value < "$TTY_INPUT"
if should_exit_input "$value"; then
safe_exit
fi
printf -v "$result_var" '%s' "$value"
}
read_secret_value() {
local prompt="$1"
local result_var="$2"
read_nonempty_value "$prompt" "$result_var"
}
show_usage() {
cat << EOF
用法:
bash install.sh
bash install.sh --tuzi-only
bash install.sh --full-install
参数:
--tuzi-only 强制跳过安装,仅接入或更新 Tuzi API 配置
--full-install 强制执行完整安装流程
-h, --help 显示帮助
EOF
}
parse_args() {
while [ $# -gt 0 ]; do
case "$1" in
--tuzi-only)
INSTALL_MODE="tuzi-only"
;;
--full-install)
INSTALL_MODE="full-install"
FORCE_REINSTALL="true"
;;
-h|--help)
show_usage
exit 0
;;
*)
log_error "未知参数: $1"
show_usage
exit 1
;;
esac
shift
done
}
is_openclaw_ready() {
if ! check_command openclaw; then
return 1
fi
if ! openclaw --version >/dev/null 2>&1; then
return 1
fi
if [ ! -d "$CONFIG_DIR" ]; then
return 1
fi
local gateway_mode
gateway_mode=$(openclaw config get gateway.mode 2>/dev/null || true)
if [ -n "$gateway_mode" ] && [ "$gateway_mode" != "undefined" ]; then
return 0
fi
if openclaw models status >/dev/null 2>&1; then
return 0
fi
return 1
}
is_tuzi_group_complete() {
local group="$1"
local env_file="$HOME/.openclaw/env"
local key_var=""
local model_var=""
case "$group" in
gaccode)
local gac_key
local gac_claude_model
local gac_codex_model
gac_key=$(get_env_file_value "$env_file" "GACCODE_API_KEY")
gac_claude_model=$(get_env_file_value "$env_file" "GAC_CLAUDE_MODEL")
gac_codex_model=$(get_env_file_value "$env_file" "GAC_CODEX_MODEL")
[ -n "$gac_key" ] && [ -n "$gac_claude_model" ] && [ -n "$gac_codex_model" ]
return
;;
codex)
key_var="TUZI_CODEX_API_KEY"
model_var="TUZI_CODEX_MODEL"
;;
*)
key_var="TUZI_CLAUDE_CODE_API_KEY"
model_var="TUZI_CLAUDE_CODE_MODEL"
;;
esac
local group_key
local group_model
group_key=$(get_env_file_value "$env_file" "$key_var")
group_model=$(get_env_file_value "$env_file" "$model_var")
[ -n "$group_key" ] && [ -n "$group_model" ]
}
is_tuzi_configured() {
local env_file="$HOME/.openclaw/env"
if [ ! -f "$env_file" ]; then
return 1
fi
if is_tuzi_group_complete "claude-code" || is_tuzi_group_complete "codex" || is_tuzi_group_complete "gaccode"; then
return 0
fi
return 1
}
show_current_tuzi_config() {
local env_file="$HOME/.openclaw/env"
local openclaw_json="$HOME/.openclaw/openclaw.json"
if [ ! -f "$env_file" ]; then
return 0
fi
local current_group
local current_model_ref
local claude_key
local claude_model
local claude_models
local codex_key
local codex_model
local codex_models
local gac_key
local gac_claude_model
local gac_claude_models
local gac_codex_model
local gac_codex_models
current_model_ref=$(get_openclaw_primary_model_file "$openclaw_json")
current_group=$(get_tuzi_group_from_model_ref "$current_model_ref")
claude_key=$(get_env_file_value "$env_file" "TUZI_CLAUDE_CODE_API_KEY")
claude_model=$(get_env_file_value "$env_file" "TUZI_CLAUDE_CODE_MODEL")
claude_models=$(get_env_file_value "$env_file" "TUZI_CLAUDE_CODE_MODELS")
codex_key=$(get_env_file_value "$env_file" "TUZI_CODEX_API_KEY")
codex_model=$(get_env_file_value "$env_file" "TUZI_CODEX_MODEL")
codex_models=$(get_env_file_value "$env_file" "TUZI_CODEX_MODELS")
gac_key=$(get_env_file_value "$env_file" "GACCODE_API_KEY")
gac_claude_model=$(get_env_file_value "$env_file" "GAC_CLAUDE_MODEL")
gac_claude_models=$(get_env_file_value "$env_file" "GAC_CLAUDE_MODELS")
gac_codex_model=$(get_env_file_value "$env_file" "GAC_CODEX_MODEL")
gac_codex_models=$(get_env_file_value "$env_file" "GAC_CODEX_MODELS")
echo -e "${CYAN}当前 Tuzi 配置:${NC}"
if [ -n "$current_group" ]; then
echo -e " 当前 Tuzi Provider: ${WHITE}$current_group${NC}"
echo -e " 当前 Tuzi 模型: ${WHITE}$(get_tuzi_model_name_from_ref "$current_model_ref")${NC}"
elif [ -n "$current_model_ref" ]; then
echo -e " 当前默认模型不属于 Tuzi: ${WHITE}$current_model_ref${NC}"
fi
if [ -n "$claude_key" ] && [ -n "$claude_model" ]; then
echo -e " Claude-Code: ${GREEN}已配置${NC}"
echo -e " 主模型: ${WHITE}$claude_model${NC}"
[ -n "$claude_models" ] && echo -e " 已选模型: ${WHITE}$claude_models${NC}"
elif [ -n "$claude_key" ]; then
echo -e " Claude-Code: ${YELLOW}未完成配置${NC}"
else
echo -e " Claude-Code: ${GRAY}(未配置)${NC}"
fi
if [ -n "$codex_key" ] && [ -n "$codex_model" ]; then
echo -e " Codex: ${GREEN}已配置${NC}"
echo -e " 主模型: ${WHITE}$codex_model${NC}"
[ -n "$codex_models" ] && echo -e " 已选模型: ${WHITE}$codex_models${NC}"
elif [ -n "$codex_key" ]; then
echo -e " Codex: ${YELLOW}未完成配置${NC}"
else
echo -e " Codex: ${GRAY}(未配置)${NC}"
fi
if [ -n "$gac_key" ] && [ -n "$gac_claude_model" ] && [ -n "$gac_codex_model" ]; then
echo -e " GACCode: ${GREEN}已配置${NC}"
echo -e " GAC Claude 主模型: ${WHITE}$gac_claude_model${NC}"
[ -n "$gac_claude_models" ] && echo -e " GAC Claude 模型: ${WHITE}$gac_claude_models${NC}"
echo -e " GAC Codex 主模型: ${WHITE}$gac_codex_model${NC}"
[ -n "$gac_codex_models" ] && echo -e " GAC Codex 模型: ${WHITE}$gac_codex_models${NC}"
elif [ -n "$gac_key" ]; then
echo -e " GACCode: ${YELLOW}未完成配置${NC}"
else
echo -e " GACCode: ${GRAY}(未配置)${NC}"
fi
}
detect_install_mode() {
if [ -n "$INSTALL_MODE" ]; then
return 0
fi
if is_openclaw_ready; then
echo -e "${CYAN}检测到本机已经安装 OpenClaw。${NC}"
echo ""
echo " 1) 继续完整安装/升级流程"
echo " 2) 只修改配置,接入或更新 Tuzi API"
echo ""
read_valid_number_choice "${YELLOW}请选择 [1-2] (默认: 2): ${NC}" 1 2 2 install_choice
case "$install_choice" in
1)
INSTALL_MODE="full-install"
FORCE_REINSTALL="true"
;;
*) INSTALL_MODE="tuzi-only" ;;
esac
else
INSTALL_MODE="full-install"
fi
}
get_shell_rc() {
local shell_name
shell_name=$(basename "${SHELL:-}")
if [ "$shell_name" = "zsh" ]; then
echo "$HOME/.zshrc"
elif [ "$shell_name" = "bash" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.zshrc" ]; then
echo "$HOME/.zshrc"
elif [ -f "$HOME/.bashrc" ]; then
echo "$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
echo "$HOME/.bash_profile"
else
echo "$HOME/.bashrc"
fi
}
ensure_path_export() {
local export_line="$1"
local shell_rc
shell_rc=$(get_shell_rc)
if [ -n "$shell_rc" ]; then
touch "$shell_rc"
if ! grep -Fq "$export_line" "$shell_rc" 2>/dev/null; then
echo "" >> "$shell_rc"
echo "# OpenClaw PATH" >> "$shell_rc"
echo "$export_line" >> "$shell_rc"
fi
fi
}
print_path_activation_hint() {
local npm_bin="$1"
local shell_rc
shell_rc=$(get_shell_rc)
log_info "已安装到用户目录: ${npm_bin%/bin}"
log_info "PATH 配置已写入: $shell_rc"
echo ""
echo -e "${YELLOW}提示:${NC} 安装脚本无法直接修改你当前外层终端的 PATH。"
echo "请执行下面任一命令后再使用 openclaw:"
echo " source \"$shell_rc\""
echo " export PATH=\"$npm_bin:\$PATH\""
}
install_homebrew() {
if ! check_command brew; then
log_step "安装 Homebrew..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 添加到 PATH
if [[ -f /opt/homebrew/bin/brew ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
elif [[ -f /usr/local/bin/brew ]]; then
eval "$(/usr/local/bin/brew shellenv)"
fi
fi
}
install_nodejs() {
log_step "检查 Node.js..."
if check_command node; then
local node_version=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$node_version" -ge "$MIN_NODE_VERSION" ]; then
log_info "Node.js 版本满足要求: $(node -v)"
return 0
else
log_warn "Node.js 版本过低: $(node -v),需要 v$MIN_NODE_VERSION+"
fi
fi
log_step "安装 Node.js $MIN_NODE_VERSION..."
case "$OS" in
macos)
install_homebrew
brew install node@22
brew link --overwrite node@22
;;
ubuntu|debian)
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
;;
centos|rhel|fedora)
curl -fsSL https://rpm.nodesource.com/setup_22.x | sudo bash -
sudo yum install -y nodejs
;;
arch|manjaro)
sudo pacman -S nodejs npm --noconfirm
;;
*)
log_error "无法自动安装 Node.js,请手动安装 v$MIN_NODE_VERSION+"
exit 1
;;
esac
log_info "Node.js 安装完成: $(node -v)"
}
install_git() {
if ! check_command git; then
log_step "安装 Git..."
case "$OS" in
macos)
install_homebrew
brew install git
;;
ubuntu|debian)
sudo apt-get update && sudo apt-get install -y git
;;
centos|rhel|fedora)
sudo yum install -y git
;;
arch|manjaro)
sudo pacman -S git --noconfirm
;;
esac
fi
log_info "Git 版本: $(git --version)"
}
install_dependencies() {
log_step "检查并安装依赖..."
# 安装基础依赖
case "$OS" in
ubuntu|debian)
sudo apt-get update
sudo apt-get install -y curl wget jq
;;
centos|rhel|fedora)
sudo yum install -y curl wget jq
;;
macos)
install_homebrew
brew install curl wget jq
;;
esac
install_git
install_nodejs
}
# ================================ OpenClaw 安装 ================================
create_directories() {
log_step "创建配置目录..."
mkdir -p "$CONFIG_DIR"
log_info "配置目录: $CONFIG_DIR"
}
install_openclaw() {
log_step "安装 OpenClaw..."
# 检查是否已安装
if check_command openclaw; then
local current_version=$(openclaw --version 2>/dev/null || echo "unknown")
log_warn "OpenClaw 已安装 (版本: $current_version)"
if [ "$FORCE_REINSTALL" != "true" ] && ! confirm "是否重新安装/更新?"; then
init_openclaw_config
return 0
fi
fi
# 优先尝试 npm 全局安装,失败时回退到用户目录安装
log_info "正在从 npm 安装 OpenClaw..."
if npm install -g openclaw@$OPENCLAW_VERSION --unsafe-perm; then
log_info "已完成全局安装"
else
log_warn "全局安装失败,正在切换到用户目录安装..."
local npm_prefix="$HOME/.local"
local npm_bin="$npm_prefix/bin"
mkdir -p "$npm_prefix"
if npm install -g openclaw@$OPENCLAW_VERSION --unsafe-perm --prefix "$npm_prefix"; then
ensure_path_export "export PATH=\"$npm_bin:\$PATH\""
print_path_activation_hint "$npm_bin"
else
log_error "OpenClaw 安装失败"
echo ""
echo -e "${YELLOW}可尝试以下方案:${NC}"
echo " 1. 使用 sudo 重新运行安装脚本"