tmuxclihook 0.5.10 → 0.5.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/scripts/adapters/claude.sh +19 -0
- package/scripts/install-claude-hooks.sh +5 -1
- package/scripts/install-codex-hooks.sh +5 -1
- package/scripts/install-opencode-hooks.sh +5 -1
- package/scripts/lib-tmux-ai-status.sh +28 -5
- package/scripts/tmux-ai-reload +29 -0
- package/scripts/tmux-ai-status +31 -4
- package/tmux-ai-hooks-status.tmux +35 -13
package/package.json
CHANGED
|
@@ -175,6 +175,24 @@ _ccswitch_warn_gui() {
|
|
|
175
175
|
fi
|
|
176
176
|
}
|
|
177
177
|
|
|
178
|
+
# live 模式的真源是 $HOME/.claude/settings.json(cc-switch 硬编码,不随
|
|
179
|
+
# CLAUDE_CONFIG_DIR 变)。若当前环境的 CLAUDE_CONFIG_DIR 指向非 cc-switch
|
|
180
|
+
# 实例目录(用户自定义目录裸跑 claude),该进程读的是自定义目录,本插件写入
|
|
181
|
+
# 的真源可能不生效——纯诊断告警,不改写入行为(不做双写,理由同 2026-10-13
|
|
182
|
+
# 决策:外部覆盖拉锯风险大于收益)。
|
|
183
|
+
_ccswitch_warn_config_dir_mismatch() {
|
|
184
|
+
[ -n "${CLAUDE_CONFIG_DIR:-}" ] || return 0
|
|
185
|
+
local inst="${HOME}/.cc-switch/instances/claude" resolved
|
|
186
|
+
resolved=$(cd "$CLAUDE_CONFIG_DIR" 2>/dev/null && pwd -P) || resolved="$CLAUDE_CONFIG_DIR"
|
|
187
|
+
case "$resolved" in
|
|
188
|
+
"$inst"|"$inst"/*) ;;
|
|
189
|
+
*)
|
|
190
|
+
echo "WARN: CLAUDE_CONFIG_DIR=$CLAUDE_CONFIG_DIR 不是 cc-switch 实例目录;" \
|
|
191
|
+
"live 模式写入 $(_ccswitch_live_file) 可能不被该进程读取" >&2
|
|
192
|
+
;;
|
|
193
|
+
esac
|
|
194
|
+
}
|
|
195
|
+
|
|
178
196
|
# 读通用配置 value → mktemp 文件,路径写入 $1 命名的变量。
|
|
179
197
|
# 行缺失/空白 → "{}";非空但非法 JSON → 明确报错 return 1(绝不覆盖用户数据)。
|
|
180
198
|
# 注意:内部临时变量名不得与调用方常用名(tmp 等)同名——bash 动态作用域下
|
|
@@ -407,6 +425,7 @@ adapter_install_hooks() {
|
|
|
407
425
|
;;
|
|
408
426
|
live)
|
|
409
427
|
_ccswitch_warn_gui
|
|
428
|
+
_ccswitch_warn_config_dir_mismatch
|
|
410
429
|
target=$(_ccswitch_live_file)
|
|
411
430
|
;;
|
|
412
431
|
*)
|
|
@@ -63,8 +63,12 @@ case "${1:-install}" in
|
|
|
63
63
|
adapter_install_hooks
|
|
64
64
|
_hooks_suppress_clear claude || true
|
|
65
65
|
;;
|
|
66
|
-
|
|
66
|
+
install)
|
|
67
67
|
adapter_install_hooks
|
|
68
68
|
_hooks_suppress_clear claude || true
|
|
69
69
|
;;
|
|
70
|
+
*)
|
|
71
|
+
echo "Usage: $0 [install|uninstall|check [--json]|auto]" >&2
|
|
72
|
+
exit 2
|
|
73
|
+
;;
|
|
70
74
|
esac
|
|
@@ -94,7 +94,7 @@ _load_pane_snapshot() {
|
|
|
94
94
|
_CURRENT_SERVER_PID=""
|
|
95
95
|
command -v tmux >/dev/null 2>&1 || return 1
|
|
96
96
|
_PANE_SNAPSHOT=$(tmux list-panes -a -F \
|
|
97
|
-
'#{pane_id}|#{pane_current_path}|#{pane_current_command}|#{pane_pid}|#{pid}|#{session_attached}|#{pane_title}' \
|
|
97
|
+
'#{pane_id}|#{s/[|]/_/:pane_current_path}|#{s/[|]/_/:pane_current_command}|#{pane_pid}|#{pid}|#{session_attached}|#{pane_title}' \
|
|
98
98
|
2>/dev/null)
|
|
99
99
|
[ -n "$_PANE_SNAPSHOT" ] || return 1
|
|
100
100
|
_PANE_SNAPSHOT_OK=1
|
|
@@ -184,7 +184,8 @@ _narrow_panes_by_adapter() {
|
|
|
184
184
|
_narrow_panes_by_cwd() {
|
|
185
185
|
local base="$1" want="" matches="" id path cmd pid server attached title
|
|
186
186
|
[ -n "${HOOK_CWD:-}" ] || { printf '%s\n' "$base"; return; }
|
|
187
|
-
|
|
187
|
+
# _normalize_path 对非空输入恒返回 0(cd 失败时返回去尾斜杠原串),无需兜底分支
|
|
188
|
+
want=$(_normalize_path "$HOOK_CWD")
|
|
188
189
|
while IFS='|' read -r id path cmd pid server attached title; do
|
|
189
190
|
[ -n "$id" ] || continue
|
|
190
191
|
[ "$(_normalize_path "$path")" = "$want" ] && matches="$matches $id"
|
|
@@ -410,7 +411,10 @@ build_all_status() {
|
|
|
410
411
|
_ai_names=$(_collect_ai_process_names)
|
|
411
412
|
# 动态追加上 monitor 的 seen 标记(无 SessionEnd 工具的进程树探测结果)。
|
|
412
413
|
# switch/volta-shim 启动的 pane 前台命令不是 codex,必须靠该标记补 idle `-`。
|
|
413
|
-
|
|
414
|
+
# 自由文本字段就地消毒:tmux format 的 s/[|]/_/ 替换(`|` 是 ERE 交替符,
|
|
415
|
+
# 必须写成 [|] 或 \|;裸 s/|/…/ 不生效)。session 名/provider 名含 `|` 时
|
|
416
|
+
# 会劈裂字段——attached 位变状态字符使 $6>0 误判、该 session 整行消失。
|
|
417
|
+
_fmt="#{pane_id}|#{s/[|]/_/:session_name}|#{window_index}|#{pane_index}|#{@ai_pane_status}|#{session_attached}|#{session_created}|#{@ai_entered_time}|#{s/[|]/_/:pane_current_command}|#{s/[|]/_/:@ai_provider}|#{s/[|]/_/:@ai_provider_id}"
|
|
414
418
|
for _n in $_ai_names; do
|
|
415
419
|
_fmt="$_fmt|#{@ai_monitor_seen_$_n}"
|
|
416
420
|
done
|
|
@@ -874,7 +878,7 @@ _is_pane_session_dead() {
|
|
|
874
878
|
# 有对应文件但文件过期且进程已死(Stop 未触发的崩溃场景),也清除。
|
|
875
879
|
_cleanup_stale_panes() {
|
|
876
880
|
local active_panes=""
|
|
877
|
-
local f pane_id
|
|
881
|
+
local f pane_id pane_status pane_dir pid pid_file
|
|
878
882
|
for f in "${_STATUS_DIR}"/*/pane-*; do
|
|
879
883
|
[ -f "$f" ] || continue
|
|
880
884
|
pane_id=$(cat "$f" 2>/dev/null)
|
|
@@ -925,7 +929,6 @@ _cleanup_stale_panes() {
|
|
|
925
929
|
# 清理孤儿 poll 进程
|
|
926
930
|
for pid_file in "${pane_dir}"*-poll-pid; do
|
|
927
931
|
[ -f "$pid_file" ] || continue
|
|
928
|
-
local pid
|
|
929
932
|
pid=$(cat "$pid_file" 2>/dev/null)
|
|
930
933
|
[ -n "$pid" ] && kill "$pid" 2>/dev/null
|
|
931
934
|
done
|
|
@@ -935,6 +938,25 @@ _cleanup_stale_panes() {
|
|
|
935
938
|
done
|
|
936
939
|
}
|
|
937
940
|
|
|
941
|
+
# 清理已断开 client 的 @ai_prev_sess_* 残留。key 是写入端有损 sanitize
|
|
942
|
+
# (tr -c 'A-Za-z0-9._-' '_'),无法从 key 反推 client → 反向枚举存活集比对。
|
|
943
|
+
# 实测关注:长期运行的 server 会累积已 detach 的 client 键(8 option / 4 存活)。
|
|
944
|
+
_prune_prev_sess_options() {
|
|
945
|
+
local alive=" " name key opt
|
|
946
|
+
while IFS= read -r name; do
|
|
947
|
+
[ -n "$name" ] || continue
|
|
948
|
+
key=$(printf '%s' "$name" | tr -c 'A-Za-z0-9._-' '_')
|
|
949
|
+
alive="$alive$key "
|
|
950
|
+
done < <(tmux list-clients -F '#{client_name}' 2>/dev/null)
|
|
951
|
+
for opt in $(tmux show-options -g 2>/dev/null | awk '/^@ai_prev_sess_/{print $1}'); do
|
|
952
|
+
key="${opt#@ai_prev_sess_}"
|
|
953
|
+
case "$alive" in
|
|
954
|
+
*" $key "*) ;;
|
|
955
|
+
*) tmux set-option -gu "$opt" 2>/dev/null || true ;;
|
|
956
|
+
esac
|
|
957
|
+
done
|
|
958
|
+
}
|
|
959
|
+
|
|
938
960
|
# 频率限制的孤儿清理(每次 hook 调用,最多每 60s 执行一次)
|
|
939
961
|
_maybe_cleanup_stale() {
|
|
940
962
|
local marker="${_STATUS_DIR}/.stale-cleanup-ts"
|
|
@@ -947,6 +969,7 @@ _maybe_cleanup_stale() {
|
|
|
947
969
|
[ $((now - ${last:-0})) -lt 60 ] && return
|
|
948
970
|
fi
|
|
949
971
|
_cleanup_stale_panes
|
|
972
|
+
_prune_prev_sess_options
|
|
950
973
|
echo "$now" > "$marker"
|
|
951
974
|
}
|
|
952
975
|
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# tmux-ai-reload — prefix+r 重载:source 实际存在的配置文件后重跑插件入口
|
|
3
|
+
#
|
|
4
|
+
# 背景:入口曾用 mktemp conf 绑定 `source-file ~/.tmux.conf \; run-shell 入口`:
|
|
5
|
+
# 1) 硬编码 ~/.tmux.conf(XDG / tmux -f 用户 reload 读错文件)
|
|
6
|
+
# 2) 一次性哨兵:source-file 内用户自己的 `bind r` 会把绑定重置回原命令,
|
|
7
|
+
# 哨兵已置位 → 纯 TPM 模式(配置无 run-shell 行)从第二次重载起插件初始化
|
|
8
|
+
# 静默停止
|
|
9
|
+
# 本 helper 每次由绑定直接调用(入口每次加载都重绑,自愈):
|
|
10
|
+
# - 按 tmux 的 #{config_files} 候选列表逐个过滤存在性后 source(顺序同启动)
|
|
11
|
+
# - 再显式跑一次入口(纯 TPM 模式也能重新初始化;配置里的 run-shell 行会让
|
|
12
|
+
# 入口再跑一次——幂等,仅一次多余启动)
|
|
13
|
+
set -u
|
|
14
|
+
|
|
15
|
+
_SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
16
|
+
_ENTRY="$(cd "${_SELF_DIR}/.." && pwd)/tmux-ai-hooks-status.tmux"
|
|
17
|
+
|
|
18
|
+
_cfg=$(tmux display-message -p '#{config_files}' 2>/dev/null || true)
|
|
19
|
+
# config_files 实测是「候选列表」而非已加载列表(含不存在路径)→ 逐个 [ -f ] 过滤;
|
|
20
|
+
# 老版本 tmux 无该 format 时回落 ~/.tmux.conf。
|
|
21
|
+
[ -n "$_cfg" ] || _cfg="$HOME/.tmux.conf"
|
|
22
|
+
# 注意用 printf '%s\n':末项无换行符时 read 返回非零,循环体会漏掉最后一项
|
|
23
|
+
printf '%s\n' "$_cfg" | tr ',' '\n' | while IFS= read -r _f; do
|
|
24
|
+
[ -n "$_f" ] && [ -f "$_f" ] || continue
|
|
25
|
+
tmux source-file "$_f" 2>/dev/null || true
|
|
26
|
+
done
|
|
27
|
+
|
|
28
|
+
[ -f "$_ENTRY" ] && tmux run-shell "$_ENTRY" 2>/dev/null || true
|
|
29
|
+
tmux display-message "配置已重载" 2>/dev/null || true
|
package/scripts/tmux-ai-status
CHANGED
|
@@ -78,6 +78,25 @@ _current_pane_status() {
|
|
|
78
78
|
tmux display-message -pt "$TMUX_PANE" -p "#{@ai_pane_status}" 2>/dev/null
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
+
# Notification「隐式」路径(idle_prompt / waiting for your input)的状态解析:
|
|
82
|
+
# 通知可能乱序晚到(async hook 无顺序保证),不能无条件降级为 idle '-'。
|
|
83
|
+
# 优先级:pending 派生(工具确实在跑 → '>')> live 审批态(!/?;claude 不写
|
|
84
|
+
# pending-perm,纯派生看不见审批态)> idle '-'。
|
|
85
|
+
# 显式重置消息(denied/cancelled/rejected)不走本函数——denial 携带「等待已结束」
|
|
86
|
+
# 语义,应无条件置 '-'。
|
|
87
|
+
_notification_implicit_status() {
|
|
88
|
+
if _derive_status_from_pending; then
|
|
89
|
+
printf '%s' "$DERIVED"
|
|
90
|
+
return
|
|
91
|
+
fi
|
|
92
|
+
local live
|
|
93
|
+
live=$(_current_pane_status)
|
|
94
|
+
case "$live" in
|
|
95
|
+
"!"|"?") printf '%s' "$live"; return ;;
|
|
96
|
+
esac
|
|
97
|
+
printf '%s' "-"
|
|
98
|
+
}
|
|
99
|
+
|
|
81
100
|
# 重建聚合状态(_refresh / _entered 共用)
|
|
82
101
|
_run_refresh() {
|
|
83
102
|
_maybe_repair_hooks
|
|
@@ -94,6 +113,7 @@ PREV_STATUS=$(_current_pane_status)
|
|
|
94
113
|
TOOL=""
|
|
95
114
|
TOOL_USE_ID=""
|
|
96
115
|
INPUT_DIGEST=""
|
|
116
|
+
_log_flags=""
|
|
97
117
|
|
|
98
118
|
case "$EVENT" in
|
|
99
119
|
Notification)
|
|
@@ -102,14 +122,15 @@ case "$EVENT" in
|
|
|
102
122
|
INPUT_DIGEST=$(echo "$INPUT" | jq -c '{notification_type, message}' 2>/dev/null)
|
|
103
123
|
case "$NOTIF_TYPE" in
|
|
104
124
|
idle_prompt)
|
|
105
|
-
STATUS
|
|
125
|
+
STATUS=$(_notification_implicit_status)
|
|
106
126
|
;;
|
|
107
127
|
*)
|
|
108
128
|
case "$MSG" in
|
|
109
129
|
*"waiting for your input"*)
|
|
110
|
-
STATUS
|
|
130
|
+
STATUS=$(_notification_implicit_status)
|
|
111
131
|
;;
|
|
112
132
|
*"denied"*|*"cancelled"*|*"canceled"*|*"rejected"*)
|
|
133
|
+
# 显式重置:denial 表示等待已结束,无条件 idle(即便有 pending 残项)
|
|
113
134
|
STATUS="-"
|
|
114
135
|
;;
|
|
115
136
|
*)
|
|
@@ -118,6 +139,8 @@ case "$EVENT" in
|
|
|
118
139
|
esac
|
|
119
140
|
;;
|
|
120
141
|
esac
|
|
142
|
+
# 写入循环内不再对 Notification 重复 re-derive:事件锁(tmux-ai-status 头部)
|
|
143
|
+
# 已串行化同 pane 事件,分支内判定即最终判定。
|
|
121
144
|
;;
|
|
122
145
|
PermissionRequest)
|
|
123
146
|
# 一次 @tsv 提取 tool_name/tool_use_id(digest 单独一次;stdin 只能读一次,
|
|
@@ -209,8 +232,12 @@ case "$EVENT" in
|
|
|
209
232
|
else
|
|
210
233
|
STATUS="✓"
|
|
211
234
|
fi
|
|
212
|
-
# fallback: TMUX_PANE 未解析时,精准清理孤儿状态(不碰其他活跃 pane
|
|
235
|
+
# fallback: TMUX_PANE 未解析时,精准清理孤儿状态(不碰其他活跃 pane)。
|
|
236
|
+
# 此时事件无处写入——不要伪造 ✓ 写入日志(真实 pane 可能仍是 !):
|
|
237
|
+
# STATUS 置空 + flags 标注,日志显示「无状态转移」。
|
|
213
238
|
if [ -n "$TMUX" ] && [ -z "$TMUX_PANE" ]; then
|
|
239
|
+
STATUS=""
|
|
240
|
+
_log_flags="PANE=unresolved"
|
|
214
241
|
_cleanup_stale_panes
|
|
215
242
|
build_all_status
|
|
216
243
|
tmux set-option -g @ai_all_status "$ALL" 2>/dev/null || true
|
|
@@ -294,7 +321,7 @@ if [ -n "$INPUT_DIGEST" ] && [ "$INPUT_DIGEST" != "null" ]; then
|
|
|
294
321
|
_log_input_line="input: $(_ai_log_truncate_input "$INPUT_DIGEST")"
|
|
295
322
|
fi
|
|
296
323
|
fi
|
|
297
|
-
_ai_log_event "$PREV_STATUS" "$STATUS" "" "$_log_tool_line" "$_log_input_line"
|
|
324
|
+
_ai_log_event "$PREV_STATUS" "$STATUS" "$_log_flags" "$_log_tool_line" "$_log_input_line"
|
|
298
325
|
|
|
299
326
|
if [ -n "$TMUX_PANE" ] && { [ -n "$TMUX" ] || command -v tmux &>/dev/null; }; then
|
|
300
327
|
# 写入前重新读取 pending,避免并发事件用过期结果覆盖仍有效的审批态。
|
|
@@ -43,19 +43,25 @@ tmux set-option -g "status-format[${CLAUDE_ROW}]" "#[align=centre]#{?#{@ai_all_s
|
|
|
43
43
|
# 不动 status-right,让用户自行管理第 1 行内容
|
|
44
44
|
# Claude 状态通过多行 status-format 的独立行显示,不修改 status-right
|
|
45
45
|
|
|
46
|
-
# --- Reload
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
#
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
tmux
|
|
58
|
-
|
|
46
|
+
# --- Reload 保护:prefix+r = source 实际配置文件 + 重跑本插件初始化 ---
|
|
47
|
+
# 绑定单命令指向 scripts/tmux-ai-reload(不再需要 mktemp conf 规避 \;)。
|
|
48
|
+
# ⚠️ 每次加载都重绑(bind-key 是 upsert,幂等):旧实现用一次性哨兵
|
|
49
|
+
# @claude_hooks_reload_registered,但 source-file 配置内用户自己的 `bind r`
|
|
50
|
+
# 会把绑定重置回原命令,哨兵已置位 → 纯 TPM 模式从第二次重载起插件初始化
|
|
51
|
+
# 静默停止。每次重绑使其自愈。
|
|
52
|
+
# 原绑定保存:覆盖前捕获(排除本插件新旧两种命令形态),供手动恢复:
|
|
53
|
+
# tmux show-option -gv @claude_hooks_saved_bind_r # 查看
|
|
54
|
+
# eval "$(tmux show-option -gv @claude_hooks_saved_bind_r)" # 回放
|
|
55
|
+
_prev_r=$(tmux list-keys -T prefix r 2>/dev/null || true)
|
|
56
|
+
case "$_prev_r" in
|
|
57
|
+
""|*tmux-ai-reload*|*tmux-ai-hooks-status.tmux*) ;;
|
|
58
|
+
*)
|
|
59
|
+
if [ -z "$(tmux show-option -gv @claude_hooks_saved_bind_r 2>/dev/null)" ]; then
|
|
60
|
+
tmux set-option -g @claude_hooks_saved_bind_r "$_prev_r" 2>/dev/null || true
|
|
61
|
+
fi
|
|
62
|
+
;;
|
|
63
|
+
esac
|
|
64
|
+
tmux bind-key r run-shell "'${CURRENT_DIR}/scripts/tmux-ai-reload'" 2>/dev/null || true
|
|
59
65
|
|
|
60
66
|
# --- 自动注册 Claude hooks(幂等,每次插件加载时确保 hooks 存在)---
|
|
61
67
|
# auto 模式:用户显式卸载过(suppressed-claude 标记在)时静默跳过,尊重卸载意图;
|
|
@@ -119,4 +125,20 @@ else
|
|
|
119
125
|
fi
|
|
120
126
|
|
|
121
127
|
# Esc 拒绝检测:! / ? 状态下 Esc → -
|
|
128
|
+
# 全局 root 表绑定会覆盖用户既有 Escape 绑定——覆盖前保存 + 一次性告警
|
|
129
|
+
# (无法安全链式调用任意已有绑定,恢复需手动):
|
|
130
|
+
# tmux show-option -gv @claude_hooks_saved_bind_escape
|
|
131
|
+
_prev_esc=$(tmux list-keys -n Escape 2>/dev/null || true)
|
|
132
|
+
case "$_prev_esc" in
|
|
133
|
+
""|*tmux-ai-esc*) ;;
|
|
134
|
+
*)
|
|
135
|
+
if [ -z "$(tmux show-option -gv @claude_hooks_saved_bind_escape 2>/dev/null)" ]; then
|
|
136
|
+
tmux set-option -g @claude_hooks_saved_bind_escape "$_prev_esc" 2>/dev/null || true
|
|
137
|
+
fi
|
|
138
|
+
if [ -z "$(tmux show-option -gv @claude_hooks_esc_warned 2>/dev/null)" ]; then
|
|
139
|
+
tmux display-message "⚠ tmux-ai-hooks-status 覆盖了全局 Esc 绑定;原绑定已存 @claude_hooks_saved_bind_escape" 2>/dev/null || true
|
|
140
|
+
tmux set-option -g @claude_hooks_esc_warned "1" 2>/dev/null || true
|
|
141
|
+
fi
|
|
142
|
+
;;
|
|
143
|
+
esac
|
|
122
144
|
tmux bind-key -n Escape run-shell "${CURRENT_DIR}/scripts/tmux-ai-esc"
|