Compare commits
2 Commits
18e24d40f0
...
e8693dad2a
| Author | SHA1 | Date | |
|---|---|---|---|
| e8693dad2a | |||
| 8ceccd75f1 |
27
.github/copilot-instructions.md
vendored
Normal file
27
.github/copilot-instructions.md
vendored
Normal 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
12
.github/hooks/rtk-rewrite.json
vendored
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
{
|
||||||
|
"hooks": {
|
||||||
|
"PreToolUse": [
|
||||||
|
{
|
||||||
|
"type": "command",
|
||||||
|
"command": "rtk hook copilot",
|
||||||
|
"cwd": ".",
|
||||||
|
"timeout": 5
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -608,10 +608,38 @@ gh-copilot-wsl explain "tar -czf backup.tar.gz ~/project --exclude node_modules"
|
|||||||
|
|
||||||
### RTK (Rust Token Killer)
|
### RTK (Rust Token Killer)
|
||||||
|
|
||||||
RTK 作为 Claude Code 的 **PreToolUse hook** 运行,自动将 bash 命令重写为 `rtk` 等效命令,过滤噪音、压缩输出,减少 60–90% token 消耗。
|
RTK 支持多种 AI 编程助手集成,过滤噪音、压缩输出,减少 60–90% token 消耗。
|
||||||
|
|
||||||
|
#### Claude Code 集成
|
||||||
|
|
||||||
|
RTK 作为 Claude Code 的 **PreToolUse hook** 运行,自动将 bash 命令重写为 `rtk` 等效命令。
|
||||||
|
|
||||||
`deploy.ps1` 会自动执行 `rtk init -g` 注册 hook,**重启 Claude Code 后即生效**。
|
`deploy.ps1` 会自动执行 `rtk init -g` 注册 hook,**重启 Claude Code 后即生效**。
|
||||||
|
|
||||||
|
#### GitHub Copilot 集成
|
||||||
|
|
||||||
|
RTK 通过 `.github/copilot-instructions.md` 文件集成 GitHub Copilot(VS 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
|
```bash
|
||||||
# 验证 hook 安装状态
|
# 验证 hook 安装状态
|
||||||
rtk init --show
|
rtk init --show
|
||||||
@@ -627,6 +655,30 @@ rtk cargo test # 只显示失败的测试
|
|||||||
rtk grep "pattern" . # 分组搜索结果
|
rtk grep "pattern" . # 分组搜索结果
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 故障排查 — gh copilot 使用 Windows 版本
|
||||||
|
|
||||||
|
**现象**:在 WSL2 中执行 `gh copilot` 报错 `exec: .../copilot.bat: not found`
|
||||||
|
|
||||||
|
**原因**:VS Code GitHub Copilot Chat 扩展将其 `copilotCli` 目录加入了 Windows 系统 PATH,WSL2 继承该路径后,`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 wrapper(gh copilot 在 WSL2 中会误调用 .bat 文件)
|
||||||
|
PATH="$(echo "$PATH" | tr ':' '\n' | grep -Fv 'copilotCli' | tr '\n' ':' | sed 's/:$//')"
|
||||||
|
EOF
|
||||||
|
source ~/.bashrc
|
||||||
|
|
||||||
|
# 验证
|
||||||
|
gh copilot --version
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
### 故障排查 — rtk 安装失败
|
### 故障排查 — rtk 安装失败
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -299,7 +299,7 @@ if ($nodeVer -match "v\d+") {
|
|||||||
Write-Info "安装 WSL2 Node.js LTS (via nvm)..."
|
Write-Info "安装 WSL2 Node.js LTS (via nvm)..."
|
||||||
$installNodeCmd = @'
|
$installNodeCmd = @'
|
||||||
export DEBIAN_FRONTEND=noninteractive
|
export DEBIAN_FRONTEND=noninteractive
|
||||||
sudo apt-get install -y -qq curl ca-certificates 2>/dev/null
|
sudo apt-get install -y -qq curl ca-certificates wslu 2>/dev/null
|
||||||
# 安装 nvm
|
# 安装 nvm
|
||||||
export NVM_DIR="$HOME/.nvm"
|
export NVM_DIR="$HOME/.nvm"
|
||||||
if [ ! -d "$NVM_DIR" ]; then
|
if [ ! -d "$NVM_DIR" ]; then
|
||||||
@@ -388,7 +388,7 @@ if ($ghVer -notmatch "MISSING") {
|
|||||||
export DEBIAN_FRONTEND=noninteractive
|
export DEBIAN_FRONTEND=noninteractive
|
||||||
sudo mkdir -p -m 755 /etc/apt/keyrings
|
sudo mkdir -p -m 755 /etc/apt/keyrings
|
||||||
if [ ! -f /etc/apt/keyrings/githubcli-archive-keyring.gpg ]; then
|
if [ ! -f /etc/apt/keyrings/githubcli-archive-keyring.gpg ]; then
|
||||||
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \
|
curl -fsSL --connect-timeout 20 --max-time 30 https://cli.github.com/packages/githubcli-archive-keyring.gpg \
|
||||||
| sudo dd of=/etc/apt/keyrings/githubcli-archive-keyring.gpg status=none
|
| sudo dd of=/etc/apt/keyrings/githubcli-archive-keyring.gpg status=none
|
||||||
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
|
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
|
||||||
fi
|
fi
|
||||||
@@ -396,14 +396,22 @@ if [ ! -f /etc/apt/sources.list.d/github-cli.list ]; then
|
|||||||
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
|
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
|
||||||
| sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
|
| sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
|
||||||
fi
|
fi
|
||||||
sudo apt-get update -qq
|
if sudo apt-get update -qq -o Acquire::http::Timeout=20 -o Acquire::https::Timeout=20 2>/dev/null && \
|
||||||
sudo apt-get install -y -qq gh
|
sudo apt-get install -y -qq -o Acquire::http::Timeout=30 gh 2>/dev/null; then
|
||||||
gh --version | head -1
|
echo GH_INSTALL_OK
|
||||||
|
else
|
||||||
|
echo GH_INSTALL_FAIL
|
||||||
|
fi
|
||||||
true
|
true
|
||||||
'@
|
'@
|
||||||
Invoke-WSL $installGhCmd
|
$ghInstallResult = Invoke-WSL $installGhCmd -IgnoreError
|
||||||
$ghVer = Invoke-WSL "gh --version 2>/dev/null | head -1 || echo MISSING" -IgnoreError
|
$ghVer = Invoke-WSL "gh --version 2>/dev/null | head -1 || echo MISSING" -IgnoreError
|
||||||
Write-OK "GitHub CLI 安装完成: $($ghVer.Trim())"
|
if ($ghInstallResult -match "GH_INSTALL_FAIL" -or $ghVer -match "MISSING") {
|
||||||
|
Write-Warn "GitHub CLI 安装失败(网络超时),跳过;后续可手动安装:wsl -d Ubuntu -- sudo apt install gh"
|
||||||
|
$ghVer = "MISSING"
|
||||||
|
} else {
|
||||||
|
Write-OK "GitHub CLI 安装完成: $($ghVer.Trim())"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($ghCopilotInstalled.Trim() -eq "0") {
|
if ($ghCopilotInstalled.Trim() -eq "0") {
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ export DEBIAN_FRONTEND=noninteractive
|
|||||||
sudo apt-get update -qq
|
sudo apt-get update -qq
|
||||||
sudo apt-get install -y -qq \
|
sudo apt-get install -y -qq \
|
||||||
curl wget git build-essential pkg-config libssl-dev \
|
curl wget git build-essential pkg-config libssl-dev \
|
||||||
ca-certificates gnupg lsb-release unzip python3
|
ca-certificates gnupg lsb-release unzip python3 wslu
|
||||||
log "系统依赖安装完成"
|
log "系统依赖安装完成"
|
||||||
|
|
||||||
# ──────────────────────────────────────────────────────────────
|
# ──────────────────────────────────────────────────────────────
|
||||||
@@ -144,7 +144,7 @@ else
|
|||||||
log "安装 GitHub CLI..."
|
log "安装 GitHub CLI..."
|
||||||
sudo mkdir -p -m 755 /etc/apt/keyrings
|
sudo mkdir -p -m 755 /etc/apt/keyrings
|
||||||
if [ ! -f /etc/apt/keyrings/githubcli-archive-keyring.gpg ]; then
|
if [ ! -f /etc/apt/keyrings/githubcli-archive-keyring.gpg ]; then
|
||||||
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg \
|
curl -fsSL --connect-timeout 20 --max-time 30 https://cli.github.com/packages/githubcli-archive-keyring.gpg \
|
||||||
| sudo dd of=/etc/apt/keyrings/githubcli-archive-keyring.gpg status=none
|
| sudo dd of=/etc/apt/keyrings/githubcli-archive-keyring.gpg status=none
|
||||||
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
|
sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
|
||||||
fi
|
fi
|
||||||
@@ -152,17 +152,35 @@ else
|
|||||||
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
|
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" \
|
||||||
| sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
|
| sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
|
||||||
fi
|
fi
|
||||||
sudo apt-get update -qq
|
if sudo apt-get update -qq -o Acquire::http::Timeout=20 -o Acquire::https::Timeout=20 2>/dev/null && \
|
||||||
sudo apt-get install -y -qq gh
|
sudo apt-get install -y -qq -o Acquire::http::Timeout=30 gh 2>/dev/null; then
|
||||||
log "GitHub CLI 安装完成: $(gh --version 2>/dev/null | head -1)"
|
log "GitHub CLI 安装完成: $(gh --version 2>/dev/null | head -1)"
|
||||||
|
else
|
||||||
|
warn "GitHub CLI 安装失败(网络超时),跳过;后续可手动安装:sudo apt install gh"
|
||||||
|
fi
|
||||||
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
|
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
|
else
|
||||||
|
NEED_COPILOT_INSTALL=true
|
||||||
|
fi
|
||||||
|
if [ "$NEED_COPILOT_INSTALL" = true ]; then
|
||||||
log "安装 GitHub Copilot CLI 扩展..."
|
log "安装 GitHub Copilot CLI 扩展..."
|
||||||
gh extension install github/gh-copilot
|
if gh extension install github/gh-copilot 2>/dev/null; then
|
||||||
log "GitHub Copilot CLI 扩展已安装(命令入口:gh copilot)"
|
log "GitHub Copilot CLI 扩展已安装(命令入口:gh copilot)"
|
||||||
|
else
|
||||||
|
warn "GitHub Copilot CLI 扩展安装失败,可稍后手动执行:gh extension install github/gh-copilot"
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
mkdir -p ~/.claude
|
mkdir -p ~/.claude
|
||||||
@@ -176,6 +194,19 @@ PROFILE_BLOCK=""
|
|||||||
[ -n "$ANTHROPIC_BASE_URL" ] && PROFILE_BLOCK+="export ANTHROPIC_BASE_URL='$ANTHROPIC_BASE_URL'\n"
|
[ -n "$ANTHROPIC_BASE_URL" ] && PROFILE_BLOCK+="export ANTHROPIC_BASE_URL='$ANTHROPIC_BASE_URL'\n"
|
||||||
PROFILE_BLOCK+="export CLAUDE_MODEL='$CLAUDE_MODEL'\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 wrapper(gh 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
|
for rc in ~/.bashrc ~/.profile; do
|
||||||
if ! grep -q "ANTHROPIC_API_KEY" "$rc" 2>/dev/null; then
|
if ! grep -q "ANTHROPIC_API_KEY" "$rc" 2>/dev/null; then
|
||||||
printf "\n# Claude Code CLI\n${PROFILE_BLOCK}" >> "$rc"
|
printf "\n# Claude Code CLI\n${PROFILE_BLOCK}" >> "$rc"
|
||||||
|
|||||||
Reference in New Issue
Block a user