Files
aiagent/scripts/startup/start_windows.ps1

239 lines
8.8 KiB
PowerShell
Raw Normal View History

# 低代码智能体平台 - Windows PowerShell 启动脚本
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "低代码智能体平台 - Windows 启动脚本" -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
# 检查Python是否安装
try {
$pythonVersion = python --version 2>&1
Write-Host "✅ Python 版本: $pythonVersion" -ForegroundColor Green
} catch {
Write-Host "❌ Python 未安装或未添加到系统PATH" -ForegroundColor Red
Write-Host "请安装 Python 3.11+ 并确保在PATH中"
pause
exit 1
}
# 检查Node.js是否安装
try {
$nodeVersion = node --version
Write-Host "✅ Node.js 版本: $nodeVersion" -ForegroundColor Green
} catch {
Write-Host "❌ Node.js 未安装或未添加到系统PATH" -ForegroundColor Red
Write-Host "请安装 Node.js 18+ 并确保在PATH中"
pause
exit 1
}
# 检查pnpm是否安装
try {
$pnpmVersion = pnpm --version
Write-Host "✅ pnpm 版本: $pnpmVersion" -ForegroundColor Green
} catch {
Write-Host "⚠️ pnpm 未安装,正在安装..." -ForegroundColor Yellow
npm install -g pnpm
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ pnpm 安装失败" -ForegroundColor Red
pause
exit 1
}
Write-Host "✅ pnpm 安装成功" -ForegroundColor Green
}
Write-Host "✅ 环境检查通过" -ForegroundColor Green
Write-Host ""
# 设置项目目录
$projectRoot = Split-Path -Parent $MyInvocation.MyCommand.Path
Set-Location $projectRoot
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "1. Redis 检查" -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
# 检查Redis服务是否运行
$redisService = Get-Service -Name "Redis" -ErrorAction SilentlyContinue
if ($null -eq $redisService -or $redisService.Status -ne "Running") {
Write-Host "❌ Redis 服务未运行" -ForegroundColor Red
Write-Host ""
Write-Host "请按以下步骤安装Redis" -ForegroundColor Yellow
Write-Host "1. 下载 Redis Windows 版本https://github.com/microsoftarchive/redis/releases"
Write-Host "2. 下载 Redis-x64-3.2.100.msi"
Write-Host "3. 运行安装程序,按照默认设置安装"
Write-Host "4. Redis 将作为 Windows 服务运行在 6379 端口"
Write-Host ""
Write-Host "安装完成后,请重新运行此脚本" -ForegroundColor Yellow
pause
exit 1
} else {
Write-Host "✅ Redis 服务正在运行 (状态: $($redisService.Status))" -ForegroundColor Green
}
Write-Host ""
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "2. 启动后端服务" -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
# 进入backend目录
Set-Location "$projectRoot\backend"
# 检查虚拟环境
if (-not (Test-Path "venv\Scripts\activate")) {
Write-Host "⚠️ 虚拟环境不存在,正在创建..." -ForegroundColor Yellow
python -m venv venv
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ 虚拟环境创建失败" -ForegroundColor Red
pause
exit 1
}
Write-Host "✅ 虚拟环境创建成功" -ForegroundColor Green
}
Write-Host "✅ 虚拟环境检查通过" -ForegroundColor Green
# 激活虚拟环境
$activateScript = "$projectRoot\backend\venv\Scripts\Activate.ps1"
if (Test-Path $activateScript) {
& $activateScript
} else {
Write-Host "❌ 虚拟环境激活脚本不存在: $activateScript" -ForegroundColor Red
pause
exit 1
}
Write-Host "📦 检查Python依赖..." -ForegroundColor Cyan
$fastapiInstalled = pip list | Select-String "fastapi"
if (-not $fastapiInstalled) {
Write-Host "⚠️ 正在安装Python依赖..." -ForegroundColor Yellow
pip install -r requirements.txt
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ Python依赖安装失败" -ForegroundColor Red
pause
exit 1
}
Write-Host "✅ Python依赖安装完成" -ForegroundColor Green
} else {
Write-Host "✅ Python依赖已安装" -ForegroundColor Green
}
Write-Host ""
Write-Host "🔧 配置环境变量..." -ForegroundColor Cyan
if (-not (Test-Path ".env")) {
Copy-Item env.example .env -ErrorAction SilentlyContinue
Write-Host "⚠️ 已创建 .env 文件,请检查配置" -ForegroundColor Yellow
}
Write-Host ""
Write-Host "🗄️ 运行数据库迁移..." -ForegroundColor Cyan
alembic upgrade head
if ($LASTEXITCODE -ne 0) {
Write-Host "⚠️ 数据库迁移失败,继续启动..." -ForegroundColor Yellow
}
Write-Host ""
Write-Host "🌐 启动后端服务..." -ForegroundColor Cyan
Write-Host "后端服务将在 http://localhost:8037 启动" -ForegroundColor Green
Write-Host "API文档http://localhost:8037/docs" -ForegroundColor Green
Write-Host ""
# 启动后端服务(新窗口)
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$projectRoot\backend'; .\venv\Scripts\Activate.ps1; uvicorn app.main:app --host 0.0.0.0 --port 8037 --reload"
Write-Host "⏳ 等待后端服务启动..." -ForegroundColor Cyan
Start-Sleep -Seconds 3
Write-Host ""
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "3. 启动 Celery Worker" -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "🔄 启动 Celery Worker..." -ForegroundColor Cyan
# Windows 下 prefork 池易卡住任务Redis 出现大量 unacked使用线程池更稳定
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$projectRoot\backend'; .\venv\Scripts\Activate.ps1; celery -A app.core.celery_app worker --loglevel=info --pool=threads --concurrency=8"
Write-Host "⏳ 等待 Celery Worker 启动..." -ForegroundColor Cyan
Start-Sleep -Seconds 2
Write-Host ""
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "4. 启动前端服务" -ForegroundColor Cyan
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
# 返回项目根目录
Set-Location $projectRoot
# 进入frontend目录
Set-Location "$projectRoot\frontend"
Write-Host "📦 检查前端依赖..." -ForegroundColor Cyan
if (-not (Test-Path "node_modules")) {
Write-Host "⚠️ 正在安装前端依赖..." -ForegroundColor Yellow
pnpm install
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ 前端依赖安装失败" -ForegroundColor Red
pause
exit 1
}
Write-Host "✅ 前端依赖安装完成" -ForegroundColor Green
} else {
Write-Host "✅ 前端依赖已安装" -ForegroundColor Green
}
Write-Host ""
Write-Host "🖥️ 启动前端服务..." -ForegroundColor Cyan
Write-Host "前端服务将在 http://localhost:3000 启动" -ForegroundColor Green
Write-Host ""
# 启动前端服务(新窗口)
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$projectRoot\frontend'; pnpm dev"
Write-Host "⏳ 等待前端服务启动..." -ForegroundColor Cyan
Start-Sleep -Seconds 5
Write-Host ""
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host "🎉 启动完成!" -ForegroundColor Green
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "服务访问地址:" -ForegroundColor White
Write-Host " 📍 前端界面: http://localhost:3000" -ForegroundColor Yellow
Write-Host " 📍 后端API: http://localhost:8037" -ForegroundColor Yellow
Write-Host " 📍 API文档: http://localhost:8037/docs" -ForegroundColor Yellow
Write-Host ""
Write-Host "服务状态:" -ForegroundColor White
Write-Host " ✅ Redis 服务: 运行中" -ForegroundColor Green
Write-Host " ✅ 后端服务: 已启动" -ForegroundColor Green
Write-Host " ✅ Celery Worker: 已启动" -ForegroundColor Green
Write-Host " ✅ 前端服务: 已启动" -ForegroundColor Green
Write-Host ""
Write-Host "📋 重要提示:" -ForegroundColor White
Write-Host " 1. 首次访问需要注册新用户" -ForegroundColor Gray
Write-Host " 2. 保持所有PowerShell窗口打开" -ForegroundColor Gray
Write-Host " 3. 停止服务关闭所有PowerShell窗口" -ForegroundColor Gray
Write-Host ""
Write-Host "==============================================" -ForegroundColor Cyan
Write-Host ""
# 返回项目根目录
Set-Location $projectRoot
Write-Host "是否要打开浏览器访问前端界面?(Y/N)" -ForegroundColor Cyan
$response = Read-Host
if ($response -eq "Y" -or $response -eq "y") {
Start-Process "http://localhost:3000"
}
Write-Host ""
Write-Host "脚本执行完成!" -ForegroundColor Green
Write-Host "按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")