Files
team/scripts/tmux-send-prompt.sh
T
2026-04-19 21:47:08 +08:00

74 lines
2.4 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# tmux-send-prompt.sh — 可靠地向 tmux pane 中的 cc 发送提示词
#
# 解决的问题:
# 1. 发送长文本后 Enter 可能未被正确接收
# 2. 前一个命令还没执行完就发送新命令
# 3. 无法确认命令是否已成功提交
# 4. 文件内容过长时 send-keys 不可靠
#
# 用法:
# tmux-send-prompt.sh <pane_id> <text_string|text_file> [--submit-delay <seconds>]
#
# 行为:
# - 文本字符串: 直接发送
# - 文件路径: 发送简短指令,让 cc 自己读取文件并执行
#
# 示例:
# tmux-send-prompt.sh %1 "短文本提示"
# tmux-send-prompt.sh %1 /path/to/prompt.md
# tmux-send-prompt.sh %1 "文本" --submit-delay 2
set -euo pipefail
PANE_ID="${1:?用法: tmux-send-prompt.sh <pane_id> <text|file> [--submit-delay <seconds>]}"
TEXT_OR_FILE="${2:?需要提供文本或文件路径}"
SUBMIT_DELAY="${3:-1}"
# 构建发送内容:文件则让 cc 自行读取,文本则直接发送
if [[ -f "$TEXT_OR_FILE" ]]; then
# 解析为绝对路径,确保 cc 能正确找到文件
FILE_PATH=$(realpath "$TEXT_OR_FILE")
TEXT="请读取 ${FILE_PATH} 并执行其中的内容"
echo "[tmux-send] 检测到文件: ${FILE_PATH},将指示 cc 自行读取"
else
TEXT="$TEXT_OR_FILE"
fi
# 步骤1: 等待 cc 的 prompt 出现(❯ 字符)
echo "[tmux-send] 等待 cc 的 prompt 就绪..."
for i in $(seq 1 30); do
LAST_LINE=$(tmux capture-pane -t "$PANE_ID" -p | tail -3 | grep -c '' || true)
if [[ "$LAST_LINE" -ge 1 ]]; then
echo "[tmux-send] cc 已就绪 (等待 ${i}s)"
break
fi
sleep 1
done
# 步骤2: 发送文本(不带 Enter)
tmux send-keys -t "$PANE_ID" "$TEXT"
sleep "${SUBMIT_DELAY}"
# 步骤3: 单独发送 Enter
tmux send-keys -t "$PANE_ID" Enter
sleep 2
# 步骤4: 验证 — 检查 ❯ 是否消失(cc 正在处理输入)
# 注意: 不能用文本匹配验证,因为 cc 会在输出中回显用户消息,导致误判
sleep 2
for i in $(seq 1 5); do
CAPTURED=$(tmux capture-pane -t "$PANE_ID" -p | tail -5)
if ! echo "$CAPTURED" | grep -q ''; then
echo "[tmux-send] 命令已成功提交 (第 $i 次检查)"
exit 0
fi
# ❯ 仍在,可能 Enter 未生效,重试
echo "[tmux-send] 仍在,重试 Enter 第 $i 次..."
tmux send-keys -t "$PANE_ID" Enter
sleep 3
done
echo "[tmux-send] 警告: 5次重试后 ❯ 仍存在,请手动检查"
exit 1