File size: 6,419 Bytes
927965d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# AI Studio Proxy API 一键安装脚本 (Windows PowerShell)
# 使用 Poetry 进行依赖管理

# 设置错误处理
$ErrorActionPreference = "Stop"

# 颜色函数
function Write-ColorOutput {
    param(
        [string]$Message,
        [string]$Color = "White"
    )
    Write-Host $Message -ForegroundColor $Color
}

function Log-Info {
    param([string]$Message)
    Write-ColorOutput "[INFO] $Message" "Blue"
}

function Log-Success {
    param([string]$Message)
    Write-ColorOutput "[SUCCESS] $Message" "Green"
}

function Log-Warning {
    param([string]$Message)
    Write-ColorOutput "[WARNING] $Message" "Yellow"
}

function Log-Error {
    param([string]$Message)
    Write-ColorOutput "[ERROR] $Message" "Red"
}

# 检查命令是否存在
function Test-Command {
    param([string]$Command)
    try {
        Get-Command $Command -ErrorAction Stop | Out-Null
        return $true
    }
    catch {
        return $false
    }
}

# 检查 Python 版本
function Test-Python {
    Log-Info "检查 Python 版本..."
    
    $pythonCmd = $null
    if (Test-Command "python") {
        $pythonCmd = "python"
    }
    elseif (Test-Command "py") {
        $pythonCmd = "py"
    }
    else {
        Log-Error "未找到 Python。请先安装 Python 3.9+"
        exit 1
    }
    
    try {
        $pythonVersion = & $pythonCmd --version 2>&1
        $versionMatch = $pythonVersion -match "Python (\d+)\.(\d+)"
        
        if ($versionMatch) {
            $major = [int]$matches[1]
            $minor = [int]$matches[2]
            
            if ($major -lt 3 -or ($major -eq 3 -and $minor -lt 9)) {
                Log-Error "Python 版本过低: $pythonVersion。需要 Python 3.9+"
                exit 1
            }
            
            Log-Success "Python 版本: $pythonVersion ✓"
            return $pythonCmd
        }
        else {
            Log-Error "无法解析 Python 版本"
            exit 1
        }
    }
    catch {
        Log-Error "Python 版本检查失败: $_"
        exit 1
    }
}

# 安装 Poetry
function Install-Poetry {
    if (Test-Command "poetry") {
        Log-Success "Poetry 已安装 ✓"
        return
    }
    
    Log-Info "安装 Poetry..."
    try {
        (Invoke-WebRequest -Uri https://install.python-poetry.org -UseBasicParsing).Content | py -
        
        # 刷新环境变量
        $env:PATH = [System.Environment]::GetEnvironmentVariable("PATH", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("PATH", "User")
        
        if (Test-Command "poetry") {
            Log-Success "Poetry 安装成功 ✓"
        }
        else {
            Log-Error "Poetry 安装失败。请手动安装 Poetry"
            exit 1
        }
    }
    catch {
        Log-Error "Poetry 安装失败: $_"
        exit 1
    }
}

# 克隆项目
function Clone-Project {
    Log-Info "克隆项目..."
    
    if (Test-Path "AIstudioProxyAPI") {
        Log-Warning "项目目录已存在,跳过克隆"
        Set-Location "AIstudioProxyAPI"
    }
    else {
        try {
            git clone https://github.com/CJackHwang/AIstudioProxyAPI.git
            Set-Location "AIstudioProxyAPI"
            Log-Success "项目克隆成功 ✓"
        }
        catch {
            Log-Error "项目克隆失败: $_"
            exit 1
        }
    }
}

# 安装依赖
function Install-Dependencies {
    Log-Info "安装项目依赖..."
    try {
        poetry install
        Log-Success "依赖安装成功 ✓"
    }
    catch {
        Log-Error "依赖安装失败: $_"
        exit 1
    }
}

# 下载 Camoufox
function Download-Camoufox {
    Log-Info "下载 Camoufox 浏览器..."
    try {
        poetry run camoufox fetch
        Log-Success "Camoufox 下载成功 ✓"
    }
    catch {
        Log-Warning "Camoufox 下载失败,但不影响主要功能: $_"
    }
}

# 安装 Playwright 依赖
function Install-PlaywrightDeps {
    Log-Info "安装 Playwright 依赖..."
    try {
        poetry run playwright install-deps firefox
    }
    catch {
        Log-Warning "Playwright 依赖安装失败,但不影响主要功能"
    }
}

# 创建配置文件
function Create-Config {
    Log-Info "创建配置文件..."
    
    if (!(Test-Path ".env") -and (Test-Path ".env.example")) {
        Copy-Item ".env.example" ".env"
        Log-Success "配置文件创建成功 ✓"
        Log-Info "请编辑 .env 文件进行个性化配置"
    }
    else {
        Log-Warning "配置文件已存在或模板不存在"
    }
}

# 验证安装
function Test-Installation {
    Log-Info "验证安装..."
    
    try {
        # 检查 Poetry 环境
        poetry env info | Out-Null
        
        # 检查关键依赖
        poetry run python -c "import fastapi, playwright, camoufox"
        
        Log-Success "安装验证成功 ✓"
    }
    catch {
        Log-Error "安装验证失败: $_"
        exit 1
    }
}

# 显示后续步骤
function Show-NextSteps {
    Write-Host ""
    Log-Success "🎉 安装完成!"
    Write-Host ""
    Write-Host "后续步骤:"
    Write-Host "1. 进入项目目录: cd AIstudioProxyAPI"
    Write-Host "2. 激活虚拟环境: poetry env activate"
    Write-Host "3. 配置环境变量: notepad .env"
    Write-Host "4. 首次认证设置: poetry run python launch_camoufox.py --debug"
    Write-Host "5. 日常运行: poetry run python launch_camoufox.py --headless"
    Write-Host ""
    Write-Host "详细文档:"
    Write-Host "- 环境配置: docs/environment-configuration.md"
    Write-Host "- 认证设置: docs/authentication-setup.md"
    Write-Host "- 日常使用: docs/daily-usage.md"
    Write-Host ""
}

# 主函数
function Main {
    Write-Host "🚀 AI Studio Proxy API 一键安装脚本"
    Write-Host "使用 Poetry 进行现代化依赖管理"
    Write-Host ""

    $pythonCmd = Test-Python
    Install-Poetry
    Clone-Project
    Install-Dependencies
    Download-Camoufox
    Install-PlaywrightDeps
    Create-Config
    Test-Installation
    Show-NextSteps
}

# 运行主函数
try {
    Main
}
catch {
    Log-Error "安装过程中发生错误: $_"
    exit 1
}