<# OpenCode 插件安装脚本:install_opencode_supermemory.ps1 功能:在 Windows 环境下,使用本地 bun.exe 安装 opencode-supermemory 插件,支持指定版本或 latest,并可注入 SUPERMEMORY_API_KEY。 用法示例: - 指定版本安装:powershell -ExecutionPolicy Bypass -File scripts\install_opencode_supermemory.ps1 -Version 3.3.8 -ApiKey "sm_你的密钥" - 安装最新版本:powershell -ExecutionPolicy Bypass -File scripts\install_opencode_supermemory.ps1 -Version latest #> param( [ValidateSet("latest","3.3.8")] [string]$Version = "3.3.8", [string]$BunExePath = "C:\\Soft\\bun-windows-x64\\bun.exe", [string]$ApiKey ) $ErrorActionPreference = "Stop" Write-Host "---------- OpenCode: Install opencode-supermemory ----------" if (-Not (Test-Path $BunExePath)) { Write-Error "Bun 可执行文件未找到:$BunExePath" Write-Host "请确保 bun.exe 位于该路径,或调整参数 -BunExePath。" exit 1 } # 1) 将 Bun 目录加入用户 PATH(确保会话后续可用) $bunDir = [System.IO.Path]::GetDirectoryName($BunExePath) try { $currentPath = [Environment]::GetEnvironmentVariable("PATH", [EnvironmentVariableTarget]::User) $paths = @($currentPath -split ";" | Where-Object { $_ -and $_ -ne $null }) if (-Not ($paths -contains $bunDir)) { $newPath = $currentPath + ";" + $bunDir [Environment]::SetEnvironmentVariable("PATH", $newPath, [EnvironmentVariableTarget]::User) Write-Host "PATH 已更新,已包含 Bun 目录:$bunDir (用户作用域)" } else { Write-Host "PATH 已包含 Bun 目录:$bunDir" } } catch { Write-Warning "无法更新 PATH:$($_.Exception.Message)" } # 2) 验证 bun 与 bunx 是否可用 try { $bunVer = & "${BunExePath}" --version Write-Host "Bun 版本:$bunVer" $bunxPath = Join-Path $bunDir "bunx.exe" if (Test-Path $bunxPath) { $bunxVer = & "$bunxPath" --version Write-Host "Bunx 版本:$bunxVer" } else { Write-Host "未找到 bunx.exe,将使用 bun.exe 进行安装。" } } catch { Write-Error "无法验证 Bun:$($_.Exception.Message)" exit 1 } # 3) 安装插件 if ($Version -eq "latest") { $installTarget = "opencode-supermemory@latest install" } else { $installTarget = "opencode-supermemory@${Version} install" } try { if (Test-Path (Join-Path $bunDir "bunx.exe")) { & (Join-Path $bunDir "bunx.exe") $installTarget } else { & (Join-Path $bunDir "bun.exe") $installTarget } } catch { Write-Error "安装插件失败: $($_.Exception.Message)" exit 1 } # 4) 设置 SUPERMEMORY_API_KEY(若提供) if (-not [string]::IsNullOrEmpty($ApiKey)) { [Environment]::SetEnvironmentVariable("SUPERMEMORY_API_KEY", $ApiKey, [EnvironmentVariableTarget]::User) Write-Host "SUPERMEMORY_API_KEY 已设置(用户级变量)" } elseif ($Env:SUPERMEMORY_API_KEY) { Write-Host "检测到已有 SUPERMEMORY_API_KEY 环境变量。" } else { Write-Host "未提供 SUPERMEMORY_API_KEY。若要使用云记忆,请提供密钥。" } Write-Host "安装流程完成。请重新打开终端以刷新 PATH 与环境变量。" Exit 0