<# .SYNOPSIS OpenCode 一键接入配置脚本(Liangfeng Relay / ai.liangfeng.net.cn) .DESCRIPTION 写入或合并 %USERPROFILE%\.config\opencode\opencode.json,添加名为 liangfeng 的 OpenAI 兼容供应商;已有配置会先备份为 opencode.json.bak。 .EXAMPLE .\configure-opencode.ps1 -ApiKey "sk-你的密钥" .EXAMPLE .\configure-opencode.ps1 -ApiKey "sk-你的密钥" -Model "glm-5.3-flash" #> param( [Parameter(Mandatory = $true)][string]$ApiKey, [string]$BaseUrl = "https://ai.liangfeng.net.cn/v1", [string]$Model = "deepseek-v4.1-flash", [string]$ProviderId = "liangfeng" ) $ErrorActionPreference = "Stop" $configDir = Join-Path $env:USERPROFILE ".config\opencode" $configPath = Join-Path $configDir "opencode.json" New-Item -ItemType Directory -Force -Path $configDir | Out-Null $models = [ordered]@{ "deepseek-v4.1-flash" = @{ name = "DeepSeek V4.1 Flash" } "deepseek-v4-pro" = @{ name = "DeepSeek V4 Pro" } "glm-5.3-flash" = @{ name = "GLM-5.3 Flash" } "qwen3.8-flash" = @{ name = "Qwen 3.8 Flash" } "gpt-5.6-terra" = @{ name = "GPT-5.6 Terra" } } if (Test-Path $configPath) { Copy-Item $configPath "$configPath.bak" -Force Write-Host "[i] 已备份原配置到 $configPath.bak" try { $cfg = Get-Content $configPath -Raw -Encoding UTF8 | ConvertFrom-Json -AsHashtable } catch { Write-Host "[!] 原配置不是合法 JSON,无法自动合并。请手动编辑:$configPath" exit 1 } } else { $cfg = @{ '$schema' = 'https://opencode.ai/config.json' } } if (-not $cfg.ContainsKey('provider') -or $null -eq $cfg['provider']) { $cfg['provider'] = @{} } $cfg['provider'][$ProviderId] = @{ npm = "@ai-sdk/openai-compatible" name = "Liangfeng Relay" options = @{ baseURL = $BaseUrl; apiKey = $ApiKey } models = $models } $cfg['model'] = "$ProviderId/$Model" $json = $cfg | ConvertTo-Json -Depth 12 [System.IO.File]::WriteAllText($configPath, $json, [System.Text.UTF8Encoding]::new($false)) Write-Host "" Write-Host "[OK] 配置已写入: $configPath" -ForegroundColor Green Write-Host " 供应商: $ProviderId ($BaseUrl)" Write-Host " 默认模型: $ProviderId/$Model" Write-Host "" Write-Host "下一步:启动 OpenCode,执行 /models 选择 '$ProviderId/$Model' 验证。"