claude-hook-notify 1.4.2 → 1.5.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 +55 -6
- package/package.json +10 -2
- package/src/activate-watcher.js +1 -1
- package/src/cli.js +0 -3
- package/src/index.js +1 -2
- package/src/notify.js +186 -85
- package/vendor/snoretoast/LICENSE +166 -0
- package/vendor/snoretoast/snoretoast-x64.exe +0 -0
- package/vendor/snoretoast/snoretoast-x86.exe +0 -0
package/README.md
CHANGED
|
@@ -14,13 +14,20 @@ npx claude-hook-notify setup
|
|
|
14
14
|
|
|
15
15
|
## 效果
|
|
16
16
|
|
|
17
|
-
当 Claude Code
|
|
17
|
+
当 Claude Code 完成任务时,你会收到系统原生桌面通知,并显示**本次任务的 token 消耗**:
|
|
18
18
|
|
|
19
19
|
```
|
|
20
|
-
|
|
21
|
-
│
|
|
22
|
-
│
|
|
23
|
-
|
|
20
|
+
┌─────────────────────────────────────────────────────┐
|
|
21
|
+
│ Claude Code · 本次任务消耗 1.2k tokens │ ← 顶部栏(Windows)
|
|
22
|
+
│ 🔔 Claude Code 完成 (my-project) │
|
|
23
|
+
│ 已完成代码重构和测试 │
|
|
24
|
+
└─────────────────────────────────────────────────────┘
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
含子代理调用的任务会额外显示子代理次数:
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
Claude Code · 本次任务消耗 42k tokens · 子代理 7
|
|
24
31
|
```
|
|
25
32
|
|
|
26
33
|
API 错误中断时:
|
|
@@ -89,12 +96,52 @@ npx claude-hook-notify setup --events Stop,TaskCompleted,Notification,PostToolUs
|
|
|
89
96
|
| macOS | `osascript` | 无(系统自带) |
|
|
90
97
|
| macOS | `terminal-notifier` | 可选: `brew install terminal-notifier`(更好) |
|
|
91
98
|
| Linux | `notify-send` | `sudo apt-get install libnotify-bin` |
|
|
92
|
-
| Windows |
|
|
99
|
+
| Windows | Toast 通知 (PowerShell) | 无(Windows 10+ 系统自带) |
|
|
93
100
|
|
|
94
101
|
## 原理
|
|
95
102
|
|
|
96
103
|
安装时会在 `~/.claude/settings.json` 中添加 hooks 配置。当对应事件触发时,Claude Code 会自动执行 `npx claude-hook-notify notify --event <事件名>`,脚本会读取 hook 传入的上下文信息(任务名称、最后的 assistant 消息等),然后通过系统原生 API 发送桌面通知。
|
|
97
104
|
|
|
105
|
+
## Token 消耗统计
|
|
106
|
+
|
|
107
|
+
在 `Stop` / `SubagentStop` / `StopFailure` 事件触发时,通知会显示**本次任务新增到对话上下文的 token 量**(而不是整个 session 的累计,也不是 API 账单总额)。
|
|
108
|
+
|
|
109
|
+
### 计算口径:context-delta
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
本次任务消耗 = (回合结束时上下文大小) − (回合开始前上下文大小) + sum(本回合 output_tokens)
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
- **"上下文大小"** = 单次 API 调用的 `input_tokens + cache_creation_input_tokens + cache_read_input_tokens`
|
|
116
|
+
- **"回合"** = 从最后一条真实用户消息到 Stop 事件之间
|
|
117
|
+
- **子代理**:通过主 transcript 的 `progress` 条目镜像统计,按 `message.id` 去重(progress 会重复触发同一 id 5~8 次)
|
|
118
|
+
|
|
119
|
+
### 为什么不直接累加 `cache_creation_input_tokens`
|
|
120
|
+
|
|
121
|
+
缓存失效(默认 5 分钟 TTL)时 Claude 会把整段对话历史重新写入缓存槽位,导致单次调用的 `cache_creation_input_tokens` 飙升到历史总量规模。直接累加会把这些"本来就在对话里"的 tokens 误算作新增消耗,出现数倍虚高(开发过程中曾经见过 146k 的虚高值)。
|
|
122
|
+
|
|
123
|
+
用 context-delta 算法规避了这个陷阱:`input + cache_creation + cache_read` 的总和表示"当前 prompt 的总上下文大小",这个值随对话进展单调递增,不受缓存重建影响。
|
|
124
|
+
|
|
125
|
+
### 准确率:~92–96%
|
|
126
|
+
|
|
127
|
+
| 场景 | 准确率 | 说明 |
|
|
128
|
+
|------|--------|------|
|
|
129
|
+
| 纯主代理的普通回合(读文件、编辑、Bash 等) | **~96%** | 主要误差为缓存边界的 ±100 tokens 级测量波动 |
|
|
130
|
+
| 含子代理的回合 | **~85–92%** | 子代理最终结果文本会被同时计入子代理 output 与主 context delta,造成轻微双重计数 |
|
|
131
|
+
| 新 session 第一次交互 | **~70%** | `prevContext=0`,system prompt(约 5k)会被算作本次新增 |
|
|
132
|
+
| 触发了 `/compact` 或 auto-compact 的长回合 | 不确定 | 上下文被压缩,`currentContext < prevContext` 时归零,可能严重低估 |
|
|
133
|
+
| 包含 API 重试的回合 | **~96%** | 按 `message.id` 去重已足够 |
|
|
134
|
+
|
|
135
|
+
### 对比其他口径
|
|
136
|
+
|
|
137
|
+
| 问题 | 我们的算法 | ccusage session 口径 | Claude Code 原生 `/context` |
|
|
138
|
+
|------|-----------|---------------------|---------------------------|
|
|
139
|
+
| "这次任务新增了多少对话内容?" | ✅ | ❌ 是累计总和 | ❌ 是当前快照 |
|
|
140
|
+
| "整个 session 总 API 账单?" | ❌ 量级对但精度低 | ✅ | ❌ |
|
|
141
|
+
| "当前上下文窗口占用?" | ❌ | 部分支持 | ✅ |
|
|
142
|
+
|
|
143
|
+
如果你想要账单精度的成本统计,推荐使用 [ccusage](https://github.com/ryoppippi/ccusage),它集成了 LiteLLM 价格表并正确按 API 账单口径累加。本工具的 token 显示定位是**让你感知单次任务的数量级**,不是账单。
|
|
144
|
+
|
|
98
145
|
## 编程接口
|
|
99
146
|
|
|
100
147
|
也可以作为库使用:
|
|
@@ -114,6 +161,8 @@ await sendNotification({
|
|
|
114
161
|
- **Ctrl+C 用户中断**: 用户手动按 Ctrl+C 取消时不会触发任何 hook 事件,因此无法发送通知。
|
|
115
162
|
- **网络完全断开**: 如果网络完全断开导致 Claude Code 进程本身退出,hook 可能无法执行。
|
|
116
163
|
- **StopFailure 事件**: `StopFailure` 不是 Claude Code 官方支持的 hook 事件名,默认不再注册。如通过 `--events` 手动指定,会导致 settings.json 校验报错,整个配置文件被跳过。
|
|
164
|
+
- **Token 消耗精度**: 桌面通知显示的"本次任务消耗"是 context-delta 估算值,准确率 ~92-96%(详见上文 [Token 消耗统计](#token-消耗统计) 章节)。不适合作为账单依据。
|
|
165
|
+
- **Windows 动态 appID**: Windows 上 token 信息通过动态 `-appID` 显示在 Toast 顶部栏,每次数值变化会在系统"通知与操作"设置里登记一条新记录,属 SnoreToast 的已知副作用。
|
|
117
166
|
|
|
118
167
|
## License
|
|
119
168
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "claude-hook-notify",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "🔔 Claude Code 任务完成桌面通知 — 一键安装,跨平台支持",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -18,8 +18,16 @@
|
|
|
18
18
|
"notify-send",
|
|
19
19
|
"cli"
|
|
20
20
|
],
|
|
21
|
-
"author": "",
|
|
21
|
+
"author": "Mr-zj388",
|
|
22
22
|
"license": "MIT",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "https://github.com/Mr-zj388/claude-code-notify.git"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/Mr-zj388/claude-code-notify#readme",
|
|
28
|
+
"bugs": {
|
|
29
|
+
"url": "https://github.com/Mr-zj388/claude-code-notify/issues"
|
|
30
|
+
},
|
|
23
31
|
"engines": {
|
|
24
32
|
"node": ">=16.0.0"
|
|
25
33
|
},
|
package/src/activate-watcher.js
CHANGED
package/src/cli.js
CHANGED
|
@@ -24,7 +24,6 @@ const HELP = `
|
|
|
24
24
|
--title <标题> 自定义通知标题
|
|
25
25
|
--message <消息> 自定义通知消息
|
|
26
26
|
--sound <音效> macOS 音效名称 (默认: Glass)
|
|
27
|
-
--no-activate 禁用点击通知后激活终端窗口
|
|
28
27
|
--dry-run 仅打印通知内容,不实际发送
|
|
29
28
|
|
|
30
29
|
示例:
|
|
@@ -40,7 +39,6 @@ function parseArgs(args) {
|
|
|
40
39
|
const key = args[i].slice(2);
|
|
41
40
|
if (
|
|
42
41
|
key === "dry-run" ||
|
|
43
|
-
key === "no-activate" ||
|
|
44
42
|
key === "global" ||
|
|
45
43
|
key === "local" ||
|
|
46
44
|
key === "help"
|
|
@@ -103,7 +101,6 @@ async function main() {
|
|
|
103
101
|
title: args.title,
|
|
104
102
|
message: args.message,
|
|
105
103
|
sound: args.sound,
|
|
106
|
-
activate: !args["no-activate"],
|
|
107
104
|
dryRun: !!args["dry-run"],
|
|
108
105
|
hookInput: input,
|
|
109
106
|
});
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
const { sendNotification, EVENT_CONFIG } = require("./notify");
|
|
2
2
|
const { setup, uninstall } = require("./setup");
|
|
3
|
-
const { detectTerminal } = require("./activate");
|
|
4
3
|
|
|
5
|
-
module.exports = { sendNotification, EVENT_CONFIG, setup, uninstall
|
|
4
|
+
module.exports = { sendNotification, EVENT_CONFIG, setup, uninstall };
|
package/src/notify.js
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
const { execSync,
|
|
1
|
+
const { execSync, execFileSync } = require("child_process");
|
|
2
2
|
const os = require("os");
|
|
3
3
|
const fs = require("fs");
|
|
4
4
|
const path = require("path");
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
const TOKEN_EVENTS = new Set(["Stop", "SubagentStop", "StopFailure"]);
|
|
7
|
+
|
|
8
|
+
const contextSize = (u) =>
|
|
9
|
+
(u.input_tokens || 0) +
|
|
10
|
+
(u.cache_creation_input_tokens || 0) +
|
|
11
|
+
(u.cache_read_input_tokens || 0);
|
|
6
12
|
|
|
7
13
|
const EVENT_CONFIG = {
|
|
8
14
|
Stop: {
|
|
@@ -47,6 +53,117 @@ const ERROR_TYPE_LABELS = {
|
|
|
47
53
|
unknown: "未知错误",
|
|
48
54
|
};
|
|
49
55
|
|
|
56
|
+
/**
|
|
57
|
+
* 提取本轮任务的 token 消耗
|
|
58
|
+
*
|
|
59
|
+
* 采用 context-delta 口径:
|
|
60
|
+
* newInput = 回合结束时上下文大小 - 回合开始前上下文大小
|
|
61
|
+
* output = 回合内所有 assistant 消息的 output_tokens 累加
|
|
62
|
+
* total = newInput + output
|
|
63
|
+
*
|
|
64
|
+
* "上下文大小" 指单次 API 调用的 input + cache_creation + cache_read。
|
|
65
|
+
* 不直接累加 cache_creation,否则缓存失效重建时会把整段历史误算作新增。
|
|
66
|
+
*
|
|
67
|
+
* 子代理通过 progress 条目转发,按 message.id 去重(progress 会重复触发同一 id 5~8 次)。
|
|
68
|
+
*/
|
|
69
|
+
function extractTokenUsage(lines) {
|
|
70
|
+
// 单次反向扫描:先找最后一条真实 user 消息,再继续往前找它之前最近的主代理 assistant
|
|
71
|
+
let startIdx = 0;
|
|
72
|
+
let prevContext = 0;
|
|
73
|
+
let foundUser = false;
|
|
74
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
75
|
+
let entry;
|
|
76
|
+
try {
|
|
77
|
+
entry = JSON.parse(lines[i]);
|
|
78
|
+
} catch {
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (!entry) continue;
|
|
82
|
+
|
|
83
|
+
if (!foundUser) {
|
|
84
|
+
if (
|
|
85
|
+
entry.type === "user" &&
|
|
86
|
+
!entry.data &&
|
|
87
|
+
!entry.isSidechain
|
|
88
|
+
) {
|
|
89
|
+
const content = entry.message?.content;
|
|
90
|
+
const isRealUser =
|
|
91
|
+
typeof content === "string" ||
|
|
92
|
+
(Array.isArray(content) &&
|
|
93
|
+
content.some((b) => b?.type !== "tool_result"));
|
|
94
|
+
if (isRealUser) {
|
|
95
|
+
startIdx = i + 1;
|
|
96
|
+
foundUser = true;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (entry.type === "assistant" && entry.message?.usage) {
|
|
103
|
+
prevContext = contextSize(entry.message.usage);
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// 前向扫描:累加回合内 output、计数子代理、记录最后一条主代理 usage
|
|
109
|
+
const seen = new Set();
|
|
110
|
+
let lastMainUsage = null;
|
|
111
|
+
let output = 0;
|
|
112
|
+
let subCount = 0;
|
|
113
|
+
|
|
114
|
+
for (let i = startIdx; i < lines.length; i++) {
|
|
115
|
+
let entry;
|
|
116
|
+
try {
|
|
117
|
+
entry = JSON.parse(lines[i]);
|
|
118
|
+
} catch {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (!entry) continue;
|
|
122
|
+
|
|
123
|
+
let msgId = null;
|
|
124
|
+
let usage = null;
|
|
125
|
+
let isSub = false;
|
|
126
|
+
|
|
127
|
+
if (entry.type === "assistant" && entry.message?.usage) {
|
|
128
|
+
msgId = entry.message.id;
|
|
129
|
+
usage = entry.message.usage;
|
|
130
|
+
} else if (entry.type === "progress") {
|
|
131
|
+
const inner = entry.data?.message;
|
|
132
|
+
if (inner?.type === "assistant" && inner.message?.usage) {
|
|
133
|
+
msgId = inner.message.id;
|
|
134
|
+
usage = inner.message.usage;
|
|
135
|
+
isSub = true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (!usage || !msgId || seen.has(msgId)) continue;
|
|
140
|
+
seen.add(msgId);
|
|
141
|
+
|
|
142
|
+
output += usage.output_tokens || 0;
|
|
143
|
+
if (isSub) subCount++;
|
|
144
|
+
else lastMainUsage = usage;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (seen.size === 0) return null;
|
|
148
|
+
|
|
149
|
+
const currentContext = lastMainUsage ? contextSize(lastMainUsage) : prevContext;
|
|
150
|
+
const newInput = Math.max(0, currentContext - prevContext);
|
|
151
|
+
return {
|
|
152
|
+
output,
|
|
153
|
+
total: newInput + output,
|
|
154
|
+
subCount,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* 将 token 数字格式化为紧凑字符串(>=1000 显示为 k)
|
|
160
|
+
*/
|
|
161
|
+
function formatTokens(n) {
|
|
162
|
+
if (n < 1000) return String(n);
|
|
163
|
+
if (n < 10000) return (n / 1000).toFixed(1) + "k";
|
|
164
|
+
return Math.round(n / 1000) + "k";
|
|
165
|
+
}
|
|
166
|
+
|
|
50
167
|
/**
|
|
51
168
|
* 从 hook 输入中提取有用信息
|
|
52
169
|
*/
|
|
@@ -81,36 +198,42 @@ function extractContext(hookInput) {
|
|
|
81
198
|
ctx.stopReason = hookInput.stop_reason;
|
|
82
199
|
}
|
|
83
200
|
|
|
84
|
-
//
|
|
85
|
-
|
|
201
|
+
// 读取 transcript 一次,供 lastMessage + tokenUsage 共享
|
|
202
|
+
let lines = null;
|
|
203
|
+
if (hookInput.transcript_path) {
|
|
86
204
|
try {
|
|
87
|
-
|
|
205
|
+
lines = fs
|
|
88
206
|
.readFileSync(hookInput.transcript_path, "utf-8")
|
|
89
207
|
.split("\n")
|
|
90
|
-
.filter(Boolean)
|
|
91
|
-
.slice(-20);
|
|
92
|
-
|
|
93
|
-
for (let i = lines.length - 1; i >= 0; i--) {
|
|
94
|
-
try {
|
|
95
|
-
const entry = JSON.parse(lines[i]);
|
|
96
|
-
if (
|
|
97
|
-
entry?.message?.role === "assistant" &&
|
|
98
|
-
entry?.message?.content?.[0]?.text
|
|
99
|
-
) {
|
|
100
|
-
ctx.lastMessage = entry.message.content[0].text
|
|
101
|
-
.replace(/\n/g, " ")
|
|
102
|
-
.slice(0, 100);
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
} catch {
|
|
106
|
-
continue;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
208
|
+
.filter(Boolean);
|
|
109
209
|
} catch {
|
|
110
210
|
// 读取失败,忽略
|
|
111
211
|
}
|
|
112
212
|
}
|
|
113
213
|
|
|
214
|
+
if (lines) {
|
|
215
|
+
// 末尾若干行内找最后一条 assistant 文本消息
|
|
216
|
+
const tailStart = Math.max(0, lines.length - 20);
|
|
217
|
+
for (let i = lines.length - 1; i >= tailStart; i--) {
|
|
218
|
+
try {
|
|
219
|
+
const entry = JSON.parse(lines[i]);
|
|
220
|
+
if (
|
|
221
|
+
entry?.message?.role === "assistant" &&
|
|
222
|
+
entry?.message?.content?.[0]?.text
|
|
223
|
+
) {
|
|
224
|
+
ctx.lastMessage = entry.message.content[0].text
|
|
225
|
+
.replace(/\n/g, " ")
|
|
226
|
+
.slice(0, 100);
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
} catch {
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
ctx.tokenUsage = extractTokenUsage(lines);
|
|
235
|
+
}
|
|
236
|
+
|
|
114
237
|
return ctx;
|
|
115
238
|
}
|
|
116
239
|
|
|
@@ -121,16 +244,16 @@ function buildNotification({ event, title, message, hookInput }) {
|
|
|
121
244
|
const config = EVENT_CONFIG[event] || EVENT_CONFIG.Stop;
|
|
122
245
|
const ctx = extractContext(hookInput || {});
|
|
123
246
|
|
|
124
|
-
let finalTitle = title || config.title
|
|
247
|
+
let finalTitle = title || `${config.title} (${ctx.project})`;
|
|
125
248
|
let finalMessage = message;
|
|
126
249
|
|
|
127
250
|
if (!finalMessage) {
|
|
128
251
|
if (event === "StopFailure" && ctx.errorType) {
|
|
129
252
|
const label = ERROR_TYPE_LABELS[ctx.errorType] || ctx.errorType;
|
|
130
|
-
finalTitle = `${config.title}: ${label}`;
|
|
253
|
+
finalTitle = `${config.title}: ${label} (${ctx.project})`;
|
|
131
254
|
finalMessage = ctx.errorMessage || config.message;
|
|
132
255
|
} else if (event === "Stop" && ctx.stopReason === "max_tokens") {
|
|
133
|
-
finalTitle =
|
|
256
|
+
finalTitle = `Claude Code 响应截断 (${ctx.project})`;
|
|
134
257
|
finalMessage = "响应达到最大 token 限制被截断";
|
|
135
258
|
} else if (event === "TaskCompleted" && ctx.taskSubject) {
|
|
136
259
|
finalMessage = ctx.taskSubject;
|
|
@@ -143,7 +266,17 @@ function buildNotification({ event, title, message, hookInput }) {
|
|
|
143
266
|
}
|
|
144
267
|
}
|
|
145
268
|
|
|
146
|
-
|
|
269
|
+
// 完成类事件提取 token 消耗字符串(由调用方决定放置位置)
|
|
270
|
+
let tokenStr = "";
|
|
271
|
+
if (TOKEN_EVENTS.has(event) && ctx.tokenUsage) {
|
|
272
|
+
const u = ctx.tokenUsage;
|
|
273
|
+
tokenStr = `本次任务消耗 ${formatTokens(u.total)} tokens`;
|
|
274
|
+
if (u.subCount > 0) {
|
|
275
|
+
tokenStr += ` · 子代理 ${u.subCount}`;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return { title: finalTitle, message: finalMessage, sound: config.sound, tokenStr };
|
|
147
280
|
}
|
|
148
281
|
|
|
149
282
|
/**
|
|
@@ -172,13 +305,13 @@ async function sendNotification(options = {}) {
|
|
|
172
305
|
sound: customSound,
|
|
173
306
|
dryRun = false,
|
|
174
307
|
hookInput = {},
|
|
175
|
-
activate = true,
|
|
176
308
|
} = options;
|
|
177
309
|
|
|
178
310
|
const {
|
|
179
311
|
title,
|
|
180
312
|
message,
|
|
181
313
|
sound: defaultSound,
|
|
314
|
+
tokenStr,
|
|
182
315
|
} = buildNotification({
|
|
183
316
|
event,
|
|
184
317
|
title: customTitle,
|
|
@@ -188,14 +321,13 @@ async function sendNotification(options = {}) {
|
|
|
188
321
|
const sound = customSound || defaultSound;
|
|
189
322
|
|
|
190
323
|
const platform = os.platform();
|
|
324
|
+
// 非 Windows 平台无顶部栏,把 token 信息追加到消息正文
|
|
325
|
+
const displayMessage =
|
|
326
|
+
platform !== "win32" && tokenStr ? `${message} · ${tokenStr}` : message;
|
|
191
327
|
let method = "unknown";
|
|
192
328
|
let command = "";
|
|
193
329
|
let args = [];
|
|
194
|
-
let
|
|
195
|
-
|
|
196
|
-
// 检测终端信息(用于点击激活)
|
|
197
|
-
const terminalInfo = activate ? detectTerminal() : null;
|
|
198
|
-
const activateCmd = terminalInfo ? getActivateCommand(terminalInfo) : null;
|
|
330
|
+
let tmpFile = null;
|
|
199
331
|
|
|
200
332
|
if (platform === "darwin") {
|
|
201
333
|
// macOS
|
|
@@ -206,31 +338,26 @@ async function sendNotification(options = {}) {
|
|
|
206
338
|
"-title",
|
|
207
339
|
title,
|
|
208
340
|
"-message",
|
|
209
|
-
|
|
341
|
+
displayMessage,
|
|
210
342
|
"-sound",
|
|
211
343
|
sound,
|
|
212
344
|
"-group",
|
|
213
|
-
`claude-code-${
|
|
345
|
+
`claude-code-${path.basename(process.cwd())}`,
|
|
214
346
|
];
|
|
215
|
-
// terminal-notifier 内置点击激活:添加 -activate bundleId
|
|
216
|
-
if (activate && terminalInfo && terminalInfo.bundleId) {
|
|
217
|
-
args.push("-activate", terminalInfo.bundleId);
|
|
218
|
-
}
|
|
219
347
|
} else {
|
|
220
348
|
method = "osascript";
|
|
221
349
|
command = "osascript";
|
|
222
|
-
const escaped =
|
|
350
|
+
const escaped = displayMessage.replace(/"/g, '\\"');
|
|
223
351
|
const escapedTitle = title.replace(/"/g, '\\"');
|
|
224
352
|
args = [
|
|
225
353
|
"-e",
|
|
226
354
|
`display notification "${escaped}" with title "${escapedTitle}" sound name "${sound}"`,
|
|
227
355
|
];
|
|
228
|
-
// osascript 不支持点击回调,跳过激活
|
|
229
356
|
}
|
|
230
357
|
} else if (platform === "linux") {
|
|
231
358
|
method = "notify-send";
|
|
232
359
|
command = "notify-send";
|
|
233
|
-
args = [title,
|
|
360
|
+
args = [title, displayMessage, "--urgency=normal", "--expire-time=5000"];
|
|
234
361
|
if (!commandExists("notify-send")) {
|
|
235
362
|
const result = {
|
|
236
363
|
sent: false,
|
|
@@ -245,27 +372,22 @@ async function sendNotification(options = {}) {
|
|
|
245
372
|
}
|
|
246
373
|
return result;
|
|
247
374
|
}
|
|
248
|
-
// Linux: 使用后台 watcher 处理点击激活
|
|
249
|
-
if (activate && activateCmd) {
|
|
250
|
-
useWatcher = true;
|
|
251
|
-
args.push("--action=default=click");
|
|
252
|
-
}
|
|
253
375
|
} else if (platform === "win32") {
|
|
254
|
-
//
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
const
|
|
260
|
-
args = [title, message,
|
|
376
|
+
// SnoreToast: 原生 Windows Toast 通知,UTF-16 支持,不抢焦点
|
|
377
|
+
// token 信息拼到 appID,会显示在顶部应用名栏
|
|
378
|
+
method = "snoretoast";
|
|
379
|
+
const arch = os.arch() === "x64" ? "x64" : "x86";
|
|
380
|
+
command = path.join(__dirname, "..", "vendor", "snoretoast", `snoretoast-${arch}.exe`);
|
|
381
|
+
const appID = tokenStr ? `Claude Code · ${tokenStr}` : "Claude.Code";
|
|
382
|
+
args = ["-t", title, "-m", message, "-appID", appID];
|
|
261
383
|
}
|
|
262
384
|
|
|
263
|
-
const result = { sent: !dryRun, method, command, args
|
|
264
|
-
if (terminalInfo) {
|
|
265
|
-
result.terminal = terminalInfo.name;
|
|
266
|
-
}
|
|
385
|
+
const result = { sent: !dryRun, method, command, args };
|
|
267
386
|
|
|
268
387
|
if (dryRun) {
|
|
388
|
+
if (tmpFile) {
|
|
389
|
+
try { fs.unlinkSync(tmpFile); } catch {}
|
|
390
|
+
}
|
|
269
391
|
result.sent = false;
|
|
270
392
|
console.log(JSON.stringify(result, null, 2));
|
|
271
393
|
return result;
|
|
@@ -273,36 +395,15 @@ async function sendNotification(options = {}) {
|
|
|
273
395
|
|
|
274
396
|
try {
|
|
275
397
|
if (command) {
|
|
276
|
-
|
|
277
|
-
// Windows: notify-helper.exe 后台运行,等待点击后激活终端
|
|
278
|
-
const child = spawn(command, args, { detached: true, stdio: "ignore", windowsHide: true });
|
|
279
|
-
child.unref();
|
|
280
|
-
} else if (useWatcher && activateCmd) {
|
|
281
|
-
// Linux: 启动后台 watcher 发送通知 + 等待点击 + 激活终端
|
|
282
|
-
const watcherConfig = {
|
|
283
|
-
platform,
|
|
284
|
-
notifyCommand: command,
|
|
285
|
-
notifyArgs: args,
|
|
286
|
-
activateCommand: activateCmd.command,
|
|
287
|
-
activateArgs: activateCmd.args,
|
|
288
|
-
activateFallbackCommand: activateCmd.fallbackCommand || null,
|
|
289
|
-
activateFallbackArgs: activateCmd.fallbackArgs || null,
|
|
290
|
-
timeout: 120000,
|
|
291
|
-
};
|
|
292
|
-
const watcher = spawn(
|
|
293
|
-
process.execPath,
|
|
294
|
-
[path.join(__dirname, "activate-watcher.js"), JSON.stringify(watcherConfig)],
|
|
295
|
-
{ detached: true, stdio: "ignore", windowsHide: true }
|
|
296
|
-
);
|
|
297
|
-
watcher.unref();
|
|
298
|
-
} else {
|
|
299
|
-
const { execFileSync } = require("child_process");
|
|
300
|
-
execFileSync(command, args, { stdio: "ignore", timeout: 8000 });
|
|
301
|
-
}
|
|
398
|
+
execFileSync(command, args, { stdio: "ignore", timeout: 8000 });
|
|
302
399
|
}
|
|
303
400
|
} catch (err) {
|
|
304
401
|
result.sent = false;
|
|
305
402
|
result.error = err.message;
|
|
403
|
+
} finally {
|
|
404
|
+
if (tmpFile) {
|
|
405
|
+
try { fs.unlinkSync(tmpFile); } catch {}
|
|
406
|
+
}
|
|
306
407
|
}
|
|
307
408
|
|
|
308
409
|
return result;
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
// Retrieved from https://github.com/KDE/snoretoast/blob/master/COPYING.LGPL-3 version 0.7.0
|
|
2
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
|
3
|
+
Version 3, 29 June 2007
|
|
4
|
+
|
|
5
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim copies
|
|
7
|
+
of this license document, but changing it is not allowed.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
This version of the GNU Lesser General Public License incorporates
|
|
11
|
+
the terms and conditions of version 3 of the GNU General Public
|
|
12
|
+
License, supplemented by the additional permissions listed below.
|
|
13
|
+
|
|
14
|
+
0. Additional Definitions.
|
|
15
|
+
|
|
16
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
|
17
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
|
18
|
+
General Public License.
|
|
19
|
+
|
|
20
|
+
"The Library" refers to a covered work governed by this License,
|
|
21
|
+
other than an Application or a Combined Work as defined below.
|
|
22
|
+
|
|
23
|
+
An "Application" is any work that makes use of an interface provided
|
|
24
|
+
by the Library, but which is not otherwise based on the Library.
|
|
25
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
|
26
|
+
of using an interface provided by the Library.
|
|
27
|
+
|
|
28
|
+
A "Combined Work" is a work produced by combining or linking an
|
|
29
|
+
Application with the Library. The particular version of the Library
|
|
30
|
+
with which the Combined Work was made is also called the "Linked
|
|
31
|
+
Version".
|
|
32
|
+
|
|
33
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
|
34
|
+
Corresponding Source for the Combined Work, excluding any source code
|
|
35
|
+
for portions of the Combined Work that, considered in isolation, are
|
|
36
|
+
based on the Application, and not on the Linked Version.
|
|
37
|
+
|
|
38
|
+
The "Corresponding Application Code" for a Combined Work means the
|
|
39
|
+
object code and/or source code for the Application, including any data
|
|
40
|
+
and utility programs needed for reproducing the Combined Work from the
|
|
41
|
+
Application, but excluding the System Libraries of the Combined Work.
|
|
42
|
+
|
|
43
|
+
1. Exception to Section 3 of the GNU GPL.
|
|
44
|
+
|
|
45
|
+
You may convey a covered work under sections 3 and 4 of this License
|
|
46
|
+
without being bound by section 3 of the GNU GPL.
|
|
47
|
+
|
|
48
|
+
2. Conveying Modified Versions.
|
|
49
|
+
|
|
50
|
+
If you modify a copy of the Library, and, in your modifications, a
|
|
51
|
+
facility refers to a function or data to be supplied by an Application
|
|
52
|
+
that uses the facility (other than as an argument passed when the
|
|
53
|
+
facility is invoked), then you may convey a copy of the modified
|
|
54
|
+
version:
|
|
55
|
+
|
|
56
|
+
a) under this License, provided that you make a good faith effort to
|
|
57
|
+
ensure that, in the event an Application does not supply the
|
|
58
|
+
function or data, the facility still operates, and performs
|
|
59
|
+
whatever part of its purpose remains meaningful, or
|
|
60
|
+
|
|
61
|
+
b) under the GNU GPL, with none of the additional permissions of
|
|
62
|
+
this License applicable to that copy.
|
|
63
|
+
|
|
64
|
+
3. Object Code Incorporating Material from Library Header Files.
|
|
65
|
+
|
|
66
|
+
The object code form of an Application may incorporate material from
|
|
67
|
+
a header file that is part of the Library. You may convey such object
|
|
68
|
+
code under terms of your choice, provided that, if the incorporated
|
|
69
|
+
material is not limited to numerical parameters, data structure
|
|
70
|
+
layouts and accessors, or small macros, inline functions and templates
|
|
71
|
+
(ten or fewer lines in length), you do both of the following:
|
|
72
|
+
|
|
73
|
+
a) Give prominent notice with each copy of the object code that the
|
|
74
|
+
Library is used in it and that the Library and its use are
|
|
75
|
+
covered by this License.
|
|
76
|
+
|
|
77
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
|
78
|
+
document.
|
|
79
|
+
|
|
80
|
+
4. Combined Works.
|
|
81
|
+
|
|
82
|
+
You may convey a Combined Work under terms of your choice that,
|
|
83
|
+
taken together, effectively do not restrict modification of the
|
|
84
|
+
portions of the Library contained in the Combined Work and reverse
|
|
85
|
+
engineering for debugging such modifications, if you also do each of
|
|
86
|
+
the following:
|
|
87
|
+
|
|
88
|
+
a) Give prominent notice with each copy of the Combined Work that
|
|
89
|
+
the Library is used in it and that the Library and its use are
|
|
90
|
+
covered by this License.
|
|
91
|
+
|
|
92
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
|
93
|
+
document.
|
|
94
|
+
|
|
95
|
+
c) For a Combined Work that displays copyright notices during
|
|
96
|
+
execution, include the copyright notice for the Library among
|
|
97
|
+
these notices, as well as a reference directing the user to the
|
|
98
|
+
copies of the GNU GPL and this license document.
|
|
99
|
+
|
|
100
|
+
d) Do one of the following:
|
|
101
|
+
|
|
102
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
|
103
|
+
License, and the Corresponding Application Code in a form
|
|
104
|
+
suitable for, and under terms that permit, the user to
|
|
105
|
+
recombine or relink the Application with a modified version of
|
|
106
|
+
the Linked Version to produce a modified Combined Work, in the
|
|
107
|
+
manner specified by section 6 of the GNU GPL for conveying
|
|
108
|
+
Corresponding Source.
|
|
109
|
+
|
|
110
|
+
1) Use a suitable shared library mechanism for linking with the
|
|
111
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
|
112
|
+
a copy of the Library already present on the user's computer
|
|
113
|
+
system, and (b) will operate properly with a modified version
|
|
114
|
+
of the Library that is interface-compatible with the Linked
|
|
115
|
+
Version.
|
|
116
|
+
|
|
117
|
+
e) Provide Installation Information, but only if you would otherwise
|
|
118
|
+
be required to provide such information under section 6 of the
|
|
119
|
+
GNU GPL, and only to the extent that such information is
|
|
120
|
+
necessary to install and execute a modified version of the
|
|
121
|
+
Combined Work produced by recombining or relinking the
|
|
122
|
+
Application with a modified version of the Linked Version. (If
|
|
123
|
+
you use option 4d0, the Installation Information must accompany
|
|
124
|
+
the Minimal Corresponding Source and Corresponding Application
|
|
125
|
+
Code. If you use option 4d1, you must provide the Installation
|
|
126
|
+
Information in the manner specified by section 6 of the GNU GPL
|
|
127
|
+
for conveying Corresponding Source.)
|
|
128
|
+
|
|
129
|
+
5. Combined Libraries.
|
|
130
|
+
|
|
131
|
+
You may place library facilities that are a work based on the
|
|
132
|
+
Library side by side in a single library together with other library
|
|
133
|
+
facilities that are not Applications and are not covered by this
|
|
134
|
+
License, and convey such a combined library under terms of your
|
|
135
|
+
choice, if you do both of the following:
|
|
136
|
+
|
|
137
|
+
a) Accompany the combined library with a copy of the same work based
|
|
138
|
+
on the Library, uncombined with any other library facilities,
|
|
139
|
+
conveyed under the terms of this License.
|
|
140
|
+
|
|
141
|
+
b) Give prominent notice with the combined library that part of it
|
|
142
|
+
is a work based on the Library, and explaining where to find the
|
|
143
|
+
accompanying uncombined form of the same work.
|
|
144
|
+
|
|
145
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
|
146
|
+
|
|
147
|
+
The Free Software Foundation may publish revised and/or new versions
|
|
148
|
+
of the GNU Lesser General Public License from time to time. Such new
|
|
149
|
+
versions will be similar in spirit to the present version, but may
|
|
150
|
+
differ in detail to address new problems or concerns.
|
|
151
|
+
|
|
152
|
+
Each version is given a distinguishing version number. If the
|
|
153
|
+
Library as you received it specifies that a certain numbered version
|
|
154
|
+
of the GNU Lesser General Public License "or any later version"
|
|
155
|
+
applies to it, you have the option of following the terms and
|
|
156
|
+
conditions either of that published version or of any later version
|
|
157
|
+
published by the Free Software Foundation. If the Library as you
|
|
158
|
+
received it does not specify a version number of the GNU Lesser
|
|
159
|
+
General Public License, you may choose any version of the GNU Lesser
|
|
160
|
+
General Public License ever published by the Free Software Foundation.
|
|
161
|
+
|
|
162
|
+
If the Library as you received it specifies that a proxy can decide
|
|
163
|
+
whether future versions of the GNU Lesser General Public License shall
|
|
164
|
+
apply, that proxy's public statement of acceptance of any version is
|
|
165
|
+
permanent authorization for you to choose that version for the
|
|
166
|
+
Library.
|
|
Binary file
|
|
Binary file
|