@xiaoqiong0v0/opencode-sound-notify 1.2.1 → 1.3.2

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
@@ -41,6 +41,8 @@ npm install @xiaoqiong0v0/opencode-sound-notify
41
41
  },
42
42
  // 子任务(sub-session)中仍触发提示音的事件列表,默认仅权限请求
43
43
  "childEvents": ["permission.asked"],
44
+ // Termux 音频流类型,仅 Termux 生效(media/alarm/notification/ring/system/voice)
45
+ "streamType": "media",
44
46
  // 语言: "zh" 或 "en"
45
47
  "lang": "en",
46
48
  // 是否启用
@@ -57,6 +59,7 @@ npm install @xiaoqiong0v0/opencode-sound-notify
57
59
  | `minIntervalMs` | `3000` | 全局最小间隔,所有声音不能小于此值,设为 0 关闭 |
58
60
  | `debounceMs` | `{}` | 按事件单独配置,key=事件名,value=防抖间隔(ms) |
59
61
  | `childEvents` | `["permission.asked"]` | 子任务中仍触发提示音的事件列表,其他事件静默 |
62
+ | `streamType` | `"media"` | Termux 音频流类型,仅 Termux 生效 |
60
63
  | `lang` | `"en"` | 语言设置 |
61
64
  | `enabled` | `true` | 总开关 |
62
65
 
@@ -94,3 +97,12 @@ npm install @xiaoqiong0v0/opencode-sound-notify
94
97
  | Windows | PowerShell SoundPlayer |
95
98
  | macOS | `afplay` |
96
99
  | Linux | `paplay` (PulseAudio) / `aplay` (ALSA) |
100
+ | Termux (Android) | play-audio(OpenSL ES,需 pkg install play-audio) |
101
+
102
+ ### Termux (Android)
103
+
104
+ ```bash
105
+ pkg install play-audio
106
+ ```
107
+
108
+ Termux 上通过 `play-audio`(OpenSL ES) 播放,默认走 `media` 音频流(可用 `streamType` 切换)。若无声,请检查手机对应音频流的音量(如媒体音量)。
@@ -16,6 +16,8 @@ const BUILTIN_SOUNDS = {
16
16
  "session.error": "session.error.wav",
17
17
  "permission.asked": "permission.asked.wav",
18
18
  };
19
+ // Termux 默认前缀目录
20
+ const TERMUX_PREFIX = "/data/data/com.termux/files/usr";
19
21
  const log = createLogger("sound-notify");
20
22
  const SAMPLE_CFG = `{
21
23
  // 默认提示音文件路径,留空使用内置 default.wav
@@ -38,6 +40,8 @@ const SAMPLE_CFG = `{
38
40
  },
39
41
  // 子任务(sub-session)中仍触发提示音的事件列表,默认仅权限请求
40
42
  "childEvents": ["permission.asked"],
43
+ // Termux 音频流类型,仅 Termux 生效(media/alarm/notification/ring/system/voice)
44
+ "streamType": "media",
41
45
  // 语言: "zh" 或 "en"
42
46
  "lang": "en",
43
47
  // 是否启用
@@ -59,6 +63,7 @@ const TX = {
59
63
  mute_save: { zh: "save=true 同时写入配置文件持久化", en: "save=true also persist to config file" },
60
64
  save_ok: { zh: "已保存到配置文件 {path}", en: "Saved to config {path}" },
61
65
  save_fail: { zh: "保存失败: {msg}", en: "Save failed: {msg}" },
66
+ termux_no_player: { zh: "Termux 未找到 play-audio,请先执行: pkg install play-audio", en: "play-audio not found in Termux, install it: pkg install play-audio" },
62
67
  };
