pi-arsenal 0.4.0 → 0.4.1
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/extensions/pi-notify.ts +45 -33
- package/package.json +1 -1
package/extensions/pi-notify.ts
CHANGED
|
@@ -2,47 +2,59 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
3
3
|
import { Type } from "typebox";
|
|
4
4
|
|
|
5
|
-
// ───
|
|
6
|
-
// Uses Windows.UI.Notifications WinRT API (available on Windows 10/11).
|
|
7
|
-
// Falls back to a simple `msg %USERNAME%` if WinRT fails.
|
|
5
|
+
// ─── Platform dispatch ────────────────────────────────────────────────────────
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
function sendNotification(title: string, body: string, urgency: "info" | "warning" | "error"): Promise<void> {
|
|
8
|
+
switch (process.platform) {
|
|
9
|
+
case "win32": return sendWindows(title, body, urgency);
|
|
10
|
+
case "darwin": return sendMac(title, body);
|
|
11
|
+
default: return sendLinux(title, body, urgency);
|
|
12
|
+
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
// ─── Windows — NotifyIcon balloon (System.Windows.Forms) ─────────────────────
|
|
16
|
+
// WinRT ToastNotificationManager requires a registered AppUserModelID;
|
|
17
|
+
// NotifyIcon works without registration and is available on all Windows versions.
|
|
18
|
+
|
|
19
|
+
function sendWindows(title: string, body: string, _urgency: "info" | "warning" | "error"): Promise<void> {
|
|
19
20
|
const ps = `
|
|
20
|
-
|
|
21
|
-
$
|
|
22
|
-
$
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
$
|
|
27
|
-
$notifier.Show($toast);
|
|
21
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
22
|
+
$n = New-Object System.Windows.Forms.NotifyIcon
|
|
23
|
+
$n.Icon = [System.Drawing.SystemIcons]::Information
|
|
24
|
+
$n.Visible = $true
|
|
25
|
+
$n.ShowBalloonTip(6000, '${esc(title)}', '${esc(body)}', [System.Windows.Forms.ToolTipIcon]::Info)
|
|
26
|
+
Start-Sleep -Seconds 4
|
|
27
|
+
$n.Dispose()
|
|
28
28
|
`;
|
|
29
|
-
return
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
29
|
+
return run("powershell.exe", ["-NoProfile", "-NonInteractive", "-Command", ps]);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ─── macOS — osascript (zero dependencies) ───────────────────────────────────
|
|
33
|
+
|
|
34
|
+
function sendMac(title: string, body: string): Promise<void> {
|
|
35
|
+
const script = `display notification "${esc(body)}" with title "${esc(title)}"`;
|
|
36
|
+
return run("osascript", ["-e", script]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// ─── Linux — notify-send ─────────────────────────────────────────────────────
|
|
40
|
+
|
|
41
|
+
function sendLinux(title: string, body: string, urgency: "info" | "warning" | "error"): Promise<void> {
|
|
42
|
+
const level = urgency === "error" ? "critical" : urgency === "warning" ? "normal" : "low";
|
|
43
|
+
return run("notify-send", ["-u", level, "-t", "6000", title, body]);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// ─── Helpers ─────────────────────────────────────────────────────────────────
|
|
47
|
+
|
|
48
|
+
/** Escape single quotes for embedding in PS / AppleScript strings. */
|
|
49
|
+
function esc(s: string): string {
|
|
50
|
+
return s.replace(/'/g, "\\'").replace(/"/g, '\\"');
|
|
37
51
|
}
|
|
38
52
|
|
|
39
|
-
function
|
|
40
|
-
// msg.exe: works on all Windows versions, shows a dialog to the current user session.
|
|
53
|
+
function run(cmd: string, args: string[]): Promise<void> {
|
|
41
54
|
return new Promise((resolve) => {
|
|
42
|
-
const
|
|
43
|
-
const proc = spawn("msg.exe", ["%USERNAME%", text], { stdio: "ignore" });
|
|
55
|
+
const proc = spawn(cmd, args, { stdio: "ignore" });
|
|
44
56
|
proc.on("close", () => resolve());
|
|
45
|
-
proc.on("error", () => resolve());
|
|
57
|
+
proc.on("error", () => resolve()); // best-effort — never throw
|
|
46
58
|
});
|
|
47
59
|
}
|
|
48
60
|
|
|
@@ -67,7 +79,7 @@ export default function piNotify(pi: ExtensionAPI) {
|
|
|
67
79
|
}),
|
|
68
80
|
async execute(_id, input, _signal, _onUpdate, _ctx) {
|
|
69
81
|
const { title, body, urgency } = input as { title: string; body: string; urgency: "info" | "warning" | "error" };
|
|
70
|
-
await
|
|
82
|
+
await sendNotification(title.slice(0, 128), body.slice(0, 256), urgency);
|
|
71
83
|
return { content: [{ type: "text" as const, text: `Notification sent: "${title}"` }] };
|
|
72
84
|
},
|
|
73
85
|
});
|