Files
dock/ai
2025-11-03 18:12:39 +08:00

343 lines
11 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 交互式AI驱动问题修复系统
# 让用户清楚知道运行状态和位置
set -e
# 配置
DEEPSEEK_API_KEY="sk-your-api-key-here" # 请替换为你的真实API密钥
SCRIPT_NAME="interactive_ai_fixer.sh"
INSTALL_DIR="/usr/local/bin"
LOG_FILE="/var/log/ai_interactive_fixer.log"
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
NC='\033[0m'
# 显示运行位置和状态
show_status() {
echo -e "${PURPLE}"
echo "╔══════════════════════════════════════════════════╗"
echo "║ AI驱动交互式修复系统 ║"
echo "╠══════════════════════════════════════════════════╣"
echo "║ 运行位置: $(pwd)"
echo "║ 脚本名称: $0"
echo "║ 系统时间: $(date)"
echo "║ 当前用户: $(whoami)"
echo "║ 系统信息: $(uname -srm)"
echo "╚══════════════════════════════════════════════════╝"
echo -e "${NC}"
echo ""
}
# 日志函数
log() {
local message="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | sudo tee -a "$LOG_FILE"
echo -e "${BLUE}[AI系统]${NC} $message"
}
# 进度显示函数
show_progress() {
local step="$1"
local total="$2"
local message="$3"
local percent=$((step * 100 / total))
echo -e "${CYAN}📊 进度: [$step/$total] $message...${NC}"
echo -e "${YELLOW} ▰▰▰▰▰▰▰▰▰▰ $percent% 完成${NC}"
echo ""
}
# 调用AI分析问题
ask_ai() {
local problem="$1"
local context="$2"
log "🤖 咨询AI解决方案: $problem"
echo -e "${CYAN}🔄 正在连接AI助手分析问题...${NC}"
# 准备API请求
local request_data=$(cat << EOF
{
"model": "deepseek-coder",
"messages": [
{
"role": "system",
"content": "你是一个Linux系统和脚本问题专家。用户遇到了脚本执行问题请直接给出具体的修复命令。用中文回答要具体可执行。"
},
{
"role": "user",
"content": "问题:$problem\n\n上下文$context\n\n请直接给出需要执行的Linux命令"
}
],
"temperature": 0.3,
"max_tokens": 1000
}
EOF
)
# 调用API
echo -e "${YELLOW}📡 发送请求到AI服务器...${NC}"
local response=$(curl -s -X POST "https://api.deepseek.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $DEEPSEEK_API_KEY" \
-d "$request_data" 2>/dev/null || echo "AI请求失败")
# 提取回复
local answer=$(echo "$response" | grep -o '"content":"[^"]*"' | cut -d'"' -f4)
if [ -z "$answer" ] || [ "$answer" = "AI请求失败" ]; then
echo -e "${RED}❌ AI连接失败使用备用方案${NC}"
return 1
fi
echo -e "${GREEN}✅ AI回复接收成功${NC}"
echo "$answer"
}
# 执行命令并显示结果
execute_command() {
local command="$1"
local description="$2"
log "执行: $description"
echo -e "${YELLOW}▶️ 执行: $command${NC}"
echo -e "${BLUE}📝 描述: $description${NC}"
# 显示执行开始时间
local start_time=$(date +%s)
# 执行命令
echo -e "${CYAN}────────────────────────────────────────────${NC}"
eval "$command" 2>&1 | while IFS= read -r line; do
echo " 📋 $line"
done
echo -e "${CYAN}────────────────────────────────────────────${NC}"
local exit_code=${PIPESTATUS[0]}
local end_time=$(date +%s)
local duration=$((end_time - start_time))
if [ $exit_code -eq 0 ]; then
echo -e "${GREEN}✅ 执行成功 (耗时: ${duration}s)${NC}"
else
echo -e "${RED}❌ 执行失败 (退出码: $exit_code, 耗时: ${duration}s)${NC}"
fi
echo ""
return $exit_code
}
# 分析当前问题
analyze_problem() {
show_progress 1 5 "分析系统环境"
echo -e "${PURPLE}🔍 开始系统分析...${NC}"
# 收集系统信息
execute_command "uname -a" "获取系统内核信息"
execute_command "cat /etc/os-release" "获取操作系统信息"
execute_command "python3 --version 2>/dev/null || echo 'Python3 未安装'" "检查Python环境"
execute_command "bash --version | head -1" "检查Bash版本"
show_progress 2 5 "测试问题脚本"
# 测试问题脚本
local test_url="https://github.com/xzx3344521/dock/raw/refs/heads/main/ai"
echo -e "${CYAN}🌐 测试问题脚本URL: $test_url${NC}"
execute_command "curl -s -I '$test_url' | head -1" "检查URL可访问性"
local script_content=$(curl -sSL "$test_url" 2>/dev/null || echo "下载失败")
echo -e "${YELLOW}📄 脚本内容预览:${NC}"
echo "$script_content" | head -10 | while IFS= read -r line; do
echo " 📃 $line"
done
local problem_context="系统分析完成,检测到远程脚本执行问题"
echo "$problem_context"
}
# 主要修复流程
main_repair() {
show_status
echo -e "${GREEN}🚀 启动AI自动修复流程...${NC}"
echo -e "${YELLOW}⚠️ 注意: 整个流程将由AI自动控制${NC}"
echo ""
# 分析问题
local problem_context=$(analyze_problem)
local problem_description="用户执行远程脚本失败,脚本内容被显示而不是执行,出现命令未找到错误"
show_progress 3 5 "咨询AI解决方案"
# 询问AI解决方案
echo -e "${PURPLE}🤖 正在咨询AI助手...${NC}"
local ai_solution=$(ask_ai "$problem_description" "$problem_context")
if [ $? -ne 0 ] || [ -z "$ai_solution" ]; then
echo -e "${RED}❌ AI咨询失败启用备用方案${NC}"
backup_solution
return
fi
show_progress 4 5 "执行AI方案"
echo -e "${GREEN}🤖 AI提供的解决方案:${NC}"
echo -e "${CYAN}$ai_solution${NC}"
echo ""
# 执行AI命令
execute_ai_commands "$ai_solution"
show_progress 5 5 "完成修复"
echo -e "${GREEN}🎉 AI修复流程完成${NC}"
}
# 执行AI返回的命令
execute_ai_commands() {
local solution="$1"
local command_count=0
echo -e "${PURPLE}🔧 开始执行AI建议的命令...${NC}"
while IFS= read -r line; do
# 跳过空行和注释
if [[ -z "$line" || "$line" =~ ^# ]]; then
continue
fi
# 识别命令
if [[ "$line" =~ ^(sudo\s+|apt\s+|curl\s+|wget\s+|python|bash\s+|chmod\s+|mkdir\s+|cd\s+|echo\s+|pip\s+) ]]; then
command_count=$((command_count + 1))
execute_command "$line" "AI建议命令 #$command_count"
fi
done <<< "$solution"
echo -e "${GREEN}✅ 共执行了 $command_count 个AI命令${NC}"
}
# 备用解决方案
backup_solution() {
echo -e "${YELLOW}🛠️ 启用备用修复方案...${NC}"
show_progress 1 3 "下载和分析脚本"
execute_command "curl -sSL -o /tmp/problem_script 'https://github.com/xzx3344521/dock/raw/refs/heads/main/ai'" "下载问题脚本"
execute_command "file /tmp/problem_script" "检测脚本类型"
execute_command "head -3 /tmp/problem_script" "查看脚本头部"
show_progress 2 3 "准备执行环境"
execute_command "sudo apt update" "更新软件包列表"
execute_command "sudo apt install -y python3 python3-pip curl wget file" "安装必要工具"
show_progress 3 3 "执行修复"
# 根据脚本类型执行
if head -1 /tmp/problem_script 2>/dev/null | grep -q "python"; then
execute_command "python3 /tmp/problem_script" "执行Python脚本"
else
execute_command "bash /tmp/problem_script" "执行Bash脚本"
fi
}
# 安装为系统命令
install_systemwide() {
echo -e "${PURPLE}📥 安装到系统命令...${NC}"
show_status
execute_command "sudo cp '$0' '$INSTALL_DIR/ai-fix'" "复制脚本到系统目录"
execute_command "sudo chmod +x '$INSTALL_DIR/ai-fix'" "设置执行权限"
execute_command "sudo chown root:root '$INSTALL_DIR/ai-fix'" "设置所有权"
echo -e "${GREEN}✅ 安装完成!现在你可以使用以下命令:${NC}"
echo -e "${CYAN} ai-fix repair - AI修复问题${NC}"
echo -e "${CYAN} ai-fix status - 显示状态${NC}"
echo -e "${CYAN} ai-fix log - 查看日志${NC}"
}
# 交互式菜单
interactive_menu() {
while true; do
show_status
echo -e "${GREEN}请选择操作:${NC}"
echo -e "${CYAN}1. 🚀 AI自动修复问题${NC}"
echo -e "${CYAN}2. 📥 安装到系统命令${NC}"
echo -e "${CYAN}3. 📊 显示系统状态${NC}"
echo -e "${CYAN}4. 📋 查看执行日志${NC}"
echo -e "${CYAN}5. 🚪 退出${NC}"
echo ""
read -p "请输入选择 (1-5): " choice
case $choice in
1)
main_repair
;;
2)
install_systemwide
;;
3)
show_status
;;
4)
execute_command "cat '$LOG_FILE' | tail -20" "显示最近日志"
;;
5)
echo -e "${GREEN}👋 再见!${NC}"
exit 0
;;
*)
echo -e "${RED}❌ 无效选择,请重新输入${NC}"
;;
esac
echo ""
read -p "按回车键继续..."
clear
done
}
# 命令行参数处理
case "${1:-}" in
"repair"|"fix")
main_repair
;;
"install")
install_systemwide
;;
"status")
show_status
;;
"log")
execute_command "cat '$LOG_FILE'" "显示完整日志"
;;
"menu"|"interactive")
interactive_menu
;;
*)
echo -e "${GREEN}🤖 AI驱动交互式修复系统${NC}"
echo ""
echo -e "${CYAN}运行位置: $(pwd)${NC}"
echo -e "${CYAN}脚本路径: $0${NC}"
echo ""
echo -e "${YELLOW}使用方法:${NC}"
echo " $0 repair - 🚀 AI自动修复问题"
echo " $0 install - 📥 安装到系统命令"
echo " $0 status - 📊 显示系统状态"
echo " $0 log - 📋 查看执行日志"
echo " $0 menu - 🖥️ 交互式菜单"
echo ""
echo -e "${PURPLE}推荐使用: $0 menu 进入交互模式${NC}"
;;
esac