-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathai_install.sh
More file actions
executable file
·1297 lines (1112 loc) · 43.6 KB
/
ai_install.sh
File metadata and controls
executable file
·1297 lines (1112 loc) · 43.6 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
# WHartTest AI 智能安装助手
# 纯 Bash 实现,无需 Python 环境
if [ -z "${BASH_VERSION:-}" ]; then
echo "This script requires bash. Please run: bash ai_install.sh" >&2
exit 1
fi
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
# 全局变量
API_URL=""
API_KEY=""
MODEL_NAME=""
SYSTEM_PROMPT=""
ASSUME_YES=0
APPROVE_ALL=0
LOG_DIR="${AI_INSTALL_LOG_DIR:-data/logs}"
LOG_FILE=""
DEBUG_LOG="${AI_INSTALL_DEBUG:-0}"
MAX_TOKENS="${AI_INSTALL_MAX_TOKENS:-30000}"
TEMPERATURE="${AI_INSTALL_TEMPERATURE:-0.7}"
INCLUDE_TEMPERATURE="${AI_INSTALL_INCLUDE_TEMPERATURE:-1}"
TIMEOUT="${AI_INSTALL_TIMEOUT:-120}"
CONNECT_TIMEOUT="${AI_INSTALL_CONNECT_TIMEOUT:-30}"
STREAM_MODE="${AI_INSTALL_STREAM:-1}"
# 临时目录(兼容 Linux/macOS/Git Bash/WSL)
TMP_DIR="${TMPDIR:-/tmp}"
if [ ! -d "$TMP_DIR" ] || [ ! -w "$TMP_DIR" ]; then
TMP_DIR="/tmp"
fi
if [ ! -d "$TMP_DIR" ] || [ ! -w "$TMP_DIR" ]; then
TMP_DIR="."
fi
CONVERSATION_FILE="$TMP_DIR/ai_install_conversation_$$.json"
# sed 扩展正则开关(macOS/GNU: -E,busybox 常见: -r)
SED_EXTENDED_FLAG="-E"
if ! printf 'x' | sed -E 's/x/x/' >/dev/null 2>&1; then
SED_EXTENDED_FLAG="-r"
fi
# 清理函数
cleanup() {
rm -f "$CONVERSATION_FILE"
}
trap cleanup EXIT
# 打印带颜色的消息
print_color() {
local color=$1
shift
printf '%b%b%b\n' "$color" "$*" "$NC"
}
# 生成不带换行的彩色提示词(用于 read -p)
prompt_color() {
local color=$1
shift
printf '%b%b%b' "$color" "$*" "$NC"
}
# 打印标题
print_header() {
printf '\n'
print_color "$CYAN" "=========================================="
print_color "$BOLD$CYAN" "$1"
print_color "$CYAN" "=========================================="
printf '\n'
}
# 初始化日志文件(默认写入 data/logs)
init_log() {
local ts
ts=$(date '+%Y%m%d_%H%M%S' 2>/dev/null || echo "unknown_time")
mkdir -p "$LOG_DIR" 2>/dev/null || true
LOG_FILE="$LOG_DIR/ai_install_${ts}_$$.log"
# 如果目标目录不可写,回退到 /tmp
if ! ( : > "$LOG_FILE" ) 2>/dev/null; then
LOG_DIR="$TMP_DIR"
LOG_FILE="$TMP_DIR/ai_install_${ts}_$$.log"
: > "$LOG_FILE" 2>/dev/null || true
fi
}
log_line() {
local level="$1"
shift
local ts
ts=$(date '+%F %T' 2>/dev/null || echo "unknown_time")
printf '[%s] [%s] %s\n' "$ts" "$level" "$(redact_secrets "$*")" >> "$LOG_FILE" 2>/dev/null || true
}
# 获取系统信息
get_system_info() {
local os_type=$(uname -s)
local os_version=$(uname -r)
local arch=$(uname -m)
echo "操作系统: $os_type $os_version"
echo "架构: $arch"
echo "工作目录: $(pwd)"
}
# 构建系统提示词
build_system_prompt() {
local sys_info=$(get_system_info)
SYSTEM_PROMPT="你是一个专业的项目安装助手。你正在帮助用户安装项目。
当前环境信息:
$sys_info
你的职责:
1. 理解用户的安装需求和环境
2. 提供清晰的安装步骤指导
3. 当需要执行命令时,把命令放在回复的**最后一行**
4. 解释每个步骤的作用
5. 处理可能出现的错误
**命令输出格式(必须严格遵守)**:
- 命令必须单独占一行,放在回复的**最后**
- 格式:{\"command\":\"命令\"}
- JSON 必须是单行对象,且只包含一个 command 字段
- 命令必须是纯 shell 命令,不能包含中文
- 每次只能输出一个命令
- 先写说明,最后一行写命令
正确格式示例:
\`\`\`
让我检查一下 Docker 版本。
{\"command\":\"docker --version\"}
\`\`\`
错误格式(禁止):
\`\`\`
{\"command\":\"docker --version\"}让我检查版本
\`\`\`
\`\`\`
让我{\"command\":\"docker --version\"}检查一下
\`\`\`
安全规则:
- 只读命令(查看、检查):可以直接执行
- 修改性操作(安装、删除、修改配置):必须先询问用户同意
- 脚本会对敏感命令进行二次确认
安装建议:
- 优先检测环境,推荐合适的安装方式
- 有 Docker 时推荐 Docker 安装
- 无 Docker 时指导手动安装
- Docker 拉取慢时优先配置 Docker daemon 镜像或私有仓库,避免依赖不稳定的公共代理
示例对话 1:
用户:帮我检查环境
你:好的,让我检查一下 Docker 是否已安装。
{\"command\":\"docker --version\"}
示例对话 2:
用户:docs 目录里有什么
你:让我查看 docs 目录的内容。
{\"command\":\"ls -la docs\"}
示例对话 3:
用户:帮我安装 Docker
你:检测到您未安装 Docker。是否允许我为您安装?
用户:可以
你:好的,正在为您安装 Docker。
{\"command\":\"curl -fsSL https://get.docker.com | sh\"}
现在,请开始与用户对话,了解他们的需求。"
}
# 从环境变量或 .env 文件加载配置
load_config_from_env() {
local env_file="${AI_INSTALL_ENV_FILE:-.env}"
# 如果存在 .env 文件,先加载它
if [ -f "$env_file" ]; then
print_color "$CYAN" "📄 检测到 $env_file 文件,正在加载配置..."
# 兼容 Windows/Git Bash:自动去掉 CRLF 的 \r,避免变量带回车导致 curl/grep 等异常
local sanitized_env="$TMP_DIR/ai_install_env_$$.tmp"
sed 's/\r$//' "$env_file" > "$sanitized_env"
# 使用 source 加载(更可靠)
set -a
source "$sanitized_env"
set +a
rm -f "$sanitized_env"
fi
# 从环境变量读取配置
if [ -n "$AI_API_URL" ]; then
API_URL="$AI_API_URL"
print_color "$GREEN" "✅ 从环境变量读取 API_URL: $API_URL"
fi
if [ -n "$AI_API_KEY" ]; then
API_KEY="$AI_API_KEY"
print_color "$GREEN" "✅ 从环境变量读取 API_KEY: ${API_KEY:0:10}..."
fi
if [ -n "$AI_MODEL" ]; then
MODEL_NAME="$AI_MODEL"
print_color "$GREEN" "✅ 从环境变量读取 MODEL: $MODEL_NAME"
fi
if [ -n "${AI_INSTALL_ASSUME_YES:-}" ]; then
ASSUME_YES="$AI_INSTALL_ASSUME_YES"
if [ "$ASSUME_YES" = "1" ]; then
print_color "$YELLOW" "⚠️ 已启用 AI_INSTALL_ASSUME_YES=1:将自动同意执行确认命令(不推荐)"
fi
fi
}
# 脱敏敏感信息(避免把密钥/令牌发给 AI)
redact_secrets() {
local text="$1"
printf '%s' "$text" | sed "$SED_EXTENDED_FLAG" \
-e 's/(AI_API_KEY=).*/\1***REDACTED***/g' \
-e 's/(OPENAI_API_KEY=).*/\1***REDACTED***/g' \
-e 's/(DEEPSEEK_API_KEY=).*/\1***REDACTED***/g' \
-e 's/(DASHSCOPE_API_KEY=).*/\1***REDACTED***/g' \
-e 's/(Authorization: Bearer )[A-Za-z0-9._-]+/\1***REDACTED***/g' \
-e 's/(Bearer )[A-Za-z0-9._-]+/\1***REDACTED***/g' \
-e 's/sk-[A-Za-z0-9]{10,}/sk-***REDACTED***/g'
}
# 判断命令是否需要用户确认(修改性/敏感信息)
command_needs_confirmation() {
local cmd="$1"
cmd="$(echo "$cmd" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')"
[ -z "$cmd" ] && return 1
# 多条命令或复杂连接,保守起见需要确认
if echo "$cmd" | grep -Eq '(^|[^\\])[;&]|&&|\|\|'; then
return 0
fi
# 输出重定向/覆盖文件
if echo "$cmd" | grep -Eq '(^|[^\\])>>?|(^|[^\\])2>>?|(^|[^\\])&>|(^|[^\\])\|[[:space:]]*tee([[:space:]]|$)'; then
return 0
fi
# 可能泄露敏感信息的只读命令:执行后输出会被回传给 AI
if echo "$cmd" | grep -Eq '(^|[[:space:]])(env|printenv)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eq '(^|[[:space:]])cat[[:space:]]+(\.env|.*\.env)([[:space:]]|$)'; then
return 0
fi
# 明确的修改性/安装类命令
if echo "$cmd" | grep -Eiq '(^|[[:space:]])(sudo|su)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])(rm|mv|cp|mkdir|rmdir|chmod|chown|chgrp|ln|truncate|dd)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])(apt|apt-get|yum|dnf|pacman|brew)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])(pip|pip3|poetry|npm|pnpm|yarn)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])(git)([[:space:]]+)(clone|checkout|switch|pull|push|reset|clean|rebase|merge|commit|tag)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])docker([[:space:]]+)(run|build|pull|push|rm|rmi|volume|network)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])docker([[:space:]]+)compose([[:space:]]+)(up|down|build|pull|push|rm)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])docker-compose([[:space:]]+)(up|down|build|pull|push|rm|start|stop|restart)([[:space:]]|$)'; then
return 0
fi
if echo "$cmd" | grep -Eiq '(^|[[:space:]])(systemctl|service)([[:space:]]|$)'; then
return 0
fi
return 1
}
# 用户拒绝命令时的反馈(全局变量)
USER_REJECT_FEEDBACK=""
confirm_command() {
local cmd="$1"
USER_REJECT_FEEDBACK=""
if [ "${APPROVE_ALL:-0}" = "1" ] || [ "${ASSUME_YES:-0}" = "1" ]; then
return 0
fi
if [ ! -t 0 ]; then
print_color "$YELLOW" "⚠️ 非交互模式:已跳过需要确认的命令:$cmd" >&2
return 1
fi
echo "" >&2
print_color "$YELLOW" "⚠️ 该命令可能会修改系统或泄露敏感信息:" >&2
print_color "$BOLD$YELLOW" " $cmd" >&2
local choice=""
read -r -p "$(prompt_color "$GREEN" "是否允许执行?[y]允许 [n]拒绝 [a]本次全部允许 (默认 n): ")" choice || true
case "$choice" in
y|Y|yes|YES)
return 0
;;
a|A)
APPROVE_ALL=1
print_color "$YELLOW" "⚠️ 已选择"本次全部允许",后续将不再逐条确认(请谨慎)" >&2
return 0
;;
*)
print_color "$CYAN" "已拒绝执行该命令。" >&2
# 询问用户是否有修正建议
local feedback=""
read -r -p "$(prompt_color "$CYAN" "请输入修正建议(直接回车跳过): ")" feedback || true
if [ -n "$feedback" ]; then
USER_REJECT_FEEDBACK="$feedback"
fi
return 1
;;
esac
}
# 获取可用模型列表
fetch_models() {
print_color "$YELLOW" "\n🔍 正在获取可用模型列表..."
local response=$(curl -s -w "\n%{http_code}" --connect-timeout "$CONNECT_TIMEOUT" --max-time "$TIMEOUT" -X GET "$API_URL/models" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" 2>/dev/null)
local http_code=$(echo "$response" | tail -n1)
local body=$(echo "$response" | head -n -1)
if [ "$http_code" = "200" ]; then
# 提取模型 ID 列表(尝试多种 JSON 格式)
local models=$(echo "$body" | grep -o '"id":"[^"]*"' | sed 's/"id":"//;s/"$//' | head -20)
if [ -n "$models" ]; then
print_color "$GREEN" "✅ 获取到以下可用模型:\n"
# 显示模型列表并编号
local index=1
local model_array=()
while IFS= read -r model; do
if [ -n "$model" ]; then
print_color "$CYAN" " [$index] $model"
model_array+=("$model")
((index++))
fi
done <<< "$models"
# 让用户选择
printf '\n'
read -r -p "$(prompt_color "$GREEN" "请选择模型编号 (或直接输入模型名称): ")" user_choice
# 判断是数字还是模型名
if [[ "$user_choice" =~ ^[0-9]+$ ]] && [ "$user_choice" -ge 1 ] && [ "$user_choice" -lt "$index" ]; then
MODEL_NAME="${model_array[$((user_choice-1))]}"
print_color "$GREEN" "✅ 已选择模型: $MODEL_NAME"
elif [ -n "$user_choice" ]; then
MODEL_NAME="$user_choice"
print_color "$GREEN" "✅ 已设置模型: $MODEL_NAME"
else
print_color "$RED" "❌ 无效的选择"
return 1
fi
return 0
fi
fi
# 如果获取失败,提示用户手动输入
print_color "$YELLOW" "⚠️ 无法自动获取模型列表"
echo ""
print_color "$CYAN" "常用模型名称参考:"
print_color "$BLUE" " - OpenAI: gpt-4, gpt-4-turbo, gpt-3.5-turbo"
print_color "$BLUE" " - DeepSeek: deepseek-chat, deepseek-coder"
print_color "$BLUE" " - 通义千问: qwen-turbo, qwen-plus, qwen-max"
print_color "$BLUE" " - 硅基流动: Qwen/Qwen2.5-7B-Instruct"
print_color "$BLUE" " - Ollama: qwen2.5:7b, llama3.1:8b"
echo ""
while [ -z "$MODEL_NAME" ]; do
read -r -p "$(prompt_color "$GREEN" "请手动输入模型名称: ")" MODEL_NAME
if [ -z "$MODEL_NAME" ]; then
print_color "$RED" "❌ 模型名称不能为空!"
fi
done
return 0
}
# 配置 API
setup_api() {
print_header "🤖 AI 智能安装助手"
# 先尝试从环境变量加载配置
load_config_from_env
# 如果环境变量中已有完整配置(包括模型),直接使用
if [ -n "$API_URL" ] && [ -n "$API_KEY" ] && [ -n "$MODEL_NAME" ]; then
echo ""
print_color "$GREEN" "✅ 使用环境变量配置:"
print_color "$CYAN" " API_URL: $API_URL"
print_color "$CYAN" " API_KEY: ${API_KEY:0:10}..."
print_color "$CYAN" " MODEL: $MODEL_NAME"
echo ""
# 测试连接
print_color "$YELLOW" "🔍 正在测试 API 连接..."
if test_api_connection; then
print_color "$GREEN" "✅ API 连接成功!\n"
return 0
else
print_color "$RED" "❌ API 连接失败,将转为手动配置...\n"
API_URL=""
API_KEY=""
MODEL_NAME=""
fi
fi
print_color "$YELLOW" "请配置 AI API(支持 OpenAI 格式的所有接口)"
echo ""
print_color "$CYAN" "💡 提示:可以创建 .env 文件预设配置(AI_API_URL, AI_API_KEY, AI_MODEL)"
echo ""
print_color "$CYAN" "示例 API 地址:"
print_color "$BLUE" " - OpenAI: https://api.openai.com/v1"
print_color "$BLUE" " - DeepSeek: https://api.deepseek.com/v1"
print_color "$BLUE" " - 通义千问: https://dashscope.aliyuncs.com/compatible-mode/v1"
print_color "$BLUE" " - 硅基流动: https://api.siliconflow.cn/v1"
print_color "$BLUE" " - Ollama本地: http://localhost:11434/v1"
echo ""
# 获取 API URL(如果环境变量中没有)
if [ -z "$API_URL" ]; then
while [ -z "$API_URL" ]; do
read -r -p "$(prompt_color "$GREEN" "请输入 API 地址: ")" API_URL
if [ -z "$API_URL" ]; then
print_color "$RED" "❌ API 地址不能为空!"
fi
done
fi
# 获取 API Key(如果环境变量中没有)
if [ -z "$API_KEY" ]; then
while [ -z "$API_KEY" ]; do
read -r -p "$(prompt_color "$GREEN" "请输入 API Key: ")" API_KEY
if [ -z "$API_KEY" ]; then
print_color "$RED" "❌ API Key 不能为空!"
fi
done
fi
# 获取模型(如果环境变量中没有)
if [ -z "$MODEL_NAME" ]; then
if ! fetch_models; then
API_URL=""
API_KEY=""
MODEL_NAME=""
return 1
fi
fi
# 测试连接
print_color "$YELLOW" "\n🔍 正在测试 API 连接..."
if test_api_connection; then
print_color "$GREEN" "✅ API 连接成功!\n"
return 0
else
print_color "$RED" "❌ API 连接失败,请检查配置!\n"
API_URL=""
API_KEY=""
MODEL_NAME=""
return 1
fi
}
# 测试 API 连接
test_api_connection() {
local payload="{\"model\":\"$MODEL_NAME\",\"messages\":[{\"role\":\"user\",\"content\":\"hi\"}],\"max_tokens\":10}"
local payload_file="$TMP_DIR/ai_install_test_payload_$$.json"
printf '%s' "$payload" > "$payload_file"
local response=$(curl -sS -w "\n%{http_code}" --connect-timeout "$CONNECT_TIMEOUT" --max-time "$TIMEOUT" -X POST "$API_URL/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-H "Expect:" \
--data-binary @"$payload_file" 2>/dev/null)
rm -f "$payload_file" 2>/dev/null || true
local http_code=$(echo "$response" | tail -n1)
if [ "$http_code" = "200" ]; then
return 0
else
return 1
fi
}
# 去除 ANSI 颜色代码
strip_ansi() {
if command -v perl >/dev/null 2>&1; then
# 覆盖 CSI/OSC/DCS 以及单字符 ESC 序列;尽量保留可读文本
# 参考:ECMA-48 / ANSI escape sequences
printf '%s' "$1" | perl -pe '
s/\e\[[0-?]*[ -\/]*[@-~]//g; # CSI ... Cmd
s/\e\][^\a]*(?:\a|\e\\)//g; # OSC ... (BEL | ST)
s/\eP.*?\e\\//gs; # DCS ... ST
s/\e[@-Z\\-_]//g; # 2-char sequences
'
else
local esc
esc=$(printf '\033')
# sed 版本差异较大:这里尽量只做“足够安全”的处理——至少去掉 ESC 本身,避免 JSON 控制字符报错
printf '%s' "$1" \
| sed "s/${esc}\[[0-9;?]*[ -\\/]*[@-~]//g" \
| sed "s/${esc}//g"
fi
}
# 去除剩余控制字符(避免 JSON 中出现非法控制字符)
strip_control_chars() {
if command -v perl >/dev/null 2>&1; then
printf '%s' "$1" | perl -pe 's/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]//g'
else
# 保留 \n \r \t(后续会转义);其余 C0 控制字符直接删除
printf '%s' "$1" | tr -d '\000-\010\013\014\016-\037\177'
fi
}
# JSON 转义函数
json_escape() {
local string="$1"
# 先去除 ANSI 颜色代码
string=$(strip_ansi "$string")
# 再去除剩余控制字符
string=$(strip_control_chars "$string")
# 转义特殊字符
string="${string//\\/\\\\}" # 反斜杠
string="${string//\"/\\\"}" # 双引号
string="${string//$'\n'/\\n}" # 换行符
string="${string//$'\r'/\\r}" # 回车符
string="${string//$'\t'/\\t}" # 制表符
printf '%s' "$string"
}
# 从 OpenAI 兼容 JSON 响应中提取 assistant content(优先使用 jq/python/perl,最后才回退正则)
extract_ai_content() {
local body="$1"
local out=""
if command -v jq >/dev/null 2>&1; then
out=$(printf '%s' "$body" | jq -r '(.choices[0].message.content // .choices[0].text // empty)' 2>/dev/null || true)
[ "$out" = "null" ] && out=""
if [ -n "$out" ]; then
printf '%s' "$out"
return 0
fi
fi
if command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1; then
local py
py=$(command -v python3 2>/dev/null || command -v python 2>/dev/null)
out=$(printf '%s' "$body" | "$py" -c 'import json,sys
try:
data=json.load(sys.stdin)
except Exception:
sys.exit(0)
choices=data.get("choices") or []
content=""
if choices:
c=choices[0] or {}
m=c.get("message") or {}
content=m.get("content") or c.get("text") or ""
if isinstance(content,str):
sys.stdout.write(content)
' 2>/dev/null || true)
if [ -n "$out" ]; then
printf '%s' "$out"
return 0
fi
fi
if command -v perl >/dev/null 2>&1; then
out=$(printf '%s' "$body" | perl -MJSON::PP -0777 -ne '
my $txt = $_;
my $data;
eval { $data = decode_json($txt); 1 } or exit 0;
my $choices = $data->{choices} || [];
my $content = "";
if (@$choices) {
my $c0 = $choices->[0] || {};
my $m = $c0->{message} || {};
$content = $m->{content} // $c0->{text} // "";
}
print $content if defined $content;
' 2>/dev/null || true)
if [ -n "$out" ]; then
printf '%s' "$out"
return 0
fi
fi
# 最后回退:尽量匹配 JSON 字符串(支持转义)
out=$(printf '%s' "$body" | sed -n 's/.*"content"[[:space:]]*:[[:space:]]*"\([^"\\]*\(\\"[^"\\]*\)*\)".*/\1/p' | head -n1 | sed 's/\\n/\n/g;s/\\"/"/g;s/\\\\/\\/g' || true)
printf '%s' "$out"
}
# 从 SSE 流式响应的单行中提取 delta.content(优化:优先用纯 bash,避免频繁启动外部进程)
extract_stream_delta() {
local line="$1"
# 跳过空行和非 data 行
[[ ! "$line" =~ ^data:\ *.+ ]] && return 0
# 去掉 "data: " 前缀
local json="${line#data: }"
# 处理 [DONE] 信号
[ "$json" = "[DONE]" ] && return 0
# 快速路径:用 bash 正则直接提取 "content":"..."
# 匹配模式:delta 后面的 content 字段
if [[ "$json" =~ \"content\":\"([^\"\\]*)\" ]]; then
printf '%s' "${BASH_REMATCH[1]}"
return 0
fi
# 处理 content 为 null 或不存在的情况
if [[ "$json" =~ \"content\":null ]] || [[ ! "$json" =~ \"content\" ]]; then
return 0
fi
# 复杂情况(含转义字符):回退到 jq
if command -v jq >/dev/null 2>&1; then
local out
out=$(printf '%s' "$json" | jq -r '(.choices[0].delta.content // empty)' 2>/dev/null || true)
[ "$out" = "null" ] && out=""
printf '%s' "$out"
fi
}
extract_error_message() {
local body="$1"
local out=""
if command -v jq >/dev/null 2>&1; then
out=$(printf '%s' "$body" | jq -r '(.error.message // .message // .msg // .error // empty)' 2>/dev/null || true)
[ "$out" = "null" ] && out=""
if [ -n "$out" ]; then
printf '%s' "$out"
return 0
fi
fi
if command -v python3 >/dev/null 2>&1 || command -v python >/dev/null 2>&1; then
local py
py=$(command -v python3 2>/dev/null || command -v python 2>/dev/null)
out=$(printf '%s' "$body" | "$py" -c 'import json,sys
try:
data=json.load(sys.stdin)
except Exception:
sys.exit(0)
err=data.get("error") or {}
msg=err.get("message") if isinstance(err,dict) else None
msg = msg or data.get("message") or data.get("msg") or data.get("error") or ""
if isinstance(msg,str):
sys.stdout.write(msg)
' 2>/dev/null || true)
if [ -n "$out" ]; then
printf '%s' "$out"
return 0
fi
fi
if command -v perl >/dev/null 2>&1; then
out=$(printf '%s' "$body" | perl -MJSON::PP -0777 -ne '
my $txt = $_;
my $data;
eval { $data = decode_json($txt); 1 } or exit 0;
my $msg = "";
if (ref($data->{error}) eq "HASH" && defined $data->{error}{message}) { $msg = $data->{error}{message}; }
elsif (defined $data->{message}) { $msg = $data->{message}; }
elsif (defined $data->{msg}) { $msg = $data->{msg}; }
elsif (defined $data->{error} && !ref($data->{error})) { $msg = $data->{error}; }
print $msg if defined $msg;
' 2>/dev/null || true)
if [ -n "$out" ]; then
printf '%s' "$out"
return 0
fi
fi
out=$(printf '%s' "$body" | grep -o '"message":"[^"]*"' | head -1 | sed 's/"message":"//;s/"$//' || true)
printf '%s' "$out"
}
# 调用 AI API
call_ai() {
local user_message="$1"
local sanitized_user_message
sanitized_user_message="$(redact_secrets "$user_message")"
# 参数校验/兼容(部分 OpenAI 兼容接口对 temperature/max_tokens 很敏感)
if ! [[ "$MAX_TOKENS" =~ ^[0-9]+$ ]]; then
MAX_TOKENS=1024
fi
if ! [[ "$TEMPERATURE" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
TEMPERATURE="0.7"
fi
# 构建消息数组
local messages="[{\"role\":\"system\",\"content\":\"$(json_escape "$SYSTEM_PROMPT")\"}"
# 读取历史对话
if [ -f "$CONVERSATION_FILE" ]; then
local history=$(cat "$CONVERSATION_FILE")
if [ -n "$history" ]; then
messages="$messages,$history"
fi
fi
# 添加当前用户消息
messages="$messages,{\"role\":\"user\",\"content\":\"$(json_escape "$sanitized_user_message")\"}]"
# 构建 payload(根据 STREAM_MODE 决定是否启用流式)
local stream_flag="false"
[ "$STREAM_MODE" = "1" ] && stream_flag="true"
local payload=""
if [ "${INCLUDE_TEMPERATURE:-1}" = "1" ]; then
payload="{
\"model\": \"$MODEL_NAME\",
\"messages\": $messages,
\"temperature\": $TEMPERATURE,
\"max_tokens\": $MAX_TOKENS,
\"stream\": $stream_flag
}"
else
payload="{
\"model\": \"$MODEL_NAME\",
\"messages\": $messages,
\"max_tokens\": $MAX_TOKENS,
\"stream\": $stream_flag
}"
fi
local payload_file="$TMP_DIR/ai_install_payload_$$.json"
printf '%s' "$payload" > "$payload_file"
if [ -n "$LOG_FILE" ] && [ "$DEBUG_LOG" = "1" ]; then
log_line "DEBUG" "AI request: url=$API_URL/chat/completions model=$MODEL_NAME stream=$stream_flag payload=$(redact_secrets "$(cat "$payload_file")")"
fi
local ai_message=""
# 流式模式
if [ "$STREAM_MODE" = "1" ]; then
local stream_error=""
local http_code=""
local temp_response="$TMP_DIR/ai_install_stream_$$.tmp"
# 使用 curl 流式读取,--no-buffer 确保实时输出
curl -sS --no-buffer --connect-timeout "$CONNECT_TIMEOUT" --max-time "$TIMEOUT" \
-w "\n__HTTP_CODE__:%{http_code}" \
-X POST "$API_URL/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-H "Expect:" \
--data-binary @"$payload_file" 2>/dev/null > "$temp_response"
rm -f "$payload_file" 2>/dev/null || true
# 提取 HTTP 状态码
http_code=$(grep "__HTTP_CODE__:" "$temp_response" 2>/dev/null | tail -n1 | sed 's/.*__HTTP_CODE__://')
[ -z "$http_code" ] && http_code="000"
if [ "$http_code" != "200" ]; then
local body=$(grep -v "__HTTP_CODE__:" "$temp_response" 2>/dev/null | tr '\n' ' ')
rm -f "$temp_response" 2>/dev/null || true
print_color "$RED" "❌ AI 调用失败"
local error_msg=$(extract_error_message "$body")
if [ -n "$error_msg" ]; then
print_color "$YELLOW" "错误信息: $error_msg"
else
print_color "$YELLOW" "HTTP 状态码: $http_code"
fi
log_line "ERROR" "AI stream call failed: http_code=$http_code body=$body"
print_color "$CYAN" "日志已写入: $LOG_FILE"
return 1
fi
# 解析流式响应并实时输出
printf '%b' "$CYAN" >&2
while IFS= read -r line || [ -n "$line" ]; do
# 跳过 HTTP 状态码标记行
[[ "$line" =~ __HTTP_CODE__: ]] && continue
# 去除可能的 \r
line="${line%$'\r'}"
# 提取 delta content
local delta=$(extract_stream_delta "$line")
if [ -n "$delta" ]; then
printf '%s' "$delta" >&2
ai_message="${ai_message}${delta}"
fi
done < "$temp_response"
printf '%b\n' "$NC" >&2
rm -f "$temp_response" 2>/dev/null || true
if [ -z "$ai_message" ]; then
print_color "$RED" "❌ AI 调用失败"
log_line "ERROR" "AI stream returned empty content"
print_color "$CYAN" "日志已写入: $LOG_FILE"
return 1
fi
# 非流式模式(原逻辑)
else
local response=$(curl -sS -w "\n%{http_code}" --connect-timeout "$CONNECT_TIMEOUT" --max-time "$TIMEOUT" -X POST "$API_URL/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-H "Expect:" \
--data-binary @"$payload_file" 2>/dev/null)
rm -f "$payload_file" 2>/dev/null || true
local http_code
http_code=$(echo "$response" | tail -n1)
local body
body=$(echo "$response" | head -n -1)
if [ -n "$LOG_FILE" ] && [ "$DEBUG_LOG" = "1" ]; then
log_line "DEBUG" "AI response: http_code=$http_code body=$(echo "$body" | tr '\n' ' ')"
fi
# 兼容回退:部分接口会对 temperature 或较大的 max_tokens 返回 400
if [ "$http_code" = "400" ]; then
local fallback_payload="{
\"model\": \"$MODEL_NAME\",
\"messages\": $messages,
\"max_tokens\": 512
}"
local fallback_payload_file="$TMP_DIR/ai_install_payload_fallback_$$.json"
printf '%s' "$fallback_payload" > "$fallback_payload_file"
if [ -n "$LOG_FILE" ] && [ "$DEBUG_LOG" = "1" ]; then
log_line "DEBUG" "AI fallback request (no temperature, max_tokens=512): payload=$(redact_secrets "$(cat "$fallback_payload_file")")"
fi
local fallback_response
fallback_response=$(curl -sS -w "\n%{http_code}" --connect-timeout "$CONNECT_TIMEOUT" --max-time "$TIMEOUT" -X POST "$API_URL/chat/completions" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-H "Expect:" \
--data-binary @"$fallback_payload_file" 2>/dev/null)
rm -f "$fallback_payload_file" 2>/dev/null || true
local fallback_code
fallback_code=$(echo "$fallback_response" | tail -n1)
local fallback_body
fallback_body=$(echo "$fallback_response" | head -n -1)
if [ -n "$LOG_FILE" ] && [ "$DEBUG_LOG" = "1" ]; then
log_line "DEBUG" "AI fallback response: http_code=$fallback_code body=$(echo "$fallback_body" | tr '\n' ' ')"
fi
if [ "$fallback_code" = "200" ]; then
http_code="$fallback_code"
body="$fallback_body"
fi
fi
if [ "$http_code" != "200" ]; then
print_color "$RED" "❌ AI 调用失败"
local error_msg=""
error_msg=$(extract_error_message "$body")
if [ -n "$error_msg" ]; then
print_color "$YELLOW" "错误信息: $error_msg"
else
print_color "$YELLOW" "HTTP 状态码: $http_code"
fi
log_line "ERROR" "AI call failed: http_code=$http_code url=$API_URL/chat/completions model=$MODEL_NAME body=$(echo "$body" | tr '\n' ' ')"
print_color "$CYAN" "日志已写入: $LOG_FILE"
if [ "$DEBUG_LOG" != "1" ]; then
print_color "$CYAN" "可用 AI_INSTALL_DEBUG=1 重新运行以记录请求 payload(已脱敏)"
fi
return 1
fi
ai_message=$(extract_ai_content "$body")
if [ -z "$ai_message" ]; then
print_color "$RED" "❌ AI 调用失败"
log_line "ERROR" "AI call returned empty content: body=$(echo "$body" | tr '\n' ' ')"
print_color "$CYAN" "日志已写入: $LOG_FILE"
return 1
fi
fi
# 保存对话历史
local user_json="{\"role\":\"user\",\"content\":\"$(json_escape "$sanitized_user_message")\"}"
local assistant_json="{\"role\":\"assistant\",\"content\":\"$(json_escape "$ai_message")\"}"
if [ -f "$CONVERSATION_FILE" ]; then
printf '%s\n' ",$user_json,$assistant_json" >> "$CONVERSATION_FILE"
else
printf '%s\n' "$user_json,$assistant_json" > "$CONVERSATION_FILE"
fi
echo "$ai_message"
}
# 执行命令
execute_command() {
local command="$1"
# 使用紫色背景和白色文字显示命令,使其更醒目
echo "" >&2
echo -e "\033[45;37m ⚡ 执行系统命令 \033[0m \033[1;35m$command\033[0m" >&2
echo -e "\033[0;90m----------------------------------------\033[0m" >&2
# 执行命令并捕获输出(同时显示在终端)
local output
local exit_code
local temp_log="$TMP_DIR/ai_install_cmd_output_$$.log"
local errexit_was_set=0
local pipefail_was_set=0
case "$-" in
*e*) errexit_was_set=1 ;;
esac
if set -o | grep -q '^pipefail[[:space:]]*on$'; then
pipefail_was_set=1
fi
# 使用 tee 实时显示输出并保存到临时文件
# set -o pipefail 确保管道中任何命令失败都返回失败
set +e
set -o pipefail
eval "$command" 2>&1 | tee "$temp_log" >&2
exit_code=${PIPESTATUS[0]}
(( pipefail_was_set )) || set +o pipefail
(( errexit_was_set )) && set -e
# 读取捕获的输出
output=$(cat "$temp_log")
rm -f "$temp_log"
echo -e "\033[0;90m----------------------------------------\033[0m" >&2
if [ $exit_code -eq 0 ]; then
print_color "$GREEN" "✅ 执行成功" >&2
else
print_color "$RED" "❌ 执行失败 (返回码: $exit_code)" >&2
fi
echo "" >&2
# 将结果反馈给 AI(去除 ANSI 颜色代码,防止 JSON 错误)
local clean_output=$(strip_ansi "$output")
clean_output=$(redact_secrets "$clean_output")
local status
if [ $exit_code -eq 0 ]; then
status="成功"
else
status="失败"
fi
local feedback=$'命令执行'"$status"$'。\n输出:\n'"$clean_output"
# 这里的 echo 是为了让调用者(process_ai_response)捕获反馈,而不是打印到屏幕
# 因为 process_ai_response 会调用 call_ai,需要这个反馈作为输入
printf '%s' "$feedback"
}
# 清理候选命令(不做猜测性修复)
sanitize_command_candidate() {
local cmd="$1"
cmd=$(printf '%s' "$cmd" | tr -d '\r')