param( [int]$ApiPort = 8037, [int]$FallbackApiPort = 8041, [int]$FrontendPort = 3001 ) $ErrorActionPreference = "Stop" $RepoRoot = Split-Path -Parent $MyInvocation.MyCommand.Path $Backend = Join-Path $RepoRoot "backend" $Frontend = Join-Path $RepoRoot "frontend" $RedisDir = Join-Path $Backend "redis" $RedisExe = Join-Path $RedisDir "redis-server.exe" $RedisCli = Join-Path $RedisDir "redis-cli.exe" function Test-PortListening([int]$Port) { $line = netstat -ano | Select-String ":$Port\s+.*LISTENING" | Select-Object -First 1 return [bool]$line } function Ensure-Redis { if (Test-PortListening 6379) { Write-Host "[OK] Redis already listening on 6379" -ForegroundColor Green return } if (-not (Test-Path $RedisExe)) { throw "Redis 可执行文件不存在:$RedisExe" } Write-Host "[RUN] Starting Redis on 6379 ..." -ForegroundColor Yellow Start-Process -FilePath $RedisExe -ArgumentList "--port 6379" -WorkingDirectory $RedisDir | Out-Null Start-Sleep -Seconds 2 if (-not (Test-PortListening 6379)) { throw "Redis 启动失败,6379 未监听" } if (Test-Path $RedisCli) { & $RedisCli -p 6379 ping | Out-Null } Write-Host "[OK] Redis started" -ForegroundColor Green } function Resolve-ApiPort { if (-not (Test-PortListening $ApiPort)) { return $ApiPort } Write-Host "[WARN] Port $ApiPort is occupied, switching to $FallbackApiPort" -ForegroundColor Yellow if (Test-PortListening $FallbackApiPort) { throw "端口 $ApiPort 和 $FallbackApiPort 都被占用,请先释放端口" } return $FallbackApiPort } Write-Host "== AIAgent Windows 一键启动 ==" -ForegroundColor Cyan Write-Host "Repo: $RepoRoot" Ensure-Redis $RealApiPort = Resolve-ApiPort $ApiBase = "http://127.0.0.1:$RealApiPort" Write-Host "[RUN] Starting backend API on $RealApiPort ..." -ForegroundColor Yellow Start-Process powershell -ArgumentList @( "-NoExit", "-Command", "cd '$Backend'; .\venv\Scripts\Activate.ps1; python -m uvicorn app.main:app --host 0.0.0.0 --port $RealApiPort" ) Write-Host "[RUN] Starting Celery worker ..." -ForegroundColor Yellow Start-Process powershell -ArgumentList @( "-NoExit", "-Command", "cd '$Backend'; .\venv\Scripts\Activate.ps1; python -m celery -A app.core.celery_app worker --loglevel=info --pool=threads --concurrency=8" ) Write-Host "[RUN] Starting frontend on $FrontendPort (proxy -> $ApiBase) ..." -ForegroundColor Yellow Start-Process powershell -ArgumentList @( "-NoExit", "-Command", "`$env:AIAGENT_API_PROXY='$ApiBase'; cd '$Frontend'; pnpm dev --port $FrontendPort" ) Write-Host "" Write-Host "[DONE] 启动命令已下发" -ForegroundColor Green Write-Host "前端: http://localhost:$FrontendPort" -ForegroundColor Cyan Write-Host "后端: $ApiBase/docs" -ForegroundColor Cyan Write-Host "Redis: 127.0.0.1:6379" -ForegroundColor Cyan