dsh-notify-tray 0.1.12 → 0.1.13

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/lib/client.js CHANGED
@@ -1,13 +1,7 @@
1
1
  // dsh-notify-tray, browser half (hand-written in DSH's client module format,
2
2
  // so no build step is needed).
3
3
  //
4
- // Polls the host's `notify-tray/state` route (its light `lite=1` form), shows
5
- // a native Notification for each new notice (completed / failed / approval /
6
- // choice / input; one per session, closed again once a question is answered)
7
- // and opens the session when a notification — or a tray menu row — is
8
- // clicked. In the
9
- // desktop app these notifications are OS-native and attributed to
10
- // "DeepSeek Harness".
4
+ // The window reports its visible session and opens explicit native notification clicks.
11
5
  window.__ModuleLoader__.load({
12
6
  id: 'dsh-notify-tray',
13
7
  factory: (require) => {
@@ -16,9 +10,7 @@ window.__ModuleLoader__.load({
16
10
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })
17
11
 
18
12
  var ROUTE = 'notify-tray/state'
19
- var RAISE_ROUTE = 'notify-tray/raise'
20
13
  var POLL_MS = 2000
21
- var SEEN_KEY = 'dsh-notify-tray:seen'
22
14
 
23
15
  function mainSessionId(sessions) {
24
16
  try {
@@ -31,17 +23,6 @@ window.__ModuleLoader__.load({
31
23
  return undefined
32
24
  }
33
25
 
34
- // Cross-tab dedupe: one notification per host event, whichever page sees it first.
35
- function claim(boot, seq) {
36
- try {
37
- var raw = localStorage.getItem(SEEN_KEY)
38
- var seen = raw ? JSON.parse(raw) : {}
39
- if (seen.boot === boot && seen.seq >= seq) return false
40
- localStorage.setItem(SEEN_KEY, JSON.stringify({ boot: boot, seq: seq }))
41
- } catch (e) {}
42
- return true
43
- }
44
-
45
26
  // Settings entries mount when their slots and the host namespace are ready.
46
27
  var inject = ['uiWorkspace', 'sessions', 'slots', 'locale', 'configForms']
47
28
 
@@ -216,16 +197,9 @@ window.__ModuleLoader__.load({
216
197
  planResetAtHint: '例如「今晚 20:00」「每周一」。',
217
198
 
218
199
  advanced: '高级设置',
219
- advancedHint: '通知通道、花费算法、自定义单价、刷新间隔等,一般不用改。',
200
+ advancedHint: '花费算法、自定义单价、刷新间隔等,一般不用改。',
220
201
  advancedChanged: '已改 {n} 项',
221
202
  groupNotify: '通知',
222
- channel: '通知从哪里弹',
223
- channelAuto: '自动',
224
- channelClient: '只用窗口',
225
- channelSystem: '只用系统',
226
- channelBoth: '都弹',
227
- channelOff: '不弹',
228
- channelHint: '自动:开着 DSH 窗口就由窗口弹,否则由系统弹。',
229
203
  suppressWhenFocused: '正看着这个会话时不弹',
230
204
  suppressWhenFocusedHint: '关掉后,哪怕你正看着它跑完也会弹一条。',
231
205
  groupTray: '状态栏',
@@ -393,16 +367,9 @@ window.__ModuleLoader__.load({
393
367
  planResetAtHint: 'e.g. "20:00 tonight", "Mondays".',
394
368
 
395
369
  advanced: 'Advanced',
396
- advancedHint: 'Notification channel, spend method, custom prices, refresh intervals — rarely needed.',
370
+ advancedHint: 'Spend method, custom prices, refresh intervals — rarely needed.',
397
371
  advancedChanged: '{n} changed',
398
372
  groupNotify: 'Notifications',
399
- channel: 'Notification channel',
400
- channelAuto: 'Auto',
401
- channelClient: 'Window only',
402
- channelSystem: 'System only',
403
- channelBoth: 'Both',
404
- channelOff: 'Off',
405
- channelHint: 'Auto: the DSH window notifies while it is open; the OS does otherwise.',
406
373
  suppressWhenFocused: 'Stay quiet while I am looking at that session',
407
374
  suppressWhenFocusedHint: 'Off: notify even when you watch it finish.',
408
375
  groupTray: 'Status bar',
@@ -485,7 +452,7 @@ window.__ModuleLoader__.load({
485
452
  /** The five "notify me when" switches, shown as one row of checkboxes. */
486
453
  const NOTIFY_FIELDS = ['notifyCompleted', 'notifyChoice', 'notifyInput', 'notifyApproval', 'notifyFailed']
487
454
  /** Fields behind the "Advanced" fold (counted for its "n changed" tag). */
488
- const ADVANCED_FIELDS = ['channel', 'suppressWhenFocused', 'showSpendInTitle', 'errorHoldMinutes', 'spendSource', 'currency', 'balancePollMinutes', 'priceSource', 'customInput', 'customCacheRead', 'customOutput', 'customPriceCurrency', 'exchangeRate', 'planAutoQuota', 'quotaPollMinutes', 'planQuotaUrl']
455
+ const ADVANCED_FIELDS = ['suppressWhenFocused', 'showSpendInTitle', 'errorHoldMinutes', 'spendSource', 'currency', 'balancePollMinutes', 'priceSource', 'customInput', 'customCacheRead', 'customOutput', 'customPriceCurrency', 'exchangeRate', 'planAutoQuota', 'quotaPollMinutes', 'planQuotaUrl']
489
456
  const TRAY_FIELDS = ['showSpendInTitle', 'errorHoldMinutes']
490
457
  /** Plan fields a preset fills in; switching provider clears them. */
491
458
  const PRESET_FIELDS = ['planId', 'planName', 'planPrice', 'planCurrency', 'planPeriod', 'planQuota']
@@ -521,6 +488,9 @@ window.__ModuleLoader__.load({
521
488
  projection() {
522
489
  const fields = {}
523
490
  for (const f of FIELDS) fields[f] = this.form.field(f)
491
+ if (fields.channel.text === 'off') {
492
+ for (const key of NOTIFY_FIELDS) fields[key] = { ...fields[key], text: 'off' }
493
+ }
524
494
  return { ...this.form.shell(), fields, notice: this.notice }
525
495
  }
526
496
 
@@ -533,6 +503,11 @@ window.__ModuleLoader__.load({
533
503
  change(field, text, reset = false) {
534
504
  if (this.disposed || !this.form.shell().writable) return
535
505
  this.notice = null
506
+ // Convert the legacy master mute to independent switches in the same save.
507
+ if (NOTIFY_FIELDS.includes(field) && this.form.field('channel').text === 'off') {
508
+ this.change('channel', 'system')
509
+ for (const key of NOTIFY_FIELDS) if (key !== field) this.change(key, 'off')
510
+ }
536
511
  if (this.inFlight) this.pending.set(field, { text, reset })
537
512
  if (reset) this.actions.resetField(field)
538
513
  else this.actions.edit(field, text)
@@ -1025,9 +1000,7 @@ window.__ModuleLoader__.load({
1025
1000
  const changed = fields.filter(name => f[name].overridden).length
1026
1001
  return h('section', { style: S.advanced },
1027
1002
  h(Disclosure, { id: 'advanced', big: true, title: t('advanced'), hint: t('advancedHint'), badge: changed ? t('advancedChanged').replace('{n}', String(changed)) : undefined },
1028
- h(Group, { title: t('groupNotify') },
1029
- h(ChoiceRow, { name: 'channel', field: f.channel, hint: 'channelHint', ...common, options: [['auto', 'channelAuto'], ['client', 'channelClient'], ['system', 'channelSystem'], ['both', 'channelBoth'], ['off', 'channelOff']] }),
1030
- toggle('suppressWhenFocused')),
1003
+ h(Group, { title: t('groupNotify') }, toggle('suppressWhenFocused')),
1031
1004
  features.tray ? h(Group, { title: t('groupTray') },
1032
1005
  features.menuBarTitle ? toggle('showSpendInTitle') : null,
1033
1006
  h('div', { style: S.fieldGap }, h(NumberField, { name: 'errorHoldMinutes', field: f.errorHoldMinutes, ...common }))) : null,
@@ -1238,13 +1211,9 @@ window.__ModuleLoader__.load({
1238
1211
 
1239
1212
 
1240
1213
  function apply(ctx) {
1241
- var cursor = -1
1242
- var boot
1214
+ var viewId = Math.random().toString(36).slice(2)
1243
1215
  var stopped = false
1244
1216
  var timer
1245
- /** Focus at the previous poll: a window that just came forward claims the session a system toast announced. */
1246
- var wasFocused = false
1247
-
1248
1217
  function open(sessionId) {
1249
1218
  try { window.focus() } catch (e) {}
1250
1219
  try { ctx.uiWorkspace.openSession(sessionId) } catch (e) {
@@ -1252,62 +1221,21 @@ window.__ModuleLoader__.load({
1252
1221
  }
1253
1222
  }
1254
1223
 
1255
- /** Notifications this window put up, by key (session): the next one for a key replaces it. */
1256
- var shown = {}
1257
-
1258
- function withdraw(notice) {
1259
- var n = shown[notice.key]
1260
- if (!n) return
1261
- delete shown[notice.key]
1262
- try { n.close() } catch (e) {}
1263
- }
1264
-
1265
- function show(event, state) {
1266
- if (event.kind === 'withdraw') return withdraw(event)
1267
- if (typeof Notification === 'undefined') return
1268
- if (state.suppressWhenFocused && document.hasFocus() && mainSessionId(ctx.sessions) === event.sessionId) return
1269
- // Without permission the host is not told we listen, so it notifies itself.
1270
- if (Notification.permission !== 'granted') return
1271
- if (!claim(boot, event.seq)) return
1272
- var display = event.display || { title: (state.titles[event.type] || 'DeepSeek Harness') + ' · ' + event.title, body: event.body }
1273
- var key = event.key || event.sessionId
1274
- // Same tag: the OS swaps the session's previous notification for this one.
1275
- var n = new Notification(display.title, { body: display.body, tag: 'dsh-notify-tray:' + key, renotify: true })
1276
- shown[key] = n
1277
- n.onclose = function () { if (shown[key] === n) delete shown[key] }
1278
- n.onclick = function () {
1279
- n.close()
1280
- // The renderer cannot move the OS foreground; the host brings the window forward where it can (Windows).
1281
- fetch(RAISE_ROUTE, { method: 'POST', cache: 'no-store' }).catch(function () {})
1282
- open(event.sessionId)
1283
- }
1284
- }
1285
-
1286
1224
  function poll() {
1287
- var granted = typeof Notification !== 'undefined' && Notification.permission === 'granted'
1288
- var focused = typeof document !== 'undefined' && typeof document.hasFocus === 'function' && document.hasFocus()
1289
- var claimToast = focused && !wasFocused
1290
- wasFocused = focused
1291
- fetch(ROUTE + '?lite=1&after=' + cursor + (granted ? '&notify=1' : '') + (claimToast ? '&claim=1' : ''), { headers: { accept: 'application/json' }, cache: 'no-store' })
1225
+ var focused = typeof document !== 'undefined' && document.hasFocus && document.hasFocus()
1226
+ var session = focused && mainSessionId(ctx.sessions)
1227
+ var query = '?lite=1&window=' + viewId + (focused ? '&foreground=1' : '') + (session ? '&view=' + encodeURIComponent(session) : '')
1228
+ fetch(ROUTE + query, { headers: { accept: 'application/json' }, cache: 'no-store' })
1292
1229
  .then(function (res) { return res.ok ? res.json() : undefined })
1293
1230
  .then(function (state) {
1294
1231
  if (!state || stopped) return
1295
1232
  // Tray clicks first: the host hands each one out once, even on our first poll.
1296
1233
  for (var j = 0; j < state.focus.length; j += 1) open(state.focus[j])
1297
- // First poll, or the host restarted (new boot id): adopt its cursor without replaying history.
1298
- if (state.boot !== boot) { boot = state.boot; cursor = state.seq; return }
1299
- for (var i = 0; i < state.events.length; i += 1) show(state.events[i], state)
1300
- cursor = Math.max(cursor, state.seq)
1301
1234
  })
1302
1235
  .catch(function () {})
1303
1236
  .then(function () { if (!stopped) timer = setTimeout(poll, POLL_MS) })
1304
1237
  }
1305
1238
 
1306
- // Browsers only show the permission prompt from a user gesture.
1307
- if (typeof Notification !== 'undefined' && Notification.permission === 'default' && typeof document.addEventListener === 'function') {
1308
- document.addEventListener('click', function () { Notification.requestPermission() }, { once: true })
1309
- }
1310
-
1311
1239
  poll()
1312
1240
  ctx.effect(function () {
1313
1241
  return function () { stopped = true; clearTimeout(timer) }