Compare commits

...

3 Commits

Author SHA1 Message Date
b892ee6e65 fix: rtk init 用 yes 管道自动确认,消除手动按 Enter
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-28 15:31:59 +08:00
ea0552be79 fix: 部署后 WSL2 默认使用 root 用户
- 安装后自动写入 /etc/wsl.conf (default=root, systemd=false)
- Invoke-WSL 改为 --user root 执行所有命令
- 跳过 OOBE 用户创建流程(--user root 启动)
- 重启 WSL2 使 wsl.conf 立即生效

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-28 15:30:09 +08:00
bdbc081cba fix: 修正灵眸模式下 API Key 警告误报
ANTHROPIC_AUTH_TOKEN 不为空时不再显示警告

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-05-28 15:02:07 +08:00

View File

@@ -135,6 +135,31 @@ if ($distroInstalled) {
Write-Warn "首次安装可能需要重启,重启后重新运行脚本" Write-Warn "首次安装可能需要重启,重启后重新运行脚本"
} }
# ── Step 1b: 首次 OOBE 处理 & 设置 root 为默认用户 ──────────
# Ubuntu 首次安装会停在 OOBE 要求创建用户;用 --root 启动可跳过
Write-Info "初始化 WSL2 发行版(确保 root 可用)..."
$oobeDone = $false
for ($i = 0; $i -lt 3; $i++) {
wsl -d $WSL_DISTRO --user root -- bash -c "exit 0" 2>$null
if ($LASTEXITCODE -eq 0) { $oobeDone = $true; break }
Start-Sleep 3
}
if (-not $oobeDone) {
# 触发 OOBE 完成(无交互,接受默认)
wsl -d $WSL_DISTRO -- bash -c "exit 0" 2>$null
Start-Sleep 5
}
# 写入 /etc/wsl.conf完整覆盖避免重复块
$wslConfContent = "[boot]`nsystemd=false`n`n[user]`ndefault=root`n"
wsl -d $WSL_DISTRO --user root -- bash -c "printf '[boot]\nsystemd=false\n\n[user]\ndefault=root\n' > /etc/wsl.conf" 2>$null
Write-OK "/etc/wsl.conf 已写入 (default=root, systemd=false)"
# 重启使 wsl.conf 生效
wsl --terminate $WSL_DISTRO 2>$null
Start-Sleep 2
Write-OK "WSL2 已重启,后续命令将以 root 运行"
# ── WSL2 执行辅助函数 ───────────────────────────────────────── # ── WSL2 执行辅助函数 ─────────────────────────────────────────
function Invoke-WSL { function Invoke-WSL {
param([string]$Command, [switch]$IgnoreError) param([string]$Command, [switch]$IgnoreError)
@@ -143,10 +168,10 @@ function Invoke-WSL {
# 多行命令base64 编码后在 bash 内解码执行,完全规避 PowerShell 管道 CRLF 问题 # 多行命令base64 编码后在 bash 内解码执行,完全规避 PowerShell 管道 CRLF 问题
$CleanCmd = $Command -replace "`r`n","`n" -replace "`r","`n" $CleanCmd = $Command -replace "`r`n","`n" -replace "`r","`n"
$b64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($CleanCmd)) $b64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($CleanCmd))
$result = wsl -d $WSL_DISTRO -- bash -c "echo '$b64' | base64 -d | bash" 2>&1 | $result = wsl -d $WSL_DISTRO --user root -- bash -c "echo '$b64' | base64 -d | bash" 2>&1 |
ForEach-Object { ($_ -replace "`0","").ToString() } ForEach-Object { ($_ -replace "`0","").ToString() }
} else { } else {
$result = wsl -d $WSL_DISTRO -- bash -c $Command 2>&1 | $result = wsl -d $WSL_DISTRO --user root -- bash -c $Command 2>&1 |
ForEach-Object { ($_ -replace "`0","").ToString() } ForEach-Object { ($_ -replace "`0","").ToString() }
} }
@@ -159,22 +184,12 @@ function Invoke-WSL {
} }
# 用 exit code 方式测试连通性,避免输出编码干扰 # 用 exit code 方式测试连通性,避免输出编码干扰
wsl -d $WSL_DISTRO -- bash -c "exit 0" 2>$null wsl -d $WSL_DISTRO --user root -- bash -c "exit 0" 2>$null
if ($LASTEXITCODE -ne 0) { if ($LASTEXITCODE -ne 0) {
Write-Fail "无法访问 WSL2 发行版 '$WSL_DISTRO'" Write-Fail "无法访问 WSL2 发行版 '$WSL_DISTRO'"
exit 1 exit 1
} }
Write-OK "WSL2 ($WSL_DISTRO) 连接正常" Write-OK "WSL2 ($WSL_DISTRO) 连接正常 (root)"
# 获取 WSL2 默认登录用户OOBE 完成后可能是非 root
$wslUser = (wsl -d $WSL_DISTRO -- bash -c "whoami" 2>&1) -replace "`0","" |
Where-Object { $_ -match "^\w+$" } | Select-Object -First 1
if (-not $wslUser) { $wslUser = "root" }
$wslUser = $wslUser.Trim()
Write-OK "WSL2 默认用户: $wslUser"
if ($wslUser -ne "root") {
Write-Info "非 root 用户,系统级命令将通过 sudo 执行"
}
# ── Step 1a: 配置 .wslconfigmirrored 网络模式) ──────────── # ── Step 1a: 配置 .wslconfigmirrored 网络模式) ────────────
$wslCfgPath = "$env:USERPROFILE\.wslconfig" $wslCfgPath = "$env:USERPROFILE\.wslconfig"
@@ -659,8 +674,8 @@ CARGO_NET_GIT_FETCH_WITH_CLI=true cargo install --git https://github.com/rtk-ai/
} }
} }
# rtk init -g安装 Claude Code PreToolUse hook幂等 # rtk init -g安装 Claude Code PreToolUse hook幂等yes 管道自动确认所有提示
Invoke-WSL ". ~/.cargo/env 2>/dev/null; rtk init -g --auto-patch 2>/dev/null || true" -IgnoreError | Out-Null Invoke-WSL ". ~/.cargo/env 2>/dev/null; yes | rtk init -g --auto-patch 2>/dev/null || true" -IgnoreError | Out-Null
Write-OK "rtk hook 已注册 (rtk init -g),重启 Claude Code 后生效" Write-OK "rtk hook 已注册 (rtk init -g),重启 Claude Code 后生效"
# ══════════════════════════════════════════════════════════════ # ══════════════════════════════════════════════════════════════
@@ -734,7 +749,9 @@ Write-Host ""
Write-Host " 4) 在 Claude Code (WSL2) 中使用:" -ForegroundColor White Write-Host " 4) 在 Claude Code (WSL2) 中使用:" -ForegroundColor White
Write-Host " claude-wsl → claude → /mcp" -ForegroundColor DarkGray Write-Host " claude-wsl → claude → /mcp" -ForegroundColor DarkGray
Write-Host "" Write-Host ""
if (-not $ANTHROPIC_API_KEY) { if (-not $ANTHROPIC_API_KEY -and -not $ANTHROPIC_AUTH_TOKEN) {
Write-Warn " ⚠ 未设置 ANTHROPIC_API_KEY,请编辑 .env 后重新运行" Write-Warn " ⚠ 未设置 API Key,请编辑 .env 后重新运行"
Write-Warn " 灵眸用户:填写 ANTHROPIC_AUTH_TOKEN=sk-xxx"
Write-Warn " 官方用户:填写 ANTHROPIC_API_KEY=sk-xxx"
} }
Write-Host "╚══════════════════════════════════════════════════════════════╝" -ForegroundColor Green Write-Host "╚══════════════════════════════════════════════════════════════╝" -ForegroundColor Green