修复copilot 与Windows 的copilot 冲突

This commit is contained in:
2026-05-28 23:02:24 +08:00
parent 8ceccd75f1
commit e8693dad2a
4 changed files with 123 additions and 4 deletions

27
.github/copilot-instructions.md vendored Normal file
View File

@@ -0,0 +1,27 @@
<!-- rtk-instructions v2 -->
# RTK — Token-Optimized CLI
**rtk** is a CLI proxy that filters and compresses command outputs, saving 60-90% tokens.
## Rule
Always prefix shell commands with `rtk`:
```bash
# Instead of: Use:
git status rtk git status
git log -10 rtk git log -10
cargo test rtk cargo test
docker ps rtk docker ps
kubectl get pods rtk kubectl pods
```
## Meta commands (use directly)
```bash
rtk gain # Token savings dashboard
rtk gain --history # Per-command savings history
rtk discover # Find missed rtk opportunities
rtk proxy <cmd> # Run raw (no filtering) but track usage
```
<!-- /rtk-instructions -->

12
.github/hooks/rtk-rewrite.json vendored Normal file
View File

@@ -0,0 +1,12 @@
{
"hooks": {
"PreToolUse": [
{
"type": "command",
"command": "rtk hook copilot",
"cwd": ".",
"timeout": 5
}
]
}
}

View File

@@ -608,10 +608,38 @@ gh-copilot-wsl explain "tar -czf backup.tar.gz ~/project --exclude node_modules"
### RTK (Rust Token Killer)
RTK 作为 Claude Code 的 **PreToolUse hook** 运行,自动将 bash 命令重写为 `rtk` 等效命令,过滤噪音、压缩输出,减少 6090% token 消耗。
RTK 支持多种 AI 编程助手集成,过滤噪音、压缩输出,减少 6090% token 消耗。
#### Claude Code 集成
RTK 作为 Claude Code 的 **PreToolUse hook** 运行,自动将 bash 命令重写为 `rtk` 等效命令。
`deploy.ps1` 会自动执行 `rtk init -g` 注册 hook**重启 Claude Code 后即生效**。
#### GitHub Copilot 集成
RTK 通过 `.github/copilot-instructions.md` 文件集成 GitHub CopilotVS Code / Copilot Chat
本仓库已包含此文件Copilot 读取后会自动在终端命令前添加 `rtk` 前缀。
```bash
# 生成 copilot-instructions 模板(如需在其他项目中集成)
rtk init -g --copilot
```
生成的 `.github/copilot-instructions.md` 内容示例:
```markdown
<!-- rtk-instructions v2 -->
# RTK — Token-Optimized CLI
Always prefix shell commands with `rtk`:
- rtk git status (instead of git status)
- rtk cargo test (instead of cargo test)
- rtk docker ps (instead of docker ps)
```
> 对已有 `copilot-instructions.md` 的项目,可手动追加 RTK 规则块,避免覆盖原有内容。
```bash
# 验证 hook 安装状态
rtk init --show
@@ -627,6 +655,30 @@ rtk cargo test # 只显示失败的测试
rtk grep "pattern" . # 分组搜索结果
```
### 故障排查 — gh copilot 使用 Windows 版本
**现象**:在 WSL2 中执行 `gh copilot` 报错 `exec: .../copilot.bat: not found`
**原因**VS Code GitHub Copilot Chat 扩展将其 `copilotCli` 目录加入了 Windows 系统 PATHWSL2 继承该路径后,`gh copilot` 内置命令从 PATH 找到了 Windows 的 `.bat` wrapper在 Linux 下无法执行。
```bash
# 方法一:重新运行 wsl-setup.sh已自动写入 PATH 修复)
bash /mnt/g/Works/server-deploy/claude-dev-stack/wsl-setup.sh
# 方法二:手动修复(写入 ~/.bashrc 并立即生效)
cat >> ~/.bashrc << 'EOF'
# 排除 VS Code Copilot Chat Windows wrappergh copilot 在 WSL2 中会误调用 .bat 文件)
PATH="$(echo "$PATH" | tr ':' '\n' | grep -Fv 'copilotCli' | tr '\n' ':' | sed 's/:$//')"
EOF
source ~/.bashrc
# 验证
gh copilot --version
```
---
### 故障排查 — rtk 安装失败
```bash

View File

@@ -160,12 +160,27 @@ else
fi
fi
COPILOT_EXT_BIN="$HOME/.local/share/gh/extensions/gh-copilot/gh-copilot"
NEED_COPILOT_INSTALL=false
if gh extension list 2>/dev/null | grep -q 'github/gh-copilot'; then
log "GitHub Copilot CLI 扩展已安装"
# 验证是否为原生 Linux 二进制,而非 VS Code 写入的 Windows .bat wrapper
if [ -f "$COPILOT_EXT_BIN" ] && grep -qiE '/mnt/[a-z]/|.bat' "$COPILOT_EXT_BIN" 2>/dev/null; then
warn "检测到 Windows 版 gh-copilot 扩展(非 Linux 原生),将重新安装..."
gh extension remove github/gh-copilot 2>/dev/null || rm -rf "$(dirname "$COPILOT_EXT_BIN")"
NEED_COPILOT_INSTALL=true
else
log "GitHub Copilot CLI 扩展已安装(原生 Linux 版)"
fi
else
NEED_COPILOT_INSTALL=true
fi
if [ "$NEED_COPILOT_INSTALL" = true ]; then
log "安装 GitHub Copilot CLI 扩展..."
gh extension install github/gh-copilot
log "GitHub Copilot CLI 扩展已安装命令入口gh copilot"
if gh extension install github/gh-copilot 2>/dev/null; then
log "GitHub Copilot CLI 扩展已安装命令入口gh copilot"
else
warn "GitHub Copilot CLI 扩展安装失败可稍后手动执行gh extension install github/gh-copilot"
fi
fi
mkdir -p ~/.claude
@@ -179,6 +194,19 @@ PROFILE_BLOCK=""
[ -n "$ANTHROPIC_BASE_URL" ] && PROFILE_BLOCK+="export ANTHROPIC_BASE_URL='$ANTHROPIC_BASE_URL'\n"
PROFILE_BLOCK+="export CLAUDE_MODEL='$CLAUDE_MODEL'\n"
# 从 PATH 过滤掉 VS Code Copilot Chat 写入的 Windows copilot 包装脚本
# 该脚本会调用 .bat 文件,在 Linux 下无法执行,导致 gh copilot 报错
if ! grep -q 'copilotCli' ~/.bashrc 2>/dev/null; then
cat >> ~/.bashrc << 'PATHFIX'
# 排除 VS Code Copilot Chat Windows wrappergh copilot 在 WSL2 中会误调用 .bat 文件)
PATH="$(echo "$PATH" | tr ':' '\n' | grep -Fv 'copilotCli' | tr '\n' ':' | sed 's/:$//')"
PATHFIX
log "已写入 PATH 修复(排除 Windows copilotCli到 ~/.bashrc"
fi
# 在当前 shell 也立即生效
PATH="$(echo "$PATH" | tr ':' '\n' | grep -Fv 'copilotCli' | tr '\n' ':' | sed 's/:$//')"
for rc in ~/.bashrc ~/.profile; do
if ! grep -q "ANTHROPIC_API_KEY" "$rc" 2>/dev/null; then
printf "\n# Claude Code CLI\n${PROFILE_BLOCK}" >> "$rc"