#Requires -Version 7 # ============================================================= # Claude Code + DeepSeek 一键配置脚本 # 用法:在 pwsh (PowerShell 7) 中执行此脚本 # ============================================================= $DEEPSEEK_API_KEY = "sk-a7e8782b1d5547a692ef2efec55b44f2" $DEEPSEEK_BASE_URL = "https://api.deepseek.com/anthropic" $DEEPSEEK_MODEL = "deepseek-v3-0324" Write-Host "=== Claude Code + DeepSeek 配置开始 ===" -ForegroundColor Cyan # ── 1. 检查 Node.js ────────────────────────────────────────── Write-Host "`n[1/4] 检查 Node.js..." -ForegroundColor Yellow if (-not (Get-Command node -ErrorAction SilentlyContinue)) { Write-Host "未找到 Node.js,请先安装:https://nodejs.org/" -ForegroundColor Red exit 1 } Write-Host "Node.js $(node --version) 已安装" -ForegroundColor Green # ── 2. 安装 Claude Code ─────────────────────────────────────── Write-Host "`n[2/4] 安装 Claude Code..." -ForegroundColor Yellow if (Get-Command claude -ErrorAction SilentlyContinue) { Write-Host "Claude Code 已安装:$(claude --version 2>&1)" -ForegroundColor Green } else { npm install -g @anthropic-ai/claude-code if ($LASTEXITCODE -ne 0) { Write-Host "安装失败,请检查 npm 网络" -ForegroundColor Red; exit 1 } Write-Host "Claude Code 安装成功" -ForegroundColor Green } # ── 3. 写入 ~/.claude/settings.json(仅保留 model) ────────── Write-Host "`n[3/4] 配置 ~/.claude/settings.json..." -ForegroundColor Yellow $claudeDir = "$env:USERPROFILE\.claude" if (-not (Test-Path $claudeDir)) { New-Item -ItemType Directory -Path $claudeDir | Out-Null } @{ model = $DEEPSEEK_MODEL } | ConvertTo-Json | Set-Content -Path "$claudeDir\settings.json" -Encoding UTF8 Write-Host "已写入 $claudeDir\settings.json" -ForegroundColor Green # ── 4. 设置用户级环境变量 + pwsh profile ────────────────────── Write-Host "`n[4/4] 设置环境变量与 PowerShell profile..." -ForegroundColor Yellow [System.Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", $DEEPSEEK_BASE_URL, "User") [System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", $DEEPSEEK_API_KEY, "User") [System.Environment]::SetEnvironmentVariable("CLAUDE_MODEL", $DEEPSEEK_MODEL, "User") Write-Host "用户级环境变量已设置" -ForegroundColor Green # 仅写入 pwsh (PowerShell 7) profile $pwshProfile = "$env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1" $profileDir = Split-Path $pwshProfile if (-not (Test-Path $profileDir)) { New-Item -ItemType Directory -Path $profileDir | Out-Null } $profileContent = @" `$env:ANTHROPIC_BASE_URL = "$DEEPSEEK_BASE_URL" `$env:ANTHROPIC_API_KEY = "$DEEPSEEK_API_KEY" `$env:CLAUDE_MODEL = "$DEEPSEEK_MODEL" "@ if (Test-Path $pwshProfile) { $existing = Get-Content $pwshProfile -Raw if ($existing -match "deepseek\.com") { Set-Content -Path $pwshProfile -Value $profileContent -Encoding UTF8 Write-Host "已更新:$pwshProfile" -ForegroundColor Green } else { Add-Content -Path $pwshProfile -Value "`n$profileContent" Write-Host "已追加到:$pwshProfile" -ForegroundColor Green } } else { Set-Content -Path $pwshProfile -Value $profileContent -Encoding UTF8 Write-Host "已创建:$pwshProfile" -ForegroundColor Green } # ── 确保执行策略允许 profile 运行 ───────────────────────────── $policy = Get-ExecutionPolicy -Scope CurrentUser if ($policy -eq "Restricted" -or $policy -eq "Undefined") { Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force Write-Host "已设置执行策略:RemoteSigned" -ForegroundColor Green } # ── 验证连接 ────────────────────────────────────────────────── Write-Host "`n=== 验证连接 ===" -ForegroundColor Cyan $env:ANTHROPIC_BASE_URL = $DEEPSEEK_BASE_URL $env:ANTHROPIC_API_KEY = $DEEPSEEK_API_KEY $result = claude -p "say hi" 2>&1 if ($LASTEXITCODE -eq 0) { Write-Host "连接成功:$result" -ForegroundColor Green } else { Write-Host "连接失败:$result" -ForegroundColor Red } Write-Host "`n=== 配置完成!新开终端直接运行 claude 即可 ===" -ForegroundColor Cyan