@freely01/opencode-notify 0.1.0 → 0.2.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
@@ -24,10 +24,12 @@ opencode 通知插件 — 监听会话中的关键事件,通过多渠道推送
24
24
  - 多渠道通知:系统通知、企业微信、飞书、自定义 Webhook(Gotify / Bark / PushDeer 等)
25
25
  - YAML 配置文件,每项参数均有详细注释
26
26
  - 去重机制:同一事件在时间窗口内不重复发送
27
- - 活跃抑制:检测到用户在操作 TUI 时可自动跳过通知
27
+ - 会话感知抑制:活跃会话按事件类型智能过滤,不遗漏 `run_failed` 等重要通知
28
28
  - 零外部运行时依赖(仅 js-yaml 用于配置解析)
29
29
  - **屏幕跑马灯**:通知时屏幕四边高亮闪烁(Linux X11,Python + GTK 内置)
30
30
  - **渠道级事件过滤**:每个渠道可独立配置监听哪些事件,灵活分流
31
+ - **远程延迟推送**:正常通知发出后,指定渠道额外延迟推送以防遗漏
32
+ - **Terminator 子屏检测**:自动检测子屏最大化场景,被遮挡的会话强制通知
31
33
 
32
34
  ## 平台支持
33
35
 
@@ -270,6 +272,41 @@ feishu:
270
272
 
271
273
  消息包含:标题头、正文(Markdown)、分割线、脚注(会话 ID)。
272
274
 
275
+ ### 远程延迟推送
276
+
277
+ 正常通知发出后,如果用户长时间未操作(未回到 opencode TUI),针对指定渠道额外再推送一次。
278
+ 用户在延迟期间回到 TUI 操作 → 自动取消该会话所有待发延迟通知。
279
+
280
+ **适用场景:** 用户离开电脑后,系统通知可能一闪而过没看到;延迟推送在用户仍未回来时再次尝试发出。
281
+
282
+ ```yaml
283
+ remote_delay_channels: # 哪些渠道需要额外延迟推送(空=不启用)
284
+ - system_message
285
+ - wechat_work
286
+ - feishu
287
+ - custom_webhook
288
+ remote_delay_seconds: 60 # 延迟秒数(默认 60)
289
+ remote_delay_max_count: 3 # 最多重复次数(默认 3)
290
+ ```
291
+
292
+ **注意:**
293
+ - `remote_delay_channels` 仅影响**额外延迟推送**,不影响正常通知(正常通知该发就发)
294
+ - 延迟推送使用渠道自身的事件过滤规则,只有渠道订阅的事件才会延迟推送
295
+ - 用户在该会话中进行任何操作(输入消息、回应权限、执行命令等)都会取消该会话所有待发延迟
296
+ - 达到 `remote_delay_max_count` 次后停止推送
297
+
298
+ ### Terminator 子屏遮挡检测(自动)
299
+
300
+ 当用户在 **Terminator** 中最大化某个子屏幕时(`Ctrl+Shift+X`),
301
+ 其他子屏幕中的 opencode 会话虽活跃但被遮挡。插件自动检测该场景,
302
+ 被遮挡的会话即使活跃也会强制发送通知。
303
+
304
+ **检测原理**:`$TERMINATOR_UUID` + `$TERMINATOR_DBUS_NAME` + `$TERMINATOR_DBUS_PATH`
305
+ 环境变量确认在 Terminator 中 → DBus 查询焦点终端 UUID → 与本屏对比
306
+ → 不一致则强制通知,无需额外配置。
307
+
308
+ **外部依赖**(任一即可):`busctl`(systemd 内置)| `python3-dbus` | `gdbus` | `xdotool`
309
+
273
310
  ## 事件映射
274
311
 
275
312
  | 通知事件 | 触发场景 | 对应 opencode 事件 |
@@ -285,12 +322,10 @@ feishu:
285
322
 
286
323
  默认路径:`~/.config/opencode/opencode-notify.yaml`