63
68
  const T = (key, params) => {
64
69
  const entry = TX[key] || { zh: key, en: key };
@@ -88,6 +93,7 @@ function loadCfg() {
88
93
  defaultDebounceMs: raw.defaultDebounceMs || 30000,
89
94
  debounceMs: raw.debounceMs,
90
95
  childEvents: raw.childEvents || ["permission.asked"],
96
+ streamType: raw.streamType || "media",
91
97
  enabled: raw.enabled !== false,
92
98
  };
93
99
  LANG = raw.lang === "zh" ? "zh" : "en";
@@ -108,7 +114,69 @@ catch (e) {
108
114
  const eventTimestamps = new Map();
109
115
  const subSessions = new Set();
110
116
  let globalLastPlay = 0;
117
+ // idle 提示音延迟(ms):延迟期内若出现新输入则取消播放
118
+ const IDLE_DELAY_MS = 500;
119
+ let idleTimer = null;
120
+ let idleSessionId;
121
+ let idleUserId;
122
+ let lastUserId;
123
+ // 取消待播放的 idle 提示音
124
+ function cancelIdle() {
125
+ if (idleTimer) {
126
+ clearTimeout(idleTimer);
127
+ idleTimer = null;
128
+ }
129
+ }
130
+ // 延迟播放 idle 提示音,等待期间可被新输入取消
131
+ function scheduleIdle(sid) {
132
+ cancelIdle();
133
+ idleSessionId = sid;
134
+ idleUserId = lastUserId;
135
+ idleTimer = setTimeout(() => {
136
+ idleTimer = null;
137
+ if (shouldPlay("session.idle")) {
138
+ play("session.idle");
139
+ }
140
+ else {
141
+ log.info(`idle skipped: ${blockReason("session.idle")}`);
142
+ }
143
+ }, IDLE_DELAY_MS);
144
+ }
145
+ // 判断事件是否表示同一会话出现新的用户输入/活动
146
+ function isNewInput(type, properties, sid) {
147
+ if (!idleTimer || sid !== idleSessionId)
148
+ return false;
149
+ if (type === "session.status") {
150
+ const status = properties?.status;
151
+ return status?.type === "busy";
152
+ }
153
+ if (type === "message.updated") {
154
+ const info = properties?.info;
155
+ // 仅当出现“新的用户消息”才算新输入;旧消息的重复推送不算
156
+ return info?.role === "user" && !!idleUserId && !!info.id && info.id !== idleUserId;
157
+ }
158
+ return false;
159
+ }
160
+ // 判断是否运行在 Termux(Android) 环境
161
+ function isTermux() {
162
+ return process.platform === "android"
163
+ || !!process.env.TERMUX_VERSION
164
+ || (process.env.PREFIX || "").includes("com.termux");
165
+ }
166
+ // 查找 Termux 内的命令绝对路径,找不到返回 null
167
+ function findTermuxBin(name) {
168
+ const prefix = process.env.PREFIX || TERMUX_PREFIX;
169
+ const p = join(prefix, "bin", name);
170
+ return existsSync(p) ? p : null;
171
+ }
111
172
  function getPlayerArgs(sound) {
173
+ if (isTermux()) {
174
+ const player = findTermuxBin("play-audio");
175
+ if (!player)
176
+ return null;
177
+ const streamArgs = cfg.streamType ? ["-s", cfg.streamType] : [];
178
+ return [player, ...streamArgs, sound];
179
+ }
112
180
  switch (process.platform) {
113
181
  case "win32":
114
182
  return ["powershell", "-NoProfile", "-Command",
@@ -146,7 +214,7 @@ function play(eventType) {
146
214
  }
147
215
  const args = getPlayerArgs(path);
148
216
  if (!args) {
149
- log.error(T("unsupported_platform", { platform: process.platform }));
217
+ log.error(isTermux() ? T("termux_no_player") : T("unsupported_platform", { platform: process.platform }));
150
218
  return Promise.resolve(false);
151
219
  }
152
220
  const [cmd, ...cmdArgs] = args;
@@ -156,23 +224,30 @@ function play(eventType) {
156
224
  ps.on("close", (code) => { resolve(code === 0); });
157
225
  });
158
226
  }
159
- function shouldPlay(eventType) {
227
+ // 返回播放被拦截的原因,null 表示允许播放
228
+ function blockReason(eventType) {
160
229
  if (muted)
161
- return false;
230
+ return "muted";
162
231
  if (!cfg.enabled)
163
- return false;
232
+ return "disabled";
164
233
  if (!cfg.events?.includes(eventType))
165
- return false;
234
+ return "not-in-events";
166
235
  const now = Date.now();
167
236
  const minMs = cfg.minIntervalMs || 0;
168
237
  const debounceMs = cfg.debounceMs?.[eventType] ?? cfg.defaultDebounceMs ?? 30000;
169
238
  const effective = minMs > debounceMs ? minMs : debounceMs;
170
239
  // 全局间隔检查:任何声音播放后 minIntervalMs 内都不再响应
171
240
  if (minMs > 0 && now - globalLastPlay < minMs)
172
- return false;
241
+ return `global-interval(${now - globalLastPlay}<${minMs})`;
173
242
  const last = eventTimestamps.get(eventType) || 0;
174
243
  if (now - last < effective)
244
+ return `debounce(${now - last}<${effective})`;
245
+ return null;
246
+ }
247
+ function shouldPlay(eventType) {
248
+ if (blockReason(eventType))
175
249
  return false;
250
+ const now = Date.now();
176
251
  eventTimestamps.set(eventType, now);
177
252
  globalLastPlay = now;
178
253
  return true;
@@ -205,6 +280,22 @@ export const SoundNotify = async () => {
205
280
  const sid = event.properties?.sessionID;
206
281
  if (sid && subSessions.has(sid) && !cfg.childEvents?.includes(event.type))
207
282
  return;
283
+ // 记录最新的用户消息 id,用于区分“新消息”与“旧消息的重复推送”
284
+ if (event.type === "message.updated") {
285
+ const info = event.properties?.info;
286
+ if (info?.role === "user" && info.id)
287
+ lastUserId = info.id;
288
+ }
289
+ // 新输入/活动 → 取消尚未播放的 idle 提示音
290
+ if (isNewInput(event.type, event.properties, sid)) {
291
+ log.info(`idle canceled by ${event.type}`);
292
+ cancelIdle();
293
+ }
294
+ // idle 延迟播放,延迟期内出现新输入则不响
295
+ if (event.type === "session.idle") {
296
+ scheduleIdle(sid);
297
+ return;
298
+ }
208
299
  if (shouldPlay(event.type))
209
300
  play(event.type);
210
301
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiaoqiong0v0/opencode-sound-notify",
3
- "version": "1.2.1",
3
+ "version": "1.3.2",
4
4
  "type": "module",
5
5
  "description": "OpenCode plugin: play notification sounds on session events (idle, error, permission asked).",
6
6
  "files": [