@ssh352/tmux-attention 0.0.0-stage → 1.0.0
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/README.md +42 -2
- package/dist/cli.js +38 -0
- package/dist/codex.js +90 -0
- package/dist/index.js +39 -0
- package/dist/install.js +89 -0
- package/dist/picker.js +147 -0
- package/dist/tmux.js +38 -0
- package/package.json +46 -4
package/README.md
CHANGED
|
@@ -1,3 +1,43 @@
|
|
|
1
|
-
#
|
|
1
|
+
# tmux-attention
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Find Pi and Codex panes that need attention, including panes in other tmux sessions. A `?` badge marks a question or permission request. A `✓` badge marks a completed turn.
|
|
4
|
+
|
|
5
|
+
Click a badge, or press the tmux prefix followed by `a` to open the fzf picker. If `a` already has a binding, the extension tries `j`. The picker shows waiting panes first. Selecting a finished pane clears its badge.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
Install Node.js 22.19 or later, tmux with popup support, and fzf. Install the package in your personal Pi configuration and reload Pi.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
pi install npm:@ssh352/tmux-attention
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
The extension activates in interactive Pi sessions inside tmux. If you used the old manual installation, move its directory or symlink out of `~/.pi/agent/extensions` before reloading Pi so the extension loads once.
|
|
16
|
+
|
|
17
|
+
## Enable Codex
|
|
18
|
+
|
|
19
|
+
Run the bundled installer from Pi’s personal npm installation.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
~/.pi/agent/npm/node_modules/.bin/tmux-attention codex install
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Open `/hooks` in Codex and review the eight command hooks. The installer uses `$CODEX_HOME/hooks.json`, which defaults to `~/.codex/hooks.json`. It migrates the Python registrations in place and preserves unrelated handlers. Running it again with the same package path makes no changes.
|
|
26
|
+
|
|
27
|
+
Codex runs the Node.js hook directly. Pi can be closed. The default installation registers the following command.
|
|
28
|
+
|
|
29
|
+
```sh
|
|
30
|
+
exec node "$HOME/.pi/agent/npm/node_modules/@ssh352/tmux-attention/dist/cli.js" codex hook
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Moving from the Python command to this command requires a new approval. Package updates at the same path keep the same command and hook hashes.
|
|
34
|
+
|
|
35
|
+
## Develop
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
npm ci
|
|
39
|
+
npm test
|
|
40
|
+
npm pack
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The build compiles the TypeScript extension, picker, and Codex CLI into `dist/`. The tests use a disposable tmux server and temporary hook configuration.
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { handle } from "./codex.js";
|
|
4
|
+
import { install } from "./install.js";
|
|
5
|
+
import { click, jump } from "./picker.js";
|
|
6
|
+
import { paneFromEnv } from "./tmux.js";
|
|
7
|
+
function main(args) {
|
|
8
|
+
const [integration, action, client, range] = args;
|
|
9
|
+
if (integration === "codex" && args.length === 2) {
|
|
10
|
+
if (action === "install")
|
|
11
|
+
return install();
|
|
12
|
+
if (action === "hook") {
|
|
13
|
+
const pane = paneFromEnv();
|
|
14
|
+
if (pane) {
|
|
15
|
+
// Pane/server shutdown can race a hook. Status reporting is best effort.
|
|
16
|
+
try {
|
|
17
|
+
handle(pane, JSON.parse(readFileSync(0, "utf8")));
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
console.error("tmux-attention:", error instanceof Error ? error.message : error);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (integration === "picker" && action === "jump" && args.length === 3)
|
|
27
|
+
return jump(client);
|
|
28
|
+
if (integration === "picker" && action === "click" && args.length === 4)
|
|
29
|
+
return click(client, range);
|
|
30
|
+
throw new Error("Usage: tmux-attention codex install | codex hook | picker jump CLIENT | picker click CLIENT RANGE");
|
|
31
|
+
}
|
|
32
|
+
try {
|
|
33
|
+
main(process.argv.slice(2));
|
|
34
|
+
}
|
|
35
|
+
catch (error) {
|
|
36
|
+
console.error("tmux-attention:", error instanceof Error ? error.message : error);
|
|
37
|
+
process.exitCode = 1;
|
|
38
|
+
}
|
package/dist/codex.js
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { setup } from "./picker.js";
|
|
3
|
+
import { marker, markOption, sessionOption, tm, turnOption, updatePane, waitOption } from "./tmux.js";
|
|
4
|
+
const clear = { [markOption]: null, [waitOption]: null };
|
|
5
|
+
function token(value) {
|
|
6
|
+
return createHash("sha256").update(value).digest("hex");
|
|
7
|
+
}
|
|
8
|
+
function updateWait(pane, session, turn, ownsTurn, payload) {
|
|
9
|
+
const tool = payload.tool_name ?? "";
|
|
10
|
+
const command = tool === "Bash" ? payload.tool_input?.command ?? null : null;
|
|
11
|
+
// Match the previous Python hook's JSON tuple so an in-flight approval
|
|
12
|
+
// can be cleared after upgrading within an existing tmux session.
|
|
13
|
+
const parts = [tool, command].map(value => JSON.stringify(value).replace(/[\u007f-\uffff]/g, char => "\\u" + char.charCodeAt(0).toString(16).padStart(4, "0")));
|
|
14
|
+
const approval = "a" + token("[" + parts.join(", ") + "]");
|
|
15
|
+
const question = "q" + token(payload.tool_use_id ?? "");
|
|
16
|
+
for (;;) {
|
|
17
|
+
const snapshot = tm("display-message", "-p", "-t", pane, "#{@pi_attention_codex_session}\t#{@pi_attention_codex_turn}\t#{@pi_attention_codex_wait}");
|
|
18
|
+
const [owner, activeTurn, previous] = snapshot.split("\t");
|
|
19
|
+
if (owner !== session || activeTurn !== turn)
|
|
20
|
+
return;
|
|
21
|
+
const pending = new Set(previous ? previous.split("_") : []);
|
|
22
|
+
switch (payload.hook_event_name) {
|
|
23
|
+
case "PermissionRequest":
|
|
24
|
+
pending.add(approval);
|
|
25
|
+
break;
|
|
26
|
+
case "PreToolUse":
|
|
27
|
+
pending.add(question);
|
|
28
|
+
break;
|
|
29
|
+
default:
|
|
30
|
+
pending.delete(approval);
|
|
31
|
+
pending.delete(question);
|
|
32
|
+
}
|
|
33
|
+
const waiting = [...pending].sort().join("_");
|
|
34
|
+
if (waiting === previous)
|
|
35
|
+
return;
|
|
36
|
+
const message = waiting.startsWith("a") ? "Codex approval requested" : "Codex waiting for input";
|
|
37
|
+
const next = waiting ? { ...marker("?", message), [waitOption]: waiting } : clear;
|
|
38
|
+
// Another hook can update the pending set between the read and the write.
|
|
39
|
+
const unchanged = "#{==:#{@pi_attention_codex_wait}," + previous + "}";
|
|
40
|
+
if (updatePane(pane, next, "#{&&:" + ownsTurn + "," + unchanged + "}"))
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
export function handle(pane, payload) {
|
|
45
|
+
if (payload.agent_id || !payload.session_id)
|
|
46
|
+
return;
|
|
47
|
+
const event = payload.hook_event_name;
|
|
48
|
+
const session = token(payload.session_id);
|
|
49
|
+
const ownsSession = "#{==:#{@pi_attention_codex_session}," + session + "}";
|
|
50
|
+
const owner = { [sessionOption]: session, "@pi_attention_pid": String(process.ppid) };
|
|
51
|
+
if (event === "SessionStart") {
|
|
52
|
+
if (payload.source === "compact")
|
|
53
|
+
return;
|
|
54
|
+
setup();
|
|
55
|
+
updatePane(pane, { ...owner, [turnOption]: null, ...clear });
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (event === "SessionEnd") {
|
|
59
|
+
updatePane(pane, { [sessionOption]: null, [turnOption]: null, ...clear }, ownsSession);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
if (!payload.turn_id)
|
|
63
|
+
return;
|
|
64
|
+
const turn = token(payload.turn_id);
|
|
65
|
+
if (event === "UserPromptSubmit") {
|
|
66
|
+
if (!tm("show-options", "-pqv", "-t", pane, sessionOption))
|
|
67
|
+
setup();
|
|
68
|
+
updatePane(pane, { ...owner, [turnOption]: turn, ...clear }, "#{||:" + ownsSession + ",#{==:#{@pi_attention_codex_session},}}");
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const ownsTurn = "#{&&:" + ownsSession + ",#{==:#{@pi_attention_codex_turn}," + turn + "}}";
|
|
72
|
+
switch (event) {
|
|
73
|
+
case "PreToolUse":
|
|
74
|
+
if (payload.tool_name !== "request_user_input") {
|
|
75
|
+
updatePane(pane, clear, "#{&&:" + ownsTurn + ",#{==:#{@pi_attention_mark},✓}}");
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
updateWait(pane, session, turn, ownsTurn, payload);
|
|
79
|
+
break;
|
|
80
|
+
case "PermissionRequest":
|
|
81
|
+
case "PostToolUse":
|
|
82
|
+
updateWait(pane, session, turn, ownsTurn, payload);
|
|
83
|
+
break;
|
|
84
|
+
case "Interrupt":
|
|
85
|
+
updatePane(pane, { [turnOption]: null, ...clear }, ownsTurn);
|
|
86
|
+
break;
|
|
87
|
+
case "Stop":
|
|
88
|
+
updatePane(pane, marker("✓", "Codex finished"), ownsTurn);
|
|
89
|
+
}
|
|
90
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { setup } from "./picker.js";
|
|
2
|
+
import { marker, markOption, paneFromEnv, sessionOption, updatePane } from "./tmux.js";
|
|
3
|
+
export default function (pi) {
|
|
4
|
+
const pane = paneFromEnv();
|
|
5
|
+
if (!pane)
|
|
6
|
+
return;
|
|
7
|
+
const owner = String(process.pid);
|
|
8
|
+
let active = false;
|
|
9
|
+
function clear() {
|
|
10
|
+
if (active)
|
|
11
|
+
updatePane(pane, { [markOption]: null }, "#{==:#{@pi_attention_pid}," + owner + "}");
|
|
12
|
+
}
|
|
13
|
+
function mark(symbol, label) {
|
|
14
|
+
if (!active)
|
|
15
|
+
return;
|
|
16
|
+
const text = Array.from(label.replace(/[\u0000-\u001f\u007f-\u009f]/g, " ")).slice(0, 100).join("");
|
|
17
|
+
updatePane(pane, { "@pi_attention_pid": owner, ...marker(symbol, text) });
|
|
18
|
+
}
|
|
19
|
+
pi.on("session_start", (_event, ctx) => {
|
|
20
|
+
if (ctx.mode !== "tui")
|
|
21
|
+
return;
|
|
22
|
+
setup();
|
|
23
|
+
updatePane(pane, { [sessionOption]: null, [markOption]: null });
|
|
24
|
+
active = true;
|
|
25
|
+
});
|
|
26
|
+
pi.on("agent_start", clear);
|
|
27
|
+
pi.on("ui_prompt_start", event => mark("?", event.title?.trim() || "Waiting for input"));
|
|
28
|
+
pi.on("ui_prompt_end", clear);
|
|
29
|
+
pi.on("agent_settled", event => {
|
|
30
|
+
if (event.aborted)
|
|
31
|
+
clear();
|
|
32
|
+
else
|
|
33
|
+
mark("✓", "Finished");
|
|
34
|
+
});
|
|
35
|
+
pi.on("session_shutdown", () => {
|
|
36
|
+
clear();
|
|
37
|
+
active = false;
|
|
38
|
+
});
|
|
39
|
+
}
|
package/dist/install.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { chmodSync, lstatSync, mkdirSync, mkdtempSync, readFileSync, readlinkSync, realpathSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
|
+
import { cliPath, shellQuote } from "./tmux.js";
|
|
5
|
+
const events = ["SessionStart", "UserPromptSubmit", "PreToolUse", "PermissionRequest",
|
|
6
|
+
"PostToolUse", "Stop", "Interrupt", "SessionEnd"];
|
|
7
|
+
function hookCommand() {
|
|
8
|
+
// Pi's public installation path stays stable even if the package manager links
|
|
9
|
+
// its contents to a versioned store.
|
|
10
|
+
const agent = process.env.PI_CODING_AGENT_DIR || join(homedir(), ".pi", "agent");
|
|
11
|
+
const installed = join(agent, "npm/node_modules/@ssh352/tmux-attention/dist/cli.js");
|
|
12
|
+
const script = statSync(installed, { throwIfNoEntry: false }) && realpathSync(installed) === realpathSync(cliPath)
|
|
13
|
+
? installed : cliPath;
|
|
14
|
+
const rel = relative(realpathSync(homedir()), script);
|
|
15
|
+
const underHome = !isAbsolute(rel) && rel !== ".." && !rel.startsWith(".." + sep);
|
|
16
|
+
const argument = underHome ? '"$HOME/' + rel.replace(/["$\\\x60]/g, "\\$&") + '"' : shellQuote(script);
|
|
17
|
+
return "exec node " + argument + " codex hook";
|
|
18
|
+
}
|
|
19
|
+
function owned(handler, command) {
|
|
20
|
+
if (handler.type !== "command" || typeof handler.command !== "string")
|
|
21
|
+
return false;
|
|
22
|
+
return handler.command === command ||
|
|
23
|
+
/^exec python3 -B .+\/tmux-attention\/codex_hook\.py["']?$/.test(handler.command) ||
|
|
24
|
+
/^exec node .+\/tmux-attention\/dist\/cli\.js["']? codex hook$/.test(handler.command);
|
|
25
|
+
}
|
|
26
|
+
function register(groups, command) {
|
|
27
|
+
let registered = false;
|
|
28
|
+
const result = [];
|
|
29
|
+
for (const group of groups) {
|
|
30
|
+
if (!Array.isArray(group.hooks))
|
|
31
|
+
throw new Error("invalid Codex hook group");
|
|
32
|
+
const handlers = [];
|
|
33
|
+
for (const handler of group.hooks) {
|
|
34
|
+
if (!owned(handler, command)) {
|
|
35
|
+
handlers.push(handler);
|
|
36
|
+
}
|
|
37
|
+
else if (!registered) {
|
|
38
|
+
handlers.push({ ...handler, command });
|
|
39
|
+
registered = true;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
if (handlers.length || !group.hooks.length)
|
|
43
|
+
result.push({ ...group, hooks: handlers });
|
|
44
|
+
}
|
|
45
|
+
if (!registered)
|
|
46
|
+
result.push({ hooks: [{ type: "command", command }] });
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
function targetFile(path) {
|
|
50
|
+
// Follow even a dangling user-managed symlink before creating the file.
|
|
51
|
+
for (let count = 0; count < 40; count++) {
|
|
52
|
+
if (!lstatSync(path, { throwIfNoEntry: false })?.isSymbolicLink())
|
|
53
|
+
return path;
|
|
54
|
+
path = resolve(dirname(path), readlinkSync(path));
|
|
55
|
+
}
|
|
56
|
+
throw new Error("too many symlinks in hooks.json path");
|
|
57
|
+
}
|
|
58
|
+
export function install() {
|
|
59
|
+
const home = process.env.CODEX_HOME || join(homedir(), ".codex");
|
|
60
|
+
const path = targetFile(join(home, "hooks.json"));
|
|
61
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
62
|
+
const mode = statSync(path, { throwIfNoEntry: false })?.mode;
|
|
63
|
+
const original = mode === undefined ? undefined : readFileSync(path, "utf8");
|
|
64
|
+
const data = JSON.parse(original ?? "{}");
|
|
65
|
+
const before = JSON.stringify(data);
|
|
66
|
+
const hooks = data.hooks ??= {};
|
|
67
|
+
if (Array.isArray(hooks) || typeof hooks !== "object")
|
|
68
|
+
throw new Error("invalid Codex hooks object");
|
|
69
|
+
const command = hookCommand();
|
|
70
|
+
for (const event of events)
|
|
71
|
+
hooks[event] = register(hooks[event] ?? [], command);
|
|
72
|
+
if (before !== JSON.stringify(data)) {
|
|
73
|
+
const temp = mkdtempSync(join(dirname(path), ".tmux-attention-"));
|
|
74
|
+
try {
|
|
75
|
+
const file = join(temp, "hooks.json");
|
|
76
|
+
writeFileSync(file, JSON.stringify(data, null, 2) + "\n", { mode: 0o600 });
|
|
77
|
+
if (mode !== undefined)
|
|
78
|
+
chmodSync(file, mode & 0o777);
|
|
79
|
+
const latest = statSync(path, { throwIfNoEntry: false }) ? readFileSync(path, "utf8") : undefined;
|
|
80
|
+
if (latest !== original)
|
|
81
|
+
throw new Error("hooks.json changed during registration. Run the installer again.");
|
|
82
|
+
renameSync(file, path);
|
|
83
|
+
}
|
|
84
|
+
finally {
|
|
85
|
+
rmSync(temp, { recursive: true, force: true });
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
console.log("Registered tmux-attention in " + path + ". Review the eight hooks with /hooks in Codex.");
|
|
89
|
+
}
|
package/dist/picker.js
ADDED
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { execFileSync, spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { cliPath, markOption, shellQuote, tm } from "./tmux.js";
|
|
6
|
+
const badgesOption = "#{E:@pi_attention_badges}";
|
|
7
|
+
const badgeRange = "#{m:pi%*,#{mouse_status_range}}";
|
|
8
|
+
const jumpCommand = "#{@pi_attention_picker} jump #{q:client_name}";
|
|
9
|
+
const clickCommand = "#{@pi_attention_picker} click #{q:client_name} #{q:mouse_status_range}";
|
|
10
|
+
function jumpKey() {
|
|
11
|
+
let available;
|
|
12
|
+
for (const key of ["a", "j"]) {
|
|
13
|
+
const binding = spawnSync("tmux", ["list-keys", "-T", "prefix", key], { encoding: "utf8" });
|
|
14
|
+
if (binding.error)
|
|
15
|
+
throw binding.error;
|
|
16
|
+
if (binding.stdout.includes(jumpCommand) || binding.stdout.includes("/tmux-attention/picker.py") &&
|
|
17
|
+
binding.stdout.includes(" jump #{q:client_name}"))
|
|
18
|
+
return key;
|
|
19
|
+
if (binding.status !== 0)
|
|
20
|
+
available ??= key;
|
|
21
|
+
}
|
|
22
|
+
if (!available)
|
|
23
|
+
throw new Error("prefix + j and prefix + a are already bound");
|
|
24
|
+
return available;
|
|
25
|
+
}
|
|
26
|
+
function bindStatusClick() {
|
|
27
|
+
const binding = spawnSync("tmux", ["list-keys", "-T", "root", "MouseDown1Status"], { encoding: "utf8" });
|
|
28
|
+
if (binding.error)
|
|
29
|
+
throw binding.error;
|
|
30
|
+
const current = binding.stdout.trim();
|
|
31
|
+
if (current.includes(clickCommand))
|
|
32
|
+
return;
|
|
33
|
+
const clickAction = "run-shell -b " + shellQuote(clickCommand);
|
|
34
|
+
// list-keys emits tmux configuration syntax. Replace just the old positive
|
|
35
|
+
// branch so the user's serialized fallback (including nested commands) survives.
|
|
36
|
+
const oldClick = /"run-shell -b (?:\\.|[^"\\])*\/tmux-attention\/picker\.py(?:\\.|[^"\\])* click (?:\\.|[^"\\])*"/;
|
|
37
|
+
if (current.includes(badgeRange) && oldClick.test(current)) {
|
|
38
|
+
execFileSync("tmux", ["source-file", "-"], {
|
|
39
|
+
input: current.replace(oldClick, () => JSON.stringify(clickAction)) + "\n",
|
|
40
|
+
});
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
const fallback = current.slice(current.indexOf("MouseDown1Status") + "MouseDown1Status".length).trim();
|
|
44
|
+
tm("bind-key", "-T", "root", "MouseDown1Status", "if-shell", "-F", badgeRange, clickAction, fallback);
|
|
45
|
+
}
|
|
46
|
+
export function setup() {
|
|
47
|
+
const key = jumpKey();
|
|
48
|
+
tm("set-option", "-g", "@pi_attention_picker", "node " + shellQuote(cliPath) + " picker");
|
|
49
|
+
bindStatusClick();
|
|
50
|
+
const status = tm("show-options", "-gv", "status-right");
|
|
51
|
+
if (!status.includes(badgesOption))
|
|
52
|
+
tm("set-option", "-g", "status-right", badgesOption + status);
|
|
53
|
+
const badges = "#{S:#{W:#{P:#{?@pi_attention_mark,#[range=user|pi#{pane_id}#{session_id}]" +
|
|
54
|
+
"#{@pi_attention_mark} #{session_name}:#{window_index}.#{pane_index} #[norange],}}}}";
|
|
55
|
+
tm("set-option", "-g", "@pi_attention_badges", badges, ";", "bind-key", "-T", "prefix", key, "run-shell", "-b", jumpCommand, ";", "set-hook", "-g", "pane-focus-in[8320]", "if-shell -F '#{==:#{@pi_attention_mark},✓}' { set-option -pu @pi_attention_mark }");
|
|
56
|
+
}
|
|
57
|
+
function clientTarget(client) {
|
|
58
|
+
const clients = tm("list-clients", "-F", "#{client_name}\t#{session_id}:#{window_id}.#{pane_id}");
|
|
59
|
+
for (const line of clients.split("\n")) {
|
|
60
|
+
const [name, target] = line.split("\t");
|
|
61
|
+
if (name === client)
|
|
62
|
+
return target;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function clearIfSame(pane, stamp, mark) {
|
|
66
|
+
const condition = "#{&&:#{==:#{@pi_attention_time}," + stamp + "},#{==:#{@pi_attention_mark}," + mark + "}}";
|
|
67
|
+
tm("if-shell", "-F", "-t", pane, condition, "set-option -pu -t " + pane + " " + markOption);
|
|
68
|
+
}
|
|
69
|
+
function entries(preferredSession) {
|
|
70
|
+
const fields = ["pane_id", markOption, "@pi_attention_time", "@pi_attention_pid", "session_id",
|
|
71
|
+
"window_id", "session_name", "window_index", "pane_index", "window_name", "@pi_attention_text"];
|
|
72
|
+
const format = fields.map(field => "#{" + field + "}").join("\t");
|
|
73
|
+
const output = tm("list-panes", "-a", "-f", "#{&&:#{@pi_attention_mark},#{!:#{pane_dead}}}", "-F", format);
|
|
74
|
+
const found = new Map();
|
|
75
|
+
for (const line of output.split("\n")) {
|
|
76
|
+
const [pane, mark, stamp, pid, session, windowId, sessionName, win, index, window, message] = line.split("\t");
|
|
77
|
+
if (!/^%\d+$/.test(pane) || !["?", "✓"].includes(mark) || !/^\d+$/.test(stamp) ||
|
|
78
|
+
!/^[1-9]\d*$/.test(pid) || !message)
|
|
79
|
+
continue;
|
|
80
|
+
try {
|
|
81
|
+
process.kill(Number(pid), 0);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
const code = error.code;
|
|
85
|
+
if (code === "ESRCH") {
|
|
86
|
+
clearIfSame(pane, stamp, mark);
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (code !== "EPERM")
|
|
90
|
+
throw error;
|
|
91
|
+
}
|
|
92
|
+
if (!found.has(pane) || session === preferredSession) {
|
|
93
|
+
found.set(pane, { pane, mark, stamp, session, target: session + ":" + windowId + "." + pane,
|
|
94
|
+
address: sessionName + ":" + win + "." + index, window, message: JSON.parse(message) });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return [...found.values()].sort((a, b) => Number(b.mark === "?") - Number(a.mark === "?") ||
|
|
98
|
+
Number(a.stamp) - Number(b.stamp) || Number(a.pane.slice(1)) - Number(b.pane.slice(1)));
|
|
99
|
+
}
|
|
100
|
+
function focus(client, chosen) {
|
|
101
|
+
const current = entries(chosen.session).find(row => row.pane === chosen.pane && row.mark === chosen.mark && row.stamp === chosen.stamp);
|
|
102
|
+
if (!current) {
|
|
103
|
+
tm("display-message", "-c", client, "That pane no longer needs attention");
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
tm("switch-client", "-c", client, "-t", current.target);
|
|
107
|
+
if (current.mark === "✓")
|
|
108
|
+
clearIfSame(current.pane, current.stamp, current.mark);
|
|
109
|
+
}
|
|
110
|
+
export function click(client, range) {
|
|
111
|
+
const match = /^pi(%\d+)(\$\d+)$/.exec(range);
|
|
112
|
+
if (!match)
|
|
113
|
+
return;
|
|
114
|
+
const [, pane, session] = match;
|
|
115
|
+
const chosen = entries(session).find(row => row.pane === pane && row.session === session);
|
|
116
|
+
if (chosen)
|
|
117
|
+
focus(client, chosen);
|
|
118
|
+
}
|
|
119
|
+
export function jump(client) {
|
|
120
|
+
const target = clientTarget(client);
|
|
121
|
+
if (!target)
|
|
122
|
+
return;
|
|
123
|
+
const rows = entries(target.split(":")[0]);
|
|
124
|
+
const directory = mkdtempSync(join(tmpdir(), "pi-attention-"));
|
|
125
|
+
try {
|
|
126
|
+
const choices = join(directory, "choices");
|
|
127
|
+
const answer = join(directory, "answer");
|
|
128
|
+
writeFileSync(choices, rows.map((r, i) => i + "\t" + r.mark + " " + r.address + " " + r.window + " " + r.message + "\n").join(""));
|
|
129
|
+
const picker = ["env", "FZF_DEFAULT_OPTS=", "FZF_DEFAULT_OPTS_FILE=/dev/null", "fzf",
|
|
130
|
+
"--layout=reverse", "--no-sort", "--delimiter=\t", "--with-nth=2..", "--prompt=Agent pane > ", "--no-multi",
|
|
131
|
+
...(rows.length ? ["--bind=left-click:accept"] : ["--header=Nothing pending", "--bind=enter:ignore"])];
|
|
132
|
+
const command = picker.map(shellQuote).join(" ") + " < " + shellQuote(choices) + " > " + shellQuote(answer);
|
|
133
|
+
const result = spawnSync("tmux", ["display-popup", "-E", "-c", client, "-t", target, "-w", "85%",
|
|
134
|
+
"-h", "65%", "-T", " Agent panes ", command], { stdio: "inherit" });
|
|
135
|
+
if (result.error)
|
|
136
|
+
throw result.error;
|
|
137
|
+
if (result.status === 0 && existsSync(answer)) {
|
|
138
|
+
const index = readFileSync(answer, "utf8").split("\t")[0];
|
|
139
|
+
// tmux restores the original active pane on popup close. Focus afterwards.
|
|
140
|
+
if (/^\d+$/.test(index) && rows[Number(index)])
|
|
141
|
+
focus(client, rows[Number(index)]);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
finally {
|
|
145
|
+
rmSync(directory, { recursive: true, force: true });
|
|
146
|
+
}
|
|
147
|
+
}
|
package/dist/tmux.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
export const cliPath = fileURLToPath(new URL("./cli.js", import.meta.url));
|
|
4
|
+
export const markOption = "@pi_attention_mark";
|
|
5
|
+
export const sessionOption = "@pi_attention_codex_session";
|
|
6
|
+
export const turnOption = "@pi_attention_codex_turn";
|
|
7
|
+
export const waitOption = "@pi_attention_codex_wait";
|
|
8
|
+
export function tm(...args) {
|
|
9
|
+
return execFileSync("tmux", args, { encoding: "utf8" }).replace(/\n+$/, "");
|
|
10
|
+
}
|
|
11
|
+
export function shellQuote(value) {
|
|
12
|
+
if (/^[A-Za-z0-9_@%+=:,./-]+$/.test(value))
|
|
13
|
+
return value;
|
|
14
|
+
return "'" + value.replaceAll("'", "'\"'\"'") + "'";
|
|
15
|
+
}
|
|
16
|
+
export function paneFromEnv() {
|
|
17
|
+
const pane = process.env.TMUX_PANE;
|
|
18
|
+
if (process.env.TMUX && pane && /^%\d+$/.test(pane))
|
|
19
|
+
return pane;
|
|
20
|
+
}
|
|
21
|
+
// Evaluate the ownership condition and apply all changes in the same tmux command.
|
|
22
|
+
export function updatePane(pane, options, condition = "1") {
|
|
23
|
+
const commands = Object.entries(options).map(([name, value]) => {
|
|
24
|
+
const args = value === null
|
|
25
|
+
? ["set-option", "-pu", "-t", pane, name]
|
|
26
|
+
: ["set-option", "-p", "-t", pane, name, value];
|
|
27
|
+
return args.map(shellQuote).join(" ");
|
|
28
|
+
});
|
|
29
|
+
commands.push("display-message -p applied");
|
|
30
|
+
return tm("if-shell", "-F", "-t", pane, condition, commands.join(" ; ")) === "applied";
|
|
31
|
+
}
|
|
32
|
+
export function marker(symbol, message) {
|
|
33
|
+
return {
|
|
34
|
+
"@pi_attention_time": String(Date.now()),
|
|
35
|
+
"@pi_attention_text": JSON.stringify(message),
|
|
36
|
+
[markOption]: symbol,
|
|
37
|
+
};
|
|
38
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ssh352/tmux-attention",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Find Pi and Codex panes waiting for attention in tmux.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"pi-extension",
|
|
9
|
+
"tmux",
|
|
10
|
+
"codex"
|
|
11
|
+
],
|
|
12
|
+
"files": [
|
|
13
|
+
"dist"
|
|
14
|
+
],
|
|
15
|
+
"pi": {
|
|
16
|
+
"extensions": [
|
|
17
|
+
"./dist/index.js"
|
|
18
|
+
]
|
|
19
|
+
},
|
|
20
|
+
"bin": {
|
|
21
|
+
"tmux-attention": "./dist/cli.js"
|
|
22
|
+
},
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc -p tsconfig.json",
|
|
25
|
+
"test": "npm run build && node --test test/*.test.mjs",
|
|
26
|
+
"prepack": "npm run build"
|
|
27
|
+
},
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=22.19.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"@earendil-works/pi-coding-agent": "*"
|
|
33
|
+
},
|
|
34
|
+
"peerDependenciesMeta": {
|
|
35
|
+
"@earendil-works/pi-coding-agent": {
|
|
36
|
+
"optional": true
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@earendil-works/pi-coding-agent": "1.6.0",
|
|
41
|
+
"@types/node": "^22.19.0",
|
|
42
|
+
"typescript": "~5.9.3"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public",
|
|
46
|
+
"registry": "https://registry.npmjs.org/"
|
|
47
|
+
}
|
|
48
|
+
}
|