File size: 5,311 Bytes
3dd8e3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# OpenCode AI Web Interface - 统一工具脚本
# 集成诊断、修复和验证功能

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

print_info() {
    echo -e "${BLUE}ℹ️  $1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️  $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

print_header() {
    echo -e "${BLUE}"
    echo "=================================="
    echo "  OpenCode AI 统一工具"
    echo "=================================="
    echo -e "${NC}"
}

# 显示帮助信息
show_help() {
    echo "OpenCode AI Web Interface 统一工具"
    echo ""
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  verify     验证服务状态"
    echo "  diagnose   运行深度诊断"
    echo "  network    网络连接诊断"
    echo "  js         JavaScript 错误诊断"
    echo "  local      启动本地部署"
    echo "  help       显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 verify     # 验证服务状态"
    echo "  $0 diagnose   # 运行完整诊断"
    echo "  $0 local      # 启动本地服务"
}

# 验证服务状态
verify_service() {
    print_info "验证 OpenCode AI 服务状态..."
    
    local base_url="https://airsltd-hfoc.hf.space"
    local health_url="$base_url/global/health"
    
    # 测试健康检查
    local health_response=$(curl -s --connect-timeout 10 "$health_url" 2>/dev/null)
    
    if echo "$health_response" | grep -q "healthy.*true"; then
        print_success "✅ 服务器健康检查通过"
        local version=$(echo "$health_response" | grep -o '"version":"[^"]*"' | cut -d'"' -f4)
        print_info "版本: $version"
        return 0
    else
        print_error "❌ 服务器健康检查失败"
        return 1
    fi
}

# 网络连接诊断
diagnose_network() {
    print_info "执行网络连接诊断..."
    
    local base_url="https://airsltd-hfoc.hf.space"
    
    # 基本网络测试
    print_info "测试基本网络连接..."
    if curl -s --connect-timeout 5 "https://www.google.com" >/dev/null 2>&1; then
        print_success "互联网连接正常"
    else
        print_warning "互联网连接可能有问题"
    fi
    
    # Hugging Face 相关测试
    print_info "测试 Hugging Face Spaces 连接..."
    if curl -s --connect-timeout 10 "$base_url" >/dev/null 2>&1; then
        print_success "Hugging Face Spaces 连接正常"
    else
        print_error "Hugging Face Spaces 连接失败"
        print_info "建议:"
        echo "1. 使用 VPN 或代理"
        echo "2. 尝试本地部署"
        echo "3. 检查网络设置"
    fi
}

# JavaScript 错误诊断
diagnose_js() {
    print_info "诊断 JavaScript 错误..."
    
    print_warning "常见的 JavaScript 错误:"
    echo "1. InvalidCharacterError: atob 解码失败"
    echo "2. 网络连接错误"
    echo "3. 资源加载失败"
    echo ""
    
    print_info "解决方案:"
    echo "1. 清除浏览器缓存: Ctrl+Shift+Delete"
    echo "2. 强制刷新: Ctrl+Shift+R"
    echo "3. 尝试无痕模式"
    echo "4. 使用本地部署作为备用"
}

# 深度诊断
deep_diagnose() {
    print_info "执行深度诊断..."
    
    # 依次执行各种诊断
    verify_service
    diagnose_network
    diagnose_js
    
    echo ""
    print_info "深度诊断完成"
}

# 启动本地部署
start_local() {
    print_info "启动本地 OpenCode 服务..."
    
    # 检查 OpenCode 是否已安装
    if ! command_exists opencode; then
        print_info "检测到 OpenCode 未安装,正在安装..."
        
        if command_exists npm; then
            print_info "使用 npm 安装 OpenCode..."
            npm install -g opencode-ai
        elif command_exists brew; then
            print_info "使用 Homebrew 安装 OpenCode..."
            brew install opencode-ai/tap/opencode
        else
            print_error "未找到 npm 或 brew,请手动安装 OpenCode"
            exit 1
        fi
    fi
    
    # 设置端口
    local PORT=7860
    if lsof -Pi :$PORT -sTCP:LISTEN -t >/dev/null 2>&1; then
        print_warning "端口 $PORT 已被占用,尝试端口 7861..."
        PORT=7861
    fi
    
    print_info "启动 OpenCode Web Interface..."
    echo ""
    echo -e "${GREEN}服务地址: http://localhost:$PORT${NC}"
    echo ""
    
    # 启动服务
    export HTTP_PROXY=""
    export HTTPS_PROXY=""
    export NO_PROXY="localhost,127.0.0.1,0.0.0.0"
    
    opencode serve --port $PORT --hostname 0.0.0.0 --cors "*"
}

# 检查命令是否存在
command_exists() {
    command -v "$1" >/dev/null 2>&1
}

# 主函数
main() {
    case "${1:-help}" in
        "verify")
            print_header
            verify_service
            ;;
        "diagnose")
            print_header
            deep_diagnose
            ;;
        "network")
            print_header
            diagnose_network
            ;;
        "js")
            print_header
            diagnose_js
            ;;
        "local")
            print_header
            start_local
            ;;
        "help"|*)
            show_help
            ;;
    esac
}

# 运行主函数
main "$@"