287
324
 
288
- 可通过环境变量 `OPENCODE_NOTIFY_CONFIG` 自定义路径。
289
-
290
325
  ### 配置优先级
291
326
 
292
327
  ```
293
- YAML 文件 > plugin options (opencode.json) > 环境变量 > 默认值
328
+ YAML 文件 > plugin options (opencode.json) > 默认值
294
329
  ```
295
330
 
296
331
  ### 全部配置项
@@ -336,44 +371,81 @@ events: # 订阅的事件列表
336
371
  # 可选值: permission_required | input_required | run_completed | run_failed
337
372
 
338
373
  dedupe_seconds: 60 # 去重时间窗口(秒)
339
- suppress_when_active: false # 用户活跃时是否跳过通知
340
- activity_timeout_ms: 30000 # 活跃超时判定(毫秒)
341
- debug_log: false # 写入 ~/.opencode-notify/plugin.log 调试日志(默认 false)
374
+ suppress_when_active: true # 会话感知抑制开关(按 suppress_events 列表过滤)
375
+ activity_timeout_ms: 15000 # 会话活跃超时(毫秒),超过此时间无操作视为离开
376
+ suppress_events_when_active: # 活跃时抑制哪些事件(不填=默认列表)
377
+ - permission_required
378
+ - input_required
379
+ # run_failed / run_completed 不在列表中 → 始终通知
380
+ session_stale_timeout_ms: 600000 # 超时会话自动淘汰(毫秒),默认 10 分钟
381
+ remote_delay_channels: [] # 远程延迟推送渠道列表(空=不启用)
382
+ # 可选值: system_message, screen_flash, wechat_work, feishu, custom_webhook
383
+ remote_delay_seconds: 60 # 远程延迟秒数(默认 60)
384
+ remote_delay_max_count: 3 # 远程延迟最多重复次数(默认 3)
385
+ log: # 日志配置
386
+ level: info # 等级: error | warn | info | debug(默认 info)
387
+ # error - 仅记录错误
388
+ # warn - 错误 + 警告
389
+ # info - 错误 + 警告 + 常规信息(推荐)
390
+ # debug - 全部日志(排查时使用)
391
+ # file: "~/.opencode-notify/plugin.log" # 日志文件路径(可选,默认同上)
342
392
  ```
343
393
 
344
- ### 环境变量
394
+ ### 活跃抑制
395
+
396
+ 当用户正在 opencode TUI 中操作(输入消息、回应权限等),通知可能冗余(屏上已可见)。
397
+ 插件通过**会话感知抑制**解决:追踪每个会话的用户操作时间戳,活跃会话按事件类型选择性过滤。
345
398
 
346
- | 变量 | 用途 |
347
- |------|------|
348
- | `OPENCODE_NOTIFY_CUSTOM_WEBHOOK_URL` | 覆盖 `custom_webhook.url` |
349
- | `OPENCODE_NOTIFY_WECHAT_WEBHOOK` | 覆盖 `wechat_work.webhook_url` |
350
- | `OPENCODE_NOTIFY_FEISHU_WEBHOOK` | 覆盖 `feishu.webhook_url` |
351
- | `OPENCODE_NOTIFY_CONFIG` | 自定义 YAML 配置文件路径 |
399
+ **检测的用户操作事件:**
400
+ `message.updated` / `permission.replied` / `question.replied` / `command.executed` / `tui.command.execute`
352
401
 
353
- ### 活跃抑制
402
+ **抑制规则:**
354
403
 
355
- 当用户正在操作 TUI(发消息、翻页、回应权限等),说明人在屏幕前,可跳过通知。
356
- 检测事件:`message.updated` / `permission.replied` / `question.replied` / `command.executed` / `tui.command.execute`
404
+ | 通知事件 | 活跃时默认行为 | 理由 |
405
+ |---------|:------------:|------|
406
+ | `permission_required` | ✅ 抑制 | 权限弹窗就在屏幕上 |
407
+ | `input_required` | ✅ 抑制 | TUI 明确在等输入 |
408
+ | `run_failed` | ❌ 不抑制 | 异步结果,人可能走开 |
409
+ | `run_completed` | ❌ 不抑制 | 同上 |
410
+
411
+ **配置示例:**
357
412
 
358
413
  ```yaml
