@freely01/opencode-notify 0.3.0 → 0.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 CHANGED
@@ -1,35 +1,20 @@
1
1
  # opencode-notify
2
2
 
3
- opencode 通知插件 — 监听会话中的关键事件,通过多渠道推送通知到你的手机、群聊或桌面。
4
-
5
- > ⚠️ **由AI辅助生成,内容及程序请辨别使用**
6
- >
7
- > **个人项目,按需使用**
8
- >
9
- > 此插件主要面向作者个人使用场景开发和测试,不一定适合所有用户和环境。
10
- >
11
- > **AI 提示:**
12
- > - Windows 系统通知需额外安装 [BurntToast](https://github.com/Windos/BurntToast) PowerShell 模块
13
- > - Linux 系统通知需 `libnotify` 包(桌面发行版通常预装)
14
- > - 事件映射基于 @opencode-ai/plugin@1.15.12 的行为,后续版本升级可能影响兼容性
15
- > - `run_completed` 事件暂未实现(opencode 无直接完成事件)
16
- > - 屏幕跑马灯效果:Linux X11(Python + PyGObject)/ Windows(PowerShell + WinForms)四边彩色流动
17
- > - 仅在 Ubuntu 24.04 (X11) 环境下测试并使用,其它平台未验证
18
- >
19
- > 如有问题欢迎提 Issue,但不保证及时响应和修复。
20
-
21
- ## 功能特性
22
-
23
- - 监听 `permission_required` / `input_required` / `run_failed` 等事件
24
- - 多渠道通知:系统通知、企业微信、飞书、自定义 Webhook(Gotify / Bark / PushDeer 等)
25
- - YAML 配置文件,每项参数均有详细注释
26
- - 去重机制:同一事件在时间窗口内不重复发送
27
- - 会话感知抑制:活跃会话按事件类型智能过滤,不遗漏 `run_failed` 等重要通知
28
- - 零外部运行时依赖(仅 js-yaml 用于配置解析)
29
- - **屏幕跑马灯**:通知时屏幕四边高亮闪烁(Linux X11,Python + GTK 内置;Windows 实验性,PowerShell + WinForms)
3
+ opencode 通知插件 — 监听会话关键事件,通过多渠道推送通知到手机、群聊或桌面。
4
+
5
+ > ⚠️ **个人项目,按需使用** — 主要在 Ubuntu 24.04 (X11) 环境测试,其他平台可能存在问题。
6
+ > 如有问题欢迎提 [Issue](https://github.com/luyanfeng/opencode-notify/issues),不保证及时响应和修复。
7
+
8
+ ## 功能一览
9
+
10
+ - **多渠道通知**:系统通知 + 屏幕跑马灯 + 企业微信 + 飞书 + 自定义 Webhook
11
+ - **会话感知抑制**:活跃会话智能过滤冗余通知,不遗漏 `run_failed` 等重要事件
12
+ - **Terminator 子屏检测**:自动识别子屏最大化遮挡场景,被隐藏的会话强制通知
13
+ - **远程延迟推送**:正常通知发出后,对远程渠道额外延迟补偿,防止错过
14
+ - **去重机制**:同一事件在时间窗口内不重复发送
30
15
  - **渠道级事件过滤**:每个渠道可独立配置监听哪些事件,灵活分流
31
- - **远程延迟推送**:正常通知发出后,指定渠道额外延迟推送以防遗漏
32
- - **Terminator 子屏检测**:自动检测子屏最大化场景,被遮挡的会话强制通知
16
+ - **零外部运行时依赖**:仅 `js-yaml` 用于配置解析
17
+ - **诊断 CLI**:验证配置、发送测试通知、调试事件流
33
18
 
34
19
  ## 平台支持
35
20
 
@@ -37,93 +22,84 @@ opencode 通知插件 — 监听会话中的关键事件,通过多渠道推送
37
22
  |------|:-----:|:-----:|:-------:|
38
23
  | 插件核心(事件监听/路由/分发) | ✅ | ✅ | ✅ |
39
24
  | 自定义 Webhook / 企业微信 / 飞书 | ✅ | ✅ | ✅ |
40
- | 诊断 CLI (`bun cli.ts`) | ✅ | ✅ | ✅ |
41
- | **系统消息通知** | ✅ `osascript` 内置 | ⚠️ 需 `libnotify` | ⚠️ BurntToast 模块 |
42
- | **屏幕跑马灯** | ❌ | ✅ Python+GTK 内置 | ✅ PowerShell+WinForms |
43
-
44
- **说明:**
45
- - **macOS**: 系统通知使用 `osascript`,系统内置,开箱即用
46
- - **Linux**: 系统通知使用 `notify-send`,来自 `libnotify`。桌面发行版通常预装,如缺失可 `apt install libnotify-bin` / `yum install libnotify`
47
- - **Windows**: 系统通知使用 PowerShell `New-BurntToastNotification`,需额外安装 [BurntToast](https://github.com/Windos/BurntToast) 模块。Webhook 渠道不受影响
48
- - **屏幕跑马灯**: Linux X11 使用 Python + PyGObject(GTK 3) 创建透明覆盖窗口,60fps 彩色四边跑马灯动画;Windows 使用 PowerShell + .NET WinForms 创建屏幕四边彩色闪烁边框(实验性)。中间完全透明可点击穿透,不影响操作。macOS 暂不支持
49
- - 非系统通知模块(Webhook 推送、CLI 诊断)均为纯 HTTP/Node API,全平台一致
25
+ | 诊断 CLI | ✅ | ✅ | ✅ |
26
+ | **系统消息通知** | ✅ `osascript` | ⚠️ 需 `libnotify` | WinRT Native Toast |
27
+ | **屏幕跑马灯** | ❌ | ✅ Ubuntu 24.04 X11 | ✅ PowerShell+WinForms |
50
28
 
51
- > **已测试渠道:** 系统通知、企业微信、自定义 Webhook(Gotify)。飞书等其他渠道理论可用,暂未做验证。
29
+ > **已测试渠道:** 系统通知、企业微信、自定义 Webhook(Gotify)。飞书等渠道理论可用,暂未验证。
52
30
 
53
31
  ## 快速开始
54
32
 
55
33
  ### 1. 安装
56
34
 
57
- 将插件添加到 `~/.config/opencode/opencode.json` 的 `plugin` 列表中:
58
-
59
- **方式一:从 npm 安装(推荐)**
60
35
  ```bash
61
36
  npm install -g @freely01/opencode-notify
62
37
  ```
63
38
 
64
- ```json
65
- {
66
- "plugin": ["@freely01/opencode-notify"]
67
- }
68
- ```
39
+ 详细安装方式(npm / 本地 / Bun)及平台依赖说明 → [doc/install.md](doc/install.md)
40
+
41
+ ### 2. 注册插件
42
+
43
+ 编辑 `~/.config/opencode/opencode.json`:
69
44
 
70
- **方式二:本地路径(开发调试)**
71
45
  ```json
72
46
  {
73
- "plugin": [
74
- "file:///home/<你的用户名>/path/to/opencode-notify/index.ts"
75
- ]
47
+ "plugin": ["@freely01/opencode-notify"]
76
48
  }
77
49
  ```
78
50
 
79
- > 本地路径替换为你实际存放项目的目录。
80
-
81
- ### 2. 配置
51
+ ### 3. 配置
82
52
 
83
- 创建 `~/.config/opencode/opencode-notify.yaml`,完整示例:
53
+ 首次启动自动生成默认配置 `~/.config/opencode/opencode-notify.yaml`,或参考项目中的 `opencode-notify.yaml.example`。
84
54
 
85
55
  ```yaml
86
56
  channels:
87
57
  system_message:
88
58
  enabled: true
89
- # events: [permission_required, input_required] # 可选,不填继承全局
90
-
91
- screen_flash:
59
+ wechat_work:
92
60
  enabled: true
93
- duration: 3.5
94
- speed: 5.0
95
- intensity: 0.85
96
-
61
+ webhook_url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx"
97
62
  custom_webhook:
98
- enabled: false
63
+ enabled: true
99
64
  url: "https://gotify.example.com/message"
100
65
  headers:
101
66
  X-Gotify-Key: "your-app-token"
102
- template: '{"title":"{{title}}","message":"{{body}}","priority":5}'
103
-
104
- wechat_work:
105
- enabled: false
106
- webhook_url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx"
107
-
108
- feishu:
109
- enabled: false
110
- webhook_url: "https://open.feishu.cn/open-apis/bot/v2/hook/xxx"
111
67
 
112
68
  events:
113
69
  - permission_required
114
70
  - input_required
115
71
  - run_failed
116
72
 
117
- dedupe_seconds: 60
118
- suppress_when_active: false
119
- activity_timeout_ms: 30000
73
+ suppress_when_active: true
74
+ activity_timeout_ms: 15000
75
+ suppress_events_when_active:
76
+ - permission_required
77
+ - input_required
78
+
79
+ log:
80
+ level: info
120
81
  ```
121
82
 
122
- ## 通知渠道
83
+ 完整配置项参考 → [配置参考](#配置参考)
84
+
85
+ ### 4. 验证
86
+
87
+ ```bash
88
+ # 查看插件加载日志
89
+ tail -f ~/.opencode-notify/plugin.log
90
+
91
+ # 使用诊断 CLI 发送测试通知
92
+ bun run cli.ts --test
93
+
94
+ # 查看配置概览
95
+ bun run cli.ts --config
96
+ ```
97
+
98
+ ---
123
99
 
124
- 每个渠道可以独立配置监听的事件,不填则继承全局 `events` 配置。
100
+ ## 通知渠道
125
101
 
126
- 可选事件值与全局 `events` 一致:
102
+ 每个渠道可独立配置监听事件,不填则继承全局 `events`。
127
103
 
128
104
  | 事件值 | 说明 |
129
105
  |--------|------|
@@ -132,69 +108,42 @@ activity_timeout_ms: 30000
132
108
  | `run_failed` | 任务执行失败 |
133
109
  | `run_completed` | 任务执行完成(技术预留,暂未实现) |
134
110
 
135
- ```yaml
136
- # 自定义 Webhook 只推送权限请求和错误,不推送等待输入
137
- custom_webhook:
138
- enabled: true
139
- events:
140
- - permission_required
141
- - run_failed
142
- ```
143
-
144
111
  ### 系统消息通知
145
112
 
146
- 弹出操作系统原生通知横幅。
147
-
148
113
  | 平台 | 实现 |
149
114
  |------|------|
150
- | macOS | `osascript` (display notification) |
151
- | Linux | `notify-send`(需安装 `libnotify`) |
152
- | Windows | PowerShell (New-BurntToastNotification) |
115
+ | macOS | `osascript`(内置) |
116
+ | Linux | `notify-send`(需 `libnotify`) |
117
+ | Windows | WinRT Native Toast |
153
118
 
154
119
  ### 屏幕跑马灯
155
120
 
156
- 通知时在屏幕四边生成彩色高亮闪烁效果(跑马灯),视觉上更醒目:
121
+ 通知时屏幕四边彩色高亮闪烁,视觉更醒目。
157
122
 
158
123
  ![跑马灯效果](doc/de.png)
159
124
 
160
- - 独立渠道,可与系统通知分开启停、分开配置事件过滤
161
- - Linux X11: 使用 Python + PyGObject(GTK 3) 创建透明覆盖窗口,60fps 彩色灯光沿四边循环运动
162
- - Windows: 使用 PowerShell + .NET WinForms 创建屏幕四边彩色闪烁边框(8px 宽),中间完全透明可点击穿透,不阻挡任何操作
163
- - 非阻塞执行,不影响通知发送速度
164
-
165
125
  ```yaml
166
126
  screen_flash:
167
127
  enabled: true
168
- # events: [run_failed] # 可选,不填继承全局
169
- duration: 3.0 # 持续秒数(默认 3.0)
170
- speed: 4.0 # 移动速度因子(默认 4.0
171
- intensity: 0.9 # 不透明度 0.0~1.0(默认 0.9)
128
+ duration: 3.0 # 持续秒数
129
+ speed: 4.0 # 移动速度因子
130
+ intensity: 0.9 # 不透明度 0.0~1.0
172
131
  ```
173
132
 
174
- ### 自定义 Webhook
175
-
176
- 通用 HTTP POST 发送器,支持任意 Webhook 服务。
177
-
178
- **支持的服务举例:**
133
+ - **Ubuntu 24.04 X11**: Python + PyGObject(GTK 3) 创建透明覆盖窗口,60fps 彩色灯光沿四边循环运动
134
+ - **Windows**: PowerShell + .NET WinForms 创建屏幕四边彩色闪烁边框(8px 宽),中间透明可点击穿透
135
+ - 非阻塞执行,不影响通知发送速度
179
136
 
180
- | 服务 | 文档 |
181
- |------|------|
182
- | Gotify | [gotify.net](https://gotify.net/) |
183
- | Bark | [github.com/Finb/Bark](https://github.com/Finb/Bark) |
184
- | PushDeer | [pushdeer.com](https://pushdeer.com/) |
185
- | Slack Webhook | [api.slack.com/messaging/webhooks](https://api.slack.com/messaging/webhooks) |
186
- | Discord Webhook | [support.discord.com](https://support.discord.com/hc/en-us/articles/228383668) |
137
+ ### 自定义 Webhook
187
138
 
188
- **配置参数:**
139
+ 通用 HTTP 发送器,支持任意 Webhook 服务(Gotify / Bark / PushDeer / Slack / Discord 等)。
189
140
 
190
141
  | 参数 | 说明 |
191
142
  |------|------|
192
143
  | `url` | Webhook 地址 |
193
- | `method` | 请求方法 `POST` / `GET`,默认 `POST` |
194
- | `headers` | 自定义请求头(如 `X-Gotify-Key`) |
195
- | `template` | 消息模板,支持占位符 `{{title}}` `{{body}}` `{{event}}` `{{agent}}` `{{sessionID}}` |
196
-
197
- **Gotify 配置示例:**
144
+ | `method` | `POST` / `GET`,默认 `POST` |
145
+ | `headers` | 自定义请求头 |
146
+ | `template` | 消息模板,支持 `{{title}}` `{{body}}` `{{event}}` `{{agent}}` `{{sessionID}}` |
198
147
 
199
148
  ```yaml
200
149
  custom_webhook:
@@ -210,110 +159,77 @@ custom_webhook:
210
159
 
211
160
  通过群机器人 Webhook 发送 Markdown 消息。
212
161
 
213
- **配置步骤:**
214
-
215
- 1. 在企业微信群中添加群机器人
216
- 2. 复制 Webhook URL
217
- 3. 填入配置文件
218
-
219
162
  ```yaml
220
163
  wechat_work:
221
164
  enabled: true
222
165
  webhook_url: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx"
223
166
  ```
224
167
 
225
- 消息格式:Markdown,实际发送的请求体:
226
-
227
- ```json
228
- {
229
- "msgtype": "markdown",
230
- "markdown": {
231
- "content": "**事件标题**\n\n事件详情...\n> 会话: sessionID"
232
- }
233
- }
234
- ```
235
-
236
168
  消息包含:标题(加粗)、事件详情、会话 ID。
237
169
 
238
170
  ### 飞书
239
171
 
240
172
  通过自定义机器人或流程触发器 Webhook 发送卡片消息。
241
173
 
242
- **配置步骤:**
243
-
244
- 1. 在飞书群中添加自定义机器人(或创建流程触发器)
245
- 2. 复制 Webhook URL
246
- 3. 填入配置文件
247
-
248
174
  ```yaml
249
175
  feishu:
250
176
  enabled: true
251
177
  webhook_url: "https://open.feishu.cn/open-apis/bot/v2/hook/xxx"
252
178
  ```
253
179
 
254
- 消息格式:卡片消息(interactive),实际发送的请求体:
255
-
256
- ```json
257
- {
258
- "msg_type": "interactive",
259
- "card": {
260
- "header": {
261
- "title": { "tag": "plain_text", "content": "事件标题" }
262
- },
263
- "elements": [
264
- { "tag": "markdown", "content": "事件详情..." },
265
- { "tag": "hr" },
266
- { "tag": "note", "elements": [{ "tag": "plain_text", "content": "会话: sessionID" }] }
267
- ]
268
- }
269
- }
270
- ```
271
-
272
- 消息包含:标题头、正文(Markdown)、分割线、脚注(会话 ID)。
180
+ 消息格式:interactive 卡片消息,包含标题头、Markdown 正文、分割线、脚注。
273
181
 
274
182
  ### 远程延迟推送
275
183
 
276
- 正常通知发出后,如果用户长时间未操作(未回到 opencode TUI),针对指定渠道额外再推送一次。
277
- 用户在延迟期间回到 TUI 操作 → 自动取消该会话所有待发延迟通知。
278
-
279
- **适用场景:** 用户离开电脑后,系统通知可能一闪而过没看到;延迟推送在用户仍未回来时再次尝试发出。
184
+ 正常通知发出后,对指定渠道额外延迟补偿推送。用户在延迟期间回到 TUI 操作 → 自动取消。
280
185
 
281
186
  ```yaml
282
- remote_delay_channels: # 哪些渠道需要额外延迟推送(空=不启用)
283
- - system_message
187
+ remote_delay_channels:
284
188
  - wechat_work
285
- - feishu
286
189
  - custom_webhook
287
- remote_delay_seconds: 60 # 延迟秒数(默认 60)
288
- remote_delay_max_count: 3 # 最多重复次数(默认 3)
190
+ remote_delay_seconds: 60
191
+ remote_delay_max_count: 3
289
192
  ```
290
193
 
291
- **注意:**
292
- - `remote_delay_channels` 仅影响**额外延迟推送**,不影响正常通知(正常通知该发就发)
293
- - 延迟推送使用渠道自身的事件过滤规则,只有渠道订阅的事件才会延迟推送
294
- - 用户在该会话中进行任何操作(输入消息、回应权限、执行命令等)都会取消该会话所有待发延迟
295
- - 达到 `remote_delay_max_count` 次后停止推送
194
+ **注意:** 仅影响额外延迟推送,不影响正常通知(该发就发)。用户在该会话中操作(输入消息、回应权限等)自动取消所有待发延迟。
296
195
 
297
- ### Terminator 子屏遮挡检测(自动)
196
+ ---
298
197
 
299
- 当用户在 **Terminator** 中最大化某个子屏幕时(`Ctrl+Shift+X`),
300
- 其他子屏幕中的 opencode 会话虽活跃但被遮挡。插件自动检测该场景,
301
- 被遮挡的会话即使活跃也会强制发送通知。
198
+ ## Terminator 子屏遮挡检测
302
199
 
303
- **检测原理**:`$TERMINATOR_UUID` + `$TERMINATOR_DBUS_NAME` + `$TERMINATOR_DBUS_PATH`
304
- 环境变量确认在 Terminator 中 → DBus 查询焦点终端 UUID → 与本屏对比
305
- → 不一致则强制通知,无需额外配置。
200
+ > 详细设计原理 [doc/features.md](doc/features.md#4-terminator-子屏幕遮挡检测)
306
201
 
307
- **外部依赖**(任一即可):`busctl`(systemd 内置)| `python3-dbus` | `gdbus` | `xdotool`
202
+ 当你在 Terminator 中最大化某个子屏幕时(`Ctrl+Shift+X`),其他子屏被完全遮挡。插件自动检测此场景,被遮挡的会话即使活跃也强制发送通知。
203
+
204
+ **检测策略:**
205
+
206
+ ```
207
+ 事件被活跃抑制
208
+
209
+ Terminator 窗口是当前 X 活跃窗口? ← xdotool + xprop
210
+ 否 → 用户在别的应用中(浏览器/IDE)→ 强制通知
211
+ 是 → 本屏可见 → 正常抑制
212
+ ```
213
+
214
+ **外部依赖**(检测失败自动降级,不影响正常抑制):
215
+
216
+ | 工具 | 用途 | 安装 |
217
+ |------|------|------|
218
+ | `xdotool` | 获取活跃窗口 ID | `apt install xdotool` |
219
+ | `xprop` | 查询窗口 WM_CLASS | 系统自带 |
220
+
221
+ ---
308
222
 
309
223
  ## 事件映射
310
224
 
311
- | 通知事件 | 触发场景 | 对应 opencode 事件 |
312
- |----------|---------|-------------------|
313
- | `permission_required` | Agent 需要授权(执行命令、读写文件等) | `permission.asked` / `question.asked` |
314
- | `input_required` | Agent 等待用户输入 | `session.idle` / `session.status` (idle) |
225
+ | 通知事件 | 触发场景 | opencode 事件 |
226
+ |----------|---------|---------------|
227
+ | `permission_required` | Agent 需要授权 | `permission.asked` / `question.asked` |
228
+ | `input_required` | Agent 等待用户输入 | `session.idle` / `session.status(idle)` |
315
229
  | `run_failed` | 任务执行失败 | `session.error` |
316
- | `run_completed` | 任务完成(预留,暂未实现) | 待组合判断 |
230
+ | `run_completed` | 任务完成(预留) | |
231
+
232
+ ---
317
233
 
318
234
  ## 配置参考
319
235
 
@@ -332,191 +248,121 @@ YAML 文件 > plugin options (opencode.json) > 默认值
332
248
  ```yaml
333
249
  channels:
334
250
  system_message:
335
- enabled: true # 系统通知开关
336
- events: [] # 可选,渠道级事件过滤(不填继承全局)
337
- # 可选值: permission_required | input_required | run_completed | run_failed
338
- screen_flash: # 屏幕跑马灯(仅 Linux X11)
339
- enabled: true # 开启(默认 false)
340
- events: [] # 可选,渠道级事件过滤(不填继承全局)
341
- # 可选值: permission_required | input_required | run_completed | run_failed
342
- duration: 3.5 # 持续秒数
343
- speed: 5.0 # 移动速度因子
344
- intensity: 0.85 # 不透明度 0.0~1.0
345
-
251
+ enabled: true
252
+ events: []
253
+ screen_flash:
254
+ enabled: false
255
+ duration: 3.5
256
+ speed: 5.0
257
+ intensity: 0.85
346
258
  custom_webhook:
347
- enabled: false # 自定义 Webhook 开关
348
- events: [] # 可选,渠道级事件过滤
349
- # 可选值: permission_required | input_required | run_completed | run_failed
350
- url: "" # Webhook 地址
351
- method: "POST" # 请求方法 POST | GET
352
- headers: {} # 自定义请求头
353
- template: "" # 消息模板
354
-
259
+ enabled: false
260
+ url: ""
261
+ method: "POST"
262
+ headers: {}
263
+ template: ""
355
264
  wechat_work:
356
- enabled: false # 企业微信开关
357
- events: [] # 可选,渠道级事件过滤
358
- # 可选值: permission_required | input_required | run_completed | run_failed
359
- webhook_url: "" # 群机器人 Webhook URL
360
- # 消息格式: Markdown (msgtype=markdown, markdown.content)
361
-
265
+ enabled: false
266
+ webhook_url: ""
362
267
  feishu:
363
- enabled: false # 飞书开关
364
- events: [] # 可选,渠道级事件过滤
365
- # 可选值: permission_required | input_required | run_completed | run_failed
366
- webhook_url: "" # 机器人/流程触发器 Webhook URL
367
- # 消息格式: 卡片消息 (msg_type=interactive, card)
368
-
369
- events: # 订阅的事件列表
370
- # 可选值: permission_required | input_required | run_completed | run_failed
371
-
372
- dedupe_seconds: 60 # 去重时间窗口(秒)
373
- suppress_when_active: true # 会话感知抑制开关(按 suppress_events 列表过滤)
374
- activity_timeout_ms: 15000 # 会话活跃超时(毫秒),超过此时间无操作视为离开
375
- suppress_events_when_active: # 活跃时抑制哪些事件(不填=默认列表)
376
- - permission_required
377
- - input_required
378
- # run_failed / run_completed 不在列表中 → 始终通知
379
- session_stale_timeout_ms: 600000 # 超时会话自动淘汰(毫秒),默认 10 分钟
380
- remote_delay_channels: [] # 远程延迟推送渠道列表(空=不启用)
381
- # 可选值: system_message, screen_flash, wechat_work, feishu, custom_webhook
382
- remote_delay_seconds: 60 # 远程延迟秒数(默认 60)
383
- remote_delay_max_count: 3 # 远程延迟最多重复次数(默认 3)
384
- log: # 日志配置
385
- level: info # 等级: error | warn | info | debug(默认 info)
386
- # error - 仅记录错误
387
- # warn - 错误 + 警告
388
- # info - 错误 + 警告 + 常规信息(推荐)
389
- # debug - 全部日志(排查时使用)
390
- # file: "~/.opencode-notify/plugin.log" # 日志文件路径(可选,默认同上)
391
- ```
392
-
393
- ### 活跃抑制
394
-
395
- 当用户正在 opencode TUI 中操作(输入消息、回应权限等),通知可能冗余(屏上已可见)。
396
- 插件通过**会话感知抑制**解决:追踪每个会话的用户操作时间戳,活跃会话按事件类型选择性过滤。
397
-
398
- **检测的用户操作事件:**
399
- `message.updated` / `permission.replied` / `question.replied` / `command.executed` / `tui.command.execute`
400
-
401
- **抑制规则:**
402
-
403
- | 通知事件 | 活跃时默认行为 | 理由 |
404
- |---------|:------------:|------|
405
- | `permission_required` | ✅ 抑制 | 权限弹窗就在屏幕上 |
406
- | `input_required` | ✅ 抑制 | TUI 明确在等输入 |
407
- | `run_failed` | ❌ 不抑制 | 异步结果,人可能走开 |
408
- | `run_completed` | ❌ 不抑制 | 同上 |
268
+ enabled: false
269
+ webhook_url: ""
409
270
 
410
- **配置示例:**
271
+ events: [permission_required, input_required, run_failed]
272
+ dedupe_seconds: 60
411
273
 
412
- ```yaml
413
- suppress_when_active: true # 开启会话感知抑制
414
- activity_timeout_ms: 15000 # 15 秒无操作视为不活跃
415
- suppress_events_when_active: # 活跃时抑制哪些事件
274
+ suppress_when_active: true
275
+ activity_timeout_ms: 15000
276
+ suppress_events_when_active:
416
277
  - permission_required
417
278
  - input_required
418
- session_stale_timeout_ms: 600000 # 10 分钟无活动自动淘汰会话(防内存泄漏)
279
+ session_stale_timeout_ms: 600000
280
+
281
+ remote_delay_channels: []
282
+ remote_delay_seconds: 60
283
+ remote_delay_max_count: 3
284
+
285
+ log:
286
+ level: info
419
287
  ```
420
288
 
421
- ## 日志与故障排查
289
+ ### 活跃抑制
422
290
 
423
- 插件日志位于 `~/.opencode-notify/plugin.log`(可通过 `log.file` 配置自定义)。
291
+ 当用户在 opencode TUI 中操作时,屏上已可见的通知被智能过滤。追踪的用户操作事件:`message.updated` / `permission.replied` / `question.replied` / `command.executed` / `tui.command.execute`。
424
292
 
425
- 日志等级由 `log.level` 控制:
293
+ | 事件 | 活跃时行为 |
294
+ |------|:---------:|
295
+ | `permission_required` | ✅ 抑制(屏上可见) |
296
+ | `input_required` | ✅ 抑制(TUI 在等输入) |
297
+ | `run_failed` | ❌ 不抑制(异步结果) |
298
+ | `run_completed` | ❌ 不抑制(预留) |
426
299
 
427
- | 等级 | 包含内容 | 建议用途 |
428
- |------|---------|---------|
429
- | `error` | 仅错误 | 生产环境,只关心失败 |
430
- | `warn` | 错误 + 警告 | 生产环境,关注潜在问题 |
431
- | `info` | 错误 + 警告 + 常规信息 | 日常运行(默认) |
432
- | `debug` | 全部日志(含详细事件流) | 排查问题 |
300
+ ---
433
301
 
434
- ```yaml
435
- log:
436
- level: info # 推荐:日常使用记录所有关键信息
437
- # level: debug # 排查问题时改为 debug
438
- # file: "~/.opencode-notify/plugin.log" # 可选自定义路径
439
- ```
302
+ ## 日志与故障排查
440
303
 
441
- 日志包含:
304
+ 日志位于 `~/.opencode-notify/plugin.log`(可通过 `log.file` 自定义)。
442
305
 
443
- - 插件加载信息(配置、已启用渠道、日志等级)
444
- - 渠道发送失败 / 成功
445
- - 会话活跃跳过通知
446
- - 远程延迟推送调度 / 取消 / 推送
447
- - 去重命中、状态存储异常等诊断信息
306
+ | 等级 | 内容 | 用途 |
307
+ |------|------|------|
308
+ | `error` | 渠道失败、异常 | 生产环境 |
309
+ | `warn` | 配置异常、部分失败 | 生产环境 |
310
+ | `info` | 渠道启用、通知分发、抑制决策 | 日常运行(默认) |
311
+ | `debug` | 全部事件流、DBus 调用详情 | 排查问题 |
448
312
 
449
313
  ```bash
450
- # 实时查看日志
451
314
  tail -f ~/.opencode-notify/plugin.log
452
-
453
- # 查看最近的插件加载信息
454
315
  grep "插件已加载" ~/.opencode-notify/plugin.log
455
-
456
- # 只看错误
457
316
  grep "\[ERROR\]" ~/.opencode-notify/plugin.log
458
-
459
- # 只看警告
460
- grep "\[WARN\]" ~/.opencode-notify/plugin.log
461
317
  ```
462
318
 
463
319
  ### 常见问题
464
320
 
465
321
  **Q: 插件未加载?**
466
- 确认 `opencode.json` 中 `plugin` 列表的路径正确,指向 `index.ts` 文件。
322
+ 确认 `opencode.json` 中 `plugin` 路径正确,指向 `index.ts`。
467
323
 
468
324
  **Q: 通知没有弹出?**
469
- 1. 检查日志中是否出现 `[event] type=` 行,确认事件是否被监听到
470
- 2. 检查渠道是否已 `enabled: true`
471
- 3. 检查 Webhook URL 是否正确
472
- 4. 查看 `~/.opencode-notify/plugin.log` 中是否有错误信息
325
+ 检查日志是否有 `[event] type=` 行确认事件被监听到;检查渠道 `enabled: true`;检查 Webhook URL 正确性。
473
326
 
474
327
  **Q: 企业微信/飞书通知失败?**
475
- 确认 Webhook URL 有效,网络可达。可通过 `curl` 直接测试:
328
+ 确认 Webhook URL 有效,网络可达:
476
329
 
477
330
  ```bash
478
- curl -X POST <webhook_url> -H "Content-Type: application/json" -d '{"msgtype":"markdown","markdown":{"content":"**测试**"}}'
331
+ curl -X POST <webhook_url> -H "Content-Type: application/json" \
332
+ -d '{"msgtype":"markdown","markdown":{"content":"**测试**"}}'
479
333
  ```
480
334
 
335
+ ---
336
+
481
337
  ## 项目结构
482
338
 
483
339
  ```
484
340
  opencode-notify/
485
- ├── index.ts # 插件入口
486
- ├── cli.ts # 诊断工具
487
- ├── config.ts # 配置解析(YAML + plugin options)
488
- ├── events.ts # 事件路由
489
- ├── log.ts # 共享日志模块
490
- ├── session-tracker.ts # 会话感知抑制
491
- ├── terminator-detect.ts # Terminator 子屏最大化检测
492
- ├── delayed-dispatcher.ts # 远程延迟推送调度器
493
- ├── dispatcher.ts # 去重分发
494
- ├── store.ts # 状态存储
495
- ├── message.ts # 消息模型
341
+ ├── index.ts # 插件入口
342
+ ├── cli.ts # 诊断工具
343
+ ├── config.ts # 配置解析
344
+ ├── events.ts # 事件路由
345
+ ├── log.ts # 日志模块
346
+ ├── message.ts # 消息模型
347
+ ├── session-tracker.ts # 会话感知抑制
348
+ ├── terminator-detect.ts # Terminator 子屏遮挡检测
349
+ ├── delayed-dispatcher.ts # 远程延迟推送
350
+ ├── dispatcher.ts # 去重分发
351
+ ├── store.ts # 状态存储
496
352
  ├── doc/
497
- │ ├── de.png # 跑马灯效果截图
498
- └── features.md # 功能说明(含 Terminator 友好体验说明)
353
+ │ ├── install.md # 安装指南
354
+ ├── features.md # 功能详解
355
+ │ └── de.png # 跑马灯效果截图
499
356
  ├── scripts/
500
- │ └── marquee.py # 屏幕跑马灯效果(Python+GTK)
357
+ │ └── marquee.py # 屏幕跑马灯脚本
501
358
  ├── senders/
502
- │ ├── types.ts # Sender 接口
503
- │ ├── system/ # 系统通知(平台分包)
504
- ├── index.ts # 注册表 + 自动选择平台
505
- ├── darwin.ts # macOS (osascript)
506
- ├── linux.ts # Linux (notify-send)
507
- └── win32.ts # Windows (PowerShell)
508
- ├── screen-flash/ # 屏幕跑马灯(Linux + Windows)
509
- │ │ ├── index.ts # 入口 + 平台路由
510
- │ │ ├── linux.ts # Linux 实现 (Python+GTK)
511
- │ │ └── win32.ts # Windows 实现 (PowerShell+WinForms)
512
- │ ├── custom-webhook.ts # 自定义 Webhook
513
- │ ├── wechat-work.ts # 企业微信
514
- │ └── feishu.ts # 飞书
515
- ├── findings.md # 研究记录
516
- ├── plan.md # 需求与实现计划
517
- ├── task_plan.md # 任务跟踪
518
- ├── progress.md # 进度日志
359
+ │ ├── types.ts # Sender 接口
360
+ │ ├── system/ # 系统通知
361
+ │ ├── screen-flash/ # 屏幕跑马灯
362
+ │ ├── custom-webhook.ts
363
+ │ ├── wechat-work.ts
364
+ │ └── feishu.ts
365
+ ├── opencode-notify.yaml.example
519
366
  ├── package.json
520
367
  └── tsconfig.json
521
368
  ```
522
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@freely01/opencode-notify",
3
- "version": "0.3.0",
3
+ "version": "0.4.0",
4
4
  "description": "opencode 通知插件 - 监听会话事件并通过多渠道推送通知",
5
5
  "type": "module",
6
6
  "main": "index.ts",
@@ -1,17 +1,40 @@
1
1
  /**
2
- * macOS 系统通知
2
+ * macOS 系统通知(⚠️ 未实际测试)
3
3
  *
4
4
  * 使用 osascript 调用原生通知中心。
5
- * - display notification: 弹窗 + 声音
5
+ * - display notification: 标题 + 副标题 + 正文 + 声音
6
6
  * - 系统内置,无需额外安装
7
+ * - 通知自动进入通知中心
8
+ *
9
+ * AppleScript 双引号使用 `""` 转义(与 shell/JScript 的 `\"` 不同)。
7
10
  */
8
11
 
9
12
  import { execSync } from "node:child_process"
10
13
 
11
14
  export async function notify(title: string, body: string): Promise<void> {
12
- const script = `display notification "${body}" with title "${title}" sound name "default"`
15
+ // sanitize() " 转义为 \",但 AppleScript 使用 "" 表示双引号。
16
+ // 此处先还原,再应用 AppleScript 转义。
17
+ const asTitle = aq(title)
18
+ const asBody = aq(body)
19
+
20
+ // 提取 title 中的 [ses_xxx] 前缀作为副标题
21
+ const sessionMatch = title.match(/^(\[[^\]]+\])/)
22
+ const asSubtitle = aq(sessionMatch?.[1] ?? "")
23
+
24
+ const script = `display notification "${asBody}" with title "${asTitle}" subtitle "${asSubtitle}" sound name "default"`
13
25
  execSync(`osascript -e ${JSON.stringify(script)}`, {
14
26
  timeout: 5000,
15
27
  stdio: "ignore",
16
28
  })
17
29
  }
30
+
31
+ /**
32
+ * 转为 AppleScript 安全字符串
33
+ * - 还原 sanitize() 的 \" 转义
34
+ * - 用 AppleScript 的 "" 方式转义双引号
35
+ */
36
+ function aq(s: string): string {
37
+ return s
38
+ .replace(/\\"/g, '"') // 还原 sanitize 转义
39
+ .replace(/"/g, '""') // AppleScript 双引号转义
40
+ }
@@ -1,24 +1,29 @@
1
1
  /**
2
- * Terminator 子屏幕最大化检测
2
+ * Terminator 子屏幕遮挡检测
3
3
  *
4
- * 当用户在 Terminator 中最大化某个子屏幕时(Ctrl+Shift+X),
5
- * 其他子屏幕被遮挡。通过 DBus 查询焦点终端 UUID,
6
- * 与本进程的 TERMINATOR_UUID 比较,判定本屏是否被遮挡。
4
+ * 检测当前子屏幕是否被真正遮挡(不可见),用于判定是否需要
5
+ * 强制通知(即使用户在该会话中活跃)。
7
6
  *
8
- * 检测策略(依次尝试):
9
- * 1. busctl(systemd 自带,Ubuntu 预装)
10
- * 2. python3-dbus(备选)
11
- * 3. gdbus(GLib 备选)
12
- * 4. xdotool 窗口类检测(回退,仅判断是否在 Terminator 窗口中)
7
+ * 检测策略(两级):
8
+ * 1. X 窗口级别:Terminator 是否是当前活跃窗口(xdotool + xprop)
9
+ * → 用户在别的应用中 → 遮挡
10
+ * 2. 子屏级别:用户当前聚焦的是哪个子屏(DBus get_focused_terminal)
11
+ * 聚焦 != 本屏 用户在另一个子屏上 → 遮挡
12
+ *
13
+ * 注意:此版本 Terminator 未暴露 get_maximized_terminal 接口,
14
+ * 无法区分"分屏可见"和"最大化遮挡"。两级结合覆盖核心场景:
15
+ * - Terminator 不活跃(浏览器/IDE):强制通知
16
+ * - 本屏聚焦:不通知
17
+ * - 另一子屏聚焦(分屏或最大化):强制通知
13
18
  */
14
19
 
15
20
  import { execSync } from "node:child_process"
16
- import { warn, debug } from "./log.js"
21
+ import { debug } from "./log.js"
17
22
 
18
23
  /** 当前进程的 TERMINATOR_UUID */
19
24
  const MY_UUID = process.env.TERMINATOR_UUID ?? null
20
25
 
21
- /** Terminator DBus 信息(每实例唯一) */
26
+ /** Terminator DBus 信息 */
22
27
  const DBUS_NAME = process.env.TERMINATOR_DBUS_NAME ?? null
23
28
  const DBUS_PATH = process.env.TERMINATOR_DBUS_PATH ?? null
24
29
 
@@ -26,10 +31,10 @@ const DBUS_PATH = process.env.TERMINATOR_DBUS_PATH ?? null
26
31
  let insideTerminator: boolean | null = null
27
32
 
28
33
  /**
29
- * 检测当前子屏幕是否被遮挡(用户在其他子屏幕上操作)
34
+ * 检测当前子屏幕是否被遮挡(不可见)
30
35
  *
31
- * @returns true → 本子屏幕被遮挡,不应抑制通知
32
- * false → 本子屏幕可见,正常抑制逻辑
36
+ * @returns true → 本子屏幕被遮挡(用户看不到,应强制通知)
37
+ * false → 本子屏幕可见(正常抑制逻辑)
33
38
  * null → 无法确定(不在 Terminator 中或检测失败)
34
39
  */
35
40
  export function isTerminalOccluded(): boolean | null {
@@ -40,57 +45,38 @@ export function isTerminalOccluded(): boolean | null {
40
45
 
41
46
  insideTerminator = true
42
47
 
43
- // 如果没有 DBus 名称/路径信息,直接回退到 xdotool
44
- if (!DBUS_NAME || !DBUS_PATH) {
45
- debug(`Terminator DBus 环境变量不全: name=${DBUS_NAME} path=${DBUS_PATH},回退到 xdotool`)
46
- return callXdotool()
47
- }
48
+ // ─── 第一级:X 窗口级别 ────────────────────────────────────────────────
49
+ // Terminator 窗口是否是当前 X 活跃窗口?
50
+ const terminatorActive = isTerminatorWindowActive()
48
51
 
49
- // 策略 1: busctl
50
- try {
51
- const focused = callBusctl()
52
- if (focused !== null) {
53
- const occluded = !uuidEqual(focused, MY_UUID)
54
- debug(`Terminator 焦点检测(busctl): 本屏=${shortId(MY_UUID)} 焦点=${shortId(focused)} 遮挡=${occluded}`)
55
- return occluded
56
- }
57
- } catch (e) {
58
- debug(`Terminator busctl 失败: ${e instanceof Error ? e.message : String(e)}`)
52
+ if (terminatorActive === false) {
53
+ // 用户在其他应用中(浏览器、IDE 等),本屏不可见
54
+ debug(`Terminator 窗口非活跃(用户在其他应用中),判定为遮挡`)
55
+ return true
59
56
  }
60
57
 
61
- // 策略 2: python3-dbus
62
- try {
63
- const focused = callPythonDBus()
64
- if (focused !== null) {
65
- const occluded = !uuidEqual(focused, MY_UUID)
66
- debug(`Terminator 焦点检测(python): 本屏=${shortId(MY_UUID)} 焦点=${shortId(focused)} 遮挡=${occluded}`)
67
- return occluded
68
- }
69
- } catch (e) {
70
- debug(`Terminator python-dbus 失败: ${e instanceof Error ? e.message : String(e)}`)
71
- }
58
+ if (terminatorActive === true) {
59
+ // ─── 第二级:聚焦终端检测 ──────────────────────────────────────────
60
+ // Terminator 窗口活跃,检查用户聚焦的是不是本屏
61
+ const focused = queryFocusedTerminal()
72
62
 
73
- // 策略 3: gdbus
74
- try {
75
- const focused = callGDBus()
76
63
  if (focused !== null) {
77
- const occluded = !uuidEqual(focused, MY_UUID)
78
- debug(`Terminator 焦点检测(gdbus): 本屏=${shortId(MY_UUID)} 焦点=${shortId(focused)} 遮挡=${occluded}`)
79
- return occluded
64
+ if (focused === MY_UUID) {
65
+ debug(`Terminator 本屏聚焦,判定为可见`)
66
+ return false
67
+ }
68
+ // 用户聚焦在另一个子屏上 → 本屏不可见(无论分屏还是最大化)
69
+ debug(`Terminator 用户聚焦在其他子屏(${shortId(focused)}),判定为遮挡`)
70
+ return true
80
71
  }
81
- } catch (e) {
82
- debug(`Terminator gdbus 失败: ${e instanceof Error ? e.message : String(e)}`)
83
- }
84
72
 
85
- // 策略 4: xdotool 窗口类检测(回退)
86
- try {
87
- const result = callXdotool()
88
- debug(`Terminator 窗口检测(xdotool): ${result}`)
89
- return result
90
- } catch (e) {
91
- warn(`Terminator 所有检测策略均失败,最后错误: ${e instanceof Error ? e.message : String(e)}`)
92
- return null
73
+ // 无法查询焦点状态,保守假设可见
74
+ debug(`Terminator 窗口活跃,无法查询焦点状态,保守假设不遮挡`)
75
+ return false
93
76
  }
77
+
78
+ // 无法确定 X 窗口状态
79
+ return null
94
80
  }
95
81
 
96
82
  /**
@@ -104,61 +90,62 @@ export function isInsideTerminator(): boolean {
104
90
 
105
91
  // ─── 工具函数 ───────────────────────────────────────────────────────────────
106
92
 
107
- /** 比较两个 UUID(忽略 urn:uuid: 前缀) */
108
- function uuidEqual(a: string, b: string): boolean {
109
- const strip = (s: string) => s.replace(/^urn:uuid:/i, "")
110
- return strip(a) === strip(b)
111
- }
112
-
113
93
  /** 取 UUID 前 8 位用于日志 */
114
94
  function shortId(uuid: string): string {
115
95
  return uuid.replace(/^urn:uuid:/i, "").slice(0, 8)
116
96
  }
117
97
 
118
- // ─── 检测策略实现 ───────────────────────────────────────────────────────────
119
-
120
- /** 策略 1: busctl(systemd) */
121
- function callBusctl(): string | null {
122
- const out = execSync(
123
- `busctl --user call ${DBUS_NAME} ${DBUS_PATH} ${DBUS_NAME} get_focused_terminal`,
124
- { encoding: "utf-8", timeout: 3000 },
125
- ).trim()
126
- // 输出格式: s "uuid-string"
127
- const m = out.match(/s\s+"([^"]+)"/)
128
- return m ? m[1] : null
129
- }
98
+ // ─── 第一级:X 窗口检测 ──────────────────────────────────────────────────
130
99
 
131
- /** 策略 2: python3-dbus */
132
- function callPythonDBus(): string | null {
133
- const out = execSync(
134
- `python3 -c "
135
- import dbus
136
- bus = dbus.SessionBus()
137
- proxy = bus.get_object('${DBUS_NAME}', '${DBUS_PATH}')
138
- focused = proxy.get_focused_terminal(dbus_interface='${DBUS_NAME}')
139
- print(focused)
140
- "`,
141
- { encoding: "utf-8", timeout: 5000 },
142
- ).trim()
143
- return out || null
100
+ /**
101
+ * 检测 Terminator 窗口是否是当前 X 活跃窗口
102
+ * @returns true=Terminator 是活跃窗口, false=不是, null=检测失败
103
+ */
104
+ function isTerminatorWindowActive(): boolean | null {
105
+ try {
106
+ const out = execSync(
107
+ `xprop -id $(xdotool getactivewindow) WM_CLASS 2>/dev/null`,
108
+ { encoding: "utf-8", timeout: 5000 },
109
+ ).trim()
110
+ return out.includes('"Terminator"')
111
+ } catch (e) {
112
+ debug(`xprop/xdotool 检测 Terminator 窗口失败: ${e instanceof Error ? e.message : String(e)}`)
113
+ return null
114
+ }
144
115
  }
145
116
 
146
- /** 策略 3: gdbus(GLib) */
147
- function callGDBus(): string | null {
148
- const out = execSync(
149
- `gdbus call --session --dest ${DBUS_NAME} --object-path ${DBUS_PATH} --method ${DBUS_NAME}.get_focused_terminal`,
150
- { encoding: "utf-8", timeout: 3000 },
151
- ).trim()
152
- // 输出格式: ('uuid-string',)
153
- const m = out.match(/\('([^']+)'/)
154
- return m ? m[1] : null
155
- }
117
+ // ─── 第二级:聚焦终端检测 ───────────────────────────────────────────────
118
+
119
+ /**
120
+ * 通过 DBus 查询当前聚焦的子屏 UUID
121
+ * @returns UUID 字符串,或 null(查询失败)
122
+ */
123
+ function queryFocusedTerminal(): string | null {
124
+ if (!DBUS_NAME || !DBUS_PATH) return null
125
+
126
+ // 策略 1: busctl(无 stderr 输出,避免未知方法报错)
127
+ try {
128
+ const out = execSync(
129
+ `busctl --user call ${DBUS_NAME} ${DBUS_PATH} ${DBUS_NAME} get_focused_terminal 2>/dev/null`,
130
+ { encoding: "utf-8", timeout: 3000 },
131
+ ).trim()
132
+ const m = out.match(/s\s+"([^"]+)"/)
133
+ if (m) return m[1]
134
+ } catch {
135
+ // 静默忽略
136
+ }
137
+
138
+ // 策略 2: gdbus(备选)
139
+ try {
140
+ const out = execSync(
141
+ `gdbus call --session --dest ${DBUS_NAME} --object-path ${DBUS_PATH} --method ${DBUS_NAME}.get_focused_terminal 2>/dev/null`,
142
+ { encoding: "utf-8", timeout: 3000 },
143
+ ).trim()
144
+ const m = out.match(/\('([^']+)'/)
145
+ if (m) return m[1]
146
+ } catch {
147
+ // 静默忽略
148
+ }
156
149
 
157
- /** 策略 4: xdotool 窗口类检测 */
158
- function callXdotool(): boolean | null {
159
- const cls = execSync(
160
- `xdotool getactivewindow getwindowclassname`,
161
- { encoding: "utf-8", timeout: 3000 },
162
- ).trim()
163
- return cls === "Terminator" ? true : null
150
+ return null
164
151
  }