@makooo/dsh-session-notifier 0.0.0-stage → 1.0.1
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/LICENSE +21 -0
- package/README.md +70 -2
- package/client.js +1074 -0
- package/cordis.patch.yml +25 -0
- package/icon.svg +5 -0
- package/index.js +601 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +59 -4
package/client.js
ADDED
|
@@ -0,0 +1,1074 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half of @makooo/dsh-session-notifier.
|
|
3
|
+
*
|
|
4
|
+
* Subscribes to the Host SSE channel, composes localized text, plays a
|
|
5
|
+
* synthesized tone per event class, and raises the browser system notification.
|
|
6
|
+
* One compact bell sits in the session header's utilities (left of the theme
|
|
7
|
+
* switcher) with a settings popover behind it.
|
|
8
|
+
*
|
|
9
|
+
* Design borrowed from dsh-notify (bitterSmilezzz, MIT):
|
|
10
|
+
* - blocking notices (approval / question) must never be replaced by a later
|
|
11
|
+
* informational notice, because closing one removes it from the Windows
|
|
12
|
+
* action center and the request would vanish;
|
|
13
|
+
* - system sound is always suppressed, so the "sound off" switch really means
|
|
14
|
+
* silent instead of leaving the OS chime on;
|
|
15
|
+
* - summary off means the banner carries only the session label.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
window.__ModuleLoader__.load({
|
|
19
|
+
id: '@makooo/dsh-session-notifier',
|
|
20
|
+
factory(require) {
|
|
21
|
+
const React = require('react');
|
|
22
|
+
const h = React.createElement;
|
|
23
|
+
|
|
24
|
+
const CHANNEL = '/dsh-session-notifier';
|
|
25
|
+
const STORAGE_KEY = 'dsh-session-notifier.enabled';
|
|
26
|
+
const ICON = `${CHANNEL}/icon.svg`;
|
|
27
|
+
const BODY_MAX = 96;
|
|
28
|
+
const SOUND_THROTTLE_MS = 300;
|
|
29
|
+
|
|
30
|
+
/** Per-kind tone motifs: rise for action-required, fall for failure. */
|
|
31
|
+
const SOUND_PATTERNS = Object.freeze({
|
|
32
|
+
approval: [
|
|
33
|
+
{ frequency: 784, start: 0, duration: 0.14, wave: 'sine', gain: 0.16 },
|
|
34
|
+
{ frequency: 1047, start: 0.16, duration: 0.26, wave: 'sine', gain: 0.16 },
|
|
35
|
+
],
|
|
36
|
+
question: [
|
|
37
|
+
{ frequency: 784, start: 0, duration: 0.14, wave: 'sine', gain: 0.16 },
|
|
38
|
+
{ frequency: 1047, start: 0.16, duration: 0.26, wave: 'sine', gain: 0.16 },
|
|
39
|
+
],
|
|
40
|
+
turn: [{ frequency: 880, start: 0, duration: 0.16, wave: 'sine', gain: 0.12 }],
|
|
41
|
+
assistant: [{ frequency: 880, start: 0, duration: 0.16, wave: 'sine', gain: 0.12 }],
|
|
42
|
+
sessionDone: [
|
|
43
|
+
{ frequency: 659, start: 0, duration: 0.14, wave: 'sine', gain: 0.13 },
|
|
44
|
+
{ frequency: 523, start: 0.16, duration: 0.24, wave: 'sine', gain: 0.13 },
|
|
45
|
+
],
|
|
46
|
+
error: [
|
|
47
|
+
{ frequency: 440, start: 0, duration: 0.16, wave: 'triangle', gain: 0.16 },
|
|
48
|
+
{ frequency: 349, start: 0.18, duration: 0.3, wave: 'triangle', gain: 0.16 },
|
|
49
|
+
],
|
|
50
|
+
subagent: [{ frequency: 880, start: 0, duration: 0.16, wave: 'sine', gain: 0.08 }],
|
|
51
|
+
idle: [{ frequency: 659, start: 0, duration: 0.14, wave: 'sine', gain: 0.1 }],
|
|
52
|
+
info: [{ frequency: 880, start: 0, duration: 0.16, wave: 'sine', gain: 0.1 }],
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
/** Copy per language. The Host sends facts, the Client words them. */
|
|
56
|
+
const TEXT = Object.freeze({
|
|
57
|
+
zh: {
|
|
58
|
+
titleTurn: '轮次完成',
|
|
59
|
+
titleSessionDone: '会话结束',
|
|
60
|
+
titleError: '会话出错',
|
|
61
|
+
titleApproval: '需要确认',
|
|
62
|
+
titleQuestion: '需要回答',
|
|
63
|
+
titleSubagent: '子代理完成',
|
|
64
|
+
titleIdle: '会话空闲',
|
|
65
|
+
titleAssistant: '新回复',
|
|
66
|
+
titleTest: 'DSH 会话通知测试',
|
|
67
|
+
bodyTest: '如果你看到这条通知,说明通知链路正常',
|
|
68
|
+
bodyTurn: '已完成',
|
|
69
|
+
bodyIdle: '任务已停止运行',
|
|
70
|
+
bodySessionDone: '会话已结束',
|
|
71
|
+
bodyUnknownError: '会话出错',
|
|
72
|
+
bodyPlanReview: '计划等待你审阅',
|
|
73
|
+
bodyQuestion: '会话向你提出了问题',
|
|
74
|
+
approvalToolFallback: '工具执行',
|
|
75
|
+
reasonLabels: {
|
|
76
|
+
completed: '已完成',
|
|
77
|
+
aborted: '已中断',
|
|
78
|
+
blocked: '已阻塞',
|
|
79
|
+
'max-tokens': '达到 token 上限',
|
|
80
|
+
interrupted: '已打断',
|
|
81
|
+
forked: '已分叉',
|
|
82
|
+
error: '出错',
|
|
83
|
+
},
|
|
84
|
+
enable: '系统通知',
|
|
85
|
+
off: '已关闭',
|
|
86
|
+
panelTitle: '会话通知',
|
|
87
|
+
testButton: '发送测试通知',
|
|
88
|
+
refresh: '刷新',
|
|
89
|
+
},
|
|
90
|
+
en: {
|
|
91
|
+
titleTurn: 'Turn complete',
|
|
92
|
+
titleSessionDone: 'Session ended',
|
|
93
|
+
titleError: 'Session error',
|
|
94
|
+
titleApproval: 'Approval needed',
|
|
95
|
+
titleQuestion: 'Question waiting',
|
|
96
|
+
titleSubagent: 'Subagent done',
|
|
97
|
+
titleIdle: 'Session idle',
|
|
98
|
+
titleAssistant: 'New reply',
|
|
99
|
+
titleTest: 'DSH notification test',
|
|
100
|
+
bodyTest: 'Seeing this banner means the notification path works',
|
|
101
|
+
bodyTurn: 'Completed',
|
|
102
|
+
bodyIdle: 'Work stopped',
|
|
103
|
+
bodySessionDone: 'The session has ended',
|
|
104
|
+
bodyUnknownError: 'The session failed',
|
|
105
|
+
bodyPlanReview: 'A plan is waiting for your review',
|
|
106
|
+
bodyQuestion: 'The session is asking you something',
|
|
107
|
+
approvalToolFallback: 'A tool call',
|
|
108
|
+
reasonLabels: {
|
|
109
|
+
completed: 'Completed',
|
|
110
|
+
aborted: 'Aborted',
|
|
111
|
+
blocked: 'Blocked',
|
|
112
|
+
'max-tokens': 'Token limit reached',
|
|
113
|
+
interrupted: 'Interrupted',
|
|
114
|
+
forked: 'Forked',
|
|
115
|
+
error: 'Error',
|
|
116
|
+
},
|
|
117
|
+
enable: 'System notifications',
|
|
118
|
+
off: 'Off',
|
|
119
|
+
panelTitle: 'Session notifications',
|
|
120
|
+
testButton: 'Send test notification',
|
|
121
|
+
refresh: 'Refresh',
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
function readEnabled() {
|
|
126
|
+
try {
|
|
127
|
+
return window.localStorage.getItem(STORAGE_KEY) !== '0';
|
|
128
|
+
} catch {
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function writeEnabled(value) {
|
|
134
|
+
try {
|
|
135
|
+
window.localStorage.setItem(STORAGE_KEY, value ? '1' : '0');
|
|
136
|
+
} catch {
|
|
137
|
+
/* storage is optional */
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function permissionState() {
|
|
142
|
+
if (typeof Notification === 'undefined') return 'unsupported';
|
|
143
|
+
return Notification.permission;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function requestPermission() {
|
|
147
|
+
if (typeof Notification === 'undefined') return 'unsupported';
|
|
148
|
+
if (Notification.permission !== 'default') return Notification.permission;
|
|
149
|
+
try {
|
|
150
|
+
return await Notification.requestPermission();
|
|
151
|
+
} catch {
|
|
152
|
+
return Notification.permission;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function platformOf() {
|
|
157
|
+
if (typeof navigator === 'undefined') return '';
|
|
158
|
+
return `${navigator.platform ?? ''} ${navigator.userAgent ?? ''}`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Windows browsers are the one host where `tag` is unreliable, so there the
|
|
163
|
+
* plugin closes the previous card explicitly instead of merging system-side.
|
|
164
|
+
*/
|
|
165
|
+
function tagSupported() {
|
|
166
|
+
const platform = platformOf();
|
|
167
|
+
if (!/\bwin/iu.test(platform)) return true;
|
|
168
|
+
return /\bElectron\//iu.test(platform);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function watchingNow() {
|
|
172
|
+
if (typeof document === 'undefined') return false;
|
|
173
|
+
return document.visibilityState === 'visible' && document.hasFocus();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function truncate(text, max = BODY_MAX) {
|
|
177
|
+
const oneLine = String(text ?? '').replace(/\s+/gu, ' ').trim();
|
|
178
|
+
if (!oneLine) return '';
|
|
179
|
+
const points = Array.from(oneLine);
|
|
180
|
+
return points.length > max ? `${points.slice(0, Math.max(1, max - 1)).join('')}…` : oneLine;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ---- synthesized sound ----------------------------------------------------
|
|
184
|
+
|
|
185
|
+
let audioContext = null;
|
|
186
|
+
let lastSoundAt = 0;
|
|
187
|
+
|
|
188
|
+
/** Lazily create one AudioContext; browsers allow it after a user gesture. */
|
|
189
|
+
function audio() {
|
|
190
|
+
if (typeof window === 'undefined') return null;
|
|
191
|
+
const Ctor = window.AudioContext || window.webkitAudioContext;
|
|
192
|
+
if (!Ctor) return null;
|
|
193
|
+
if (!audioContext) {
|
|
194
|
+
try {
|
|
195
|
+
audioContext = new Ctor();
|
|
196
|
+
} catch {
|
|
197
|
+
return null;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
if (audioContext.state === 'suspended' && typeof audioContext.resume === 'function') {
|
|
201
|
+
void audioContext.resume().catch(() => {});
|
|
202
|
+
}
|
|
203
|
+
return audioContext;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Play one event class tone. Throttled per burst so a fan-out of subagents
|
|
208
|
+
* cannot turn into a jingle, and every motif stays under 0.6s.
|
|
209
|
+
*/
|
|
210
|
+
function playSound(kind, enabled) {
|
|
211
|
+
if (!enabled) return;
|
|
212
|
+
const pattern = SOUND_PATTERNS[kind] || SOUND_PATTERNS.info;
|
|
213
|
+
const now = Date.now();
|
|
214
|
+
if (now - lastSoundAt < SOUND_THROTTLE_MS) return;
|
|
215
|
+
lastSoundAt = now;
|
|
216
|
+
const context = audio();
|
|
217
|
+
if (!context) return;
|
|
218
|
+
try {
|
|
219
|
+
const base = context.currentTime;
|
|
220
|
+
for (const tone of pattern) {
|
|
221
|
+
const oscillator = context.createOscillator();
|
|
222
|
+
const gain = context.createGain();
|
|
223
|
+
oscillator.type = tone.wave;
|
|
224
|
+
oscillator.frequency.value = tone.frequency;
|
|
225
|
+
gain.gain.setValueAtTime(tone.gain, base + tone.start);
|
|
226
|
+
gain.gain.exponentialRampToValueAtTime(0.0001, base + tone.start + tone.duration);
|
|
227
|
+
oscillator.connect(gain);
|
|
228
|
+
gain.connect(context.destination);
|
|
229
|
+
oscillator.start(base + tone.start);
|
|
230
|
+
oscillator.stop(base + tone.start + tone.duration);
|
|
231
|
+
}
|
|
232
|
+
} catch {
|
|
233
|
+
/* audio is a nicety, never a dependency */
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ---- shared runtime state -------------------------------------------------
|
|
238
|
+
|
|
239
|
+
const listeners = new Set();
|
|
240
|
+
const shared = {
|
|
241
|
+
enabled: readEnabled(),
|
|
242
|
+
permission: permissionState(),
|
|
243
|
+
config: null,
|
|
244
|
+
bounds: null,
|
|
245
|
+
status: '',
|
|
246
|
+
locale: 'zh',
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const emitChange = () => {
|
|
250
|
+
for (const fn of listeners) fn(shared);
|
|
251
|
+
};
|
|
252
|
+
|
|
253
|
+
const setShared = (patch) => {
|
|
254
|
+
Object.assign(shared, patch);
|
|
255
|
+
emitChange();
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
const strings = () => TEXT[shared.locale] || TEXT.zh;
|
|
259
|
+
|
|
260
|
+
/** Localized approval hint: displayReason dict, then reason, then nothing. */
|
|
261
|
+
function approvalDetailOf(detail, locale) {
|
|
262
|
+
if (!detail || typeof detail !== 'object') return undefined;
|
|
263
|
+
const dict = detail.displayReason;
|
|
264
|
+
if (dict && typeof dict === 'object') {
|
|
265
|
+
const normalized = String(locale || '').toLowerCase();
|
|
266
|
+
const primary = normalized.split(/[-_]/u)[0] || '';
|
|
267
|
+
const exact = typeof dict[normalized] === 'string' ? dict[normalized] : '';
|
|
268
|
+
const byPrimary = typeof dict[primary] === 'string' ? dict[primary] : '';
|
|
269
|
+
const en = typeof dict.en === 'string' ? dict.en : '';
|
|
270
|
+
if (exact) return exact;
|
|
271
|
+
if (byPrimary) return byPrimary;
|
|
272
|
+
if (en) return en;
|
|
273
|
+
}
|
|
274
|
+
if (typeof detail.reason === 'string' && detail.reason) return detail.reason;
|
|
275
|
+
return undefined;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Compose title and body for one Host-sent notice. */
|
|
279
|
+
function compose(notice) {
|
|
280
|
+
const t = strings();
|
|
281
|
+
const config = shared.config || {};
|
|
282
|
+
const showDetail = config.summary !== false;
|
|
283
|
+
const kind = String(notice.kind || 'info');
|
|
284
|
+
const label = notice.label || '';
|
|
285
|
+
|
|
286
|
+
const titles = {
|
|
287
|
+
turn: t.titleTurn,
|
|
288
|
+
sessionDone: t.titleSessionDone,
|
|
289
|
+
error: t.titleError,
|
|
290
|
+
approval: t.titleApproval,
|
|
291
|
+
question: t.titleQuestion,
|
|
292
|
+
subagent: t.titleSubagent,
|
|
293
|
+
idle: t.titleIdle,
|
|
294
|
+
assistant: t.titleAssistant,
|
|
295
|
+
};
|
|
296
|
+
const title = label ? `${titles[kind] || titles.turn} · ${label}` : titles[kind] || t.titleTurn;
|
|
297
|
+
|
|
298
|
+
let detail = '';
|
|
299
|
+
const raw = notice.detail;
|
|
300
|
+
if (raw && typeof raw === 'object') {
|
|
301
|
+
if (typeof raw.text === 'string' && raw.text) {
|
|
302
|
+
if (raw.text === 'turn-error' || raw.text === 'session-error') detail = t.bodyUnknownError;
|
|
303
|
+
else detail = raw.text;
|
|
304
|
+
}
|
|
305
|
+
if (typeof raw.question === 'string' && raw.question) detail = raw.question;
|
|
306
|
+
else if (raw.planReview === true) detail = t.bodyPlanReview;
|
|
307
|
+
else if (typeof raw.header === 'string' && raw.header && !detail) detail = raw.header;
|
|
308
|
+
if (kind === 'error' && raw.text === 'turn-error') detail = t.bodyUnknownError;
|
|
309
|
+
if (kind === 'turn' && (detail === '' || detail === undefined)) {
|
|
310
|
+
const reason = typeof raw.reason === 'string' ? raw.reason : '';
|
|
311
|
+
detail = t.reasonLabels[reason] || t.bodyTurn;
|
|
312
|
+
}
|
|
313
|
+
if (kind === 'approval') {
|
|
314
|
+
const tool = typeof raw.toolName === 'string' && raw.toolName ? raw.toolName : t.approvalToolFallback;
|
|
315
|
+
const hint = approvalDetailOf(raw, shared.locale);
|
|
316
|
+
detail = hint ? `${tool} · ${hint}` : tool;
|
|
317
|
+
}
|
|
318
|
+
if (kind === 'subagent') {
|
|
319
|
+
const stop = typeof raw.stopReason === 'string' ? raw.stopReason : '';
|
|
320
|
+
detail = stop === 'completed' || stop === '' ? '' : (t.reasonLabels[stop] || stop);
|
|
321
|
+
}
|
|
322
|
+
} else if (kind === 'sessionDone') {
|
|
323
|
+
detail = t.bodySessionDone;
|
|
324
|
+
} else if (kind === 'idle') {
|
|
325
|
+
detail = t.bodyIdle;
|
|
326
|
+
} else if (kind === 'question') {
|
|
327
|
+
detail = t.bodyQuestion;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Summary off: the banner carries the label only, so the notification
|
|
331
|
+
// center never accumulates work content.
|
|
332
|
+
if (!showDetail) {
|
|
333
|
+
return { title, body: label || title, kind };
|
|
334
|
+
}
|
|
335
|
+
const body = detail ? truncate(`${label ? `${label} · ` : ''}${detail}`, BODY_MAX) : title;
|
|
336
|
+
return { title, body, kind };
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// ---- notification delivery ------------------------------------------------
|
|
340
|
+
|
|
341
|
+
/** Held notifications: informational and blocking are tracked separately. */
|
|
342
|
+
const infoLive = new Map();
|
|
343
|
+
const blockingLive = new Map();
|
|
344
|
+
const HOLD_MS = 5 * 60 * 1000;
|
|
345
|
+
|
|
346
|
+
function release(map, sessionId, notification) {
|
|
347
|
+
if (map.get(sessionId)?.notification === notification) map.delete(sessionId);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
function sweep() {
|
|
351
|
+
const now = Date.now();
|
|
352
|
+
for (const [key, entry] of infoLive) if (now - entry.at >= HOLD_MS) infoLive.delete(key);
|
|
353
|
+
for (const [key, entry] of blockingLive) if (now - entry.at >= HOLD_MS) blockingLive.delete(key);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
let openSession = null;
|
|
357
|
+
|
|
358
|
+
function focusSession(sessionId) {
|
|
359
|
+
try { window.focus(); } catch { /* ignore */ }
|
|
360
|
+
if (!sessionId || typeof openSession !== 'function') return false;
|
|
361
|
+
try {
|
|
362
|
+
openSession(sessionId);
|
|
363
|
+
return true;
|
|
364
|
+
} catch {
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Raise the actual system notification, with replacement rules applied. */
|
|
370
|
+
function deliver(notice) {
|
|
371
|
+
if (typeof Notification === 'undefined') return false;
|
|
372
|
+
if (Notification.permission !== 'granted') return false;
|
|
373
|
+
// Read the persisted flag live: another tab may have toggled it.
|
|
374
|
+
const enabled = readEnabled();
|
|
375
|
+
if (enabled !== shared.enabled) setShared({ enabled });
|
|
376
|
+
if (!enabled) return false;
|
|
377
|
+
|
|
378
|
+
sweep();
|
|
379
|
+
const { title, body, kind } = compose(notice);
|
|
380
|
+
const sessionId = String(notice.sessionId || '');
|
|
381
|
+
const blocking = notice.priority === 'blocking' || kind === 'approval' || kind === 'question';
|
|
382
|
+
const target = blocking ? blockingLive : infoLive;
|
|
383
|
+
|
|
384
|
+
// A blocking card is only replaced by another blocking card; an
|
|
385
|
+
// informational notice must never close the approval the user still has
|
|
386
|
+
// to act on (closing removes it from the Windows action center).
|
|
387
|
+
if (blocking) {
|
|
388
|
+
const previous = blockingLive.get(sessionId);
|
|
389
|
+
if (previous) {
|
|
390
|
+
try { previous.notification.close(); } catch { /* already closed */ }
|
|
391
|
+
blockingLive.delete(sessionId);
|
|
392
|
+
}
|
|
393
|
+
} else {
|
|
394
|
+
const previous = infoLive.get(sessionId);
|
|
395
|
+
if (previous) {
|
|
396
|
+
try { previous.notification.close(); } catch { /* already closed */ }
|
|
397
|
+
infoLive.delete(sessionId);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const options = {
|
|
402
|
+
body,
|
|
403
|
+
// The tone is synthesized below; suppressing the OS chime keeps a
|
|
404
|
+
// single sound and makes the sound switch honest.
|
|
405
|
+
silent: true,
|
|
406
|
+
requireInteraction: blocking,
|
|
407
|
+
};
|
|
408
|
+
if (tagSupported()) options.tag = `dsh-session-notifier:${sessionId}:${blocking ? 'blocking' : 'info'}`;
|
|
409
|
+
|
|
410
|
+
let notification;
|
|
411
|
+
try {
|
|
412
|
+
notification = new Notification(title, options);
|
|
413
|
+
} catch {
|
|
414
|
+
try {
|
|
415
|
+
notification = new Notification(title, { body, silent: true });
|
|
416
|
+
} catch {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
playSound(kind, shared.config ? shared.config.sound !== false : true);
|
|
422
|
+
|
|
423
|
+
const entry = { notification, at: Date.now() };
|
|
424
|
+
target.set(sessionId, entry);
|
|
425
|
+
|
|
426
|
+
notification.onclick = () => {
|
|
427
|
+
release(infoLive, sessionId, notification);
|
|
428
|
+
release(blockingLive, sessionId, notification);
|
|
429
|
+
if (!focusSession(sessionId)) {
|
|
430
|
+
try { window.focus(); } catch { /* ignore */ }
|
|
431
|
+
}
|
|
432
|
+
try { notification.close(); } catch { /* ignore */ }
|
|
433
|
+
};
|
|
434
|
+
notification.onclose = () => {
|
|
435
|
+
release(infoLive, sessionId, notification);
|
|
436
|
+
release(blockingLive, sessionId, notification);
|
|
437
|
+
};
|
|
438
|
+
notification.onerror = () => {
|
|
439
|
+
release(infoLive, sessionId, notification);
|
|
440
|
+
release(blockingLive, sessionId, notification);
|
|
441
|
+
};
|
|
442
|
+
return true;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Raise one sample notification so the user can confirm permission, sound
|
|
447
|
+
* and icon. Ignores the hidden-only gate: a test must be visible now.
|
|
448
|
+
*/
|
|
449
|
+
const sendTestNotification = async () => {
|
|
450
|
+
if (typeof Notification === 'undefined') {
|
|
451
|
+
setShared({ status: strings().off });
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
let permission = permissionState();
|
|
455
|
+
if (permission === 'default') permission = await requestPermission();
|
|
456
|
+
setShared({ permission });
|
|
457
|
+
if (permission !== 'granted') {
|
|
458
|
+
setShared({ status: permission === 'denied' ? '权限被拒绝,请在浏览器站点设置里允许通知' : '未授予通知权限' });
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
const t = strings();
|
|
462
|
+
const stamp = new Date().toLocaleTimeString();
|
|
463
|
+
const body = `${t.bodyTest}(${stamp})`;
|
|
464
|
+
// Same silent + fixed tag as real notices, so what is heard matches.
|
|
465
|
+
const options = { body, silent: true };
|
|
466
|
+
if (tagSupported()) options.tag = 'dsh-session-notifier:test';
|
|
467
|
+
try {
|
|
468
|
+
const notification = new Notification(t.titleTest, options);
|
|
469
|
+
notification.onclick = () => {
|
|
470
|
+
try { window.focus(); } catch { /* ignore */ }
|
|
471
|
+
};
|
|
472
|
+
} catch {
|
|
473
|
+
setShared({ status: '通知发送失败' });
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
playSound('turn', shared.config ? shared.config.sound !== false : true);
|
|
477
|
+
setShared({ status: `已发送测试通知 ${stamp}` });
|
|
478
|
+
};
|
|
479
|
+
|
|
480
|
+
let source = null;
|
|
481
|
+
let retry = null;
|
|
482
|
+
let stopped = false;
|
|
483
|
+
|
|
484
|
+
const fetchConfig = async () => {
|
|
485
|
+
try {
|
|
486
|
+
const response = await fetch(`${CHANNEL}/config`, { credentials: 'same-origin' });
|
|
487
|
+
if (!response.ok) return;
|
|
488
|
+
const payload = await response.json();
|
|
489
|
+
setShared({ config: payload.config || null, bounds: payload.bounds || null });
|
|
490
|
+
} catch {
|
|
491
|
+
/* the panel still renders its defaults */
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
|
|
495
|
+
const saveConfig = async (patch) => {
|
|
496
|
+
try {
|
|
497
|
+
const response = await fetch(`${CHANNEL}/config`, {
|
|
498
|
+
method: 'POST',
|
|
499
|
+
credentials: 'same-origin',
|
|
500
|
+
headers: { 'content-type': 'application/json' },
|
|
501
|
+
body: JSON.stringify(patch),
|
|
502
|
+
});
|
|
503
|
+
if (!response.ok) {
|
|
504
|
+
setShared({ status: '设置保存失败' });
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
const payload = await response.json();
|
|
508
|
+
setShared({
|
|
509
|
+
config: payload.config || null,
|
|
510
|
+
bounds: payload.bounds || null,
|
|
511
|
+
// The Host answers before it persists (the write restarts the plugin
|
|
512
|
+
// and would otherwise cut this response), so the honest wording is
|
|
513
|
+
// "applied"; durability is confirmed by the next reload.
|
|
514
|
+
status: '已保存',
|
|
515
|
+
});
|
|
516
|
+
} catch {
|
|
517
|
+
setShared({ status: '设置保存失败' });
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
|
|
521
|
+
const connect = () => {
|
|
522
|
+
if (stopped) return;
|
|
523
|
+
try {
|
|
524
|
+
source = new EventSource(`${CHANNEL}/events`);
|
|
525
|
+
} catch {
|
|
526
|
+
retry = reconnectLater();
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
source.onmessage = (message) => {
|
|
531
|
+
let frame;
|
|
532
|
+
try {
|
|
533
|
+
frame = JSON.parse(message.data);
|
|
534
|
+
} catch {
|
|
535
|
+
return;
|
|
536
|
+
}
|
|
537
|
+
if (frame && frame.type === 'hello') {
|
|
538
|
+
if (typeof frame.notifyOnlyWhenHidden === 'boolean') {
|
|
539
|
+
window.__dshNotifierOnlyWhenHidden = frame.notifyOnlyWhenHidden;
|
|
540
|
+
}
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (frame && frame.type === 'settings') {
|
|
544
|
+
setShared({ config: frame.config || null });
|
|
545
|
+
return;
|
|
546
|
+
}
|
|
547
|
+
if (!frame || typeof frame.kind !== 'string') return;
|
|
548
|
+
|
|
549
|
+
// Re-read the persisted flag and permission so a toggle from another
|
|
550
|
+
// tab, or a revoked grant, takes effect without a reload.
|
|
551
|
+
const enabled = readEnabled();
|
|
552
|
+
if (enabled !== shared.enabled) setShared({ enabled });
|
|
553
|
+
if (!enabled) return;
|
|
554
|
+
const permission = permissionState();
|
|
555
|
+
if (permission !== shared.permission) setShared({ permission });
|
|
556
|
+
if (permission !== 'granted') return;
|
|
557
|
+
|
|
558
|
+
// Blocking notices always arrive; informational ones respect the
|
|
559
|
+
// hidden gate and never interrupt while the user watches that session.
|
|
560
|
+
const blocking = frame.priority === 'blocking';
|
|
561
|
+
if (!blocking) {
|
|
562
|
+
if (window.__dshNotifierOnlyWhenHidden !== false && !watchingNow()) {
|
|
563
|
+
/* the user is away: this is exactly when to notify */
|
|
564
|
+
} else if (window.__dshNotifierOnlyWhenHidden !== false && watchingNow()) {
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
deliver(frame);
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
source.onerror = () => {
|
|
572
|
+
try { source.close(); } catch { /* ignore */ }
|
|
573
|
+
if (!stopped) retry = reconnectLater();
|
|
574
|
+
};
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
function reconnectLater() {
|
|
578
|
+
return setTimeout(() => connect(), 5000);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
// ---- header control + settings popover ------------------------------------
|
|
582
|
+
|
|
583
|
+
const SETTING_ROWS = [
|
|
584
|
+
{ key: 'notifyAssistant', label: '助手新回复', hint: '一条回复完成后提醒' },
|
|
585
|
+
{ key: 'notifyTurn', label: '轮次完成', hint: '一轮结束收尾时提醒' },
|
|
586
|
+
{ key: 'notifySessionDone', label: '会话结束', hint: '会话离开运行列表时提醒' },
|
|
587
|
+
{ key: 'notifyApproval', label: '需要确认', hint: '工具执行等待你批准,始终送达' },
|
|
588
|
+
{ key: 'notifyQuestion', label: '需要回答', hint: '会话向你提问,始终送达' },
|
|
589
|
+
{ key: 'notifyError', label: '会话出错', hint: '异常退出时提醒,不受免打扰限制' },
|
|
590
|
+
{ key: 'notifySubagent', label: '子代理完成', hint: '子任务收尾时提醒' },
|
|
591
|
+
{ key: 'notifyIdle', label: '转为空闲', hint: '任务停止运行时提醒' },
|
|
592
|
+
{ key: 'notifyOnlyWhenHidden', label: '仅页面隐藏时提醒', hint: '关闭后,正在看的会话也会弹通知' },
|
|
593
|
+
{ key: 'sound', label: '提示音', hint: '按事件类型合成音色,关闭则完全静音' },
|
|
594
|
+
{ key: 'summary', label: '正文显示详情', hint: '关闭后横幅只显示会话名,内容不进通知中心' },
|
|
595
|
+
];
|
|
596
|
+
|
|
597
|
+
const NUMERIC_ROWS = [
|
|
598
|
+
{ key: 'dedupeWindowMs', label: '合并窗口', unit: 'ms', min: 0, max: 60000, step: 500 },
|
|
599
|
+
{ key: 'maxBodyChars', label: '正文长度', unit: '字', min: 20, max: 400, step: 10 },
|
|
600
|
+
];
|
|
601
|
+
|
|
602
|
+
const styles = {
|
|
603
|
+
trigger: {
|
|
604
|
+
display: 'inline-flex',
|
|
605
|
+
alignItems: 'center',
|
|
606
|
+
justifyContent: 'center',
|
|
607
|
+
width: '28px',
|
|
608
|
+
height: '28px',
|
|
609
|
+
padding: 0,
|
|
610
|
+
border: 'none',
|
|
611
|
+
borderRadius: '6px',
|
|
612
|
+
background: 'transparent',
|
|
613
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
614
|
+
cursor: 'pointer',
|
|
615
|
+
flex: 'none',
|
|
616
|
+
},
|
|
617
|
+
panel: {
|
|
618
|
+
position: 'absolute',
|
|
619
|
+
top: 'calc(100% + 6px)',
|
|
620
|
+
right: 0,
|
|
621
|
+
zIndex: 1200,
|
|
622
|
+
width: '316px',
|
|
623
|
+
maxHeight: 'min(70vh, 620px)',
|
|
624
|
+
overflowY: 'auto',
|
|
625
|
+
padding: '12px',
|
|
626
|
+
borderRadius: 'var(--dsw-radius-lg, 12px)',
|
|
627
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
628
|
+
background: 'var(--dsw-specific-menu, #fff)',
|
|
629
|
+
boxShadow: 'var(--dsw-elevation-prominent)',
|
|
630
|
+
color: 'var(--dsw-alias-label-secondary)',
|
|
631
|
+
fontSize: '12px',
|
|
632
|
+
lineHeight: '18px',
|
|
633
|
+
},
|
|
634
|
+
row: {
|
|
635
|
+
display: 'flex',
|
|
636
|
+
alignItems: 'center',
|
|
637
|
+
justifyContent: 'space-between',
|
|
638
|
+
gap: '12px',
|
|
639
|
+
padding: '5px 0',
|
|
640
|
+
},
|
|
641
|
+
rowText: { display: 'flex', flexDirection: 'column', minWidth: 0 },
|
|
642
|
+
rowLabel: { color: 'var(--dsw-alias-label-primary)' },
|
|
643
|
+
rowHint: { color: 'var(--dsw-alias-label-tertiary)', fontSize: '11px', lineHeight: '15px' },
|
|
644
|
+
rule: { borderTop: '1px solid var(--dsw-alias-border-l2)', margin: '8px 0' },
|
|
645
|
+
title: {
|
|
646
|
+
display: 'flex',
|
|
647
|
+
alignItems: 'center',
|
|
648
|
+
justifyContent: 'space-between',
|
|
649
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
650
|
+
fontWeight: 500,
|
|
651
|
+
marginBottom: '8px',
|
|
652
|
+
},
|
|
653
|
+
footer: { marginTop: '10px', display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: '8px' },
|
|
654
|
+
note: { color: 'var(--dsw-alias-label-tertiary)', fontSize: '11px' },
|
|
655
|
+
button: {
|
|
656
|
+
padding: '4px 10px',
|
|
657
|
+
borderRadius: '6px',
|
|
658
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
659
|
+
background: 'var(--dsw-alias-interactive-bg-hover)',
|
|
660
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
661
|
+
font: 'inherit',
|
|
662
|
+
cursor: 'pointer',
|
|
663
|
+
},
|
|
664
|
+
number: {
|
|
665
|
+
width: '76px',
|
|
666
|
+
padding: '3px 6px',
|
|
667
|
+
borderRadius: '6px',
|
|
668
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
669
|
+
background: 'var(--dsw-alias-surface-primary, transparent)',
|
|
670
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
671
|
+
font: 'inherit',
|
|
672
|
+
textAlign: 'right',
|
|
673
|
+
},
|
|
674
|
+
text: {
|
|
675
|
+
width: '68px',
|
|
676
|
+
padding: '3px 6px',
|
|
677
|
+
borderRadius: '6px',
|
|
678
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
679
|
+
background: 'var(--dsw-alias-surface-primary, transparent)',
|
|
680
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
681
|
+
font: 'inherit',
|
|
682
|
+
textAlign: 'center',
|
|
683
|
+
},
|
|
684
|
+
};
|
|
685
|
+
|
|
686
|
+
function Switch({ checked, onChange, label }) {
|
|
687
|
+
return h('button', {
|
|
688
|
+
type: 'button',
|
|
689
|
+
role: 'switch',
|
|
690
|
+
'aria-checked': checked ? 'true' : 'false',
|
|
691
|
+
'aria-label': label,
|
|
692
|
+
onClick: () => onChange(!checked),
|
|
693
|
+
style: {
|
|
694
|
+
position: 'relative',
|
|
695
|
+
flex: 'none',
|
|
696
|
+
width: '34px',
|
|
697
|
+
height: '18px',
|
|
698
|
+
padding: 0,
|
|
699
|
+
border: 'none',
|
|
700
|
+
borderRadius: '999px',
|
|
701
|
+
cursor: 'pointer',
|
|
702
|
+
background: checked ? 'var(--dsw-alias-brand-primary, #5b8def)' : 'var(--dsw-alias-border-l2)',
|
|
703
|
+
transition: 'background .15s ease',
|
|
704
|
+
},
|
|
705
|
+
}, h('span', {
|
|
706
|
+
'aria-hidden': 'true',
|
|
707
|
+
style: {
|
|
708
|
+
position: 'absolute',
|
|
709
|
+
top: '2px',
|
|
710
|
+
left: checked ? '18px' : '2px',
|
|
711
|
+
width: '14px',
|
|
712
|
+
height: '14px',
|
|
713
|
+
borderRadius: '999px',
|
|
714
|
+
background: 'var(--dsw-alias-surface-primary, #fff)',
|
|
715
|
+
transition: 'left .15s ease',
|
|
716
|
+
},
|
|
717
|
+
}));
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
const DEFAULTS_FOR_PANEL = {
|
|
721
|
+
enabled: true,
|
|
722
|
+
notifyAssistant: true,
|
|
723
|
+
notifyTurn: true,
|
|
724
|
+
notifySessionDone: true,
|
|
725
|
+
notifyApproval: true,
|
|
726
|
+
notifyQuestion: true,
|
|
727
|
+
notifyError: true,
|
|
728
|
+
notifySubagent: false,
|
|
729
|
+
notifyIdle: false,
|
|
730
|
+
notifyOnlyWhenHidden: true,
|
|
731
|
+
sound: true,
|
|
732
|
+
summary: true,
|
|
733
|
+
quietHours: false,
|
|
734
|
+
quietStart: '22:00',
|
|
735
|
+
quietEnd: '08:00',
|
|
736
|
+
dedupeWindowMs: 5000,
|
|
737
|
+
maxBodyChars: 96,
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
function SettingsPanel({ onClose }) {
|
|
741
|
+
const [state, setState] = React.useState({ ...shared });
|
|
742
|
+
|
|
743
|
+
React.useEffect(() => {
|
|
744
|
+
const listener = (next) => setState({ ...next });
|
|
745
|
+
listeners.add(listener);
|
|
746
|
+
void fetchConfig();
|
|
747
|
+
return () => listeners.delete(listener);
|
|
748
|
+
}, []);
|
|
749
|
+
|
|
750
|
+
React.useEffect(() => {
|
|
751
|
+
const onKeyDown = (event) => {
|
|
752
|
+
if (event.key === 'Escape') onClose();
|
|
753
|
+
};
|
|
754
|
+
document.addEventListener('keydown', onKeyDown);
|
|
755
|
+
return () => document.removeEventListener('keydown', onKeyDown);
|
|
756
|
+
}, [onClose]);
|
|
757
|
+
|
|
758
|
+
const config = state.config || DEFAULTS_FOR_PANEL;
|
|
759
|
+
const bounds = state.bounds || {};
|
|
760
|
+
const t = strings();
|
|
761
|
+
|
|
762
|
+
/**
|
|
763
|
+
* Apply the change locally at once, then persist.
|
|
764
|
+
*
|
|
765
|
+
* Persisting through the Host restarts this plugin (the config editor
|
|
766
|
+
* reconciles profile patches), so the authoritative echo can lag by a
|
|
767
|
+
* whole reload. Tracking the switch optimistically keeps it responsive
|
|
768
|
+
* and the Host response simply confirms the same value.
|
|
769
|
+
*/
|
|
770
|
+
const setConfig = (localPatch, remotePatch = localPatch) => {
|
|
771
|
+
setState((previous) => ({
|
|
772
|
+
...previous,
|
|
773
|
+
config: { ...(previous.config || DEFAULTS_FOR_PANEL), ...localPatch },
|
|
774
|
+
}));
|
|
775
|
+
// Keep the shared snapshot in step so a reconnect or a settings echo
|
|
776
|
+
// cannot snap the switch back to the pre-click value.
|
|
777
|
+
setShared({ config: { ...(shared.config || DEFAULTS_FOR_PANEL), ...localPatch } });
|
|
778
|
+
void saveConfig(remotePatch);
|
|
779
|
+
};
|
|
780
|
+
|
|
781
|
+
const enableToggle = async () => {
|
|
782
|
+
if (!state.enabled) {
|
|
783
|
+
writeEnabled(true);
|
|
784
|
+
const result = await requestPermission();
|
|
785
|
+
setShared({ enabled: true, permission: result, status: result === 'granted' ? '已开启系统通知' : `权限未授予:${result}` });
|
|
786
|
+
return;
|
|
787
|
+
}
|
|
788
|
+
if (state.permission !== 'granted') {
|
|
789
|
+
const result = await requestPermission();
|
|
790
|
+
setShared({ enabled: true, permission: result, status: result === 'granted' ? '已开启系统通知' : `权限未授予:${result}` });
|
|
791
|
+
return;
|
|
792
|
+
}
|
|
793
|
+
writeEnabled(false);
|
|
794
|
+
setShared({ enabled: false, status: '已关闭系统通知' });
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
const permissionLine = () => {
|
|
798
|
+
if (state.permission === 'unsupported') return '当前浏览器不支持系统通知';
|
|
799
|
+
if (state.permission === 'granted') return '系统通知权限已授予';
|
|
800
|
+
if (state.permission === 'denied') return '权限被拒绝,请在浏览器站点设置里允许通知';
|
|
801
|
+
return '尚未授予系统通知权限';
|
|
802
|
+
};
|
|
803
|
+
|
|
804
|
+
const head = h('div', { style: styles.title }, [
|
|
805
|
+
h('span', { key: 'k' }, t.panelTitle),
|
|
806
|
+
h('button', {
|
|
807
|
+
key: 'x',
|
|
808
|
+
type: 'button',
|
|
809
|
+
'aria-label': '关闭设置',
|
|
810
|
+
onClick: onClose,
|
|
811
|
+
style: { ...styles.trigger, width: '22px', height: '22px' },
|
|
812
|
+
}, '×'),
|
|
813
|
+
]);
|
|
814
|
+
|
|
815
|
+
const enableRow = h('div', { style: styles.row, key: 'enable' }, [
|
|
816
|
+
h('div', { key: 't', style: styles.rowText }, [
|
|
817
|
+
h('span', { key: 'l', style: styles.rowLabel }, t.enable),
|
|
818
|
+
h('span', { key: 'h', style: styles.rowHint }, permissionLine()),
|
|
819
|
+
]),
|
|
820
|
+
h(Switch, {
|
|
821
|
+
key: 's',
|
|
822
|
+
checked: state.enabled && state.permission === 'granted',
|
|
823
|
+
onChange: () => void enableToggle(),
|
|
824
|
+
label: t.enable,
|
|
825
|
+
}),
|
|
826
|
+
]);
|
|
827
|
+
|
|
828
|
+
const toggleRows = SETTING_ROWS.map((row) => h('div', { style: styles.row, key: row.key }, [
|
|
829
|
+
h('div', { key: 't', style: styles.rowText }, [
|
|
830
|
+
h('span', { key: 'l', style: styles.rowLabel }, row.label),
|
|
831
|
+
h('span', { key: 'h', style: styles.rowHint }, row.hint),
|
|
832
|
+
]),
|
|
833
|
+
h(Switch, {
|
|
834
|
+
key: 's',
|
|
835
|
+
checked: config[row.key] === true,
|
|
836
|
+
// Apply locally first so the switch tracks the click immediately,
|
|
837
|
+
// then let the Host confirm. Without this the control looks dead
|
|
838
|
+
// until the round-trip lands.
|
|
839
|
+
onChange: (next) => setConfig({ [row.key]: next }, { [row.key]: next }),
|
|
840
|
+
label: row.label,
|
|
841
|
+
}),
|
|
842
|
+
]));
|
|
843
|
+
|
|
844
|
+
const numberRows = NUMERIC_ROWS.map((row) => {
|
|
845
|
+
const limit = bounds[row.key] || row;
|
|
846
|
+
return h('div', { style: styles.row, key: row.key }, [
|
|
847
|
+
h('div', { key: 't', style: styles.rowText }, [
|
|
848
|
+
h('span', { key: 'l', style: styles.rowLabel }, row.label),
|
|
849
|
+
h('span', { key: 'h', style: styles.rowHint }, `${limit.min}–${limit.max} ${row.unit}`),
|
|
850
|
+
]),
|
|
851
|
+
h('input', {
|
|
852
|
+
key: 'i',
|
|
853
|
+
type: 'number',
|
|
854
|
+
min: limit.min,
|
|
855
|
+
max: limit.max,
|
|
856
|
+
step: row.step,
|
|
857
|
+
value: String(config[row.key] ?? limit.min),
|
|
858
|
+
'aria-label': row.label,
|
|
859
|
+
style: styles.number,
|
|
860
|
+
onChange: (event) => {
|
|
861
|
+
const value = Number(event.target.value);
|
|
862
|
+
if (!Number.isFinite(value)) return;
|
|
863
|
+
setConfig({ [row.key]: value });
|
|
864
|
+
},
|
|
865
|
+
}),
|
|
866
|
+
]);
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
// Quiet hours: an overnight range silences informational notices only.
|
|
870
|
+
const quietRows = [
|
|
871
|
+
h('div', { style: styles.row, key: 'quietHours' }, [
|
|
872
|
+
h('div', { key: 't', style: styles.rowText }, [
|
|
873
|
+
h('span', { key: 'l', style: styles.rowLabel }, '免打扰时段'),
|
|
874
|
+
h('span', { key: 'h', style: styles.rowHint }, '只静音信息类;审批、提问与出错不受影响'),
|
|
875
|
+
]),
|
|
876
|
+
h(Switch, {
|
|
877
|
+
key: 's',
|
|
878
|
+
checked: config.quietHours === true,
|
|
879
|
+
onChange: (next) => setConfig({ quietHours: next }),
|
|
880
|
+
label: '免打扰时段',
|
|
881
|
+
}),
|
|
882
|
+
]),
|
|
883
|
+
];
|
|
884
|
+
if (config.quietHours === true) {
|
|
885
|
+
quietRows.push(h('div', { style: styles.row, key: 'quietRange' }, [
|
|
886
|
+
h('div', { key: 't', style: styles.rowText }, [
|
|
887
|
+
h('span', { key: 'l', style: styles.rowLabel }, '起止时间'),
|
|
888
|
+
h('span', { key: 'h', style: styles.rowHint }, '支持跨午夜,如 22:00 → 08:00'),
|
|
889
|
+
]),
|
|
890
|
+
h('div', { key: 'i', style: { display: 'flex', alignItems: 'center', gap: '6px' } }, [
|
|
891
|
+
h('input', {
|
|
892
|
+
key: 's',
|
|
893
|
+
type: 'time',
|
|
894
|
+
value: String(config.quietStart || '22:00'),
|
|
895
|
+
'aria-label': '免打扰开始时间',
|
|
896
|
+
style: styles.text,
|
|
897
|
+
onChange: (event) => setConfig({ quietStart: event.target.value }),
|
|
898
|
+
}),
|
|
899
|
+
h('span', { key: 'd', style: { color: 'var(--dsw-alias-label-tertiary)' } }, '→'),
|
|
900
|
+
h('input', {
|
|
901
|
+
key: 'e',
|
|
902
|
+
type: 'time',
|
|
903
|
+
value: String(config.quietEnd || '08:00'),
|
|
904
|
+
'aria-label': '免打扰结束时间',
|
|
905
|
+
style: styles.text,
|
|
906
|
+
onChange: (event) => setConfig({ quietEnd: event.target.value }),
|
|
907
|
+
}),
|
|
908
|
+
]),
|
|
909
|
+
]));
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
const testButton = h('button', {
|
|
913
|
+
key: 'test',
|
|
914
|
+
type: 'button',
|
|
915
|
+
style: { ...styles.button, width: '100%', marginTop: '2px' },
|
|
916
|
+
onClick: () => void sendTestNotification(),
|
|
917
|
+
}, t.testButton);
|
|
918
|
+
|
|
919
|
+
return h('div', {
|
|
920
|
+
role: 'dialog',
|
|
921
|
+
'aria-label': t.panelTitle,
|
|
922
|
+
style: styles.panel,
|
|
923
|
+
onClick: (event) => event.stopPropagation(),
|
|
924
|
+
}, [
|
|
925
|
+
head,
|
|
926
|
+
enableRow,
|
|
927
|
+
h('div', { key: 'r1', style: styles.rule, 'aria-hidden': 'true' }),
|
|
928
|
+
...toggleRows,
|
|
929
|
+
h('div', { key: 'r2', style: styles.rule, 'aria-hidden': 'true' }),
|
|
930
|
+
...quietRows,
|
|
931
|
+
h('div', { key: 'r3', style: styles.rule, 'aria-hidden': 'true' }),
|
|
932
|
+
...numberRows,
|
|
933
|
+
h('div', { key: 'r4', style: styles.rule, 'aria-hidden': 'true' }),
|
|
934
|
+
testButton,
|
|
935
|
+
h('div', { key: 'f', style: styles.footer }, [
|
|
936
|
+
h('span', { key: 'n', style: styles.note }, state.status || '设置在主机端持久化'),
|
|
937
|
+
h('button', {
|
|
938
|
+
key: 'b',
|
|
939
|
+
type: 'button',
|
|
940
|
+
style: styles.button,
|
|
941
|
+
onClick: () => void fetchConfig(),
|
|
942
|
+
}, t.refresh),
|
|
943
|
+
]),
|
|
944
|
+
]);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
function NotifierButton() {
|
|
948
|
+
const [open, setOpen] = React.useState(false);
|
|
949
|
+
const [state, setState] = React.useState({ ...shared });
|
|
950
|
+
const rootRef = React.useRef(null);
|
|
951
|
+
|
|
952
|
+
React.useEffect(() => {
|
|
953
|
+
const listener = (next) => setState({ ...next });
|
|
954
|
+
listeners.add(listener);
|
|
955
|
+
const refresh = () => setShared({ permission: permissionState() });
|
|
956
|
+
const timer = setInterval(refresh, 2000);
|
|
957
|
+
return () => {
|
|
958
|
+
listeners.delete(listener);
|
|
959
|
+
clearInterval(timer);
|
|
960
|
+
};
|
|
961
|
+
}, []);
|
|
962
|
+
|
|
963
|
+
React.useEffect(() => {
|
|
964
|
+
if (!open) return;
|
|
965
|
+
const onPointerDown = (event) => {
|
|
966
|
+
const root = rootRef.current;
|
|
967
|
+
if (root && !root.contains(event.target)) setOpen(false);
|
|
968
|
+
};
|
|
969
|
+
document.addEventListener('pointerdown', onPointerDown);
|
|
970
|
+
return () => document.removeEventListener('pointerdown', onPointerDown);
|
|
971
|
+
}, [open]);
|
|
972
|
+
|
|
973
|
+
React.useEffect(() => {
|
|
974
|
+
if (open) void fetchConfig();
|
|
975
|
+
}, [open]);
|
|
976
|
+
|
|
977
|
+
// Blocking notices still pending: shown as a badge so the bell is
|
|
978
|
+
// actionable even when the notification center was dismissed.
|
|
979
|
+
const armed = state.enabled && state.permission === 'granted';
|
|
980
|
+
const label = armed ? '会话通知已开启' : '会话通知未开启';
|
|
981
|
+
|
|
982
|
+
return h('div', { ref: rootRef, style: { position: 'relative', display: 'inline-flex' } }, [
|
|
983
|
+
h('button', {
|
|
984
|
+
key: 'trigger',
|
|
985
|
+
type: 'button',
|
|
986
|
+
title: `${label} · 点击配置`,
|
|
987
|
+
'aria-label': label,
|
|
988
|
+
'aria-haspopup': 'dialog',
|
|
989
|
+
'aria-expanded': open ? 'true' : 'false',
|
|
990
|
+
onClick: () => setOpen((value) => !value),
|
|
991
|
+
style: {
|
|
992
|
+
...styles.trigger,
|
|
993
|
+
...(open ? {
|
|
994
|
+
background: 'var(--dsw-alias-interactive-bg-hover)',
|
|
995
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
996
|
+
} : {}),
|
|
997
|
+
},
|
|
998
|
+
}, h('span', { 'aria-hidden': 'true', style: { fontSize: '15px', lineHeight: 1 } }, armed ? '🔔' : '🔕')),
|
|
999
|
+
open ? h(SettingsPanel, { key: 'panel', onClose: () => setOpen(false) }) : null,
|
|
1000
|
+
]);
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
return {
|
|
1004
|
+
inject: ['slots'],
|
|
1005
|
+
// Underscored test seams: let a harness drive delivery and composition
|
|
1006
|
+
// without emulating React. Nothing else uses them.
|
|
1007
|
+
__test: Object.freeze({
|
|
1008
|
+
deliver,
|
|
1009
|
+
compose,
|
|
1010
|
+
sendTestNotification,
|
|
1011
|
+
playSound,
|
|
1012
|
+
shared,
|
|
1013
|
+
settingsView: () => ({ ...shared }),
|
|
1014
|
+
setShared,
|
|
1015
|
+
SettingsPanel,
|
|
1016
|
+
DEFAULTS_FOR_PANEL,
|
|
1017
|
+
}),
|
|
1018
|
+
apply(ctx) {
|
|
1019
|
+
const workspace = ctx.get('uiWorkspace');
|
|
1020
|
+
if (workspace && typeof workspace.openSession === 'function') {
|
|
1021
|
+
openSession = (id) => workspace.openSession(id);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// Follow the interface language so banner copy matches the app.
|
|
1025
|
+
try {
|
|
1026
|
+
const locale = ctx.get('locale');
|
|
1027
|
+
if (locale && typeof locale.getSnapshot === 'function') {
|
|
1028
|
+
const snapshot = locale.getSnapshot();
|
|
1029
|
+
const id = typeof snapshot?.locale === 'string' ? snapshot.locale : '';
|
|
1030
|
+
if (id) setShared({ locale: id.toLowerCase().startsWith('en') ? 'en' : 'zh' });
|
|
1031
|
+
}
|
|
1032
|
+
if (locale && typeof locale.subscribe === 'function') {
|
|
1033
|
+
ctx.effect(() => locale.subscribe(() => {
|
|
1034
|
+
const snapshot = locale.getSnapshot();
|
|
1035
|
+
const id = typeof snapshot?.locale === 'string' ? snapshot.locale : '';
|
|
1036
|
+
if (id) setShared({ locale: id.toLowerCase().startsWith('en') ? 'en' : 'zh' });
|
|
1037
|
+
}), 'dsh-session-notifier: locale');
|
|
1038
|
+
}
|
|
1039
|
+
} catch {
|
|
1040
|
+
/* the panel falls back to Chinese copy */
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
connect();
|
|
1044
|
+
void fetchConfig();
|
|
1045
|
+
|
|
1046
|
+
// Order 0 puts this control left of the theme switcher the bloom theme
|
|
1047
|
+
// prepends into the same utilities row.
|
|
1048
|
+
ctx.slots.inject('conversation.session.header.utilities', () => ctx.slots.register({
|
|
1049
|
+
name: 'conversation.session.header.utilities',
|
|
1050
|
+
id: '@makooo/dsh-session-notifier',
|
|
1051
|
+
order: 0,
|
|
1052
|
+
}, NotifierButton));
|
|
1053
|
+
|
|
1054
|
+
ctx.effect(() => () => {
|
|
1055
|
+
stopped = true;
|
|
1056
|
+
if (retry) clearTimeout(retry);
|
|
1057
|
+
if (source) {
|
|
1058
|
+
try { source.close(); } catch { /* ignore */ }
|
|
1059
|
+
}
|
|
1060
|
+
for (const entry of [...infoLive.values(), ...blockingLive.values()]) {
|
|
1061
|
+
try { entry.notification.close(); } catch { /* ignore */ }
|
|
1062
|
+
}
|
|
1063
|
+
infoLive.clear();
|
|
1064
|
+
blockingLive.clear();
|
|
1065
|
+
listeners.clear();
|
|
1066
|
+
if (audioContext) {
|
|
1067
|
+
try { void audioContext.close(); } catch { /* ignore */ }
|
|
1068
|
+
audioContext = null;
|
|
1069
|
+
}
|
|
1070
|
+
}, '@makooo/dsh-session-notifier client cleanup');
|
|
1071
|
+
},
|
|
1072
|
+
};
|
|
1073
|
+
},
|
|
1074
|
+
});
|