@xiaoqiong0v0/opencode-sound-notify 1.3.0 → 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/dist/sound-notify.js +71 -5
- package/package.json +1 -1
package/dist/sound-notify.js
CHANGED
|
@@ -114,6 +114,49 @@ catch (e) {
|
|
|
114
114
|
const eventTimestamps = new Map();
|
|
115
115
|
const subSessions = new Set();
|
|
116
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
|
+
}
|
|
117
160
|
// 判断是否运行在 Termux(Android) 环境
|
|
118
161
|
function isTermux() {
|
|
119
162
|
return process.platform === "android"
|
|
@@ -181,23 +224,30 @@ function play(eventType) {
|
|
|
181
224
|
ps.on("close", (code) => { resolve(code === 0); });
|
|
182
225
|
});
|
|
183
226
|
}
|
|
184
|
-
|
|
227
|
+
// 返回播放被拦截的原因,null 表示允许播放
|
|
228
|
+
function blockReason(eventType) {
|
|
185
229
|
if (muted)
|
|
186
|
-
return
|
|
230
|
+
return "muted";
|
|
187
231
|
if (!cfg.enabled)
|
|
188
|
-
return
|
|
232
|
+
return "disabled";
|
|
189
233
|
if (!cfg.events?.includes(eventType))
|
|
190
|
-
return
|
|
234
|
+
return "not-in-events";
|
|
191
235
|
const now = Date.now();
|
|
192
236
|
const minMs = cfg.minIntervalMs || 0;
|
|
193
237
|
const debounceMs = cfg.debounceMs?.[eventType] ?? cfg.defaultDebounceMs ?? 30000;
|
|
194
238
|
const effective = minMs > debounceMs ? minMs : debounceMs;
|
|
195
239
|
// 全局间隔检查:任何声音播放后 minIntervalMs 内都不再响应
|
|
196
240
|
if (minMs > 0 && now - globalLastPlay < minMs)
|
|
197
|
-
return
|
|
241
|
+
return `global-interval(${now - globalLastPlay}<${minMs})`;
|
|
198
242
|
const last = eventTimestamps.get(eventType) || 0;
|
|
199
243
|
if (now - last < effective)
|
|
244
|
+
return `debounce(${now - last}<${effective})`;
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
function shouldPlay(eventType) {
|
|
248
|
+
if (blockReason(eventType))
|
|
200
249
|
return false;
|
|
250
|
+
const now = Date.now();
|
|
201
251
|
eventTimestamps.set(eventType, now);
|
|
202
252
|
globalLastPlay = now;
|
|
203
253
|
return true;
|
|
@@ -230,6 +280,22 @@ export const SoundNotify = async () => {
|
|
|
230
280
|
const sid = event.properties?.sessionID;
|
|
231
281
|
if (sid && subSessions.has(sid) && !cfg.childEvents?.includes(event.type))
|
|
232
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
|
+
}
|
|
233
299
|
if (shouldPlay(event.type))
|
|
234
300
|
play(event.type);
|
|
235
301
|
},
|
package/package.json
CHANGED