# 5laoban AI gateway: one-step Claude Code setup for Windows (PowerShell 5.1 or later). # macOS / Linux: use claude-setup.sh in the same folder. # # Usage, in Command Prompt or PowerShell: # powershell -c "irm https://ai.5lb.cn/helper/claude-setup.ps1 | iex" # or save this file, then run: powershell -ExecutionPolicy Bypass -File claude-setup.ps1 # There is no automatic undo (decided 2026-11-23): when undoing, the script cannot tell who set the other # credentials, so removing the address could send a company credential set elsewhere to Anthropic, and keeping # it could send the member's own account credential to the gateway. CLAUDE_SETUP_REMOVE=1 only prints the # manual steps and changes nothing. # # What it does (the same rules as claude-setup.sh): # 1. Asks for the member's Claude key (applied for under "My keys" at https://ai.5lb.cn/me/; it is not the # GPT personal key). When settings.json already points at the gateway, pressing Enter keeps the key it # has. The key is checked with GET /v1/models: rejected (401), unreachable, not a model list, or no # claude-* model in data[] (a GPT key) all stop here and change nothing. # 2. Merges into %USERPROFILE%\.claude\settings.json (or $env:CLAUDE_CONFIG_DIR\settings.json): # env.ANTHROPIC_BASE_URL, env.ANTHROPIC_AUTH_TOKEN, env.ANTHROPIC_DEFAULT_{OPUS,SONNET,HAIKU,FABLE}_MODEL, # env.CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC and the top-level skipWebFetchPreflight. Every other key # is kept. It stops on a file that is not strict JSON, holds a number that would change when written # back, is nested too deeply, has a non-object top level or env, or is a link / reparse point. # The wulaoban-claude-setup.json an older version wrote next to it (an install record) is no longer read # or written; the manual steps say it can be deleted. # 3. If Claude Code is missing and Node.js 22 or later with npm is available, installs it from the China # npm mirror; otherwise says how to install Node.js. The settings are written either way. An installed # Claude Code that is too old for the pinned models is reported, not upgraded. # 4. If PowerShell would run npm's claude.ps1 shim and the execution policy blocks it, offers to allow # local scripts for your account, and only changes it when you agree (as codex-setup.ps1 does). # Files holding the key are created empty, restricted to the current user and checked before anything is # written into them; a file that cannot be restricted stops the run. One instance at a time changes a given # settings.json (an exclusive lock file); if the file changes between the snapshot and the swap, it is merged # again (up to 3 times). # # Optional environment variables: # WULAOBAN_CLAUDE_KEY pass the Claude key without typing it (removed from this process right after) # CLAUDE_CONFIG_DIR Claude Code's configuration folder (Claude Code reads it too) # CLAUDE_SETUP_SKIP_INSTALL set to 1 to skip installing Claude Code # CLAUDE_SETUP_REMOVE set to 1 to print the manual undo steps instead of setting up # CLAUDE_SETUP_LOCK_TRIES how many 0.2 s waits for another instance (tests only, default 75) # # PowerShell's own JSON reader is lenient (comments, trailing commas, single quotes), so the file is first # checked by Test-StrictJson; ConvertFrom-Json and ConvertTo-Json then do the merge, and the result is checked # again (strict JSON, same numbers) before it is swapped in. # # This file is ASCII only on purpose, like codex-setup.ps1: Windows PowerShell 5.1 misreads UTF-8 files that # have no BOM, so every Chinese message is written as \uXXXX escapes and decoded at run time by the U # function. The messages must not contain any other backslash ([regex]::Unescape would read it as an escape). function Invoke-WulaobanClaudeSetup { $Gateway = 'https://ai.5lb.cn' $ModelsUrl = "$Gateway/v1/models" $KeyPage = 'https://ai.5lb.cn/me/' $Fixed = [ordered]@{ ANTHROPIC_DEFAULT_OPUS_MODEL = 'claude-opus-5-5' ANTHROPIC_DEFAULT_SONNET_MODEL = 'claude-sonnet-5-5' ANTHROPIC_DEFAULT_HAIKU_MODEL = 'claude-haiku-5-5' ANTHROPIC_DEFAULT_FABLE_MODEL = 'claude-fable-5-2' CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC = '1' } $Spec = @{ Base = $Gateway; Fixed = $Fixed } $NpmPackage = '@anthropic-ai/claude-code@latest' $NpmRegistry = 'https://registry.npmmirror.com' $NodeMirror = 'https://npmmirror.com/mirrors/node' $MinNode = 22 $ClaudeDir = Join-Path $HOME '.claude' if ($env:CLAUDE_CONFIG_DIR) { $ClaudeDir = $env:CLAUDE_CONFIG_DIR } $Settings = Join-Path $ClaudeDir 'settings.json' $Stamp = Get-Date -Format 'yyyyMMdd-HHmmss' $Utf8NoBom = New-Object System.Text.UTF8Encoding($false) $Remove = ($env:CLAUDE_SETUP_REMOVE -eq '1') # The key from the environment is read once and removed from this process, so npm, claude and anything # else started later do not inherit it. $ApiKey = $env:WULAOBAN_CLAUDE_KEY [Environment]::SetEnvironmentVariable('WULAOBAN_CLAUDE_KEY', $null, 'Process') if ($Remove) { Write-Host (U '=== \u65e0\u8001\u677fAI\uff1a\u64a4\u56de Claude Code \u7684\u7f51\u5173\u8bbe\u7f6e ===') -ForegroundColor Green Write-Host (U '\u811a\u672c\u4e0d\u81ea\u52a8\u64a4\u56de\uff0c\u8fd9\u6b21\u4e5f\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') Write-ManualSteps return } # The gateway only accepts TLS 1.2; older Windows PowerShell does not enable it by default. try { [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12 } catch { } Write-Host (U '=== \u65e0\u8001\u677fAI\uff1aClaude Code \u914d\u7f6e ===') -ForegroundColor Green Write-Host ((U '\u7f51\u5173\u5730\u5740\uff1a{0}') -f $Gateway) Write-Host '' if (Test-Path -LiteralPath $Settings -PathType Container) { Write-Host ((U '\u9519\u8bef\uff1a{0} \u662f\u4e2a\u76ee\u5f55\uff0c\u811a\u672c\u4e0d\u5904\u7406\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $Settings) -ForegroundColor Red return } if (Test-ReparsePoint $Settings) { Write-Host ((U '\u9519\u8bef\uff1a{0} \u662f\u4e2a\u94fe\u63a5\uff08\u6216\u8005\u522b\u7684\u91cd\u89e3\u6790\u70b9\uff09\uff0cWindows \u7248\u811a\u672c\u4e0d\u5904\u7406\uff1a\u8bf7\u628a\u5b83\u6362\u6210\u666e\u901a\u6587\u4ef6\u540e\u91cd\u65b0\u8fd0\u884c\uff0c\u6216\u8005\u624b\u52a8\u914d\u7f6e\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $Settings) -ForegroundColor Red return } # 1. Read the current settings.json first: stop if it cannot be handled; take the old key when it # already points at this gateway. $OldKey = $null if (Test-Path -LiteralPath $Settings -PathType Leaf) { $state = Read-Settings ([IO.File]::ReadAllBytes($Settings)) $Spec if ($state.Status -ne 'ok') { Write-SettingsError $state.Status $Settings; return } $OldKey = Get-ReusableKey $state.Data $Gateway } # 2. Read the key. Already set up for the gateway: pressing Enter keeps the key it has. $Reused = $false if (-not $ApiKey) { if ($OldKey) { Write-Host ((U '\u8fd9\u53f0\u7535\u8111\u7684 Claude Code \u5df2\u7ecf\u914d\u7f6e\u8fc7\u65e0\u8001\u677fAI \u7684\u5bc6\u94a5\uff08\u7ed3\u5c3e ****{0}\uff09\u3002') -f $OldKey.Substring($OldKey.Length - 4)) $prompt = U '\u76f4\u63a5\u56de\u8f66\u6cbf\u7528\u8fd9\u628a\u5bc6\u94a5\uff1b\u8981\u6362\u5bc6\u94a5\u5c31\u7c98\u8d34\u65b0\u7684\uff08\u7c98\u8d34\u65f6\u5c4f\u5e55\u4e0a\u4e0d\u663e\u793a\uff09\uff0c\u7136\u540e\u56de\u8f66' } else { Write-Host ((U 'Claude \u5bc6\u94a5\u5728 {0} \u7684\u300c\u6211\u7684\u5bc6\u94a5\u300d\u91cc\u7533\u8bf7\uff0c\u7ba1\u7406\u5458\u5ba1\u6279\u540e\u5c31\u80fd\u770b\u5230\uff1b\u5b83\u548c GPT \u7684\u4e2a\u4eba\u5bc6\u94a5\u4e0d\u662f\u540c\u4e00\u628a\u3002') -f $KeyPage) $prompt = U '\u8bf7\u7c98\u8d34\u4f60\u7684 Claude \u5bc6\u94a5\uff08\u7c98\u8d34\u65f6\u5c4f\u5e55\u4e0a\u4e0d\u663e\u793a\uff09\uff0c\u7136\u540e\u56de\u8f66' } $secure = Read-Host -Prompt $prompt -AsSecureString if ($secure) { $ApiKey = (New-Object System.Net.NetworkCredential('', $secure)).Password } } if ($ApiKey) { $ApiKey = $ApiKey -replace '\s', '' } if (-not $ApiKey -and $OldKey) { $ApiKey = $OldKey $Reused = $true Write-Host (U '\u6cbf\u7528\u539f\u6765\u7684\u5bc6\u94a5\u3002') } if (-not $ApiKey) { Write-Host (U '\u9519\u8bef\uff1a\u5bc6\u94a5\u4e0d\u80fd\u4e3a\u7a7a\u3002') -ForegroundColor Red return } if ($ApiKey -notmatch '^[A-Za-z0-9_-]+$') { Write-Host (U '\u9519\u8bef\uff1a\u5bc6\u94a5\u91cc\u6709\u4e0d\u8be5\u51fa\u73b0\u7684\u5b57\u7b26\uff0c\u8bf7\u91cd\u65b0\u590d\u5236\u4e00\u6b21\uff08\u53ea\u5e94\u5305\u542b\u5b57\u6bcd\u3001\u6570\u5b57\u3001- \u548c _\uff09\u3002') -ForegroundColor Red return } if ($ApiKey -cnotmatch '^sk-') { Write-Host (U '\u63d0\u793a\uff1a\u5bc6\u94a5\u901a\u5e38\u4ee5 sk- \u5f00\u5934\uff0c\u8bf7\u786e\u8ba4\u590d\u5236\u5b8c\u6574\u3002') -ForegroundColor Yellow } # 3. Check the key against the gateway: it must be accepted, the answer must be a model list, and the # list must have Claude models. Write-Host (U '\u6b63\u5728\u5411\u7f51\u5173\u6821\u9a8c\u5bc6\u94a5\u2026\u2026') $content = $null try { $resp = Invoke-WebRequest -UseBasicParsing -Uri $ModelsUrl -Headers @{ Authorization = "Bearer $ApiKey" } -TimeoutSec 20 $content = [string]$resp.Content } catch { $code = 0 try { $code = [int]$_.Exception.Response.StatusCode } catch { } if ($code -eq 401 -and $Reused) { Write-Host ((U '\u9519\u8bef\uff1a\u7f51\u5173\u4e0d\u8ba4\u539f\u6765\u7684\u5bc6\u94a5\uff08401\uff09\uff0c\u5b83\u53ef\u80fd\u5df2\u7ecf\u505c\u7528\u3002\u8bf7\u5230 {0} \u67e5\u770b\uff0c\u6216\u8005\u627e\u7ba1\u7406\u5458\uff0c\u91cd\u65b0\u8fd0\u884c\u65f6\u7c98\u8d34\u65b0\u7684 Claude \u5bc6\u94a5\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $KeyPage) -ForegroundColor Red } elseif ($code -eq 401) { Write-Host ((U '\u9519\u8bef\uff1a\u7f51\u5173\u4e0d\u8ba4\u8fd9\u4e2a\u5bc6\u94a5\uff08401\uff09\u3002\u8bf7\u786e\u8ba4\u590d\u5236\u5b8c\u6574\uff0c\u6216\u8005\u5230 {0} \u67e5\u770b\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $KeyPage) -ForegroundColor Red } elseif ($code -eq 0) { Write-ConnectFailure $Gateway $_.Exception } else { Write-Host ((U '\u9519\u8bef\uff1a\u7f51\u5173\u8fd4\u56de\u4e86 {0}\uff0c\u8bf7\u7a0d\u540e\u91cd\u8bd5\u6216\u8054\u7cfb\u7ba1\u7406\u5458\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $code) -ForegroundColor Red } return } $Ids = Get-ModelIds $content if ($null -eq $Ids) { Write-Host (U '\u9519\u8bef\uff1a\u7f51\u5173\u7684\u56de\u5e94\u4e0d\u662f\u6a21\u578b\u5217\u8868\uff08\u53ef\u80fd\u88ab\u4ee3\u7406\u6216\u9632\u706b\u5899\u6539\u8fc7\uff09\uff0c\u770b\u4e0d\u51fa\u662f\u4e0d\u662f Claude \u5bc6\u94a5\u3002\u8bf7\u7a0d\u540e\u91cd\u8bd5\uff1b\u8fd8\u4e0d\u884c\u5c31\u627e\u7ba1\u7406\u5458\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -ForegroundColor Red return } $ClaudeIds = @($Ids | Where-Object { $_ -clike 'claude-*' }) if ($ClaudeIds.Count -eq 0) { if (@($Ids).Count -gt 0) { Write-Host ((U '\u9519\u8bef\uff1a\u8fd9\u628a\u5bc6\u94a5\u80fd\u7528\uff0c\u4f46\u5b83\u6ca1\u6709 Claude \u6a21\u578b\uff0c\u591a\u534a\u662f GPT \u7684\u4e2a\u4eba\u5bc6\u94a5\u3002Claude Code \u8981\u7528\u5355\u72ec\u7684 Claude \u5bc6\u94a5\uff1a\u5230 {0} \u7684\u300c\u6211\u7684\u5bc6\u94a5\u300d\u7533\u8bf7\uff0c\u7ba1\u7406\u5458\u5ba1\u6279\u540e\u5c31\u80fd\u770b\u5230\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $KeyPage) -ForegroundColor Red } else { Write-Host (U '\u9519\u8bef\uff1a\u7f51\u5173\u8ba4\u8fd9\u4e2a\u5bc6\u94a5\uff0c\u4f46\u6ca1\u6709\u8fd4\u56de\u6a21\u578b\u5217\u8868\uff0c\u770b\u4e0d\u51fa\u662f\u4e0d\u662f Claude \u5bc6\u94a5\u3002\u8bf7\u7a0d\u540e\u91cd\u8bd5\uff1b\u8fd8\u4e0d\u884c\u5c31\u627e\u7ba1\u7406\u5458\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -ForegroundColor Red } return } Write-Host ((U '\u5bc6\u94a5\u6709\u6548\u3002\u53ef\u7528\u7684 Claude \u6a21\u578b\uff1a{0}') -f ($ClaudeIds -join ' ')) -ForegroundColor Green $missing = @($Fixed.Values | Where-Object { $_ -like 'claude-*' -and $ClaudeIds -cnotcontains $_ }) if ($missing.Count -gt 0) { Write-Host ((U '\u63d0\u793a\uff1a\u7f51\u5173\u7684\u6a21\u578b\u5217\u8868\u91cc\u6ca1\u6709 {0}\uff0c\u7528\u5230\u65f6\u4f1a\u62a5\u9519\uff0c\u8bf7\u544a\u8bc9\u7ba1\u7406\u5458\u3002') -f ($missing -join (U '\u3001'))) -ForegroundColor Yellow } # 4. Write settings.json. $written = Write-ClaudeSettings $Spec $Settings $Stamp $Utf8NoBom $ApiKey if ($null -eq $written) { return } Write-Host ((U '\u914d\u7f6e\u5df2\u5199\u5165\uff1a{0}\uff08\u5bc6\u94a5\u7ed3\u5c3e ****{1}\uff0c\u6587\u4ef6\u53ea\u6709\u4f60\u80fd\u8bfb\uff09') -f $Settings, $ApiKey.Substring($ApiKey.Length - 4)) -ForegroundColor Green Write-Conflicts $written Write-Host '' # 5. Install Claude Code when it is missing and Node.js 22+ with npm is available; check the version of # one that is already there. $HasCli = $false $Claude = Find-Claude $Npm = Find-Shim 'npm' $Node = Get-Command 'node' -CommandType Application -ErrorAction SilentlyContinue | Select-Object -First 1 $ShowNode = $false if ($Claude) { $HasCli = $true $ver = Get-ClaudeVersion $Claude Write-Host ((U '\u5df2\u5b89\u88c5 Claude Code\uff1a{0}') -f (Show-Version $ver)) Write-PathHint $Claude Test-ClaudeVersion $ver $Claude $NpmPackage $NpmRegistry } elseif ($env:CLAUDE_SETUP_SKIP_INSTALL -eq '1') { Write-Host (U '\u6309\u8981\u6c42\u8df3\u8fc7\u4e86 Claude Code \u7684\u5b89\u88c5\u3002') } elseif (-not $Node) { Write-Host (U '\u6ca1\u6709\u68c0\u6d4b\u5230 Claude Code\uff0c\u4e5f\u6ca1\u6709 Node.js\uff0c\u6ca1\u6cd5\u81ea\u52a8\u5b89\u88c5\u3002') -ForegroundColor Yellow $ShowNode = $true } else { $nodeVer = '' try { $nodeVer = [string](& $Node.Path --version 2>$null | Select-Object -First 1) } catch { } $major = 0 if ($nodeVer -match '^v?(\d+)\.') { $major = [int]$Matches[1] } if ($major -lt $MinNode) { Write-Host ((U '\u6ca1\u6709\u68c0\u6d4b\u5230 Claude Code\u3002\u8fd9\u53f0\u7535\u8111\u7684 Node.js \u662f {0}\uff0cClaude Code \u8981 {1} \u4ee5\u4e0a\uff0c\u6ca1\u6cd5\u81ea\u52a8\u5b89\u88c5\u3002') -f (Show-Version $nodeVer), $MinNode) -ForegroundColor Yellow $ShowNode = $true } elseif (-not $Npm) { Write-Host ((U '\u6ca1\u6709\u68c0\u6d4b\u5230 Claude Code\u3002\u6709 Node.js {0}\uff0c\u4f46\u627e\u4e0d\u5230 npm\uff0c\u6ca1\u6cd5\u81ea\u52a8\u5b89\u88c5\u3002\u7528\u5b98\u65b9\u5b89\u88c5\u5305\u91cd\u88c5 Node.js\uff08\u5b83\u81ea\u5e26 npm\uff09\u540e\u91cd\u65b0\u8fd0\u884c\u672c\u811a\u672c\u3002\u914d\u7f6e\u5df2\u7ecf\u5199\u597d\u3002') -f $nodeVer) -ForegroundColor Yellow } else { Write-Host (U '\u6ca1\u6709\u68c0\u6d4b\u5230 Claude Code\uff0c\u6b63\u5728\u7528 npm \u4ece\u56fd\u5185\u955c\u50cf\u5b89\u88c5\uff08\u8bf7\u7a0d\u7b49\uff09\u2026\u2026') & $Npm.Path install -g $NpmPackage "--registry=$NpmRegistry" if ($LASTEXITCODE -eq 0) { $Claude = Find-Claude if ($Claude) { $HasCli = $true $ver = Get-ClaudeVersion $Claude Write-Host ((U '\u5b89\u88c5\u5b8c\u6210\uff1a{0}') -f (Show-Version $ver)) -ForegroundColor Green # Some npm versions or settings skip install scripts and print an install-scripts warning; tested on # 2026-11-23 (npm 11.19.1) Claude Code works without its postinstall. The script does not allow # install scripts on the member's behalf. Only say it works when claude --version exited 0 and # printed an X.Y.Z version (code review R4 C1-11). if ($ver -cmatch '^\d+\.\d+\.\d+') { Write-Host (U ' \u4e0a\u9762\u5982\u679c\u6709 npm \u5173\u4e8e install-scripts\uff08\u5b89\u88c5\u811a\u672c\uff09\u7684\u8b66\u544a\uff0c\u53ef\u4ee5\u5ffd\u7565\uff1aClaude Code \u80fd\u6b63\u5e38\u7528\uff0c\u811a\u672c\u521a\u624d\u5df2\u7ecf\u7528 claude --version \u786e\u8ba4\u8fc7\u3002') } Write-PathHint $Claude Test-ClaudeVersion $ver $Claude $NpmPackage $NpmRegistry } else { Write-Host (U 'npm \u88c5\u5b8c\u4e86\uff0c\u4f46\u8fd8\u627e\u4e0d\u5230 claude \u547d\u4ee4\u3002\u5173\u6389\u8fd9\u4e2a\u7a97\u53e3\u91cd\u65b0\u6253\u5f00\u4e00\u4e2a\u518d\u8bd5\u3002') -ForegroundColor Yellow } } else { # Usually the global prefix is not writable (Node.js installed for all users with the prefix under # Program Files). Retrying the same command would fail again; move the prefix to the user profile. $appNpm = '%APPDATA%\npm' if ($env:APPDATA) { $appNpm = Join-Path $env:APPDATA 'npm' } Write-Host (U '\u81ea\u52a8\u5b89\u88c5\u6ca1\u6709\u6210\u529f\uff08\u5e38\u89c1\u539f\u56e0\u662f\u6ca1\u6709\u5199\u5165 npm \u5168\u5c40\u76ee\u5f55\u7684\u6743\u9650\uff09\u3002\u53ef\u4ee5\u628a npm \u7684\u5168\u5c40\u76ee\u5f55\u6362\u5230\u4f60\u81ea\u5df1\u7684\u7528\u6237\u76ee\u5f55\uff0c\u518d\u91cd\u65b0\u8fd0\u884c\u672c\u811a\u672c\uff1a') -ForegroundColor Yellow Write-Host ' npm config set prefix "%APPDATA%\npm"' -ForegroundColor Yellow Write-Host ((U ' \u7136\u540e\u786e\u8ba4\u7528\u6237\u73af\u5883\u53d8\u91cf Path \u91cc\u6709 {0}\uff08Node.js \u5b89\u88c5\u7a0b\u5e8f\u4e00\u822c\u5df2\u7ecf\u52a0\u597d\uff09\uff0c\u5173\u6389\u7a97\u53e3\u91cd\u65b0\u6253\u5f00\u547d\u4ee4\u63d0\u793a\u7b26\u3002') -f $appNpm) -ForegroundColor Yellow Write-Host (U '\u914d\u7f6e\u5df2\u7ecf\u5199\u597d\uff0c\u88c5\u4e0a\u5c31\u80fd\u7528\u3002') -ForegroundColor Yellow } } } if ($ShowNode) { # The newest LTS release on the mirror, e.g. v24.22.0; left empty when the index cannot be read. $NodeVersion = $null try { $index = ConvertFrom-Json (Invoke-WebRequest -UseBasicParsing -Uri "$NodeMirror/index.json" -TimeoutSec 15).Content foreach ($release in $index) { if ($release.lts) { $NodeVersion = $release.version; break } } } catch { } $nodeArch = 'x64' try { if ([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture -eq 'Arm64') { $nodeArch = 'arm64' } } catch { if ($env:PROCESSOR_ARCHITECTURE -eq 'ARM64') { $nodeArch = 'arm64' } } Write-Host ((U '\u5148\u5b89\u88c5 Node.js \u957f\u671f\u652f\u6301\u7248\uff08{0} \u4ee5\u4e0a\uff0c\u81ea\u5e26 npm\uff09\uff0c\u518d\u91cd\u65b0\u8fd0\u884c\u672c\u811a\u672c\uff1a') -f $MinNode) -ForegroundColor Yellow if ($NodeVersion) { Write-Host ((U ' \u56fd\u5185\u955c\u50cf\uff1a{0}') -f "$NodeMirror/$NodeVersion/node-$NodeVersion-$nodeArch.msi") -ForegroundColor Yellow } Write-Host (U ' \u5b98\u65b9\u4e0b\u8f7d\uff1ahttps://nodejs.org/zh-cn/download') -ForegroundColor Yellow Write-Host (U '\u914d\u7f6e\u5df2\u7ecf\u5199\u597d\uff0c\u88c5\u597d Node.js \u540e\u91cd\u65b0\u8fd0\u884c\u672c\u811a\u672c\uff0c\u5c31\u4f1a\u81ea\u52a8\u88c5\u4e0a Claude Code\u3002') -ForegroundColor Yellow } # Only npm's claude.ps1 shim is blocked by the execution policy; claude.exe (the official installer) and # claude.cmd are not. Ask only when typing claude in PowerShell would run that shim. $Lasting = $null if ($HasCli -and (Test-NeedsScriptPolicy $Claude)) { try { $Lasting = Get-LastingExecutionPolicy } catch { } } if ($Lasting -and (@('Restricted', 'AllSigned') -contains $Lasting[1])) { Write-Host '' Write-Host (U '\u8fd9\u53f0\u7535\u8111\u7684 PowerShell \u4e0d\u8ba9\u8fd0\u884c\u811a\u672c\uff0c\u5728 PowerShell \u91cc\u76f4\u63a5\u8f93\u5165 claude \u4f1a\u63d0\u793a\u300c\u7981\u6b62\u8fd0\u884c\u811a\u672c\u300d\uff0c\u547d\u4ee4\u63d0\u793a\u7b26\u91cc\u4e0d\u53d7\u5f71\u54cd\u3002') -ForegroundColor Yellow if ($Lasting[0] -like '*Policy') { Write-Host (U '\u8fd9\u9879\u8bbe\u7f6e\u7531\u516c\u53f8\u7684\u7ec4\u7b56\u7565\u7ba1\u7740\uff0c\u8fd9\u91cc\u6539\u4e0d\u4e86\uff1a\u5728 PowerShell \u91cc\u6539\u8f93\u5165 claude.cmd\uff0c\u6216\u8005\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u91cc\u4f7f\u7528\u3002') -ForegroundColor Yellow } else { Write-Host (U '\u53ef\u4ee5\u6539\u6210\u5141\u8bb8\u8fd0\u884c\u672c\u673a\u7684\u811a\u672c\uff08\u4ece\u7f51\u4e0a\u4e0b\u8f7d\u7684\u811a\u672c\u4ecd\u8981\u6709\u7b7e\u540d\uff09\uff0c\u53ea\u6539\u4f60\u81ea\u5df1\u7684\u8d26\u53f7\u3002') $answer = Read-Host -Prompt (U '\u73b0\u5728\u6539\u5417\uff1f(y/N)') $Changed = $false if ($answer -match '^[Yy]') { try { Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force -ErrorAction SilentlyContinue } catch { } try { $Changed = ([string](Get-ExecutionPolicy -Scope CurrentUser) -eq 'RemoteSigned') } catch { } } if ($Changed) { Write-Host (U '\u5df2\u6539\u597d\uff0c\u91cd\u65b0\u6253\u5f00 PowerShell \u540e\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165 claude\u3002') -ForegroundColor Green } else { Write-Host (U '\u6ca1\u6709\u6539\u3002\u5728 PowerShell \u91cc\u53ef\u4ee5\u6539\u8f93\u5165 claude.cmd\uff1b\u4ee5\u540e\u60f3\u6539\uff0c\u5728 PowerShell \u91cc\u8fd0\u884c\u4e00\u6b21\u4e0b\u9762\u8fd9\u884c\uff1a') -ForegroundColor Yellow Write-Host ' Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force' -ForegroundColor Yellow } } } Write-Host '' Write-Host (U '\u5b8c\u6210\u3002\u7528\u6cd5\uff1a\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u6216 PowerShell \u91cc\u8fdb\u5165\u4f60\u7684\u9879\u76ee\u76ee\u5f55\uff0c\u8fd0\u884c claude\u3002') -ForegroundColor Green Write-Host (U ' \u7b2c\u4e00\u6b21\u542f\u52a8\u65f6\u6309\u63d0\u793a\u9009\u597d\u754c\u9762\u98ce\u683c\u7b49\uff1b\u5bc6\u94a5\u5df2\u7ecf\u5199\u5728\u914d\u7f6e\u91cc\uff0c\u4e0d\u7528\u767b\u5f55 claude.ai \u8d26\u53f7\u3002') Write-Host (U '\u53ef\u7528\u6a21\u578b\uff1aclaude-sonnet-5-5\uff08\u65e5\u5e38\u5199\u4ee3\u7801\u7528\u5b83\uff09\u3001claude-opus-5-5\uff08\u6700\u5f3a\uff0c\u4e5f\u6700\u8d35\uff09\u3001claude-haiku-5-5\uff08\u5feb\u3001\u4fbf\u5b9c\uff0c\u540e\u53f0\u7684\u5c0f\u4efb\u52a1\u4e5f\u7528\u5b83\uff09\u3001claude-fable-5-2\u3001claude-fable-5-1\u3002') Write-Host (U ' \u5728 Claude Code \u91cc\u8f93\u5165 /model \u67e5\u770b\u548c\u5207\u6362\u3002Claude Code \u9ed8\u8ba4\u53ef\u80fd\u7528 opus\uff0c\u65e5\u5e38\u5efa\u8bae\u8f93\u5165 /model sonnet \u5207\u5230 sonnet\uff08\u4f1a\u8bb0\u4f4f\uff09\u3002') Write-Host (U '\u8d39\u7528\uff1aClaude \u6309\u5b9e\u9645\u7528\u91cf\u4ed8\u8d39\uff08\u4e0d\u662f\u5305\u6708\uff09\uff0copus \u6700\u8d35\uff1b\u65e5\u5e38\u7528 sonnet\uff0c\u96be\u9898\u518d\u6362 opus\u3002\u7528\u91cf\u548c GPT \u7684\u4e00\u8d77\u7b97\u8fdb\u6bcf\u5468\u989d\u5ea6\u3002') -ForegroundColor Yellow Write-Host (U 'VS Code \u63d2\u4ef6\uff1a\u63d2\u4ef6\u7684\u767b\u5f55\u68c0\u67e5\u4e0d\u8bfb\u8fd9\u4efd settings.json\uff0c\u8981\u5728 VS Code \u7684\u7528\u6237\u8bbe\u7f6e claudeCode.environmentVariables \u91cc\u53e6\u914d ANTHROPIC_BASE_URL \u548c ANTHROPIC_AUTH_TOKEN\uff1b\u811a\u672c\u4e0d\u6539 VS Code \u7684\u8bbe\u7f6e\u3002') Write-Host (U '\u4e3a\u4e86\u4e0d\u8bbf\u95ee\u56fd\u5916\u7684\u670d\u52a1\u5668\uff0cClaude Code \u7684\u81ea\u52a8\u66f4\u65b0\u5df2\u7ecf\u5173\u6389\u3002npm \u88c5\u7684\u8981\u66f4\u65b0\u65f6\u5728\u547d\u4ee4\u63d0\u793a\u7b26\u91cc\u8fd0\u884c\uff1a') Write-Host " npm install -g $NpmPackage --registry=$NpmRegistry" Write-Host (U '\u8981\u6362\u5bc6\u94a5\uff0c\u91cd\u65b0\u8fd0\u884c\u672c\u811a\u672c\u3002\u811a\u672c\u4e0d\u81ea\u52a8\u64a4\u56de\uff1a') Write-ManualSteps } # The manual undo steps (decided 2026-11-23: no automatic undo). The same text as print_manual_steps in # claude-setup.sh and README section 6; change all three together. The backslash in the Windows path goes in # through -f, because U would read it as an escape. function Write-ManualSteps { Write-Host (U '\u4e0d\u518d\u7528\u7f51\u5173\u7684 Claude \u65f6\uff0c\u624b\u52a8\u64a4\u56de\uff08\u6539\u5b8c\u3001\u67e5\u5b8c\u4e4b\u524d\u5148\u522b\u6253\u5f00 Claude Code\uff09\uff1a') Write-Host ((U ' 1. \u6253\u5f00 Claude Code \u7684\u7528\u6237\u8bbe\u7f6e ~/.claude/settings.json\uff08Windows \u662f\u7528\u6237\u76ee\u5f55\u4e0b\u7684 .claude{0}settings.json\uff1b\u8bbe\u4e86 CLAUDE_CONFIG_DIR \u7684\u5728\u90a3\u4e2a\u76ee\u5f55\u4e0b\uff09\u3002') -f '\') Write-Host (U ' 2. \u5728 env \u91cc\u5220\u6389\u6240\u6709 ANTHROPIC_ \u5f00\u5934\u7684\u9879\u548c CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC\uff0c\u518d\u5220\u6389\u9876\u5c42\u7684 skipWebFetchPreflight \u548c apiKeyHelper\u3002\u786e\u5b9a\u662f\u4f60\u81ea\u5df1\u53e6\u914d\u3001\u8fd8\u8981\u7ee7\u7eed\u7528\u7684\uff0c\u7559\u7740\u3002\u88c5\u811a\u672c\u4e4b\u524d\u4f60\u81ea\u5df1\u914d\u8fc7\u7684\u503c\uff0c\u53ef\u4ee5\u4ece\u540c\u4e00\u76ee\u5f55\u7684 settings.json.bak-* \u5907\u4efd\u91cc\u627e\u56de\u6765\u3002') Write-Host (U ' 3. \u518d\u67e5\u4e00\u904d\u522b\u7684\u5730\u65b9\u6709\u6ca1\u6709\u914d\u8fc7\u7f51\u5173\u5730\u5740\u6216\u7f51\u5173\u5bc6\u94a5\uff1a\u7ec8\u7aef\u7684\u73af\u5883\u53d8\u91cf\u548c ~/.zshrc\u3001~/.bashrc \u7b49\u542f\u52a8\u6587\u4ef6\uff0cVS Code \u8bbe\u7f6e\u91cc\u7684 claudeCode.environmentVariables\uff0c\u9879\u76ee\u76ee\u5f55\u91cc\u7684 .claude/settings.json\u3001.claude/settings.local.json\u3002\u91cc\u9762\u7ed9\u672c\u7f51\u5173\u914d\u7684 ANTHROPIC_BASE_URL\u3001ANTHROPIC_AUTH_TOKEN\u3001ANTHROPIC_API_KEY\u3001ANTHROPIC_CUSTOM_HEADERS\u3001apiKeyHelper\uff0c\u4e00\u8d77\u5220\u3002') Write-Host (U ' 4. \u627e\u56de\u9700\u8981\u7684\u539f\u503c\u4ee5\u540e\uff0c\u5220\u6389 settings.json.bak-* \u5907\u4efd\uff08\u91cc\u9762\u6709\u5bc6\u94a5\uff09\uff1b\u65e7\u7248\u811a\u672c\u7559\u4e0b\u7684 wulaoban-claude-setup.json \u4e5f\u53ef\u4ee5\u5220\u3002') Write-Host (U ' 5. \u6253\u5f00 Claude Code\uff1a\u76f4\u63a5\u8fdb\u767b\u5f55\u9875\uff0c\u8bf4\u660e\u7f51\u5173\u914d\u7f6e\u5df2\u7ecf\u6e05\u6389\uff0c\u53ef\u4ee5\u767b\u5f55\u81ea\u5df1\u7684\u8d26\u53f7\uff1b\u767b\u5f55\u540e\u6216\u8005\u6ca1\u8fdb\u767b\u5f55\u9875\u65f6\uff0c\u8f93\u5165 /status \u6838\u5bf9\u63a5\u5165\u5730\u5740\u4e0d\u662f ai.5lb.cn\u3001\u4e5f\u6ca1\u5728\u7528\u7f51\u5173\u7684\u5bc6\u94a5\u3002\u8fd8\u80fd\u770b\u5230\u7f51\u5173\u5730\u5740\u6216\u7f51\u5173\u5bc6\u94a5\uff0c\u8bf4\u660e\u522b\u5904\u8fd8\u6709\u914d\u7f6e\uff0c\u9000\u51fa\u540e\u627e\u5230\u5220\u6389\u518d\u770b\u3002') } # Decodes the \uXXXX escapes used for every Chinese message in this file. function U([string]$Text) { return [regex]::Unescape($Text) } function Show-Version([string]$Text) { if ($Text) { return $Text } return (U '\uff08\u8bfb\u4e0d\u51fa\u7248\u672c\uff09') } # ---- settings.json (the same rules as WLB_CORE in claude-setup.sh) ---- # Strict JSON (RFC 8259) check. ConvertFrom-Json accepts more than JSON (comments, trailing commas, single # quotes, NaN), and the sh version stops on all of those, so this runs first. Iterative, no recursion. # Returns @{ Ok; Depth (deepest nesting); Numbers (every number token, in document order) }. function Test-StrictJson([string]$Text) { $ws = [regex]'\G[ \t\n\r]*' $str = [regex]'\G"(?:[^"\\\u0000-\u001f]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4}))*"' $num = [regex]'\G-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?' $lit = [regex]'\G(?:true|false|null)' $result = @{ Ok = $false; Depth = 0; Numbers = (New-Object System.Collections.Generic.List[string]) } $stack = New-Object System.Collections.Generic.Stack[char] $i = 0 $n = $Text.Length $expect = 'value' while ($true) { $i += $ws.Match($Text, $i).Length if ($i -ge $n) { break } $c = $Text[$i] if ($expect -eq 'value') { if ($c -eq '{' -or $c -eq '[') { $stack.Push($c) if ($stack.Count -gt $result.Depth) { $result.Depth = $stack.Count } $i++ $i += $ws.Match($Text, $i).Length if ($i -lt $n -and (($c -eq '{' -and $Text[$i] -eq '}') -or ($c -eq '[' -and $Text[$i] -eq ']'))) { [void]$stack.Pop() $i++ $expect = 'after' } elseif ($c -eq '{') { $expect = 'key' } continue } $m = $str.Match($Text, $i) if (-not $m.Success) { $m = $num.Match($Text, $i) if ($m.Success) { $result.Numbers.Add($m.Value) } } if (-not $m.Success) { $m = $lit.Match($Text, $i) } if (-not $m.Success -or $m.Length -eq 0) { return $result } $i += $m.Length $expect = 'after' } elseif ($expect -eq 'key') { $m = $str.Match($Text, $i) if (-not $m.Success) { return $result } $i += $m.Length $expect = 'colon' } elseif ($expect -eq 'colon') { if ($c -ne ':') { return $result } $i++ $expect = 'value' } else { if ($stack.Count -eq 0) { return $result } $top = $stack.Peek() if ($c -eq ',') { $i++ if ($top -eq '{') { $expect = 'key' } else { $expect = 'value' } } elseif (($c -eq '}' -and $top -eq '{') -or ($c -eq ']' -and $top -eq '[')) { [void]$stack.Pop() $i++ } else { return $result } } } $result.Ok = ($expect -eq 'after' -and $stack.Count -eq 0) return $result } # A number written as sign, digits and exponent with no leading or trailing zeros ("1.50e2" -> "15e1"), so two # spellings of the same value compare equal. Zero has no sign. function Get-CanonNumber([string]$S) { $s = $S.Trim() $neg = $s.StartsWith('-') if ($neg -or $s.StartsWith('+')) { $s = $s.Substring(1) } $e = 0 $k = $s.IndexOfAny([char[]]'eE') if ($k -ge 0) { $e = [int]::Parse($s.Substring($k + 1), [Globalization.CultureInfo]::InvariantCulture) $s = $s.Substring(0, $k) } $dot = $s.IndexOf('.') if ($dot -ge 0) { $e -= $s.Length - $dot - 1 $s = $s.Remove($dot, 1) } $s = $s.TrimStart('0') if ($s.Length -eq 0) { return '0' } $t = $s.Length - $s.TrimEnd('0').Length if ($t -gt 0) { $s = $s.Substring(0, $s.Length - $t); $e += $t } $sign = '' if ($neg) { $sign = '-' } return ($sign + $s + 'e' + $e) } # True when the number survives a round trip through a double, as in claude-setup.sh and in Claude Code itself. function Test-NumberToken([string]$Token) { try { $d = [double]::Parse($Token, [Globalization.NumberStyles]::Float, [Globalization.CultureInfo]::InvariantCulture) } catch { return $false } if ([double]::IsNaN($d) -or [double]::IsInfinity($d)) { return $false } return ((Get-CanonNumber $Token) -ceq (Get-CanonNumber $d.ToString('R', [Globalization.CultureInfo]::InvariantCulture))) } # A DateTime anywhere in the parsed data: PowerShell 7.0-7.4 turns date-like strings into dates, and Windows # PowerShell 5.1 does the same with "\/Date(...)\/"; writing them back would change the text. function Test-HasDate($Node, [int]$Depth) { if ($null -eq $Node -or $Depth -gt 100) { return $false } if ($Node -is [datetime] -or $Node -is [datetimeoffset]) { return $true } if ($Node -is [System.Management.Automation.PSCustomObject]) { foreach ($p in $Node.PSObject.Properties) { if (Test-HasDate $p.Value ($Depth + 1)) { return $true } } } elseif ($Node -is [array]) { foreach ($x in $Node) { if (Test-HasDate $x ($Depth + 1)) { return $true } } } return $false } # The property named exactly $Name (JSON keys are case-sensitive; PowerShell property lookups are not). function Get-ExactProp($Obj, [string]$Name) { foreach ($p in $Obj.PSObject.Properties) { if ($p.Name -ceq $Name) { return $p } } return $null } # A property whose name differs from $Name only in case. PowerShell objects cannot hold both, so the # script stops instead of writing into the wrong one. function Test-CaseClash($Obj, [string]$Name) { foreach ($p in $Obj.PSObject.Properties) { if ($p.Name -ieq $Name -and $p.Name -cne $Name) { return $true } } return $false } function Test-IsObject($Value) { return ($null -ne $Value -and $Value -is [System.Management.Automation.PSCustomObject]) } function Set-Prop($Obj, [string]$Name, $Value) { $p = Get-ExactProp $Obj $Name if ($p) { $p.Value = $Value } else { $Obj | Add-Member -NotePropertyName $Name -NotePropertyValue $Value } } # Same type and value (strings compared case-sensitively). function Test-SameValue($A, $B) { if ($B -is [string]) { return ($A -is [string] -and $A -ceq $B) } if ($B -is [bool]) { return ($A -is [bool] -and $A -eq $B) } return $false } # Parses settings.json bytes. Status: ok, invalid (not UTF-8 or not strict JSON), lossy (a number that would # change when written back), toodeep, notobject, badenv, unsupported (strict JSON that PowerShell still cannot # hold or would rewrite: an empty key, two keys differing only in case, a date-like string). function Read-Settings([byte[]]$Bytes, $Spec) { $r = @{ Status = 'ok'; Data = $null; Numbers = @() } try { $text = (New-Object System.Text.UTF8Encoding($false, $true)).GetString($Bytes) } catch { $r.Status = 'invalid' return $r } if ($text.Length -gt 0 -and $text[0] -eq [char]0xFEFF) { $text = $text.Substring(1) } $blank = " `t`r`n".ToCharArray() if ($text.Trim($blank).Length -eq 0) { $r.Data = [pscustomobject]@{} return $r } $check = Test-StrictJson $text if (-not $check.Ok) { $r.Status = 'invalid'; return $r } if ($check.Depth -gt 90) { $r.Status = 'toodeep'; return $r } foreach ($t in $check.Numbers) { if (-not (Test-NumberToken $t)) { $r.Status = 'lossy'; return $r } } $r.Numbers = @($check.Numbers) if (-not $text.TrimStart($blank).StartsWith('{')) { $r.Status = 'notobject'; return $r } try { $cmd = Get-Command ConvertFrom-Json if ($cmd.Parameters.ContainsKey('DateKind')) { # PowerShell 7.5+: keep date-like strings as strings instead of turning them into DateTime. $data = ConvertFrom-Json -InputObject $text -DateKind String -ErrorAction Stop } else { $data = ConvertFrom-Json -InputObject $text -ErrorAction Stop } } catch { $r.Status = 'unsupported' return $r } if (-not (Test-IsObject $data)) { $r.Status = 'notobject'; return $r } if (Test-HasDate $data 0) { $r.Status = 'unsupported'; return $r } foreach ($name in 'env', 'skipWebFetchPreflight') { if (Test-CaseClash $data $name) { $r.Status = 'unsupported'; return $r } } $e = Get-ExactProp $data 'env' if ($e) { if (-not (Test-IsObject $e.Value)) { $r.Status = 'badenv'; return $r } foreach ($name in @('ANTHROPIC_BASE_URL', 'ANTHROPIC_AUTH_TOKEN') + @($Spec.Fixed.Keys)) { if (Test-CaseClash $e.Value $name) { $r.Status = 'unsupported'; return $r } } } $r.Data = $data return $r } function Write-SettingsError([string]$Status, [string]$Path) { switch ($Status) { 'invalid' { $msg = U '{0} \u4e0d\u662f\u5408\u6cd5\u7684 JSON\uff08\u53ef\u80fd\u624b\u5de5\u6539\u574f\u4e86\uff0c\u6216\u8005\u4e0d\u662f UTF-8 \u7f16\u7801\uff09\u3002\u811a\u672c\u4e0d\u6539\u5b83\uff1a\u8bf7\u5148\u4fee\u597d\uff0c\u6216\u8005\u628a\u5b83\u6539\u540d\u7559\u4f5c\u5907\u4efd\u540e\u91cd\u65b0\u8fd0\u884c\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002' } 'lossy' { $msg = U '{0} \u91cc\u6709\u5199\u56de\u53bb\u4f1a\u53d8\u503c\u7684\u6570\u5b57\uff08\u6bd4\u5982\u8d85\u8fc7 9007199254740991 \u7684\u6574\u6570\u30011e309 \u8fd9\u6837\u8d85\u51fa\u8303\u56f4\u7684\u6570\uff0c\u6216\u8005\u4f4d\u6570\u592a\u591a\u7684\u5c0f\u6570\uff09\u3002\u811a\u672c\u4e0d\u6539\u5b83\uff1a\u8bf7\u5148\u6539\u6389\u8fd9\u4e2a\u6570\u5b57\uff0c\u6216\u8005\u624b\u52a8\u914d\u7f6e\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002' } 'toodeep' { $msg = U '{0} \u5d4c\u5957\u5c42\u6570\u592a\u591a\uff0c\u811a\u672c\u5904\u7406\u4e0d\u4e86\uff0c\u4e0d\u6539\u5b83\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002' } 'notobject' { $msg = U '{0} \u6700\u5916\u5c42\u4e0d\u662f\u5bf9\u8c61\uff08\u5e94\u8be5\u662f\u4e00\u5bf9\u5927\u62ec\u53f7\u62ec\u8d77\u6765\u7684\u8bbe\u7f6e\uff09\uff0c\u811a\u672c\u4e0d\u6539\u5b83\uff0c\u8bf7\u5148\u4fee\u597d\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002' } 'badenv' { $msg = U '{0} \u91cc\u7684 env \u4e0d\u662f\u5bf9\u8c61\uff08\u5e94\u8be5\u662f\u4e00\u5bf9\u5927\u62ec\u53f7\u62ec\u8d77\u6765\u7684\u53d8\u91cf\uff09\uff0c\u811a\u672c\u4e0d\u6539\u5b83\uff0c\u8bf7\u5148\u4fee\u597d\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002' } default { $msg = U 'PowerShell \u8bfb\u4e0d\u4e86\u6216\u8005\u4f1a\u6539\u5199 {0} \u7684\u5185\u5bb9\uff08\u6bd4\u5982\u6709\u540d\u5b57\u4e3a\u7a7a\u7684\u952e\u3001\u53ea\u5dee\u5927\u5c0f\u5199\u7684\u540c\u540d\u952e\uff0c\u6216\u8005\u50cf\u65e5\u671f\u7684\u5b57\u7b26\u4e32\uff09\uff0c\u811a\u672c\u4e0d\u6539\u5b83\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002' } } Write-Host ((U '\u9519\u8bef\uff1a') + ($msg -f $Path)) -ForegroundColor Red } # The key from an earlier run, or $null: env.ANTHROPIC_BASE_URL is this gateway and the token is well formed. function Get-ReusableKey($Data, [string]$Base) { $e = Get-ExactProp $Data 'env' if (-not $e -or -not (Test-IsObject $e.Value)) { return $null } $b = Get-ExactProp $e.Value 'ANTHROPIC_BASE_URL' $t = Get-ExactProp $e.Value 'ANTHROPIC_AUTH_TOKEN' if ($b -and $t -and (Test-SameValue $b.Value $Base) -and $t.Value -is [string] -and $t.Value -cmatch '^[A-Za-z0-9_-]{8,}\z') { return $t.Value } return $null } # The ids in a strict {"data": [{"id": "..."}, ...]} answer, or $null when the answer is anything else. function Get-ModelIds([string]$Content) { if (-not $Content) { return $null } if ($Content[0] -eq [char]0xFEFF) { $Content = $Content.Substring(1) } if (-not (Test-StrictJson $Content).Ok) { return $null } try { $d = ConvertFrom-Json -InputObject $Content -ErrorAction Stop } catch { return $null } if (-not (Test-IsObject $d)) { return $null } $p = Get-ExactProp $d 'data' if (-not $p -or -not ($p.Value -is [array])) { return $null } $ids = New-Object System.Collections.Generic.List[string] foreach ($item in $p.Value) { if (-not (Test-IsObject $item)) { return $null } $idp = Get-ExactProp $item 'id' if (-not $idp -or -not ($idp.Value -is [string]) -or $idp.Value -cnotmatch '^[!-~]+\z') { return $null } if (-not $ids.Contains($idp.Value)) { $ids.Add($idp.Value) } } return ,@($ids | Sort-Object -CaseSensitive) } # Merges this script's keys into $Data; every other key (other env variables too) is kept where it is. function Set-ClaudeSettings($Data, $Spec, [string]$Token) { $e = Get-ExactProp $Data 'env' if ($e) { $envObj = $e.Value } else { $envObj = [pscustomobject]@{} $Data | Add-Member -NotePropertyName 'env' -NotePropertyValue $envObj } Set-Prop $envObj 'ANTHROPIC_BASE_URL' $Spec.Base Set-Prop $envObj 'ANTHROPIC_AUTH_TOKEN' $Token foreach ($k in $Spec.Fixed.Keys) { Set-Prop $envObj $k $Spec.Fixed[$k] } Set-Prop $Data 'skipWebFetchPreflight' $true } # CLAUDE_CODE_USE_BEDROCK / VERTEX / FOUNDRY outrank ANTHROPIC_AUTH_TOKEN (the first rule of Claude Code's # authentication precedence), so requests would skip the gateway. Names only, never values. function Write-Conflicts($Data) { $names = @() $e = Get-ExactProp $Data 'env' foreach ($k in 'CLAUDE_CODE_USE_BEDROCK', 'CLAUDE_CODE_USE_VERTEX', 'CLAUDE_CODE_USE_FOUNDRY') { if ($e) { $p = Get-ExactProp $e.Value $k if ($p -and (Test-Truthy $p.Value)) { $names += $k; continue } } if (Test-Truthy ([Environment]::GetEnvironmentVariable($k))) { $names += ($k + (U '\uff08\u7ec8\u7aef\u73af\u5883\u53d8\u91cf\uff09')) } } if ($names.Count -gt 0) { Write-Host ((U '\u6ce8\u610f\uff1a\u5f00\u7740 {0}\uff0cClaude Code \u4f1a\u6539\u8d70\u4e91\u670d\u52a1\u5546\uff0c\u4e0d\u8d70\u7f51\u5173\u3002') -f ($names -join ' ')) -ForegroundColor Yellow Write-Host (U ' \u5728 settings.json \u7684 env \u91cc\u5220\u6389\u5b83\uff1b\u662f\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u7684\uff0c\u5230\u300c\u7f16\u8f91\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u300d\u91cc\u53bb\u6389\uff0c\u6216\u8005\u5728 settings.json \u7684 env \u91cc\u628a\u5b83\u8bbe\u6210\u7a7a\u5b57\u7b26\u4e32\u3002') -ForegroundColor Yellow } } function Test-Truthy($Value) { if ($null -eq $Value) { return $false } return (@('1', 'true', 'yes', 'on') -contains ([string]$Value).Trim().ToLowerInvariant()) } # The JSON about to be swapped in: strict JSON, and every number in it is one of the original's numbers with # the same value, in the same order. The merge only adds strings and booleans; it can only drop a number that # sat in one of this script's own keys, so the result's numbers are a subsequence of the original's. function Test-Candidate([string]$Json, $OriginalNumbers) { $check = Test-StrictJson $Json if (-not $check.Ok) { return $false } $want = @($OriginalNumbers | ForEach-Object { Get-CanonNumber $_ }) $j = 0 foreach ($t in $check.Numbers) { $g = Get-CanonNumber $t while ($j -lt $want.Count -and $want[$j] -cne $g) { $j++ } if ($j -ge $want.Count) { return $false } $j++ } return $true } # ---- Writing files ---- # One instance at a time per settings.json: an exclusive handle on a lock file next to it, deleted on close # (and released by Windows if the process dies). Returns the handle or $null. function Lock-Settings([string]$Path) { $lock = Join-Path (Split-Path -Parent $Path) ('.' + (Split-Path -Leaf $Path) + '.claude-setup.lock') $tries = 75 if ($env:CLAUDE_SETUP_LOCK_TRIES) { $tries = [int]$env:CLAUDE_SETUP_LOCK_TRIES } for ($i = 0; $i -lt $tries; $i++) { try { return (New-Object System.IO.FileStream($lock, [IO.FileMode]::OpenOrCreate, [IO.FileAccess]::ReadWrite, [IO.FileShare]::None, 1, [IO.FileOptions]::DeleteOnClose)) } catch { Start-Sleep -Milliseconds 200 } } return $null } # The bytes of settings.json at the moment of the merge: merge, backup and the final comparison use this. function Read-Snapshot([string]$Path) { $s = @{ Had = $false; Bytes = [byte[]]@() } if (Test-Path -LiteralPath $Path -PathType Leaf) { $s.Had = $true $s.Bytes = [IO.File]::ReadAllBytes($Path) } return $s } function Test-Unchanged([string]$Path, $Snap) { if (Test-ReparsePoint $Path) { return $false } if (-not $Snap.Had) { return (-not (Test-Path -LiteralPath $Path)) } if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) { return $false } return (Test-FileBytes $Path $Snap.Bytes) } # Writes settings.json under the lock: snapshot, merge, check the result, swap it in only if the file did not # change since the snapshot (otherwise merge again, up to 3 times). Returns the merged data, or $null after # printing why. function Write-ClaudeSettings($Spec, [string]$Settings, [string]$Stamp, $Utf8, [string]$ApiKey) { $dir = Split-Path -Parent $Settings try { if (-not (Test-Path -LiteralPath $dir)) { New-Item -ItemType Directory -Path $dir -Force -ErrorAction Stop | Out-Null } } catch { Write-Host ((U '\u9519\u8bef\uff1a\u5efa\u4e0d\u4e86\u76ee\u5f55 {0}\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $dir) -ForegroundColor Red return $null } [void](Protect-Dir $dir) $lock = Lock-Settings $Settings if (-not $lock) { Write-Host ((U '\u9519\u8bef\uff1a\u53e6\u4e00\u4e2a\u914d\u7f6e\u811a\u672c\u6b63\u5728\u6539 {0}\u3002\u7b49\u5b83\u7ed3\u675f\u518d\u8fd0\u884c\u3002\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $Settings) -ForegroundColor Red return $null } try { for ($attempt = 1; $true; $attempt++) { $snap = Read-Snapshot $Settings $state = Read-Settings $snap.Bytes $Spec if ($state.Status -ne 'ok') { Write-SettingsError $state.Status $Settings; return $null } Set-ClaudeSettings $state.Data $Spec $ApiKey $json = ConvertTo-Json -InputObject $state.Data -Depth 100 if (-not (Test-Candidate $json $state.Numbers)) { Write-Host (U '\u9519\u8bef\uff1a\u751f\u6210\u7684 settings.json \u6ca1\u901a\u8fc7\u6838\u5bf9\uff0c\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002\u8bf7\u628a\u8fd9\u4e00\u884c\u53d1\u7ed9\u7ba1\u7406\u5458\u3002') -ForegroundColor Red return $null } if (Test-Unchanged $Settings $snap) { break } if ($attempt -ge 3) { Write-Host ((U '\u9519\u8bef\uff1a{0} \u5728\u811a\u672c\u8fd0\u884c\u671f\u95f4\u4e00\u76f4\u5728\u88ab\u6539\uff08\u53ef\u80fd Claude Code \u6b63\u5f00\u7740\uff09\uff0c\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002\u8bf7\u5173\u6389 Claude Code \u540e\u91cd\u65b0\u8fd0\u884c\u3002') -f $Settings) -ForegroundColor Red return $null } Write-Host ((U '{0} \u521a\u88ab\u522b\u7684\u7a0b\u5e8f\u6539\u8fc7\uff0c\u6309\u65b0\u5185\u5bb9\u91cd\u65b0\u5408\u5e76\u2026\u2026') -f $Settings) } if (-not (Save-Settings $Settings $json $snap $Stamp $Utf8)) { return $null } return $state.Data } finally { $lock.Dispose() } } # Swaps the new settings.json in. Every file that holds the key is created empty and restricted before anything # is written into it; the existing file is restricted before the swap (File.Replace keeps the replaced file's # ACL); the backup is created exclusively (never over an older one) from the snapshot bytes and checked; the # swap happens only if the file still matches the snapshot. If the swap fails and the original path is gone, # the original bytes are put back. Returns $false after printing why. function Save-Settings([string]$Settings, [string]$Json, $Snap, [string]$Stamp, $Utf8) { $cand = New-SiblingTemp $Settings $bak = $null try { if (-not (New-PrivateFile $cand)) { Write-Host ((U '\u9519\u8bef\uff1a\u5728 {0} \u91cc\u5efa\u4e0d\u51fa\u53ea\u6709\u4f60\u80fd\u8bfb\u7684\u6587\u4ef6\uff08\u53ef\u80fd\u5728\u7f51\u7edc\u76d8\u4e0a\uff0c\u6216\u8005\u76ee\u5f55\u7684\u6743\u9650\u7ed9\u4e86\u522b\u4eba\uff09\uff0c\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f (Split-Path -Parent $Settings)) -ForegroundColor Red return $false } try { [IO.File]::WriteAllText($cand, $Json + "`n", $Utf8) } catch { Write-Host ((U '\u9519\u8bef\uff1a\u5199\u4e0d\u4e86\u4e34\u65f6\u6587\u4ef6\uff08{0}\uff09\uff0c\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $_.Exception.Message) -ForegroundColor Red return $false } if ($Snap.Had) { if (-not ((Protect-File $Settings) -and (Test-PrivateFile $Settings))) { Write-Host ((U '\u9519\u8bef\uff1a\u6ca1\u80fd\u628a {0} \u8bbe\u6210\u53ea\u6709\u4f60\u80fd\u8bfb\uff0c\u4e0d\u5f80\u91cc\u5199\u5bc6\u94a5\u3002\u6ca1\u6709\u6539\u52a8\u6587\u4ef6\u5185\u5bb9\u3002') -f $Settings) -ForegroundColor Red return $false } $bak = New-Backup $Settings $Stamp $Snap.Bytes if (-not $bak) { Write-Host ((U '\u9519\u8bef\uff1a\u5907\u4efd {0} \u5931\u8d25\uff08\u5efa\u4e0d\u51fa\u53ea\u6709\u4f60\u80fd\u8bfb\u7684\u5907\u4efd\u6587\u4ef6\uff09\uff0c\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002') -f $Settings) -ForegroundColor Red return $false } } if (-not (Test-Unchanged $Settings $Snap)) { if ($bak) { Remove-Item -LiteralPath $bak -Force -ErrorAction SilentlyContinue } Write-Host ((U '\u9519\u8bef\uff1a{0} \u521a\u88ab\u522b\u7684\u7a0b\u5e8f\u6539\u8fc7\uff08\u53ef\u80fd Claude Code \u6b63\u5f00\u7740\uff09\uff0c\u6ca1\u6709\u6539\u52a8\u4efb\u4f55\u6587\u4ef6\u3002\u8bf7\u5173\u6389 Claude Code \u540e\u91cd\u65b0\u8fd0\u884c\u3002') -f $Settings) -ForegroundColor Red return $false } if ($bak) { Write-Host ((U '\u539f\u6765\u7684 settings.json \u5df2\u5907\u4efd\uff1a{0}') -f $bak) } if (-not (Move-Into $cand $Settings)) { # ReplaceFile can leave the target path empty (ERROR_UNABLE_TO_MOVE_REPLACEMENT and _2); put the # original bytes back in a new private file. if ($Snap.Had -and -not (Test-Path -LiteralPath $Settings)) { if (Restore-Snapshot $Settings $Snap) { Write-Host ((U '\u9519\u8bef\uff1a\u5199\u4e0d\u8fdb {0}\uff0c\u5df2\u7ecf\u6309\u539f\u6837\u6062\u590d\u3002\u6ca1\u6709\u5b8c\u6210\u914d\u7f6e\u3002') -f $Settings) -ForegroundColor Red } else { Write-Host ((U '\u9519\u8bef\uff1a\u5199\u4e0d\u8fdb {0}\uff0c\u539f\u6587\u4ef6\u4e5f\u6ca1\u80fd\u6062\u590d\uff1a\u8bf7\u628a {1} \u6539\u540d\u56de settings.json\u3002') -f $Settings, $bak) -ForegroundColor Red } } else { Write-Host ((U '\u9519\u8bef\uff1a\u5199\u4e0d\u8fdb {0}\u3002\u539f\u6587\u4ef6\u6ca1\u6709\u6539\u52a8\u3002') -f $Settings) -ForegroundColor Red } return $false } if (-not (Test-PrivateFile $Settings)) { [void](Protect-File $Settings) } if (-not (Test-PrivateFile $Settings)) { Write-Host ((U '\u9519\u8bef\uff1a\u5df2\u7ecf\u5199\u8fdb {0}\uff0c\u4f46\u5b83\u4e0d\u662f\u53ea\u6709\u4f60\u80fd\u8bfb\uff08\u91cc\u9762\u6709\u5bc6\u94a5\uff09\u3002\u8bf7\u5728\u6587\u4ef6\u5c5e\u6027\u7684\u300c\u5b89\u5168\u300d\u91cc\u53ea\u7559\u4f60\u81ea\u5df1\uff0c\u6216\u8005\u5220\u6389\u5b83\u3002') -f $Settings) -ForegroundColor Red return $false } return $true } finally { if (Test-Path -LiteralPath $cand) { Remove-Item -LiteralPath $cand -Force -ErrorAction SilentlyContinue } } } # Puts settings.json back to the snapshot: the original bytes through a new private temporary file that is checked # before it is swapped in (never written straight into the final path), or the file removed when there was none. function Restore-Snapshot([string]$Settings, $Snap) { if (-not $Snap.Had) { try { Remove-Item -LiteralPath $Settings -Force -ErrorAction Stop } catch { } return (-not (Test-Path -LiteralPath $Settings)) } $t = New-SiblingTemp $Settings try { if (-not (New-PrivateFile $t)) { return $false } [IO.File]::WriteAllBytes($t, $Snap.Bytes) if (-not (Test-FileBytes $t $Snap.Bytes)) { return $false } if (-not (Move-Into $t $Settings)) { return $false } if (-not (Test-PrivateFile $Settings)) { [void](Protect-File $Settings) } return ((Test-FileBytes $Settings $Snap.Bytes) -and (Test-PrivateFile $Settings)) } catch { return $false } finally { if (Test-Path -LiteralPath $t) { Remove-Item -LiteralPath $t -Force -ErrorAction SilentlyContinue } } } # settings.json.bak-