359
- suppress_when_active: true # 开启抑制
360
- activity_timeout_ms: 30000 # 30 秒无操作视为离开
414
+ suppress_when_active: true # 开启会话感知抑制
415
+ activity_timeout_ms: 15000 # 15 秒无操作视为不活跃
416
+ suppress_events_when_active: # 活跃时抑制哪些事件
417
+ - permission_required
418
+ - input_required
419
+ session_stale_timeout_ms: 600000 # 10 分钟无活动自动淘汰会话(防内存泄漏)
361
420
  ```
362
421
 
363
422
  ## 日志与故障排查
364
423
 
365
- 插件日志位于 `~/.opencode-notify/plugin.log`,默认**关闭**,需在配置中开启:
424
+ 插件日志位于 `~/.opencode-notify/plugin.log`(可通过 `log.file` 配置自定义)。
425
+
426
+ 日志等级由 `log.level` 控制:
427
+
428
+ | 等级 | 包含内容 | 建议用途 |
429
+ |------|---------|---------|
430
+ | `error` | 仅错误 | 生产环境,只关心失败 |
431
+ | `warn` | 错误 + 警告 | 生产环境,关注潜在问题 |
432
+ | `info` | 错误 + 警告 + 常规信息 | 日常运行(默认) |
433
+ | `debug` | 全部日志(含详细事件流) | 排查问题 |
366
434
 
367
435
  ```yaml
368
- debug_log: true
436
+ log:
437
+ level: info # 推荐:日常使用记录所有关键信息
438
+ # level: debug # 排查问题时改为 debug
439
+ # file: "~/.opencode-notify/plugin.log" # 可选自定义路径
369
440
  ```
370
441
 
371
- 开启后日志包含:
442
+ 日志包含:
372
443
 
373
- - 插件加载信息(配置、已启用渠道)
374
- - 收到的事件(`[event] type=...`)
375
- - 匹配到的通知
376
- - 发送结果
444
+ - 插件加载信息(配置、已启用渠道、日志等级)
445
+ - 渠道发送失败 / 成功
446
+ - 会话活跃跳过通知
447
+ - 远程延迟推送调度 / 取消 / 推送
448
+ - 去重命中、状态存储异常等诊断信息
377
449
 
378
450
  ```bash
379
451
  # 实时查看日志
@@ -382,8 +454,11 @@ tail -f ~/.opencode-notify/plugin.log
382
454
  # 查看最近的插件加载信息
383
455
  grep "插件已加载" ~/.opencode-notify/plugin.log
384
456
 
