mirror of
https://github.com/affaan-m/everything-claude-code.git
synced 2026-03-30 21:53:28 +08:00
Compare commits
9 Commits
fix/codex-
...
fix/deskto
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2595c92983 | ||
|
|
99ff568c0e | ||
|
|
3d5ae70c74 | ||
|
|
4ea72dec99 | ||
|
|
02206e87a5 | ||
|
|
76c13ac5fb | ||
|
|
2c1ae27a3a | ||
|
|
d9d16a0d4a | ||
|
|
a57c3c5dd5 |
@@ -310,7 +310,7 @@
|
||||
"timeout": 10
|
||||
}
|
||||
],
|
||||
"description": "Send macOS desktop notification with task summary when Claude responds"
|
||||
"description": "Send desktop notification (macOS/WSL) with task summary when Claude responds"
|
||||
}
|
||||
],
|
||||
"SessionEnd": [
|
||||
|
||||
@@ -3,9 +3,11 @@
|
||||
* Desktop Notification Hook (Stop)
|
||||
*
|
||||
* Sends a native desktop notification with the task summary when Claude
|
||||
* finishes responding. Currently supports macOS (osascript); other
|
||||
* platforms exit silently. Windows (PowerShell) and Linux (notify-send)
|
||||
* support is planned.
|
||||
* finishes responding. Supports:
|
||||
* - macOS: osascript (native)
|
||||
* - WSL: PowerShell 7 or Windows PowerShell + BurntToast module
|
||||
*
|
||||
* On WSL, if BurntToast is not installed, logs a tip for installation.
|
||||
*
|
||||
* Hook ID : stop:desktop-notify
|
||||
* Profiles: standard, strict
|
||||
@@ -19,6 +21,64 @@ const { isMacOS, log } = require('../lib/utils');
|
||||
const TITLE = 'Claude Code';
|
||||
const MAX_BODY_LENGTH = 100;
|
||||
|
||||
/**
|
||||
* Memoized WSL detection at module load (avoids repeated /proc/version reads).
|
||||
*/
|
||||
let isWSL = false;
|
||||
if (process.platform === 'linux') {
|
||||
try {
|
||||
isWSL = require('fs').readFileSync('/proc/version', 'utf8').toLowerCase().includes('microsoft');
|
||||
} catch {
|
||||
isWSL = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Find available PowerShell executable on WSL.
|
||||
* Returns first accessible path, or null if none found.
|
||||
*/
|
||||
function findPowerShell() {
|
||||
if (!isWSL) return null;
|
||||
|
||||
const candidates = [
|
||||
'pwsh.exe', // WSL interop resolves from Windows PATH
|
||||
'powershell.exe', // WSL interop for Windows PowerShell
|
||||
'/mnt/c/Program Files/PowerShell/7/pwsh.exe', // PowerShell 7 (default install)
|
||||
'/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe', // Windows PowerShell
|
||||
];
|
||||
|
||||
for (const path of candidates) {
|
||||
try {
|
||||
const result = spawnSync(path, ['-Command', 'exit 0'],
|
||||
{ stdio: ['ignore', 'pipe', 'ignore'], timeout: 1000 });
|
||||
if (result.status === 0) {
|
||||
return path;
|
||||
}
|
||||
} catch {
|
||||
// continue
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Windows Toast notification via PowerShell BurntToast.
|
||||
* Returns true on success, false on failure.
|
||||
*/
|
||||
function notifyWindows(pwshPath, title, body) {
|
||||
const safeBody = body.replace(/'/g, "''");
|
||||
const safeTitle = title.replace(/'/g, "''");
|
||||
const command = `Import-Module BurntToast; New-BurntToastNotification -Text '${safeTitle}', '${safeBody}'`;
|
||||
const result = spawnSync(pwshPath, ['-Command', command],
|
||||
{ stdio: ['ignore', 'pipe', 'pipe'], timeout: 5000 });
|
||||
if (result.error || result.status !== 0) {
|
||||
const stderr = typeof result.stderr?.toString === 'function' ? result.stderr.toString().trim() : '';
|
||||
log(`[DesktopNotify] BurntToast failed (exit ${result.status}): ${result.error ? result.error.message : stderr}`);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract a short summary from the last assistant message.
|
||||
* Takes the first non-empty line and truncates to MAX_BODY_LENGTH chars.
|
||||
@@ -53,20 +113,28 @@ function notifyMacOS(title, body) {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: future platform support
|
||||
// function notifyWindows(title, body) { ... }
|
||||
// function notifyLinux(title, body) { ... }
|
||||
|
||||
/**
|
||||
* Fast-path entry point for run-with-flags.js (avoids extra process spawn).
|
||||
*/
|
||||
function run(raw) {
|
||||
try {
|
||||
if (!isMacOS) return raw;
|
||||
|
||||
const input = raw.trim() ? JSON.parse(raw) : {};
|
||||
const summary = extractSummary(input.last_assistant_message);
|
||||
notifyMacOS(TITLE, summary);
|
||||
|
||||
if (isMacOS) {
|
||||
notifyMacOS(TITLE, summary);
|
||||
} else if (isWSL) {
|
||||
const ps = findPowerShell();
|
||||
if (ps && notifyWindows(ps, TITLE, summary)) {
|
||||
// notification sent successfully
|
||||
} else if (ps) {
|
||||
// PowerShell found but BurntToast not available
|
||||
log('[DesktopNotify] Tip: Install BurntToast module to enable notifications');
|
||||
} else {
|
||||
// No PowerShell found
|
||||
log('[DesktopNotify] Tip: Install BurntToast module in PowerShell for notifications');
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
log(`[DesktopNotify] Error: ${err.message}`);
|
||||
}
|
||||
|
||||
118
tests/hooks/desktop-notify.test.js
Normal file
118
tests/hooks/desktop-notify.test.js
Normal file
@@ -0,0 +1,118 @@
|
||||
/**
|
||||
* Tests for scripts/hooks/desktop-notify.js
|
||||
*/
|
||||
|
||||
const assert = require('assert');
|
||||
const fs = require('fs');
|
||||
const Module = require('module');
|
||||
const path = require('path');
|
||||
|
||||
const modulePath = path.join(__dirname, '..', '..', 'scripts', 'hooks', 'desktop-notify.js');
|
||||
const moduleSource = fs.readFileSync(modulePath, 'utf8');
|
||||
|
||||
function test(name, fn) {
|
||||
try {
|
||||
fn();
|
||||
console.log(` ✓ ${name}`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.log(` ✗ ${name}`);
|
||||
console.log(` Error: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function loadDesktopNotify({ procVersion = 'Linux version microsoft', spawnImpl, isMacOS = false }) {
|
||||
const logs = [];
|
||||
const mod = new Module(modulePath, module);
|
||||
mod.filename = modulePath;
|
||||
mod.paths = Module._nodeModulePaths(path.dirname(modulePath));
|
||||
|
||||
const originalRequire = mod.require.bind(mod);
|
||||
mod.require = request => {
|
||||
if (request === 'child_process') {
|
||||
return { spawnSync: spawnImpl };
|
||||
}
|
||||
if (request === '../lib/utils') {
|
||||
return {
|
||||
isMacOS,
|
||||
log: message => logs.push(message),
|
||||
};
|
||||
}
|
||||
if (request === 'fs') {
|
||||
return {
|
||||
...fs,
|
||||
readFileSync(target, encoding) {
|
||||
if (target === '/proc/version') {
|
||||
return procVersion;
|
||||
}
|
||||
return fs.readFileSync(target, encoding);
|
||||
}
|
||||
};
|
||||
}
|
||||
return originalRequire(request);
|
||||
};
|
||||
|
||||
const platformDescriptor = Object.getOwnPropertyDescriptor(process, 'platform');
|
||||
Object.defineProperty(process, 'platform', {
|
||||
configurable: true,
|
||||
value: 'linux',
|
||||
});
|
||||
|
||||
try {
|
||||
mod._compile(moduleSource, modulePath);
|
||||
} finally {
|
||||
Object.defineProperty(process, 'platform', platformDescriptor);
|
||||
}
|
||||
|
||||
return { run: mod.exports.run, logs };
|
||||
}
|
||||
|
||||
let passed = 0;
|
||||
let failed = 0;
|
||||
|
||||
if (
|
||||
test('successful WSL toast does not log BurntToast install guidance', () => {
|
||||
const calls = [];
|
||||
const { run, logs } = loadDesktopNotify({
|
||||
spawnImpl(command, args) {
|
||||
calls.push({ command, args });
|
||||
if (calls.length === 1) {
|
||||
return { status: 0, stderr: Buffer.from('') };
|
||||
}
|
||||
return { status: 0, stderr: Buffer.from('') };
|
||||
}
|
||||
});
|
||||
|
||||
const payload = JSON.stringify({ last_assistant_message: 'Build completed successfully' });
|
||||
assert.strictEqual(run(payload), payload);
|
||||
assert.strictEqual(calls.length, 2, 'Expected PowerShell probe and notification send');
|
||||
assert.strictEqual(logs.length, 0, `Expected no warnings, got: ${logs.join('\n')}`);
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
if (
|
||||
test('failed WSL toast logs failure and install guidance once', () => {
|
||||
const { run, logs } = loadDesktopNotify({
|
||||
spawnImpl(command, args) {
|
||||
if (args[1] === 'exit 0') {
|
||||
return { status: 0, stderr: Buffer.from('') };
|
||||
}
|
||||
return { status: 1, stderr: Buffer.from('module missing') };
|
||||
}
|
||||
});
|
||||
|
||||
const payload = JSON.stringify({ last_assistant_message: 'Done' });
|
||||
assert.strictEqual(run(payload), payload);
|
||||
assert.ok(logs.some(message => message.includes('BurntToast failed')), 'Expected BurntToast failure log');
|
||||
assert.ok(logs.some(message => message.includes('Install BurntToast module')), 'Expected install tip');
|
||||
})
|
||||
)
|
||||
passed++;
|
||||
else failed++;
|
||||
|
||||
console.log(`\nPassed: ${passed}`);
|
||||
console.log(`Failed: ${failed}`);
|
||||
process.exit(failed > 0 ? 1 : 0);
|
||||
Reference in New Issue
Block a user