Repository navigation
Expand file tree
/
Copy pathclient.js
More file actions
622 lines (599 loc) · 24.2 KB
/
Copy pathclient.js
File metadata and controls
622 lines (599 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/**
* @hmydk/dsh-notify — browser half.
*
* Runs in the Harness Web page. It opens one SSE stream to the Host half,
* applies the configured focus condition, and shows a native Notification.
* Every delivery attempt is acknowledged back to the Host so an installation
* can be verified end-to-end (`GET /notify/status`).
*
* It also adds a **Notifications** page to the Settings dialog. The page reads
* and writes the plugin row's volatile config through the client `configForms`
* service, so a change made here lands in the profile patch and is streamed to
* the Host half without editing YAML by hand. The page only mounts where that
* service exists; notification delivery keeps working without it.
*
* Loaded through the client module table: the factory runs at materialization
* time and registers the plugin on the page's Cordis tree.
*/
window.__ModuleLoader__.load({
id: '@hmydk/dsh-notify',
factory(require) {
/** Host plugin entry id the settings page reads and writes. */
const NAMESPACE = 'dsh-notify'
/** Notification event ids the config accepts. */
const EVENT_IDS = ['turn-complete', 'approval-requested']
/** A one-line turn summary shorter than this reads as noise, so prefer the prompt. */
const TURN_BODY_MIN = 8
/** Client locale namespace for the settings page copy. */
const LOCALE_NS = 'dsh-notify'
/** Settings page dictionaries; the shared locale service picks the active one. */
const DICTIONARIES = {
en: {
nav: 'Notifications',
title: 'Notifications',
description:
'Native desktop notifications when a root session finishes a turn, or when a tool call is waiting for your approval.',
enable: 'Desktop notifications',
enableHint: 'When off, nothing is announced; approval requests keep waiting as before.',
condition: 'When to alert',
conditionHint:
'The default only alerts while the Harness window is in the background, so a run you are watching stays quiet.',
conditionUnfocused: 'Only when unfocused',
conditionAlways: 'Always',
events: 'Events',
turnComplete: 'Turn complete',
approvalRequested: 'Approval needed',
grace: 'Approval grace',
graceHint:
'How long an approval request may wait before it is announced (0–60000 ms); requests that resolve themselves stay silent.',
graceUnit: 'ms',
test: 'Send a test notification',
testHint: 'Bypasses the focus condition and raises one test notification now.',
readonly:
'This profile fixes the configuration in a higher layer (home patch or command-line overlay); the page is read-only.',
unavailable: 'The Host exposes no editable configuration for this plugin right now.',
loading: 'Reading configuration…',
saveFailed: 'Save failed: the Host rejected this change; try again.',
invalidGrace: 'Enter a number between 0 and 60000 milliseconds.',
},
zh: {
nav: '通知',
title: '通知',
description: '根会话完成一轮任务、或工具调用需要你审批时,弹出系统桌面通知。',
enable: '桌面通知',
enableHint: '关闭后不再弹通知;审批请求仍然按原有流程等待。',
condition: '提醒时机',
conditionHint: '默认只在 Harness 窗口不在前台时提醒,避免盯着屏幕时被打扰。',
conditionUnfocused: '仅窗口失焦时',
conditionAlways: '总是提醒',
events: '提醒事件',
turnComplete: '任务完成',
approvalRequested: '需要审批',
grace: '审批提醒延迟',
graceHint: '审批请求等待多久仍在挂起才提醒(0–60000 毫秒);自动放行的请求不会打扰。',
graceUnit: '毫秒',
test: '发送测试通知',
testHint: '忽略失焦条件,立即弹一条测试通知。',
readonly: '当前 profile 的配置由上层(home patch 或命令行覆盖)决定,这里只能查看。',
unavailable: 'Host 没有提供该插件的可变配置,暂时无法在此修改。',
loading: '正在读取配置…',
saveFailed: '保存失败:Host 拒绝了这次修改,请稍后重试。',
invalidGrace: '请输入 0–60000 之间的毫秒数。',
},
}
/** Detect the notification language once; the settings page follows the app locale. */
const navigatorLanguage =
typeof navigator === 'object' && navigator !== null && typeof navigator.language === 'string'
? navigator.language.toLowerCase()
: ''
const zh = navigatorLanguage.startsWith('zh')
/**
* Mount the Notifications page in the Settings dialog.
*
* The page is optional: it registers only when the client exposes `slots`,
* `locale` and the settings `configForms` service, and a failure here never
* affects notification delivery.
* @param ctx - the browser plugin context.
* @param requireModule - the client module table's `require`.
*/
const mountSettingsPage = (ctx, requireModule) => {
if (typeof requireModule !== 'function' || typeof ctx.inject !== 'function') return
ctx.inject(['slots', 'locale', 'configForms'], (scope) => {
try {
const react = requireModule('react')
const primitives = requireModule('@deepseek-ai/dsh-client-ui-primitives')
const form = scope.configForms.get(NAMESPACE)
const translate = scope.locale.bind(LOCALE_NS)
const NotificationsPage = createNotificationsPage(react, primitives, form, scope.locale, translate)
scope.effect(() => scope.locale.register(LOCALE_NS, DICTIONARIES), 'dsh-notify: dictionaries')
scope.effect(
() =>
scope.configForms.whileServed([NAMESPACE], () =>
scope.slots.inject('settings.section', () =>
scope.slots.register(
{
name: 'settings.section',
id: 'notifications',
order: 18,
label: () => translate('nav'),
},
NotificationsPage,
),
),
),
'dsh-notify: settings page',
)
} catch (error) {
console.warn('[dsh-notify] settings page unavailable:', error)
}
})
}
/**
* Build the Settings page component over one config form.
* @param react - React.
* @param primitives - the shared UI primitives.
* @param form - `configForms.get('dsh-notify')`.
* @param locale - the client locale service (rerenders the page on a language switch).
* @param translate - namespace-bound translation function.
* @returns the page component.
*/
function createNotificationsPage(react, primitives, form, locale, translate) {
const h = react.createElement
const styles = {
page: {
display: 'flex',
flexDirection: 'column',
gap: '20px',
maxWidth: '720px',
padding: '4px 0 24px',
},
header: { display: 'flex', flexDirection: 'column', gap: '6px' },
title: { margin: 0, fontSize: '18px', fontWeight: 600, color: 'var(--dsw-alias-label-primary, inherit)' },
description: {
margin: 0,
fontSize: '13px',
lineHeight: 1.5,
color: 'var(--dsw-alias-label-tertiary, inherit)',
},
card: {
display: 'flex',
flexDirection: 'column',
padding: '0 16px',
border: '0.5px solid var(--dsw-alias-border-l2, rgba(0, 0, 0, 0.12))',
borderRadius: '12px',
background: 'var(--dsw-alias-settings-card-fill, transparent)',
},
row: (last) => ({
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
gap: '24px',
padding: '14px 0',
borderBottom: last ? 'none' : '0.5px solid var(--dsw-alias-border-l2, rgba(0, 0, 0, 0.08))',
}),
rowText: { display: 'flex', flexDirection: 'column', gap: '3px', minWidth: 0 },
rowTitle: { fontSize: '13.5px', fontWeight: 500, color: 'var(--dsw-alias-label-primary, inherit)' },
rowHint: {
fontSize: '12px',
lineHeight: 1.45,
color: 'var(--dsw-alias-label-tertiary, inherit)',
},
control: { display: 'flex', alignItems: 'center', gap: '8px', flex: 'none' },
grace: { display: 'flex', alignItems: 'center', gap: '8px' },
unit: { fontSize: '12px', color: 'var(--dsw-alias-label-tertiary, inherit)' },
note: {
margin: 0,
fontSize: '12px',
lineHeight: 1.45,
color: 'var(--dsw-alias-label-tertiary, inherit)',
},
error: { margin: 0, fontSize: '12px', lineHeight: 1.45, color: 'var(--dsw-alias-state-error-primary, #d14343)' },
}
/** One titled row with a control on its trailing edge. */
const row = (key, title, hint, control, last = false) =>
h(
'div',
{ key, style: styles.row(last) },
h(
'div',
{ style: styles.rowText },
h('span', { style: styles.rowTitle }, title),
hint === undefined ? null : h('span', { style: styles.rowHint }, hint),
),
h('div', { style: styles.control }, control),
)
/**
* The page itself: reads the form snapshot and writes single fields back.
* @returns the rendered page.
*/
return function NotificationsPage() {
const snapshot = react.useSyncExternalStore(
(listener) => form.subscribe(listener),
() => form.getSnapshot(),
() => form.getSnapshot(),
)
// A language switch re-renders the page with the next dictionary.
react.useSyncExternalStore(
(listener) => locale.subscribe(listener),
() => locale.getSnapshot(),
() => locale.getSnapshot(),
)
const [graceDraft, setGraceDraft] = react.useState(undefined)
const [error, setError] = react.useState('')
const [sending, setSending] = react.useState(false)
const value = snapshot.status === 'ready' ? snapshot.value : undefined
const writable = snapshot.writable !== false && value !== undefined
/** Write one field and surface a rejected write. */
const write = (field, next) => {
setError('')
Promise.resolve(form.set(field, next)).then(
(accepted) => {
if (accepted) return
setError(translate('saveFailed'))
},
() => setError(translate('saveFailed')),
)
}
/** Commit the grace input, keeping an invalid draft visible. */
const commitGrace = () => {
if (graceDraft === undefined) return
const parsed = Number(graceDraft)
if (!Number.isFinite(parsed) || parsed < 0 || parsed > 60000) {
setError(translate('invalidGrace'))
return
}
setGraceDraft(undefined)
write('approvalGraceMs', Math.round(parsed))
}
/** Raise one forced test notification through the Host route. */
const sendTest = () => {
setSending(true)
let url
try {
url = new URL('notify/test?kind=turn-complete', document.baseURI).toString()
} catch {
setSending(false)
setError(translate('saveFailed'))
return
}
fetch(url)
.catch(() => {})
.finally(() => setSending(false))
}
if (snapshot.status === 'loading' && value === undefined) {
return h('p', { style: styles.note }, translate('loading'))
}
if (snapshot.status === 'unavailable' || value === undefined) {
return h('p', { style: styles.note }, translate('unavailable'))
}
const events = Array.isArray(value.events) ? value.events : EVENT_IDS
const grace = graceDraft === undefined ? String(value.approvalGraceMs) : graceDraft
return h(
'div',
{ style: styles.page },
h(
'header',
{ style: styles.header },
h('h2', { style: styles.title }, translate('title')),
h('p', { style: styles.description }, translate('description')),
),
h(
'section',
{ style: styles.card },
row(
'enabled',
translate('enable'),
translate('enableHint'),
h(primitives.Switch, {
checked: value.enabled === true,
label: translate('enable'),
disabled: !writable,
onChange: (checked) => write('enabled', checked),
}),
),
row(
'condition',
translate('condition'),
translate('conditionHint'),
h(primitives.SegmentedControl, {
label: translate('condition'),
value: value.condition === 'always' ? 'always' : 'unfocused',
disabled: !writable,
options: [
{ value: 'unfocused', label: translate('conditionUnfocused') },
{ value: 'always', label: translate('conditionAlways') },
],
onChange: (next) => write('condition', next),
}),
),
row(
'events',
translate('events'),
undefined,
h(
'div',
{ style: { display: 'flex', flexDirection: 'column', gap: '8px', alignItems: 'flex-start' } },
h(primitives.Checkbox, {
label: translate('turnComplete'),
checked: events.includes('turn-complete'),
disabled: !writable,
onChange: (checked) => {
const next = checked
? [...new Set([...events, 'turn-complete'])]
: events.filter((event) => event !== 'turn-complete')
write('events', next)
},
}),
h(primitives.Checkbox, {
label: translate('approvalRequested'),
checked: events.includes('approval-requested'),
disabled: !writable,
onChange: (checked) => {
const next = checked
? [...new Set([...events, 'approval-requested'])]
: events.filter((event) => event !== 'approval-requested')
write('events', next)
},
}),
),
),
row(
'grace',
translate('grace'),
translate('graceHint'),
h(
'div',
{ style: styles.grace },
h(primitives.Input, {
type: 'number',
'aria-label': translate('grace'),
min: 0,
max: 60000,
step: 100,
value: grace,
disabled: !writable,
onChange: (event) => setGraceDraft(event.target.value),
onBlur: commitGrace,
onKeyDown: (event) => {
if (event.key === 'Enter') {
event.preventDefault()
commitGrace()
} else if (event.key === 'Escape') {
setGraceDraft(undefined)
}
},
style: { width: '96px' },
}),
h('span', { style: styles.unit }, translate('graceUnit')),
),
false,
),
row(
'test',
translate('test'),
translate('testHint'),
h(
primitives.Button,
{
variant: 'outline',
size: 'sm',
disabled: sending,
onClick: sendTest,
},
translate('test'),
),
true,
),
),
error === '' ? null : h('p', { style: styles.error }, error),
writable ? null : h('p', { style: styles.note }, translate('readonly')),
)
}
}
return {
apply(ctx) {
const NotificationApi = typeof Notification === 'undefined' ? undefined : Notification
if (NotificationApi === undefined) return
let config = {
enabled: true,
condition: 'unfocused',
events: EVENT_IDS,
approvalGraceMs: 2000,
}
let permission = NotificationApi.permission
const requestPermission = async () => {
if (permission === 'granted') return true
if (permission === 'denied') return false
try {
permission = await NotificationApi.requestPermission()
} catch {
return false
}
return permission === 'granted'
}
const routeUrl = (path) => {
try {
return new URL(path, document.baseURI).toString()
} catch {
return undefined
}
}
/**
* Best-effort jump to the session the notification belongs to. The full
* Web GUI exposes `uiWorkspace.openSession`; other shells fall back to
* focusing the window alone.
*/
const revealSession = (sessionId) => {
if (typeof sessionId !== 'string' || sessionId === '') return
try {
const workspace = ctx.get('uiWorkspace')
if (workspace !== undefined && typeof workspace.openSession === 'function') {
workspace.openSession(sessionId)
}
} catch {
// Navigation is best-effort; the click still focuses the window.
}
}
const ack = (event, state) => {
const url = routeUrl('notify/ack')
if (url === undefined) return
try {
void fetch(url, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ id: event.id, type: event.type, state }),
keepalive: true,
}).catch(() => {})
} catch {
// Diagnostics only; a failed ack never affects delivery.
}
}
const compose = (event) => {
if (event.type === 'test') {
return zh
? { title: 'DSH 通知测试', body: '通知已接通:任务完成或需要审批时会提醒你。' }
: {
title: 'DSH notification test',
body: 'Notifications are connected. You will be alerted when a turn finishes or needs your approval.',
}
}
const title =
typeof event.title === 'string' && event.title.trim() !== ''
? event.title.trim()
: zh
? 'DSH 会话'
: 'DSH session'
if (event.type === 'approval-requested') {
// Approval bodies carry what the tool is about to do (command or
// closest argument) plus the localized reason, not the session title.
const tool = typeof event.toolName === 'string' && event.toolName !== '' ? event.toolName : undefined
const detail = typeof event.toolDetail === 'string' && event.toolDetail !== '' ? event.toolDetail : undefined
const display = event.displayReason
const localized =
display !== null && typeof display === 'object'
? (zh ? display.zh : display.en) ?? display.en ?? display.zh
: undefined
const reason =
typeof localized === 'string' && localized !== ''
? localized
: typeof event.reason === 'string' && event.reason !== ''
? event.reason
: undefined
const head = detail === undefined ? tool ?? title : tool === undefined ? detail : `${tool} · ${detail}`
return {
title: zh ? '需要审批' : 'Approval needed',
body: reason === undefined ? head : `${head}\n${reason}`,
}
}
// The body describes the turn that just ended: the Host half sends a
// display-ready preview of its closing reply and its request. Fall
// back to the request, then to the session title.
const response = typeof event.response === 'string' && event.response !== '' ? event.response : undefined
const prompt = typeof event.prompt === 'string' && event.prompt !== '' ? event.prompt : undefined
const body =
(response !== undefined && response.length >= TURN_BODY_MIN ? response : undefined) ??
prompt ??
response ??
title
return { title: zh ? '任务完成' : 'Task complete', body }
}
const show = (event) => {
if (event.type !== 'test') {
if (config.enabled === false) {
ack(event, 'disabled')
return
}
if (Array.isArray(config.events) && !config.events.includes(event.type)) {
ack(event, 'filtered')
return
}
}
const focused =
typeof document !== 'undefined' &&
document.visibilityState === 'visible' &&
document.hasFocus()
const forced = event.force === true || event.type === 'test'
if (!forced && config.condition !== 'always' && focused) {
ack(event, 'suppressed')
return
}
void requestPermission().then((granted) => {
if (!granted) {
ack(event, 'unsupported')
return
}
const { title, body } = compose(event)
let notification
try {
notification = new NotificationApi(title, {
body,
tag: typeof event.sessionId === 'string' ? `dsh-notify:${event.sessionId}` : 'dsh-notify',
silent: false,
})
} catch {
ack(event, 'failed')
return
}
notification.onclick = () => {
try {
window.focus()
} catch {
// Browsers may refuse to focus a background window.
}
revealSession(event.sessionId)
try {
notification.close()
} catch {
// Ignore a double close.
}
ack(event, 'clicked')
}
ack(event, 'shown')
})
}
ctx.effect(() => {
const url = routeUrl('notify/events')
if (url === undefined) return
let source
try {
source = new EventSource(url)
} catch {
return
}
const onConfig = (raw) => {
try {
const parsed = JSON.parse(raw.data)
if (parsed !== null && typeof parsed === 'object') config = { ...config, ...parsed }
} catch {
// Ignore malformed config frames.
}
}
const onNotify = (raw) => {
try {
const event = JSON.parse(raw.data)
if (event !== null && typeof event === 'object' && typeof event.type === 'string') show(event)
} catch {
// Ignore malformed notification frames.
}
}
source.addEventListener('config', onConfig)
source.addEventListener('notify', onNotify)
return () => {
try {
source.close()
} catch {
// Already closed.
}
}
})
// The settings page is additive: without the settings client services
// it is skipped, and notification delivery above is unaffected.
try {
mountSettingsPage(ctx, require)
} catch (error) {
console.warn('[dsh-notify] settings page unavailable:', error)
}
},
}
},
})