claude-hook-notify 1.2.2 → 1.4.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 +1 -1
- package/package.json +2 -1
- package/src/activate-watcher.js +82 -0
- package/src/activate.js +243 -0
- package/src/cli.js +3 -0
- package/src/index.js +2 -1
- package/src/notify.js +56 -24
- package/vendor/notify-helper/ClaudeCodeNotify.exe +0 -0
- package/vendor/notify-helper/NotifyHelper.cs +111 -0
package/README.md
CHANGED
|
@@ -89,7 +89,7 @@ npx claude-hook-notify setup --events Stop,TaskCompleted,Notification,PostToolUs
|
|
|
89
89
|
| macOS | `osascript` | 无(系统自带) |
|
|
90
90
|
| macOS | `terminal-notifier` | 可选: `brew install terminal-notifier`(更好) |
|
|
91
91
|
| Linux | `notify-send` | `sudo apt-get install libnotify-bin` |
|
|
92
|
-
| Windows |
|
|
92
|
+
| Windows | 桌面通知 (内置工具) | 无(Windows 10+ 系统自带,点击通知跳转终端) |
|
|
93
93
|
|
|
94
94
|
## 原理
|
|
95
95
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "claude-hook-notify",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"description": "🔔 Claude Code 任务完成桌面通知 — 一键安装,跨平台支持",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
},
|
|
26
26
|
"files": [
|
|
27
27
|
"src/",
|
|
28
|
+
"vendor/",
|
|
28
29
|
"README.md"
|
|
29
30
|
]
|
|
30
31
|
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 后台点击监听脚本
|
|
5
|
+
*
|
|
6
|
+
* 由主进程 spawn 为 detached 后台进程。
|
|
7
|
+
* 执行通知命令(带等待标志),检测用户点击后激活终端窗口。
|
|
8
|
+
*
|
|
9
|
+
* 参数格式:
|
|
10
|
+
* node activate-watcher.js <JSON配置>
|
|
11
|
+
*
|
|
12
|
+
* JSON 配置:
|
|
13
|
+
* {
|
|
14
|
+
* "platform": "win32" | "linux",
|
|
15
|
+
* "notifyCommand": "...",
|
|
16
|
+
* "notifyArgs": ["..."],
|
|
17
|
+
* "activateCommand": "...",
|
|
18
|
+
* "activateArgs": ["..."],
|
|
19
|
+
* "activateFallbackCommand": "...", // 可选
|
|
20
|
+
* "activateFallbackArgs": ["..."], // 可选
|
|
21
|
+
* "timeout": 120000
|
|
22
|
+
* }
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
const { spawn } = require("child_process");
|
|
26
|
+
|
|
27
|
+
// 120 秒超时自动退出,防僵尸进程
|
|
28
|
+
const config = parseConfig();
|
|
29
|
+
const selfTimeout = setTimeout(() => process.exit(0), config.timeout || 120000);
|
|
30
|
+
selfTimeout.unref();
|
|
31
|
+
|
|
32
|
+
function parseConfig() {
|
|
33
|
+
try {
|
|
34
|
+
return JSON.parse(process.argv[2] || "{}");
|
|
35
|
+
} catch {
|
|
36
|
+
process.exit(1);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function activateTerminal() {
|
|
41
|
+
const { activateCommand, activateArgs, activateFallbackCommand, activateFallbackArgs } = config;
|
|
42
|
+
if (!activateCommand) return;
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
execFileSync(activateCommand, activateArgs || [], { stdio: "ignore", timeout: 5000 });
|
|
46
|
+
} catch {
|
|
47
|
+
// 主命令失败,尝试回退
|
|
48
|
+
if (activateFallbackCommand) {
|
|
49
|
+
try {
|
|
50
|
+
execFileSync(activateFallbackCommand, activateFallbackArgs || [], { stdio: "ignore", timeout: 5000 });
|
|
51
|
+
} catch {
|
|
52
|
+
// 回退也失败,静默退出
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function main() {
|
|
59
|
+
const { platform, notifyCommand, notifyArgs } = config;
|
|
60
|
+
if (!notifyCommand) process.exit(1);
|
|
61
|
+
|
|
62
|
+
if (platform === "linux") {
|
|
63
|
+
// notify-send --action 模式:stdout 输出 action 名
|
|
64
|
+
const child = spawn(notifyCommand, notifyArgs, {
|
|
65
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
66
|
+
timeout: 120000,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
let output = "";
|
|
70
|
+
child.stdout.on("data", (chunk) => {
|
|
71
|
+
output += chunk.toString();
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
child.on("close", () => {
|
|
75
|
+
if (output.trim() === "default") {
|
|
76
|
+
activateTerminal();
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
main().catch(() => process.exit(1));
|
package/src/activate.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
const { execSync, execFileSync } = require("child_process");
|
|
2
|
+
const os = require("os");
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 终端进程名到 macOS bundleId 的映射
|
|
6
|
+
*/
|
|
7
|
+
const MAC_BUNDLE_MAP = {
|
|
8
|
+
"Terminal": "com.apple.Terminal",
|
|
9
|
+
"iTerm2": "com.googlecode.iterm2",
|
|
10
|
+
"iTerm2-v3": "com.googlecode.iterm2",
|
|
11
|
+
"Electron": null, // 需进一步判断
|
|
12
|
+
"Code": "com.microsoft.VSCode",
|
|
13
|
+
"Code - Insiders": "com.microsoft.VSCodeInsiders",
|
|
14
|
+
"Cursor": "com.todesktop.230313mzl4w4u92",
|
|
15
|
+
"WarpTerminal": "dev.warp.Warp-Stable",
|
|
16
|
+
"Alacritty": "org.alacritty",
|
|
17
|
+
"kitty": "net.kovidgoyal.kitty",
|
|
18
|
+
"Hyper": "co.zeit.hyper",
|
|
19
|
+
"Tabby": "org.tabby",
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Windows 终端进程名(小写,不含 .exe)
|
|
24
|
+
*/
|
|
25
|
+
const WIN_TERMINAL_NAMES = new Set([
|
|
26
|
+
"windowsterminal",
|
|
27
|
+
"code",
|
|
28
|
+
"cursor",
|
|
29
|
+
"cmd",
|
|
30
|
+
"powershell",
|
|
31
|
+
"pwsh",
|
|
32
|
+
"mintty",
|
|
33
|
+
"alacritty",
|
|
34
|
+
"wezterm-gui",
|
|
35
|
+
"hyper",
|
|
36
|
+
"tabby",
|
|
37
|
+
"conhost",
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Linux 终端进程名
|
|
42
|
+
*/
|
|
43
|
+
const LINUX_TERMINALS = new Set([
|
|
44
|
+
"gnome-terminal-server",
|
|
45
|
+
"gnome-terminal",
|
|
46
|
+
"konsole",
|
|
47
|
+
"xfce4-terminal",
|
|
48
|
+
"mate-terminal",
|
|
49
|
+
"tilix",
|
|
50
|
+
"terminator",
|
|
51
|
+
"alacritty",
|
|
52
|
+
"kitty",
|
|
53
|
+
"wezterm-gui",
|
|
54
|
+
"foot",
|
|
55
|
+
"code",
|
|
56
|
+
"cursor",
|
|
57
|
+
"hyper",
|
|
58
|
+
"tabby",
|
|
59
|
+
]);
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 沿进程树向上查找终端进程
|
|
63
|
+
*/
|
|
64
|
+
function detectTerminal() {
|
|
65
|
+
const platform = os.platform();
|
|
66
|
+
try {
|
|
67
|
+
if (platform === "darwin") return detectMac();
|
|
68
|
+
if (platform === "win32") return detectWindows();
|
|
69
|
+
if (platform === "linux") return detectLinux();
|
|
70
|
+
} catch {
|
|
71
|
+
// 检测失败
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function detectMac() {
|
|
77
|
+
let pid = process.ppid;
|
|
78
|
+
for (let i = 0; i < 20 && pid > 1; i++) {
|
|
79
|
+
try {
|
|
80
|
+
const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
|
|
81
|
+
const match = info.match(/^\s*(\d+)\s+(.+)$/);
|
|
82
|
+
if (!match) break;
|
|
83
|
+
const ppid = parseInt(match[1], 10);
|
|
84
|
+
const comm = match[2].trim().split("/").pop();
|
|
85
|
+
|
|
86
|
+
if (MAC_BUNDLE_MAP[comm] !== undefined) {
|
|
87
|
+
const bundleId = MAC_BUNDLE_MAP[comm];
|
|
88
|
+
if (bundleId) {
|
|
89
|
+
return { name: comm, bundleId, pid, processName: comm };
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
pid = ppid;
|
|
93
|
+
} catch {
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// 回退:检查 TERM_PROGRAM 环境变量
|
|
98
|
+
const termProgram = process.env.TERM_PROGRAM;
|
|
99
|
+
if (termProgram) {
|
|
100
|
+
const map = {
|
|
101
|
+
"Apple_Terminal": { name: "Terminal", bundleId: "com.apple.Terminal" },
|
|
102
|
+
"iTerm.app": { name: "iTerm2", bundleId: "com.googlecode.iterm2" },
|
|
103
|
+
"vscode": { name: "Code", bundleId: "com.microsoft.VSCode" },
|
|
104
|
+
"WarpTerminal": { name: "Warp", bundleId: "dev.warp.Warp-Stable" },
|
|
105
|
+
"Hyper": { name: "Hyper", bundleId: "co.zeit.hyper" },
|
|
106
|
+
};
|
|
107
|
+
if (map[termProgram]) {
|
|
108
|
+
return { ...map[termProgram], pid: 0, processName: termProgram };
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Windows: 优先环境变量,回退单次 PowerShell 批量查询
|
|
116
|
+
*/
|
|
117
|
+
function detectWindows() {
|
|
118
|
+
// 1. 环境变量快速检测
|
|
119
|
+
if (process.env.WT_SESSION) {
|
|
120
|
+
// Windows Terminal 设置了 WT_SESSION
|
|
121
|
+
const info = findProcessByName("WindowsTerminal");
|
|
122
|
+
if (info) return info;
|
|
123
|
+
}
|
|
124
|
+
if (process.env.VSCODE_PID) {
|
|
125
|
+
const pid = parseInt(process.env.VSCODE_PID, 10);
|
|
126
|
+
if (pid > 0) return { name: "Code", pid, processName: "code.exe" };
|
|
127
|
+
}
|
|
128
|
+
if (process.env.TERM_PROGRAM === "vscode") {
|
|
129
|
+
const info = findProcessByName("Code");
|
|
130
|
+
if (info) return info;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 2. 单次 PowerShell 调用批量遍历进程树
|
|
134
|
+
try {
|
|
135
|
+
const script = [
|
|
136
|
+
"$cpid=" + process.ppid,
|
|
137
|
+
"for($i=0;$i -lt 20 -and $cpid -gt 0;$i++){",
|
|
138
|
+
" $p=Get-Process -Id $cpid -EA SilentlyContinue",
|
|
139
|
+
" if(-not $p){break}",
|
|
140
|
+
" Write-Output \"$($p.ProcessName)|$($p.Id)|$(if($p.Parent){$p.Parent.Id}else{0})\"",
|
|
141
|
+
" $cpid=if($p.Parent){$p.Parent.Id}else{0}",
|
|
142
|
+
"}",
|
|
143
|
+
].join(";");
|
|
144
|
+
const output = execFileSync("powershell", ["-NoProfile", "-Command", script], {
|
|
145
|
+
encoding: "utf-8",
|
|
146
|
+
timeout: 8000,
|
|
147
|
+
}).trim();
|
|
148
|
+
|
|
149
|
+
for (const line of output.split("\n")) {
|
|
150
|
+
const parts = line.trim().split("|");
|
|
151
|
+
if (parts.length < 3) continue;
|
|
152
|
+
const name = parts[0].toLowerCase();
|
|
153
|
+
const pid = parseInt(parts[1], 10);
|
|
154
|
+
if (WIN_TERMINAL_NAMES.has(name)) {
|
|
155
|
+
return { name: parts[0], pid, processName: name + ".exe" };
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
} catch {
|
|
159
|
+
// PowerShell 调用失败
|
|
160
|
+
}
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 按名称查找进程(Windows)
|
|
166
|
+
*/
|
|
167
|
+
function findProcessByName(name) {
|
|
168
|
+
try {
|
|
169
|
+
const script = `$p=Get-Process -Name '${name}' -EA SilentlyContinue | Select-Object -First 1; if($p){Write-Output "$($p.ProcessName)|$($p.Id)"}`;
|
|
170
|
+
const output = execFileSync("powershell", ["-NoProfile", "-Command", script], {
|
|
171
|
+
encoding: "utf-8",
|
|
172
|
+
timeout: 5000,
|
|
173
|
+
}).trim();
|
|
174
|
+
if (output) {
|
|
175
|
+
const parts = output.split("|");
|
|
176
|
+
return { name: parts[0], pid: parseInt(parts[1], 10), processName: parts[0].toLowerCase() + ".exe" };
|
|
177
|
+
}
|
|
178
|
+
} catch {
|
|
179
|
+
// 查找失败
|
|
180
|
+
}
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function detectLinux() {
|
|
185
|
+
// 1. 环境变量快速检测
|
|
186
|
+
if (process.env.VSCODE_PID) {
|
|
187
|
+
const pid = parseInt(process.env.VSCODE_PID, 10);
|
|
188
|
+
if (pid > 0) return { name: "code", pid, processName: "code" };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 2. 进程树遍历
|
|
192
|
+
let pid = process.ppid;
|
|
193
|
+
for (let i = 0; i < 20 && pid > 1; i++) {
|
|
194
|
+
try {
|
|
195
|
+
const info = execSync(`ps -o ppid=,comm= -p ${pid}`, { encoding: "utf-8" }).trim();
|
|
196
|
+
const match = info.match(/^\s*(\d+)\s+(.+)$/);
|
|
197
|
+
if (!match) break;
|
|
198
|
+
const ppid = parseInt(match[1], 10);
|
|
199
|
+
const comm = match[2].trim();
|
|
200
|
+
|
|
201
|
+
if (LINUX_TERMINALS.has(comm)) {
|
|
202
|
+
return { name: comm, pid, processName: comm };
|
|
203
|
+
}
|
|
204
|
+
pid = ppid;
|
|
205
|
+
} catch {
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* 获取激活终端窗口的命令和参数
|
|
214
|
+
*/
|
|
215
|
+
function getActivateCommand(terminalInfo) {
|
|
216
|
+
if (!terminalInfo) return null;
|
|
217
|
+
const platform = os.platform();
|
|
218
|
+
|
|
219
|
+
if (platform === "darwin") {
|
|
220
|
+
if (terminalInfo.bundleId) {
|
|
221
|
+
return {
|
|
222
|
+
command: "osascript",
|
|
223
|
+
args: ["-e", `tell application id "${terminalInfo.bundleId}" to activate`],
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
return null;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Windows 使用 notify-helper.exe 自行处理激活,不需要此函数
|
|
230
|
+
|
|
231
|
+
if (platform === "linux") {
|
|
232
|
+
return {
|
|
233
|
+
command: "xdotool",
|
|
234
|
+
args: ["search", "--pid", String(terminalInfo.pid), "--onlyvisible", "windowactivate"],
|
|
235
|
+
fallbackCommand: "wmctrl",
|
|
236
|
+
fallbackArgs: ["-ia", String(terminalInfo.pid)],
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return null;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
module.exports = { detectTerminal, getActivateCommand };
|
package/src/cli.js
CHANGED
|
@@ -24,6 +24,7 @@ const HELP = `
|
|
|
24
24
|
--title <标题> 自定义通知标题
|
|
25
25
|
--message <消息> 自定义通知消息
|
|
26
26
|
--sound <音效> macOS 音效名称 (默认: Glass)
|
|
27
|
+
--no-activate 禁用点击通知后激活终端窗口
|
|
27
28
|
--dry-run 仅打印通知内容,不实际发送
|
|
28
29
|
|
|
29
30
|
示例:
|
|
@@ -39,6 +40,7 @@ function parseArgs(args) {
|
|
|
39
40
|
const key = args[i].slice(2);
|
|
40
41
|
if (
|
|
41
42
|
key === "dry-run" ||
|
|
43
|
+
key === "no-activate" ||
|
|
42
44
|
key === "global" ||
|
|
43
45
|
key === "local" ||
|
|
44
46
|
key === "help"
|
|
@@ -101,6 +103,7 @@ async function main() {
|
|
|
101
103
|
title: args.title,
|
|
102
104
|
message: args.message,
|
|
103
105
|
sound: args.sound,
|
|
106
|
+
activate: !args["no-activate"],
|
|
104
107
|
dryRun: !!args["dry-run"],
|
|
105
108
|
hookInput: input,
|
|
106
109
|
});
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { sendNotification, EVENT_CONFIG } = require("./notify");
|
|
2
2
|
const { setup, uninstall } = require("./setup");
|
|
3
|
+
const { detectTerminal } = require("./activate");
|
|
3
4
|
|
|
4
|
-
module.exports = { sendNotification, EVENT_CONFIG, setup, uninstall };
|
|
5
|
+
module.exports = { sendNotification, EVENT_CONFIG, setup, uninstall, detectTerminal };
|
package/src/notify.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
const { execSync } = require("child_process");
|
|
1
|
+
const { execSync, spawn } = require("child_process");
|
|
2
2
|
const os = require("os");
|
|
3
3
|
const fs = require("fs");
|
|
4
4
|
const path = require("path");
|
|
5
|
+
const { detectTerminal, getActivateCommand } = require("./activate");
|
|
5
6
|
|
|
6
7
|
const EVENT_CONFIG = {
|
|
7
8
|
Stop: {
|
|
@@ -120,16 +121,16 @@ function buildNotification({ event, title, message, hookInput }) {
|
|
|
120
121
|
const config = EVENT_CONFIG[event] || EVENT_CONFIG.Stop;
|
|
121
122
|
const ctx = extractContext(hookInput || {});
|
|
122
123
|
|
|
123
|
-
let finalTitle = title ||
|
|
124
|
+
let finalTitle = title || config.title;
|
|
124
125
|
let finalMessage = message;
|
|
125
126
|
|
|
126
127
|
if (!finalMessage) {
|
|
127
128
|
if (event === "StopFailure" && ctx.errorType) {
|
|
128
129
|
const label = ERROR_TYPE_LABELS[ctx.errorType] || ctx.errorType;
|
|
129
|
-
finalTitle = `${config.title}: ${label}
|
|
130
|
+
finalTitle = `${config.title}: ${label}`;
|
|
130
131
|
finalMessage = ctx.errorMessage || config.message;
|
|
131
132
|
} else if (event === "Stop" && ctx.stopReason === "max_tokens") {
|
|
132
|
-
finalTitle =
|
|
133
|
+
finalTitle = "Claude Code 响应截断";
|
|
133
134
|
finalMessage = "响应达到最大 token 限制被截断";
|
|
134
135
|
} else if (event === "TaskCompleted" && ctx.taskSubject) {
|
|
135
136
|
finalMessage = ctx.taskSubject;
|
|
@@ -171,6 +172,7 @@ async function sendNotification(options = {}) {
|
|
|
171
172
|
sound: customSound,
|
|
172
173
|
dryRun = false,
|
|
173
174
|
hookInput = {},
|
|
175
|
+
activate = true,
|
|
174
176
|
} = options;
|
|
175
177
|
|
|
176
178
|
const {
|
|
@@ -189,7 +191,11 @@ async function sendNotification(options = {}) {
|
|
|
189
191
|
let method = "unknown";
|
|
190
192
|
let command = "";
|
|
191
193
|
let args = [];
|
|
192
|
-
let
|
|
194
|
+
let useWatcher = false;
|
|
195
|
+
|
|
196
|
+
// 检测终端信息(用于点击激活)
|
|
197
|
+
const terminalInfo = activate ? detectTerminal() : null;
|
|
198
|
+
const activateCmd = terminalInfo ? getActivateCommand(terminalInfo) : null;
|
|
193
199
|
|
|
194
200
|
if (platform === "darwin") {
|
|
195
201
|
// macOS
|
|
@@ -206,6 +212,10 @@ async function sendNotification(options = {}) {
|
|
|
206
212
|
"-group",
|
|
207
213
|
`claude-code-${extractContext(hookInput).project}`,
|
|
208
214
|
];
|
|
215
|
+
// terminal-notifier 内置点击激活:添加 -activate bundleId
|
|
216
|
+
if (activate && terminalInfo && terminalInfo.bundleId) {
|
|
217
|
+
args.push("-activate", terminalInfo.bundleId);
|
|
218
|
+
}
|
|
209
219
|
} else {
|
|
210
220
|
method = "osascript";
|
|
211
221
|
command = "osascript";
|
|
@@ -215,6 +225,7 @@ async function sendNotification(options = {}) {
|
|
|
215
225
|
"-e",
|
|
216
226
|
`display notification "${escaped}" with title "${escapedTitle}" sound name "${sound}"`,
|
|
217
227
|
];
|
|
228
|
+
// osascript 不支持点击回调,跳过激活
|
|
218
229
|
}
|
|
219
230
|
} else if (platform === "linux") {
|
|
220
231
|
method = "notify-send";
|
|
@@ -234,24 +245,25 @@ async function sendNotification(options = {}) {
|
|
|
234
245
|
}
|
|
235
246
|
return result;
|
|
236
247
|
}
|
|
248
|
+
// Linux: 使用后台 watcher 处理点击激活
|
|
249
|
+
if (activate && activateCmd) {
|
|
250
|
+
useWatcher = true;
|
|
251
|
+
args.push("--action=default=click");
|
|
252
|
+
}
|
|
237
253
|
} else if (platform === "win32") {
|
|
238
|
-
//
|
|
239
|
-
method = "
|
|
240
|
-
command = "
|
|
241
|
-
const
|
|
242
|
-
|
|
243
|
-
const vbsScript = `CreateObject("WScript.Shell").Popup "${escapedMessage}", 5, "${escapedTitle}", 64`;
|
|
244
|
-
tmpFile = path.join(os.tmpdir(), `claude-notify-${process.pid}.vbs`);
|
|
245
|
-
fs.writeFileSync(tmpFile, vbsScript, "utf-8");
|
|
246
|
-
args = ["//nologo", tmpFile];
|
|
254
|
+
// Windows: notify-helper.exe 通知 + 点击激活终端(无控制台窗口)
|
|
255
|
+
method = "notify-helper";
|
|
256
|
+
command = path.join(__dirname, "..", "vendor", "notify-helper", "ClaudeCodeNotify.exe");
|
|
257
|
+
const targetNames = ["WindowsTerminal", "Code", "cursor", "cmd", "pwsh", "powershell", "mintty"];
|
|
258
|
+
args = [title, message, targetNames.join(",")];
|
|
247
259
|
}
|
|
248
260
|
|
|
249
|
-
const result = { sent: !dryRun, method, command, args };
|
|
261
|
+
const result = { sent: !dryRun, method, command, args, activate: !!activateCmd };
|
|
262
|
+
if (terminalInfo) {
|
|
263
|
+
result.terminal = terminalInfo.name;
|
|
264
|
+
}
|
|
250
265
|
|
|
251
266
|
if (dryRun) {
|
|
252
|
-
if (tmpFile) {
|
|
253
|
-
try { fs.unlinkSync(tmpFile); } catch {}
|
|
254
|
-
}
|
|
255
267
|
result.sent = false;
|
|
256
268
|
console.log(JSON.stringify(result, null, 2));
|
|
257
269
|
return result;
|
|
@@ -259,16 +271,36 @@ async function sendNotification(options = {}) {
|
|
|
259
271
|
|
|
260
272
|
try {
|
|
261
273
|
if (command) {
|
|
262
|
-
|
|
263
|
-
|
|
274
|
+
if (method === "notify-helper") {
|
|
275
|
+
// Windows: notify-helper.exe 后台运行,等待点击后激活终端
|
|
276
|
+
const child = spawn(command, args, { detached: true, stdio: "ignore", windowsHide: true });
|
|
277
|
+
child.unref();
|
|
278
|
+
} else if (useWatcher && activateCmd) {
|
|
279
|
+
// Linux: 启动后台 watcher 发送通知 + 等待点击 + 激活终端
|
|
280
|
+
const watcherConfig = {
|
|
281
|
+
platform,
|
|
282
|
+
notifyCommand: command,
|
|
283
|
+
notifyArgs: args,
|
|
284
|
+
activateCommand: activateCmd.command,
|
|
285
|
+
activateArgs: activateCmd.args,
|
|
286
|
+
activateFallbackCommand: activateCmd.fallbackCommand || null,
|
|
287
|
+
activateFallbackArgs: activateCmd.fallbackArgs || null,
|
|
288
|
+
timeout: 120000,
|
|
289
|
+
};
|
|
290
|
+
const watcher = spawn(
|
|
291
|
+
process.execPath,
|
|
292
|
+
[path.join(__dirname, "activate-watcher.js"), JSON.stringify(watcherConfig)],
|
|
293
|
+
{ detached: true, stdio: "ignore", windowsHide: true }
|
|
294
|
+
);
|
|
295
|
+
watcher.unref();
|
|
296
|
+
} else {
|
|
297
|
+
const { execFileSync } = require("child_process");
|
|
298
|
+
execFileSync(command, args, { stdio: "ignore", timeout: 8000 });
|
|
299
|
+
}
|
|
264
300
|
}
|
|
265
301
|
} catch (err) {
|
|
266
302
|
result.sent = false;
|
|
267
303
|
result.error = err.message;
|
|
268
|
-
} finally {
|
|
269
|
-
if (tmpFile) {
|
|
270
|
-
try { fs.unlinkSync(tmpFile); } catch {}
|
|
271
|
-
}
|
|
272
304
|
}
|
|
273
305
|
|
|
274
306
|
return result;
|
|
Binary file
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
using System;
|
|
2
|
+
using System.Diagnostics;
|
|
3
|
+
using System.Drawing;
|
|
4
|
+
using System.IO;
|
|
5
|
+
using System.Runtime.InteropServices;
|
|
6
|
+
using System.Windows.Forms;
|
|
7
|
+
|
|
8
|
+
[assembly: System.Reflection.AssemblyTitle("Claude Code 完成通知")]
|
|
9
|
+
[assembly: System.Reflection.AssemblyProduct("Claude Code Notify")]
|
|
10
|
+
|
|
11
|
+
class NotifyHelper
|
|
12
|
+
{
|
|
13
|
+
[DllImport("user32.dll")]
|
|
14
|
+
static extern bool SetForegroundWindow(IntPtr hWnd);
|
|
15
|
+
|
|
16
|
+
[DllImport("user32.dll")]
|
|
17
|
+
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
|
|
18
|
+
|
|
19
|
+
// Claude sparkle 图标 PNG(内嵌,无需外部文件)
|
|
20
|
+
static string IconBase64 = "iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAFVBMVEXZdlXabkzaaEP99vT66uXtw7jimoUHIS5GAAALV0lEQVR4nO1ai3bcuA4TaUv//8nXEkkQlDxNuo+T3XvWadMZP0QQACnZbmv/bd/axk8DkJ8G8Ge2P8Xej1P/3/ZHt3+CdD+O4V9duXX7cS4/bf9YYP+vm0pT1R8E0O/77v3Hikv79QC4r/sdgfTx9yKTcdt29ddAD7IP0P6q7cZ2pvqguyY5f6NBtCeAfsQRk+d+J+cvB3BQveRZFPzR4WUO+Uv0DOAauh+8ZvzHHt+M9rpH4uMrEB03U8BniRXI9dGfsgf9nKrUT8I7b6ZAyili+S8AX7ngqzOk1bHzQGhwkdnEr7hdgkMbpdbJ452fjz07Dhk7BXSGW/Da0Y/eZ3/aNCg8E49Ewe7ylU2/wusP10qeWV1gccDDizTzZgFL6ZY8pQmfcOokLV0wERhMG1qHw/JGFAFvaw63IlalQeo+wV8ioXAz7hjySgqaAVg/hZfmsvhuHp6cyOEz7mkCu8JFuLwSceFIYRAiekP2bimBCgEFU7FMYUmbj1lc0KINXFEE6wqAterk4dmp0spXYml9UeHzreVfbkSFXC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GgWhjU+j8NIyIbPE2A27mk0Lf8VryABzB2qlOzYtQn3jkuEASwOHrDyGkfn27YFOHL3xTRbX29XrVNiscYMgX5djGyTl42fM/Wl1f1muarlQAAAAASUVORK5CYII=";
|
|
21
|
+
|
|
22
|
+
static IntPtr targetHandle = IntPtr.Zero;
|
|
23
|
+
static NotifyIcon notify;
|
|
24
|
+
|
|
25
|
+
static void Main(string[] args)
|
|
26
|
+
{
|
|
27
|
+
string title = args.Length > 0 ? args[0] : "Claude Code";
|
|
28
|
+
string message = args.Length > 1 ? args[1] : "任务完成";
|
|
29
|
+
string targetName = args.Length > 2 ? args[2] : "";
|
|
30
|
+
|
|
31
|
+
// 查找目标终端窗口
|
|
32
|
+
if (!string.IsNullOrEmpty(targetName))
|
|
33
|
+
{
|
|
34
|
+
foreach (var name in targetName.Split(','))
|
|
35
|
+
{
|
|
36
|
+
try
|
|
37
|
+
{
|
|
38
|
+
var procs = Process.GetProcessesByName(name.Trim());
|
|
39
|
+
foreach (var p in procs)
|
|
40
|
+
{
|
|
41
|
+
if (p.MainWindowHandle != IntPtr.Zero)
|
|
42
|
+
{
|
|
43
|
+
targetHandle = p.MainWindowHandle;
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (targetHandle != IntPtr.Zero) break;
|
|
48
|
+
}
|
|
49
|
+
catch { }
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
notify = new NotifyIcon();
|
|
54
|
+
notify.Icon = LoadEmbeddedIcon();
|
|
55
|
+
notify.Visible = true;
|
|
56
|
+
notify.BalloonTipTitle = title;
|
|
57
|
+
notify.BalloonTipText = message;
|
|
58
|
+
notify.BalloonTipIcon = ToolTipIcon.None;
|
|
59
|
+
|
|
60
|
+
notify.BalloonTipClicked += delegate { ActivateAndExit(); };
|
|
61
|
+
notify.Click += delegate { ActivateAndExit(); };
|
|
62
|
+
notify.BalloonTipClosed += delegate { Cleanup(); };
|
|
63
|
+
|
|
64
|
+
notify.ShowBalloonTip(15000);
|
|
65
|
+
|
|
66
|
+
var timer = new Timer();
|
|
67
|
+
timer.Interval = 60000;
|
|
68
|
+
timer.Tick += delegate { Cleanup(); };
|
|
69
|
+
timer.Start();
|
|
70
|
+
|
|
71
|
+
Application.Run();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
static Icon LoadEmbeddedIcon()
|
|
75
|
+
{
|
|
76
|
+
try
|
|
77
|
+
{
|
|
78
|
+
var bytes = Convert.FromBase64String(IconBase64);
|
|
79
|
+
using (var ms = new MemoryStream(bytes))
|
|
80
|
+
{
|
|
81
|
+
var bmp = new Bitmap(ms);
|
|
82
|
+
var resized = new Bitmap(bmp, 32, 32);
|
|
83
|
+
return Icon.FromHandle(resized.GetHicon());
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch
|
|
87
|
+
{
|
|
88
|
+
return SystemIcons.Information;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
static void ActivateAndExit()
|
|
93
|
+
{
|
|
94
|
+
if (targetHandle != IntPtr.Zero)
|
|
95
|
+
{
|
|
96
|
+
ShowWindow(targetHandle, 9);
|
|
97
|
+
SetForegroundWindow(targetHandle);
|
|
98
|
+
}
|
|
99
|
+
Cleanup();
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
static void Cleanup()
|
|
103
|
+
{
|
|
104
|
+
if (notify != null)
|
|
105
|
+
{
|
|
106
|
+
notify.Visible = false;
|
|
107
|
+
notify.Dispose();
|
|
108
|
+
}
|
|
109
|
+
Application.ExitThread();
|
|
110
|
+
}
|
|
111
|
+
}
|