/** * Pi Notify Extension * * Sends a native terminal notification when Pi agent is done and waiting for input. * Supports multiple terminal protocols: * - OSC 777: Ghostty, WezTerm, rxvt-unicode, Windows Terminal 1.26+ * - OSC 9: iTerm2 * - OSC 99: Kitty * - tmux passthrough wrapper for OSC notifications * - Optional sound hook via PI_NOTIFY_SOUND_CMD */ import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; function wrapForTmux(sequence: string): string { if (!process.env.TMUX) return sequence; // tmux passthrough: wrap in DCS and escape inner ESC bytes. const escaped = sequence.split("\x1b").join("\x1b\x1b"); return `\x1bPtmux;${escaped}\x1b\\`; } function notifyOSC777(title: string, body: string): void { const sequence = `\x1b]777;notify;${title};${body}\x07`; process.stdout.write(wrapForTmux(sequence)); } function notifyOSC9(message: string): void { const sequence = `\x1b]9;${message}\x07`; process.stdout.write(wrapForTmux(sequence)); } function notifyOSC99(title: string, body: string): void { // Kitty OSC 99: i=notification id, d=0 means not done yet, p=body for second part const titleSequence = `\x1b]99;i=1:d=0;${title}\x1b\\`; const bodySequence = `\x1b]99;i=1:p=body;${body}\x1b\\`; process.stdout.write(wrapForTmux(titleSequence)); process.stdout.write(wrapForTmux(bodySequence)); } function runSoundHook(): void { const command = process.env.PI_NOTIFY_SOUND_CMD?.trim(); if (!command) return; try { const { spawn } = require("node:child_process"); const child = spawn(command, { shell: true, detached: true, stdio: "ignore", }); child.unref(); } catch { // Ignore hook errors to avoid breaking notifications } } function notify(title: string, body: string): void { const isIterm2 = process.env.TERM_PROGRAM === "iTerm.app" || Boolean(process.env.ITERM_SESSION_ID); if (process.env.WT_SESSION) { notifyOSC777(title, body); } else if (process.env.KITTY_WINDOW_ID) { notifyOSC99(title, body); } else if (isIterm2) { notifyOSC9(`${title}: ${body}`); } else { notifyOSC777(title, body); } runSoundHook(); } export default function (pi: ExtensionAPI) { pi.on("agent_end", async () => { notify("Pi", "Ready for input"); }); }