-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathstart.ps1
More file actions
555 lines (489 loc) · 19.1 KB
/
start.ps1
File metadata and controls
555 lines (489 loc) · 19.1 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
#Requires -Version 5.1
<#
.SYNOPSIS
Warp2Api Windows PowerShell 快速启动脚本
.DESCRIPTION
启动两个服务器:Protobuf桥接服务器和OpenAI兼容API服务器
.PARAMETER Verbose
启用详细日志输出
.PARAMETER Stop
停止所有服务器
.EXAMPLE
.\start.ps1 # 启动服务器(静默模式)
.\start.ps1 -Verbose # 启动服务器(详细模式)
.\start.ps1 -Stop # 停止服务器
#>
param(
[switch]$Verbose,
[switch]$Stop
)
# 设置控制台编码为UTF-8
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# 从 .env 文件加载环境变量(如果存在)
if (Test-Path ".env") {
Get-Content ".env" | ForEach-Object {
if ($_ -match '^([^#][^=]+)=(.*)$') {
$key = $matches[1].Trim()
$value = $matches[2].Trim()
[Environment]::SetEnvironmentVariable($key, $value)
}
}
# 自动配置环境变量
function Initialize-Environment {
Write-LogInfo "自动配置环境变量..."
# 如果 .env 不存在,从 .env.example 复制
if (-not (Test-Path ".env")) {
if (Test-Path ".env.example") {
Copy-Item ".env.example" ".env"
Write-LogSuccess "已从 .env.example 复制配置到 .env"
} else {
Write-LogWarning ".env.example 文件不存在,跳过配置复制"
}
}
# 检查并生成 API_TOKEN
if (Test-Path ".env") {
$envContent = Get-Content ".env"
$currentApiToken = $null
foreach ($line in $envContent) {
if ($line -match '^API_TOKEN=(.*)') {
$currentApiToken = $matches[1].Trim('"')
break
}
}
# 如果API_TOKEN不存在或为默认值001,则设置为固定值0000
if (-not $currentApiToken -or $currentApiToken -eq "001") {
$apiToken = "0000"
# 替换或添加API_TOKEN行
$newContent = @()
$found = $false
foreach ($line in $envContent) {
if ($line -match '^API_TOKEN=') {
$newContent += "API_TOKEN=$apiToken"
$found = $true
} else {
$newContent += $line
}
}
if (-not $found) {
$newContent += "API_TOKEN=$apiToken"
}
$newContent | Set-Content ".env"
Write-LogSuccess "已设置固定API_TOKEN: $apiToken"
} else {
Write-LogInfo "API_TOKEN 已存在且非默认值,跳过设置"
}
# 设置日志开关为默认状态(静默模式)
$verboseFound = $false
foreach ($line in $envContent) {
if ($line -match '^W2A_VERBOSE=') {
$verboseFound = $true
break
}
}
if (-not $verboseFound) {
Add-Content ".env" "W2A_VERBOSE=false"
Write-LogSuccess "已设置日志输出为静默模式"
}
}
# 重新加载环境变量
if (Test-Path ".env") {
Get-Content ".env" | ForEach-Object {
if ($_ -match '^([^#][^=]+)=(.*)$') {
$key = $matches[1].Trim()
$value = $matches[2].Trim()
[Environment]::SetEnvironmentVariable($key, $value)
}
}
# 重新设置日志开关变量,保持原有值或默认false
$env:W2A_VERBOSE = if ($env:W2A_VERBOSE) { $env:W2A_VERBOSE } else { "false" }
}
}
}
# 环境变量控制日志输出,默认不打印日志
$env:W2A_VERBOSE = if ($Verbose) { "true" } else { $env:W2A_VERBOSE ?? "false" }
# 设置代理排除列表,避免本地服务被代理干扰
if (-not $env:NO_PROXY) {
$env:NO_PROXY = "127.0.0.1,localhost"
}
# 日志函数
function Write-LogInfo {
param([string]$Message)
if ($env:W2A_VERBOSE -eq "true") {
Write-Host "[$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))] INFO: $Message" -ForegroundColor Blue
}
}
function Write-LogSuccess {
param([string]$Message)
if ($env:W2A_VERBOSE -eq "true") {
Write-Host "[$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))] SUCCESS: $Message" -ForegroundColor Green
}
}
function Write-LogWarning {
param([string]$Message)
if ($env:W2A_VERBOSE -eq "true") {
Write-Host "[$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))] WARNING: $Message" -ForegroundColor Yellow
}
}
function Write-LogError {
param([string]$Message)
Write-Host "[$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))] ERROR: $Message" -ForegroundColor Red
}
# 检查Python版本
function Test-PythonVersion {
Write-LogInfo "检查Python版本..."
try {
$pythonVersion = python --version 2>&1
if ($LASTEXITCODE -ne 0) {
Write-LogError "未找到Python,请确保Python 3.9+已安装"
exit 1
}
$versionString = $pythonVersion -replace 'Python ', ''
Write-LogInfo "Python版本: $versionString"
$versionParts = $versionString -split '\.'
$major = [int]$versionParts[0]
$minor = [int]$versionParts[1]
if ($major -lt 3 -or ($major -eq 3 -and $minor -lt 9)) {
Write-LogWarning "推荐使用Python 3.13+,但当前版本 $versionString 仍可工作"
}
}
catch {
Write-LogError "Python检查失败: $($_.Exception.Message)"
exit 1
}
}
# 检查依赖
function Test-Dependencies {
Write-LogInfo "检查项目依赖..."
$packages = @("fastapi", "uvicorn", "httpx", "protobuf", "websockets", "openai")
$missingPackages = @()
foreach ($package in $packages) {
try {
python -c "import $package" 2>$null
if ($LASTEXITCODE -ne 0) {
$missingPackages += $package
}
}
catch {
$missingPackages += $package
}
}
if ($missingPackages.Count -eq 0) {
Write-LogSuccess "所有依赖包已安装"
return
}
Write-LogWarning "缺少以下依赖包: $($missingPackages -join ', ')"
Write-LogInfo "正在尝试自动安装..."
try {
$installResult = python -m pip install $missingPackages
if ($LASTEXITCODE -eq 0) {
Write-LogSuccess "依赖包安装成功"
} else {
Write-LogError "依赖包安装失败,请手动运行: python -m pip install $($missingPackages -join ' ')"
exit 1
}
}
catch {
Write-LogError "依赖包安装失败: $($_.Exception.Message)"
exit 1
}
}
# 检查网络连通性
function Test-NetworkConnectivity {
Write-LogInfo "检查网络连通性..."
try {
$response = Invoke-WebRequest -Uri "https://app.warp.dev" -TimeoutSec 10 -ErrorAction Stop
Write-LogSuccess "网络连通性检查通过"
Write-Host "✅ 运行时请保证 https://app.warp.dev 网络联通性"
}
catch {
Write-LogWarning "网络连通性检查失败,请确保可以访问 https://app.warp.dev"
Write-Host "⚠️ 运行时请保证 https://app.warp.dev 网络联通性"
Write-Host " 如果网络连接失败,服务可能无法正常工作"
}
# 终端即时测试联通性并打印结果
try {
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$headResp = Invoke-WebRequest -Uri "https://app.warp.dev" -Method Head -TimeoutSec 10 -ErrorAction Stop
$sw.Stop()
$ms = [math]::Round($sw.Elapsed.TotalMilliseconds)
Write-Host "🌐 当前 https://app.warp.dev 联通: 是 (HTTP $($headResp.StatusCode), 耗时 ${ms}ms)"
}
catch {
$code = if ($_.Exception.Response -and $_.Exception.Response.StatusCode) { [int]$_.Exception.Response.StatusCode } else { "N/A" }
Write-Host "🌐 当前 https://app.warp.dev 联通: 否 (HTTP $code)"
}
}
# 检查端口是否被占用
function Test-PortAvailable {
param([int]$Port)
$connections = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
return $connections.Count -eq 0
}
# 终止端口进程
function Stop-PortProcess {
param([int]$Port)
try {
$connections = Get-NetTCPConnection -LocalPort $Port -ErrorAction SilentlyContinue
foreach ($conn in $connections) {
Stop-Process -Id $conn.OwningProcess -Force -ErrorAction SilentlyContinue
}
}
catch {
# 忽略错误
}
}
# 启动Protobuf桥接服务器
function Start-BridgeServer {
Write-LogInfo "启动Protobuf桥接服务器..."
# 使用小众端口28888避免与其他应用冲突
$bridgePort = 28888
# 检查端口是否被占用
if (-not (Test-PortAvailable $bridgePort)) {
Write-LogWarning "端口$bridgePort已被占用,尝试终止现有进程..."
Stop-PortProcess $bridgePort
Start-Sleep -Seconds 2
}
# 启动服务器(后台运行)
try {
$process = Start-Process -FilePath "python" -ArgumentList "server.py", "--port", $bridgePort -NoNewWindow -RedirectStandardOutput "bridge_server.log" -RedirectStandardError "bridge_server.log" -PassThru
$bridgePid = $process.Id
# 等待服务器启动
Write-LogInfo "等待Protobuf桥接服务器启动..."
Start-Sleep -Seconds 5
# 检查服务器是否启动成功
try {
$response = Invoke-WebRequest -Uri "http://localhost:$bridgePort/healthz" -TimeoutSec 5 -ErrorAction Stop
Write-LogSuccess "Protobuf桥接服务器启动成功 (PID: $bridgePid)"
Write-LogInfo "📍 Protobuf桥接服务器地址: http://localhost:$bridgePort"
return $true
}
catch {
Write-LogError "Protobuf桥接服务器启动失败"
if (Test-Path "bridge_server.log") {
Get-Content "bridge_server.log" | Write-Host
}
return $false
}
}
catch {
Write-LogError "启动Protobuf桥接服务器失败: $($_.Exception.Message)"
return $false
}
}
# 启动OpenAI兼容API服务器
function Start-OpenAIServer {
Write-LogInfo "启动OpenAI兼容API服务器..."
# 使用小众端口28889避免与其他应用冲突
$openaiPort = 28889
# 检查端口是否被占用
if (-not (Test-PortAvailable $openaiPort)) {
Write-LogWarning "端口$openaiPort已被占用,尝试终止现有进程..."
Stop-PortProcess $openaiPort
Start-Sleep -Seconds 2
}
# 启动服务器(后台运行)
try {
$process = Start-Process -FilePath "python" -ArgumentList "openai_compat.py", "--port", $openaiPort -NoNewWindow -RedirectStandardOutput "openai_server.log" -RedirectStandardError "openai_server.log" -PassThru
$openaiPid = $process.Id
# 等待服务器启动
Write-LogInfo "等待OpenAI兼容API服务器启动..."
Start-Sleep -Seconds 5
# 检查服务器是否启动成功
try {
$response = Invoke-WebRequest -Uri "http://localhost:$openaiPort/healthz" -TimeoutSec 5 -ErrorAction Stop
Write-LogSuccess "OpenAI兼容API服务器启动成功 (PID: $openaiPid)"
Write-LogInfo "📍 OpenAI兼容API服务器地址: http://localhost:$openaiPort"
return $true
}
catch {
Write-LogError "OpenAI兼容API服务器启动失败"
if (Test-Path "openai_server.log") {
Get-Content "openai_server.log" | Write-Host
}
return $false
}
}
catch {
Write-LogError "启动OpenAI兼容API服务器失败: $($_.Exception.Message)"
return $false
}
}
# 显示服务器状态
function Show-Status {
Write-Host ""
Write-Host "============================================"
Write-Host "🚀 Warp2Api 服务器状态"
Write-Host "============================================"
Write-Host "📍 Protobuf桥接服务器: http://localhost:28888"
Write-Host "📍 OpenAI兼容API服务器: http://localhost:28889"
Write-Host "📍 API文档: http://localhost:28889/docs"
Write-Host "🔗 Roocode / KiloCode baseUrl: http://127.0.0.1:28889/v1"
Write-Host "⬇️ KilloCode 下载地址:https://app.kilocode.ai/users/sign_up?referral-code=df16bc60-be35-480f-be2c-b1c6685b6089"
Write-Host ""
Write-Host "🔧 支持的模型:http://127.0.0.1:28889/v1/models"
Write-Host " • claude-4-sonnet"
Write-Host " • claude-4-opus"
Write-Host " • claude-4.1-opus"
Write-Host " • gemini-2.5-pro"
Write-Host " • gpt-4.1"
Write-Host " • gpt-4o"
Write-Host " • gpt-5"
Write-Host " • gpt-5 (high reasoning)"
Write-Host " • o3"
Write-Host " • o4-mini"
Write-Host ""
Write-Host "🔑 当前API接口Token:" -NoNewline
Write-Host " "
if (Test-Path ".env") {
$envContent = Get-Content ".env"
$warpApiToken = $null
foreach ($line in $envContent) {
if ($line -match '^API_TOKEN=(.*)') {
$warpApiToken = $matches[1].Trim('"')
}
}
if ($warpApiToken) {
Write-Host $warpApiToken
} else {
Write-Host "未设置"
}
} else {
Write-Host ".env 文件不存在"
}
Write-Host ""
Write-Host "📝 测试命令:"
$warpApiToken = if ($warpApiToken) { $warpApiToken } else { "your_token_here" }
Write-Host "PowerShell命令:"
Write-Host "Invoke-WebRequest -Uri 'http://localhost:28889/v1/chat/completions' -Method POST -ContentType 'application/json' -Headers @{\"Authorization\" = \"Bearer $warpApiToken\"} -Body '{\"model\": \"claude-4-sonnet\", \"messages\": [{\"role\": \"user\", \"content\": \"你好\"}], \"stream\": true}'"
Write-Host ""
Write-Host "cURL命令:"
Write-Host "curl -X POST http://localhost:28889/v1/chat/completions \"
Write-Host " -H \"Content-Type: application/json\" \"
Write-Host " -H \"Authorization: Bearer $warpApiToken\" \"
Write-Host " -d '{\"model\": \"claude-4-sonnet\", \"messages\": [{\"role\": \"user\", \"content\": \"你好\"}], \"stream\": true}'"
Write-Host ""
Write-Host "🛑 要停止服务器,请运行: .\stop.ps1"
Write-Host "============================================"
}
# 停止服务器
function Stop-Servers {
Write-LogInfo "停止所有服务器..."
# 首先尝试通过进程名优雅终止
Write-LogInfo "尝试通过进程名优雅终止服务器..."
Get-Process | Where-Object { $_.ProcessName -eq "python" -or $_.ProcessName -eq "python3" } | ForEach-Object {
try {
$commandLine = (Get-WmiObject Win32_Process -Filter "ProcessId=$($_.Id)").CommandLine
if ($commandLine -match "server\.py|openai_compat\.py") {
Write-LogInfo "优雅终止服务器进程 (PID: $($_.Id))"
Stop-Process -Id $_.Id -ErrorAction SilentlyContinue
}
}
catch {
# 忽略无法获取命令行的进程
}
}
Start-Sleep -Seconds 2
# 检查并清理端口进程,只终止我们的Python进程
Write-LogInfo "检查并清理端口进程..."
# 检查端口28888
$connections = Get-NetTCPConnection -LocalPort 28888 -ErrorAction SilentlyContinue
foreach ($conn in $connections) {
try {
$process = Get-Process -Id $conn.OwningProcess -ErrorAction SilentlyContinue
if ($process) {
$commandLine = (Get-WmiObject Win32_Process -Filter "ProcessId=$($process.Id)").CommandLine
if ($commandLine -match "server\.py|openai_compat\.py") {
Write-LogWarning "终止我们的服务器进程 (PID: $($process.Id))"
# 首先尝试优雅终止
Stop-Process -Id $process.Id -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1
# 如果仍在运行,再强制终止
if (Get-Process -Id $process.Id -ErrorAction SilentlyContinue) {
Write-LogWarning "优雅终止失败,强制终止进程 (PID: $($process.Id))"
Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue
}
} else {
Write-LogWarning "端口28888被其他进程占用 (PID: $($process.Id)),跳过终止"
}
}
}
catch {
# 忽略错误
}
}
# 检查端口28889
$connections = Get-NetTCPConnection -LocalPort 28889 -ErrorAction SilentlyContinue
foreach ($conn in $connections) {
try {
$process = Get-Process -Id $conn.OwningProcess -ErrorAction SilentlyContinue
if ($process) {
$commandLine = (Get-WmiObject Win32_Process -Filter "ProcessId=$($process.Id)").CommandLine
if ($commandLine -match "server\.py|openai_compat\.py") {
Write-LogWarning "终止我们的服务器进程 (PID: $($process.Id))"
# 首先尝试优雅终止
Stop-Process -Id $process.Id -ErrorAction SilentlyContinue
Start-Sleep -Seconds 1
# 如果仍在运行,再强制终止
if (Get-Process -Id $process.Id -ErrorAction SilentlyContinue) {
Write-LogWarning "优雅终止失败,强制终止进程 (PID: $($process.Id))"
Stop-Process -Id $process.Id -Force -ErrorAction SilentlyContinue
}
} else {
Write-LogWarning "端口28889被其他进程占用 (PID: $($process.Id)),跳过终止"
}
}
}
catch {
# 忽略错误
}
}
Write-LogSuccess "所有服务器已停止"
}
# 主函数
# 自动配置环境变量
Initialize-Environment
function Main {
if ($Stop) {
Stop-Servers
return
}
Write-Host "============================================"
Write-Host "🚀 Warp2Api PowerShell 快速启动脚本"
Write-Host "============================================"
# 检查环境
Test-PythonVersion
Test-Dependencies
Test-NetworkConnectivity
# 启动服务器
$bridgeStarted = Start-BridgeServer
if (-not $bridgeStarted) {
Write-LogError "Protobuf桥接服务器启动失败,退出"
exit 1
}
$openaiStarted = Start-OpenAIServer
if (-not $openaiStarted) {
Write-LogError "OpenAI兼容API服务器启动失败,退出"
exit 1
}
# 显示状态信息
Show-Status
if ($env:W2A_VERBOSE -eq "true") {
Write-LogSuccess "Warp2Api启动完成!"
Write-LogInfo "服务器正在后台运行,按 Ctrl+C 退出"
Write-Host ""
Write-Host "📋 实时日志监控 (按 Ctrl+C 退出):"
Write-Host "----------------------------------------"
# PowerShell 中可以同时监控多个日志文件
try {
Get-Content "bridge_server.log", "openai_server.log" -Wait -ErrorAction Stop
}
catch {
Write-Host "日志监控已停止"
}
}
else {
Write-LogSuccess "Warp2Api启动完成!服务器正在后台运行。"
}
}
# 执行主函数
Main