385
- # 查看事件流
386
- grep "\[event\]" ~/.opencode-notify/plugin.log
457
+ # 只看错误
458
+ grep "\[ERROR\]" ~/.opencode-notify/plugin.log
459
+
460
+ # 只看警告
461
+ grep "\[WARN\]" ~/.opencode-notify/plugin.log
387
462
  ```
388
463
 
389
464
  ### 常见问题
@@ -410,17 +485,30 @@ curl -X POST <webhook_url> -H "Content-Type: application/json" -d '{"msgtype":"m
410
485
  opencode-notify/
411
486
  ├── index.ts # 插件入口
412
487
  ├── cli.ts # 诊断工具
413
- ├── config.ts # 配置解析(YAML + options + env
488
+ ├── config.ts # 配置解析(YAML + plugin options)
414
489
  ├── events.ts # 事件路由
490
+ ├── log.ts # 共享日志模块
491
+ ├── session-tracker.ts # 会话感知抑制
492
+ ├── terminator-detect.ts # Terminator 子屏最大化检测
493
+ ├── delayed-dispatcher.ts # 远程延迟推送调度器
415
494
  ├── dispatcher.ts # 去重分发
416
495
  ├── store.ts # 状态存储
417
496
  ├── message.ts # 消息模型
497
+ ├── doc/
498
+ │ ├── de.png # 跑马灯效果截图
499
+ │ └── features.md # 功能说明(含 Terminator 友好体验说明)
418
500
  ├── scripts/
419
501
  │ └── marquee.py # 屏幕跑马灯效果(Python+GTK)
420
502
  ├── senders/
421
503
  │ ├── types.ts # Sender 接口
422
- │ ├── system.ts # 系统通知
423
- │ ├── screen-flash.ts # 屏幕跑马灯
504
+ │ ├── system/ # 系统通知(平台分包)
505
+ ├── index.ts # 注册表 + 自动选择平台
506
+ │ │ ├── darwin.ts # macOS (osascript)
507
+ │ │ ├── linux.ts # Linux (notify-send)
508
+ │ │ └── win32.ts # Windows (PowerShell)
509
+ │ ├── screen-flash/ # 屏幕跑马灯(Linux X11)
510
+ │ │ ├── index.ts # 入口 + 平台守卫
511
+ │ │ └── linux.ts # Linux 实现 (Python+GTK)
424
512
  │ ├── custom-webhook.ts # 自定义 Webhook
425
513
  │ ├── wechat-work.ts # 企业微信
426
514
  │ └── feishu.ts # 飞书
package/cli.ts CHANGED
@@ -14,7 +14,7 @@
14
14
  import { loadYamlConfig, resolveConfig, mergeConfig } from "./config.js"
15
15
  import type { PluginConfig, ChannelsConfig } from "./config.js"
16
16
  import type { Message } from "./message.js"
17
- import { SystemSender } from "./senders/system.js"
17
+ import { SystemSender } from "./senders/system/index.js"
18
18
  import { CustomWebhookSender } from "./senders/custom-webhook.js"
19
19
  import { WechatWorkSender } from "./senders/wechat-work.js"
20
20
  import { FeishuSender } from "./senders/feishu.js"
@@ -25,8 +25,14 @@ import { join } from "node:path"
25
25
  // ─── 常量 ───────────────────────────────────────────────────────────────────
26
26
 
27
27
  const PKG = JSON.parse(readFileSync(join(import.meta.dirname, "package.json"), "utf-8"))
28
- const LOG_FILE = join(homedir(), ".opencode-notify", "plugin.log")
29
- const YAML_PATH = process.env.OPENCODE_NOTIFY_CONFIG ?? join(homedir(), ".config", "opencode", "opencode-notify.yaml")
28
+ const DEFAULT_LOG_FILE = join(homedir(), ".opencode-notify", "plugin.log")
29
+ const YAML_PATH = join(homedir(), ".config", "opencode", "opencode-notify.yaml")
30
+
31
+ /** 读取有效日志文件路径(优先从配置中读取) */
32
+ function getLogFile(): string {
33
+ const yamlCfg = loadYamlConfig()
34
+ return yamlCfg?.log?.file ?? DEFAULT_LOG_FILE
35
+ }
30
36
 
31
37
  const SAMPLE_MSG: Message = {
32
38
  agent: "opencode",
@@ -72,7 +78,7 @@ function cmdCheck() {
72
78
  // 1. 检查配置文件是否存在
73
79
  if (!existsSync(YAML_PATH)) {
74
80
  console.log(`\n❌ 配置文件不存在: ${YAML_PATH}`)
75
- console.log(" 请创建配置文件,或设置 OPENCODE_NOTIFY_CONFIG 环境变量指向正确的路径")
81
+ console.log(" 请创建 ~/.config/opencode/opencode-notify.yaml 配置文件")
76
82
  process.exit(1)
77
83
  }
78
84
  console.log(`\n✅ 配置文件: ${YAML_PATH}`)
@@ -145,6 +151,9 @@ function cmdCheck() {
145
151
  // 6. 检查活跃抑制
146
152
  console.log(`\n🔇 活跃抑制: ${cfg.suppress_when_active ? "开启" : "关闭"} (超时 ${cfg.activity_timeout_ms}ms)`)
147
153
 
154
+ // 6.5 日志配置
155
+ console.log(`\n📝 日志: 等级=${cfg.log?.level ?? "info"} 文件=${getLogFile()}`)
156
+
148
157
  // 7. 检查去重
149
158
  console.log(`\n🔄 去重窗口: ${cfg.dedupe_seconds} 秒`)
150
159
 
@@ -219,17 +228,18 @@ async function cmdTest(channel?: string) {
219
228
  // ─── 命令: log ───────────────────────────────────────────────────────────────
220
229
 
221
230
  function cmdLog(lines: number) {
222
- if (!existsSync(LOG_FILE)) {
223
- console.log(`日志文件不存在: ${LOG_FILE}`)
231
+ const logFile = getLogFile()
232
+ if (!existsSync(logFile)) {
233
+ console.log(`日志文件不存在: ${logFile}`)
224
234
  console.log("插件尚未运行过,或日志已被清理。")
225
235
  process.exit(1)
226
236
  }
227
237
 
228
- const stat = statSync(LOG_FILE)
229
- const all = readFileSync(LOG_FILE, "utf-8").trimEnd()
238
+ const stat = statSync(logFile)
239
+ const all = readFileSync(logFile, "utf-8").trimEnd()
230
240
  const entries = all.split("\n")
231
241
 
232
- console.log(`📄 ${LOG_FILE} (${(stat.size / 1024).toFixed(1)} KB, ${entries.length} 行)\n`)
242
+ console.log(`📄 ${logFile} (${(stat.size / 1024).toFixed(1)} KB, ${entries.length} 行)\n`)
233
243
 
234
244
  const tail = entries.slice(-lines)
235
245
  for (const line of tail) {
@@ -270,16 +280,17 @@ function cmdInfo() {
270
280
  }
271
281
 
272
282
  // 日志文件
283
+ const logFile = getLogFile()
273
284
  console.log(`\n 📝 日志文件:`)
274
- if (existsSync(LOG_FILE)) {
275
- const stat = statSync(LOG_FILE)
276
- const lines = readFileSync(LOG_FILE, "utf-8").split("\n").length
277
- const recent = readFileSync(LOG_FILE, "utf-8").trimEnd().split("\n").slice(-1)[0] ?? ""
278
- console.log(` 路径: ${LOG_FILE}`)
285
+ if (existsSync(logFile)) {
286
+ const stat = statSync(logFile)
287
+ const lines = readFileSync(logFile, "utf-8").split("\n").length
288
+ const recent = readFileSync(logFile, "utf-8").trimEnd().split("\n").slice(-1)[0] ?? ""
289
+ console.log(` 路径: ${logFile}`)
279
290
  console.log(` 大小: ${(stat.size / 1024).toFixed(1)} KB, ${lines} 行`)
280
291
  console.log(` 最新: ${recent}`)
281
292
  } else {
282
- console.log(` 路径: ${LOG_FILE} (暂无日志)`)
293
+ console.log(` 路径: ${logFile} (暂无日志)`)
283
294
  }
284
295
 
285
296
  // 配置详情
@@ -308,6 +319,7 @@ function cmdInfo() {
308
319
 
309
320
  console.log(`\n 📋 订阅事件: ${cfg.events?.join(", ") ?? "(无)"}`)
310
321
  console.log(` 🔇 活跃抑制: ${cfg.suppress_when_active ? "开启" : "关闭"}`)
322
+ console.log(` 📝 日志等级: ${cfg.log?.level ?? "info"}`)
311
323
  console.log(` ⏱ 去重窗口: ${cfg.dedupe_seconds} 秒`)
312
324
  } else {
313
325
  console.log(`\n ⚠️ 未加载到配置`)