@foxfirecodes/pi-alerts 0.1.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/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +36 -0
- package/extensions/pi-alerts.js +80 -0
- package/package.json +27 -0
- package/test/pi-alerts.test.js +134 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 foxfirecodes
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# pi-alerts
|
|
2
|
+
|
|
3
|
+
Pi package that rings the terminal bell (`\a`) when pi needs your input.
|
|
4
|
+
|
|
5
|
+
## Behavior
|
|
6
|
+
|
|
7
|
+
- Rings when an agent turn ends and pi is ready for another prompt.
|
|
8
|
+
- Rings before extension UI prompts such as `select`, `confirm`, `input`, `editor`, and `custom` dialogs. This covers permission prompts built with `ctx.ui.confirm()`.
|
|
9
|
+
|
|
10
|
+
The bell is written directly to the attached terminal when one is available.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pi install npm:@foxfirecodes/pi-alerts
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Development
|
|
19
|
+
|
|
20
|
+
From this checkout:
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
pi install .
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Or try it for one run:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pi -e /path/to/pi-extensions/packages/pi-alerts
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
To run automated tests:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
pnpm test
|
|
36
|
+
```
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const BELL = "\x07";
|
|
2
|
+
const UI_BELL_INSTALLED = Symbol.for("@foxfirecodes/pi-alerts.uiBellInstalled");
|
|
3
|
+
const PROMPT_METHODS = ["select", "confirm", "input", "editor", "custom"];
|
|
4
|
+
|
|
5
|
+
function defaultBellOutput() {
|
|
6
|
+
if (process.stdout?.isTTY) return process.stdout;
|
|
7
|
+
if (process.stderr?.isTTY) return process.stderr;
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function ringTerminalBell(output = defaultBellOutput()) {
|
|
12
|
+
if (!output) return false;
|
|
13
|
+
if ("isTTY" in output && output.isTTY === false) return false;
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
output.write(BELL);
|
|
17
|
+
return true;
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function promptOptions(methodName, args) {
|
|
24
|
+
if (
|
|
25
|
+
methodName === "select" ||
|
|
26
|
+
methodName === "confirm" ||
|
|
27
|
+
methodName === "input"
|
|
28
|
+
) {
|
|
29
|
+
return args[2];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function promptAlreadyAborted(methodName, args) {
|
|
36
|
+
return Boolean(promptOptions(methodName, args)?.signal?.aborted);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function installPromptBells(ui, ring = ringTerminalBell) {
|
|
40
|
+
if (!ui || ui[UI_BELL_INSTALLED]) return false;
|
|
41
|
+
|
|
42
|
+
let installed = false;
|
|
43
|
+
|
|
44
|
+
for (const methodName of PROMPT_METHODS) {
|
|
45
|
+
const original = ui[methodName];
|
|
46
|
+
if (typeof original !== "function") continue;
|
|
47
|
+
|
|
48
|
+
ui[methodName] = function promptBellWrapper(...args) {
|
|
49
|
+
if (!promptAlreadyAborted(methodName, args)) {
|
|
50
|
+
ring();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return original.apply(this, args);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
installed = true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (installed) {
|
|
60
|
+
Object.defineProperty(ui, UI_BELL_INSTALLED, {
|
|
61
|
+
value: true,
|
|
62
|
+
enumerable: false,
|
|
63
|
+
configurable: true,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return installed;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export default function piAlerts(pi) {
|
|
71
|
+
pi.on("session_start", async (_event, ctx) => {
|
|
72
|
+
if (ctx.hasUI) installPromptBells(ctx.ui);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
pi.on("agent_end", async (_event, ctx) => {
|
|
76
|
+
if (!ctx.hasUI || ctx.hasPendingMessages()) return;
|
|
77
|
+
|
|
78
|
+
ringTerminalBell();
|
|
79
|
+
});
|
|
80
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@foxfirecodes/pi-alerts",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Pi extension that rings the terminal bell when pi needs your input.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/foxfirecodes/pi-extensions.git",
|
|
13
|
+
"directory": "packages/pi-alerts"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/foxfirecodes/pi-extensions/issues"
|
|
17
|
+
},
|
|
18
|
+
"homepage": "https://github.com/foxfirecodes/pi-extensions/tree/main/packages/pi-alerts#readme",
|
|
19
|
+
"pi": {
|
|
20
|
+
"extensions": [
|
|
21
|
+
"./extensions/pi-alerts.js"
|
|
22
|
+
]
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "node --test test/*.test.js"
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import test from "node:test";
|
|
3
|
+
|
|
4
|
+
import piAlerts, {
|
|
5
|
+
installPromptBells,
|
|
6
|
+
ringTerminalBell,
|
|
7
|
+
} from "../extensions/pi-alerts.js";
|
|
8
|
+
|
|
9
|
+
function createOutput({ isTTY = true } = {}) {
|
|
10
|
+
return {
|
|
11
|
+
isTTY,
|
|
12
|
+
chunks: [],
|
|
13
|
+
write(chunk) {
|
|
14
|
+
this.chunks.push(chunk);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
test("exports a pi extension", () => {
|
|
20
|
+
assert.equal(typeof piAlerts, "function");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("registers session and agent-end handlers", () => {
|
|
24
|
+
const handlers = new Map();
|
|
25
|
+
const pi = {
|
|
26
|
+
on(eventName, handler) {
|
|
27
|
+
handlers.set(eventName, handler);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
piAlerts(pi);
|
|
32
|
+
|
|
33
|
+
assert.equal(typeof handlers.get("session_start"), "function");
|
|
34
|
+
assert.equal(typeof handlers.get("agent_end"), "function");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("ringTerminalBell writes a bell to TTY output", () => {
|
|
38
|
+
const output = createOutput();
|
|
39
|
+
|
|
40
|
+
assert.equal(ringTerminalBell(output), true);
|
|
41
|
+
assert.deepEqual(output.chunks, ["\x07"]);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
test("ringTerminalBell skips non-TTY output", () => {
|
|
45
|
+
const output = createOutput({ isTTY: false });
|
|
46
|
+
|
|
47
|
+
assert.equal(ringTerminalBell(output), false);
|
|
48
|
+
assert.deepEqual(output.chunks, []);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("installPromptBells rings before extension UI prompts", async () => {
|
|
52
|
+
const calls = [];
|
|
53
|
+
const ui = {
|
|
54
|
+
async select(title) {
|
|
55
|
+
calls.push(["select", title]);
|
|
56
|
+
return "selected";
|
|
57
|
+
},
|
|
58
|
+
async confirm(title, message) {
|
|
59
|
+
calls.push(["confirm", title, message]);
|
|
60
|
+
return true;
|
|
61
|
+
},
|
|
62
|
+
async input(title, placeholder) {
|
|
63
|
+
calls.push(["input", title, placeholder]);
|
|
64
|
+
return "typed";
|
|
65
|
+
},
|
|
66
|
+
async editor(title, prefill) {
|
|
67
|
+
calls.push(["editor", title, prefill]);
|
|
68
|
+
return "edited";
|
|
69
|
+
},
|
|
70
|
+
async custom() {
|
|
71
|
+
calls.push(["custom"]);
|
|
72
|
+
return "custom-result";
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
let bells = 0;
|
|
76
|
+
|
|
77
|
+
assert.equal(
|
|
78
|
+
installPromptBells(ui, () => bells++),
|
|
79
|
+
true,
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
assert.equal(await ui.select("Choose"), "selected");
|
|
83
|
+
assert.equal(await ui.confirm("Allow?", "Run it?"), true);
|
|
84
|
+
assert.equal(await ui.input("Name", "placeholder"), "typed");
|
|
85
|
+
assert.equal(await ui.editor("Edit", "prefill"), "edited");
|
|
86
|
+
assert.equal(await ui.custom(), "custom-result");
|
|
87
|
+
|
|
88
|
+
assert.equal(bells, 5);
|
|
89
|
+
assert.deepEqual(calls, [
|
|
90
|
+
["select", "Choose"],
|
|
91
|
+
["confirm", "Allow?", "Run it?"],
|
|
92
|
+
["input", "Name", "placeholder"],
|
|
93
|
+
["editor", "Edit", "prefill"],
|
|
94
|
+
["custom"],
|
|
95
|
+
]);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("installPromptBells is idempotent for the same UI context", async () => {
|
|
99
|
+
const ui = {
|
|
100
|
+
async confirm() {
|
|
101
|
+
return true;
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
let bells = 0;
|
|
105
|
+
|
|
106
|
+
assert.equal(
|
|
107
|
+
installPromptBells(ui, () => bells++),
|
|
108
|
+
true,
|
|
109
|
+
);
|
|
110
|
+
assert.equal(
|
|
111
|
+
installPromptBells(ui, () => bells++),
|
|
112
|
+
false,
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
await ui.confirm("Allow?", "Run it?");
|
|
116
|
+
|
|
117
|
+
assert.equal(bells, 1);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("installPromptBells skips prompts whose signal is already aborted", async () => {
|
|
121
|
+
const ui = {
|
|
122
|
+
async select() {
|
|
123
|
+
return undefined;
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
const controller = new AbortController();
|
|
127
|
+
controller.abort();
|
|
128
|
+
let bells = 0;
|
|
129
|
+
|
|
130
|
+
installPromptBells(ui, () => bells++);
|
|
131
|
+
await ui.select("Choose", ["A"], { signal: controller.signal });
|
|
132
|
+
|
|
133
|
+
assert.equal(bells, 0);
|
|
134
|
+
});
|