install-native.ps1 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # ============================================================
  2. # LambdaAgentPaaS — Windows Native 安装(无 Docker)
  3. #
  4. # 用法:
  5. # .\deploy\install-native.ps1 # 安装
  6. # .\deploy\install-native.ps1 -Start # 安装后启动
  7. #
  8. # 要求: Python 3.10+, Node.js 18+
  9. # ============================================================
  10. param([switch]$Start)
  11. $ErrorActionPreference = "Stop"
  12. $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
  13. $ProjectDir = Split-Path -Parent $ScriptDir
  14. $VenvDir = Join-Path $ProjectDir ".venv"
  15. $DataDir = Join-Path $env:USERPROFILE ".agentpaas\data"
  16. $EnvFile = Join-Path $env:USERPROFILE ".agentpaas\.env"
  17. function Write-Info { param($m) Write-Host "ℹ $m" -ForegroundColor Cyan }
  18. function Write-Success { param($m) Write-Host "✅ $m" -ForegroundColor Green }
  19. function Write-Warn { param($m) Write-Host "⚠️ $m" -ForegroundColor Yellow }
  20. function Exit-Error { param($m) Write-Host "❌ $m" -ForegroundColor Red; exit 1 }
  21. Write-Host "`n=== LambdaAgentPaaS Native 安装 (Windows) ===`n" -ForegroundColor Magenta
  22. # ── 检查 Python ──────────────────────────────────────────────
  23. $pythonCmd = $null
  24. foreach ($cmd in @("python", "python3", "py")) {
  25. try {
  26. $ver = & $cmd -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')" 2>$null
  27. $major, $minor = $ver.Split('.') | ForEach-Object { [int]$_ }
  28. if ($major -gt 3 -or ($major -eq 3 -and $minor -ge 10)) {
  29. $pythonCmd = $cmd; break
  30. }
  31. } catch {}
  32. }
  33. if (-not $pythonCmd) {
  34. Exit-Error "需要 Python 3.10+。请从 https://www.python.org/ 安装(勾选 Add to PATH)。"
  35. }
  36. Write-Info "使用 Python: $(& $pythonCmd --version)"
  37. # ── 检查 Node.js ─────────────────────────────────────────────
  38. if (-not (Get-Command node -ErrorAction SilentlyContinue)) {
  39. Exit-Error "需要 Node.js 18+。请从 https://nodejs.org/ 安装后重试。"
  40. }
  41. $nodeVer = (node -e "process.stdout.write(process.version.replace('v',''))").Split('.')[0]
  42. if ([int]$nodeVer -lt 18) { Exit-Error "需要 Node.js 18+(当前 v$nodeVer)" }
  43. Write-Info "使用 Node.js: $(node --version)"
  44. # ── 创建虚拟环境 ─────────────────────────────────────────────
  45. if (-not (Test-Path $VenvDir)) {
  46. Write-Info "创建 Python 虚拟环境..."
  47. & $pythonCmd -m venv $VenvDir
  48. }
  49. $venvPy = Join-Path $VenvDir "Scripts\python.exe"
  50. $venvPip = Join-Path $VenvDir "Scripts\pip.exe"
  51. # ── 安装 Python 依赖 ─────────────────────────────────────────
  52. Write-Info "安装 Python 依赖(首次约 3-5 分钟)..."
  53. Set-Location $ProjectDir
  54. # 国内网络可改为 https://pypi.tuna.tsinghua.edu.cn/simple
  55. $pipIndex = "" # "-i https://pypi.tuna.tsinghua.edu.cn/simple"
  56. & $venvPip install --upgrade pip $pipIndex -q
  57. & $venvPip install -e "lambdagent[all]" $pipIndex -q
  58. & $venvPip install -e "agentpaas[all]" $pipIndex -q
  59. & $venvPip install pymupdf $pipIndex -q
  60. Write-Success "Python 依赖安装完成"
  61. # ── 构建前端 ─────────────────────────────────────────────────
  62. Write-Info "安装前端依赖..."
  63. Set-Location (Join-Path $ProjectDir "webui")
  64. npm ci --prefer-offline --loglevel=error
  65. Write-Info "构建前端..."
  66. npm run build
  67. Write-Success "前端构建完成 → webui-dist/"
  68. Set-Location $ProjectDir
  69. # ── 初始化数据目录 ────────────────────────────────────────────
  70. $dirs = @($DataDir,
  71. (Join-Path $DataDir "instances"),
  72. (Join-Path $DataDir "knowledge_bases"),
  73. (Join-Path $DataDir "logs"))
  74. foreach ($d in $dirs) {
  75. if (-not (Test-Path $d)) { New-Item -ItemType Directory $d -Force | Out-Null }
  76. }
  77. # ── 初始化配置文件 ────────────────────────────────────────────
  78. if (-not (Test-Path $EnvFile)) {
  79. $configDir = Split-Path -Parent $EnvFile
  80. if (-not (Test-Path $configDir)) { New-Item -ItemType Directory $configDir -Force | Out-Null }
  81. Copy-Item (Join-Path $ProjectDir ".env.template") $EnvFile -Force
  82. # 生成 MASTER_KEY
  83. $bytes = [byte[]]::new(32)
  84. [System.Security.Cryptography.RandomNumberGenerator]::Create().GetBytes($bytes)
  85. $masterKey = ($bytes | ForEach-Object { $_.ToString("x2") }) -join ""
  86. $content = Get-Content $EnvFile
  87. $content = $content -replace "^AGENTPAAS_DATA_DIR=.*", "AGENTPAAS_DATA_DIR=$DataDir"
  88. $content = $content -replace "^AGENTPAAS_MASTER_KEY=.*", "AGENTPAAS_MASTER_KEY=$masterKey"
  89. $content | Set-Content $EnvFile -Encoding UTF8
  90. Write-Success "配置文件已创建: $EnvFile"
  91. Write-Warn "请编辑 $EnvFile 填写至少一个 LLM API Key"
  92. }
  93. # ── 创建 Windows 启动快捷脚本 ────────────────────────────────
  94. $runnerScript = Join-Path (Split-Path -Parent $EnvFile) "run.ps1"
  95. @"
  96. # LambdaAgentPaaS 启动器(由 install-native.ps1 生成)
  97. Get-Content '$EnvFile' | ForEach-Object {
  98. if (`$_ -match '^\s*([^#][^=]+)=(.*)$') {
  99. [System.Environment]::SetEnvironmentVariable(`$matches[1].Trim(), `$matches[2].Trim(), 'Process')
  100. }
  101. }
  102. `$env:AGENTPAAS_DATA_DIR = '$DataDir'
  103. Set-Location '$ProjectDir'
  104. & '$venvPy' -m agentpaas serve --host 0.0.0.0 --port (`$env:AGENTPAAS_PORT ?? '8000')
  105. "@ | Set-Content $runnerScript -Encoding UTF8
  106. # ── 创建桌面快捷方式 ─────────────────────────────────────────
  107. $desktopShortcut = Join-Path ([System.Environment]::GetFolderPath('Desktop')) "LambdaAgentPaaS.lnk"
  108. $wsh = New-Object -ComObject WScript.Shell
  109. $shortcut = $wsh.CreateShortcut($desktopShortcut)
  110. $shortcut.TargetPath = "powershell.exe"
  111. $shortcut.Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$runnerScript`""
  112. $shortcut.WorkingDirectory = $ProjectDir
  113. $shortcut.Description = "LambdaAgentPaaS"
  114. $shortcut.IconLocation = "powershell.exe,0"
  115. $shortcut.Save()
  116. Write-Success "桌面快捷方式已创建"
  117. # ── 打印完成信息 ─────────────────────────────────────────────
  118. Write-Host ""
  119. Write-Host "╔══════════════════════════════════════════════╗" -ForegroundColor Green
  120. Write-Host "║ ✅ LambdaAgentPaaS 安装完成! ║" -ForegroundColor Green
  121. Write-Host "╠══════════════════════════════════════════════╣" -ForegroundColor Green
  122. Write-Host "║ 启动方式 1: 双击桌面 LambdaAgentPaaS 图标 ║" -ForegroundColor Green
  123. Write-Host "║ 启动方式 2: powershell $runnerScript ║" -ForegroundColor Green
  124. Write-Host "║ 配置文件: $EnvFile" -ForegroundColor Green
  125. Write-Host "║ 数据目录: $DataDir" -ForegroundColor Green
  126. Write-Host "╚══════════════════════════════════════════════╝" -ForegroundColor Green
  127. Write-Host ""
  128. if ($Start) {
  129. Write-Info "正在启动服务..."
  130. & powershell -NoProfile -ExecutionPolicy Bypass -File $runnerScript
  131. }