dsh-bark-push 0.2.2 → 0.3.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/CHANGELOG.md +15 -0
- package/README.md +55 -12
- package/cordis.patch.yml +12 -0
- package/docs/browser-notifications-design.md +691 -0
- package/docs/settings-0.3.0.png +0 -0
- package/docs/settings-browser-persistence-mobile.png +0 -0
- package/docs/settings-browser-persistence.png +0 -0
- package/lib/browser-notify.js +396 -0
- package/lib/client.js +270 -11
- package/lib/config.js +60 -0
- package/lib/events.js +21 -5
- package/lib/http.js +33 -4
- package/lib/index.js +25 -5
- package/lib/state.js +51 -7
- package/lib/ui.js +885 -182
- package/package.json +6 -4
package/lib/client.js
CHANGED
|
@@ -32,30 +32,31 @@ window.__ModuleLoader__.load({
|
|
|
32
32
|
const API = {
|
|
33
33
|
state: "/api/bark-push/state",
|
|
34
34
|
toggle: "/api/bark-push/toggle",
|
|
35
|
+
browserEvents: "/api/bark-push/browser-events",
|
|
35
36
|
};
|
|
36
37
|
|
|
37
38
|
const COPY = {
|
|
38
39
|
zh: {
|
|
39
|
-
nav: "
|
|
40
|
+
nav: "通知",
|
|
40
41
|
mute: "静音推送通知",
|
|
41
42
|
unmute: "取消静音推送通知",
|
|
42
43
|
muted: "推送通知已静音",
|
|
43
44
|
mutedWorkspace: "工作区推送通知已静音",
|
|
44
45
|
unmuted: "推送通知已取消静音",
|
|
45
|
-
settingsTitle: "
|
|
46
|
-
settingsIntro: "
|
|
46
|
+
settingsTitle: "通知",
|
|
47
|
+
settingsIntro: "在下方管理推送通知与浏览器通知;两套渠道可以独立开启。",
|
|
47
48
|
unavailable: "管理接口不可用;请确认 Web 运行时与 client-connection 已加载。",
|
|
48
49
|
failed: "操作失败",
|
|
49
50
|
},
|
|
50
51
|
en: {
|
|
51
|
-
nav: "
|
|
52
|
+
nav: "Notifications",
|
|
52
53
|
mute: "Mute push notifications",
|
|
53
54
|
unmute: "Unmute push notifications",
|
|
54
55
|
muted: "Push notifications muted",
|
|
55
56
|
mutedWorkspace: "Workspace push notifications muted",
|
|
56
57
|
unmuted: "Push notifications unmuted",
|
|
57
|
-
settingsTitle: "
|
|
58
|
-
settingsIntro: "Manage push
|
|
58
|
+
settingsTitle: "Notifications",
|
|
59
|
+
settingsIntro: "Manage push notifications and browser notifications below; the two channels are independent.",
|
|
59
60
|
unavailable: "Management API unavailable; check that the Web runtime and client-connection are mounted.",
|
|
60
61
|
failed: "Action failed",
|
|
61
62
|
},
|
|
@@ -85,6 +86,16 @@ window.__ModuleLoader__.load({
|
|
|
85
86
|
sessionWorkspace: new Map(),
|
|
86
87
|
/** Workspace id -> root path, used to infer a workspace from a Session cwd. */
|
|
87
88
|
workspacePaths: new Map(),
|
|
89
|
+
/** Browser-notification switches mirrored from the latest state payload. */
|
|
90
|
+
browserConfig: {
|
|
91
|
+
enabled: false,
|
|
92
|
+
skipForeground: true,
|
|
93
|
+
persistent: {
|
|
94
|
+
reply: false,
|
|
95
|
+
approval: true,
|
|
96
|
+
question: true,
|
|
97
|
+
},
|
|
98
|
+
},
|
|
88
99
|
listeners: new Set(),
|
|
89
100
|
};
|
|
90
101
|
|
|
@@ -155,10 +166,25 @@ window.__ModuleLoader__.load({
|
|
|
155
166
|
return best;
|
|
156
167
|
}
|
|
157
168
|
|
|
169
|
+
function browserPersistenceOf(config) {
|
|
170
|
+
return {
|
|
171
|
+
reply: config.browserNotifyReplyPersistent === true,
|
|
172
|
+
approval: config.browserNotifyApprovalPersistent !== false,
|
|
173
|
+
question: config.browserNotifyQuestionPersistent !== false,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
158
177
|
function adopt(payload) {
|
|
159
178
|
if (!payload || !payload.mute) return;
|
|
160
179
|
store.workspaces = new Set(Array.isArray(payload.mute.workspaces) ? payload.mute.workspaces : []);
|
|
161
180
|
store.sessions = new Set(Array.isArray(payload.mute.sessions) ? payload.mute.sessions : []);
|
|
181
|
+
if (payload.config && typeof payload.config === "object") {
|
|
182
|
+
store.browserConfig = {
|
|
183
|
+
enabled: payload.config.browserNotifyEnabled === true,
|
|
184
|
+
skipForeground: payload.config.browserNotifySkipForeground !== false,
|
|
185
|
+
persistent: browserPersistenceOf(payload.config),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
162
188
|
if (payload.catalog && typeof payload.catalog === "object") {
|
|
163
189
|
const catalog = payload.catalog;
|
|
164
190
|
const paths = new Map();
|
|
@@ -454,6 +480,191 @@ window.__ModuleLoader__.load({
|
|
|
454
480
|
);
|
|
455
481
|
}
|
|
456
482
|
|
|
483
|
+
/** Browser Notification API from the top-level window, when available. */
|
|
484
|
+
function notificationApi() {
|
|
485
|
+
try {
|
|
486
|
+
if (typeof window !== "undefined" && typeof window.Notification === "function") return window.Notification;
|
|
487
|
+
} catch (_) {
|
|
488
|
+
// Fall through to the global for unusual test harnesses.
|
|
489
|
+
}
|
|
490
|
+
try {
|
|
491
|
+
return typeof Notification === "function" ? Notification : undefined;
|
|
492
|
+
} catch (_) {
|
|
493
|
+
return undefined;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/** Current main-view Session id, matching the DSH sidebar's own rule. */
|
|
498
|
+
function currentSessionId(ctx) {
|
|
499
|
+
try {
|
|
500
|
+
let sessions = ctx && ctx.sessions ? ctx.sessions : undefined;
|
|
501
|
+
if (!sessions && ctx && typeof ctx.get === "function") {
|
|
502
|
+
sessions = ctx.get("sessions");
|
|
503
|
+
}
|
|
504
|
+
const snapshot = sessions && sessions.list && typeof sessions.list.getSnapshot === "function"
|
|
505
|
+
? sessions.list.getSnapshot()
|
|
506
|
+
: undefined;
|
|
507
|
+
if (!snapshot || !snapshot.byId) return undefined;
|
|
508
|
+
const row = Object.values(snapshot.byId).find((entry) => (entry?.retainedBy?.mainView ?? 0) > 0);
|
|
509
|
+
return row && typeof row.id === "string" ? row.id : undefined;
|
|
510
|
+
} catch (_) {
|
|
511
|
+
return undefined;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/** Whether a browser event should be shown under the foreground rule. */
|
|
516
|
+
function browserEventShouldDisplay(ctx, event) {
|
|
517
|
+
if (!event || typeof event !== "object") return false;
|
|
518
|
+
if (store.loaded && (!store.browserConfig || store.browserConfig.enabled !== true)) return false;
|
|
519
|
+
if (store.browserConfig.skipForeground === false) return true;
|
|
520
|
+
const focused = typeof document.hasFocus === "function" ? document.hasFocus() : true;
|
|
521
|
+
const foreground = document.visibilityState === "visible" && focused;
|
|
522
|
+
if (!foreground) return true;
|
|
523
|
+
const current = currentSessionId(ctx);
|
|
524
|
+
return current === undefined || event.sessionId !== current;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/** Open a Session from the top document after a notification click. */
|
|
528
|
+
function openSessionFromNotification(ctx, sessionId) {
|
|
529
|
+
if (typeof sessionId !== "string" || sessionId.length === 0) return;
|
|
530
|
+
try {
|
|
531
|
+
if (ctx && ctx.uiWorkspace && typeof ctx.uiWorkspace.openSession === "function") {
|
|
532
|
+
ctx.uiWorkspace.openSession(sessionId);
|
|
533
|
+
return;
|
|
534
|
+
}
|
|
535
|
+
} catch (_) {
|
|
536
|
+
// Fall through to the injected scope.
|
|
537
|
+
}
|
|
538
|
+
try {
|
|
539
|
+
ctx.inject(["uiWorkspace"], (scope) => {
|
|
540
|
+
try {
|
|
541
|
+
scope.uiWorkspace.openSession(sessionId);
|
|
542
|
+
} catch (_) {
|
|
543
|
+
// The workspace UI may be tearing down.
|
|
544
|
+
}
|
|
545
|
+
});
|
|
546
|
+
} catch (_) {
|
|
547
|
+
// No workspace UI in this composition.
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** Show one browser notification and wire its click-through. */
|
|
552
|
+
function showBrowserNotification(ctx, event) {
|
|
553
|
+
const Api = notificationApi();
|
|
554
|
+
if (!Api || Api.permission !== "granted") return false;
|
|
555
|
+
if (!browserEventShouldDisplay(ctx, event)) return false;
|
|
556
|
+
const title = typeof event.sessionTitle === "string" && event.sessionTitle.length > 0
|
|
557
|
+
? event.sessionTitle
|
|
558
|
+
: "当前会话";
|
|
559
|
+
const body = typeof event.body === "string" ? event.body : (event.status || "");
|
|
560
|
+
try {
|
|
561
|
+
const notification = new Api(title, {
|
|
562
|
+
body,
|
|
563
|
+
tag: `dsh-bark:${event.kind || "notice"}:${event.sessionId || ""}`,
|
|
564
|
+
renotify: true,
|
|
565
|
+
requireInteraction: store.browserConfig.persistent[event.kind] === true,
|
|
566
|
+
data: { sessionId: event.sessionId, kind: event.kind },
|
|
567
|
+
});
|
|
568
|
+
notification.onclick = () => {
|
|
569
|
+
try {
|
|
570
|
+
window.focus();
|
|
571
|
+
} catch (_) {
|
|
572
|
+
// A background tab may not be focusable.
|
|
573
|
+
}
|
|
574
|
+
openSessionFromNotification(ctx, event.sessionId);
|
|
575
|
+
try {
|
|
576
|
+
notification.close();
|
|
577
|
+
} catch (_) {
|
|
578
|
+
// Already closed.
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
return true;
|
|
582
|
+
} catch (_) {
|
|
583
|
+
return false;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
/** Request permission from the top document for the settings iframe. */
|
|
588
|
+
function requestTopNotificationPermission() {
|
|
589
|
+
const Api = notificationApi();
|
|
590
|
+
if (!Api || typeof Api.requestPermission !== "function") return Promise.resolve("unsupported");
|
|
591
|
+
try {
|
|
592
|
+
return Promise.resolve(Api.requestPermission());
|
|
593
|
+
} catch (_) {
|
|
594
|
+
return Promise.resolve("denied");
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** Show a test notification from the top document. */
|
|
599
|
+
function showTopTestNotification() {
|
|
600
|
+
const Api = notificationApi();
|
|
601
|
+
if (!Api || Api.permission !== "granted") return false;
|
|
602
|
+
try {
|
|
603
|
+
new Api("DSH 浏览器通知测试", { body: "浏览器通知已开启。" });
|
|
604
|
+
return true;
|
|
605
|
+
} catch (_) {
|
|
606
|
+
return false;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Connect the browser-notification SSE stream. A leader lock keeps multiple
|
|
612
|
+
* DSH tabs from showing the same notification twice.
|
|
613
|
+
*/
|
|
614
|
+
function installBrowserNotifications(ctx) {
|
|
615
|
+
let source = null;
|
|
616
|
+
let started = false;
|
|
617
|
+
let releaseLeader;
|
|
618
|
+
const start = () => {
|
|
619
|
+
if (started) return;
|
|
620
|
+
const EventSourceCtor = typeof window !== "undefined" ? window.EventSource : undefined;
|
|
621
|
+
if (typeof EventSourceCtor !== "function") return;
|
|
622
|
+
started = true;
|
|
623
|
+
try {
|
|
624
|
+
source = new EventSourceCtor(API.browserEvents);
|
|
625
|
+
source.addEventListener("browser-notify", (message) => {
|
|
626
|
+
try {
|
|
627
|
+
const event = JSON.parse(message.data);
|
|
628
|
+
showBrowserNotification(ctx, event);
|
|
629
|
+
} catch (_) {
|
|
630
|
+
// Malformed events must not break the stream.
|
|
631
|
+
}
|
|
632
|
+
});
|
|
633
|
+
} catch (_) {
|
|
634
|
+
started = false;
|
|
635
|
+
source = null;
|
|
636
|
+
}
|
|
637
|
+
};
|
|
638
|
+
const stop = () => {
|
|
639
|
+
if (source && typeof source.close === "function") source.close();
|
|
640
|
+
source = null;
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
try {
|
|
644
|
+
const locks = typeof navigator !== "undefined" ? navigator.locks : undefined;
|
|
645
|
+
if (locks && typeof locks.request === "function") {
|
|
646
|
+
const hold = new Promise((resolve) => { releaseLeader = resolve; });
|
|
647
|
+
locks.request("dsh-bark-push:browser-notify-leader", { mode: "exclusive" }, async () => {
|
|
648
|
+
start();
|
|
649
|
+
await hold;
|
|
650
|
+
}).catch(() => start());
|
|
651
|
+
} else {
|
|
652
|
+
start();
|
|
653
|
+
}
|
|
654
|
+
} catch (_) {
|
|
655
|
+
start();
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
try {
|
|
659
|
+
ctx.effect(() => () => {
|
|
660
|
+
releaseLeader?.();
|
|
661
|
+
stop();
|
|
662
|
+
}, "bark-push: browser notification stream");
|
|
663
|
+
} catch (_) {
|
|
664
|
+
// Non-DOM test harnesses can still call showBrowserNotification directly.
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
457
668
|
function BarkSettings() {
|
|
458
669
|
const [failed, setFailed] = React.useState(false);
|
|
459
670
|
const frameRef = React.useRef(null);
|
|
@@ -464,9 +675,37 @@ window.__ModuleLoader__.load({
|
|
|
464
675
|
if (event.origin !== window.location.origin) return;
|
|
465
676
|
if (!frameRef.current || event.source !== frameRef.current.contentWindow) return;
|
|
466
677
|
const data = event.data;
|
|
467
|
-
if (!data
|
|
468
|
-
|
|
469
|
-
|
|
678
|
+
if (!data) return;
|
|
679
|
+
if (data.type === "dsh-bark-push:height") {
|
|
680
|
+
const height = Number(data.height);
|
|
681
|
+
if (Number.isFinite(height)) setFrameHeight(Math.max(480, Math.min(4000, Math.ceil(height))));
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
if (data.type === "dsh-bark-push:browser-permission-request") {
|
|
685
|
+
void requestTopNotificationPermission().then((permission) => {
|
|
686
|
+
try {
|
|
687
|
+
frameRef.current?.contentWindow?.postMessage(
|
|
688
|
+
{ type: "dsh-bark-push:browser-permission-result", permission },
|
|
689
|
+
window.location.origin,
|
|
690
|
+
);
|
|
691
|
+
} catch (_) {
|
|
692
|
+
// The iframe may have been closed while the prompt was open.
|
|
693
|
+
}
|
|
694
|
+
});
|
|
695
|
+
return;
|
|
696
|
+
}
|
|
697
|
+
if (data.type === "dsh-bark-push:browser-test") {
|
|
698
|
+
showTopTestNotification();
|
|
699
|
+
return;
|
|
700
|
+
}
|
|
701
|
+
if (data.type === "dsh-bark-push:config-updated") {
|
|
702
|
+
const config = data.config && typeof data.config === "object" ? data.config : {};
|
|
703
|
+
store.browserConfig = {
|
|
704
|
+
enabled: config.browserNotifyEnabled === true,
|
|
705
|
+
skipForeground: config.browserNotifySkipForeground !== false,
|
|
706
|
+
persistent: browserPersistenceOf(config),
|
|
707
|
+
};
|
|
708
|
+
}
|
|
470
709
|
};
|
|
471
710
|
window.addEventListener("message", onMessage);
|
|
472
711
|
return () => window.removeEventListener("message", onMessage);
|
|
@@ -496,6 +735,7 @@ window.__ModuleLoader__.load({
|
|
|
496
735
|
src: "/api/bark-push/ui?embed=1",
|
|
497
736
|
style: frameStyle,
|
|
498
737
|
title: COPY.zh.settingsTitle,
|
|
738
|
+
allow: "notifications",
|
|
499
739
|
onError: () => setFailed(true),
|
|
500
740
|
}),
|
|
501
741
|
);
|
|
@@ -551,7 +791,7 @@ window.__ModuleLoader__.load({
|
|
|
551
791
|
}
|
|
552
792
|
}
|
|
553
793
|
|
|
554
|
-
const inject = ["slots"];
|
|
794
|
+
const inject = ["slots", "sessions"];
|
|
555
795
|
|
|
556
796
|
function apply(ctx) {
|
|
557
797
|
propsContext = ctx;
|
|
@@ -614,6 +854,8 @@ window.__ModuleLoader__.load({
|
|
|
614
854
|
// No window (non-web composition): notices stay local to the iframe.
|
|
615
855
|
}
|
|
616
856
|
|
|
857
|
+
installBrowserNotifications(ctx);
|
|
858
|
+
|
|
617
859
|
if (!ctx.slots) return;
|
|
618
860
|
|
|
619
861
|
// Frame-wide overlay entry hosted by the layout shell (top document,
|
|
@@ -790,7 +1032,24 @@ window.__ModuleLoader__.load({
|
|
|
790
1032
|
|
|
791
1033
|
exports.apply = apply;
|
|
792
1034
|
exports.inject = inject;
|
|
793
|
-
exports.__internals = {
|
|
1035
|
+
exports.__internals = {
|
|
1036
|
+
store,
|
|
1037
|
+
toastStore,
|
|
1038
|
+
request,
|
|
1039
|
+
toggle,
|
|
1040
|
+
adopt,
|
|
1041
|
+
useMuted,
|
|
1042
|
+
COPY,
|
|
1043
|
+
NS,
|
|
1044
|
+
BELL_PATHS,
|
|
1045
|
+
browser: {
|
|
1046
|
+
currentSessionId,
|
|
1047
|
+
shouldDisplay: browserEventShouldDisplay,
|
|
1048
|
+
show: showBrowserNotification,
|
|
1049
|
+
requestTopPermission: requestTopNotificationPermission,
|
|
1050
|
+
showTopTest: showTopTestNotification,
|
|
1051
|
+
},
|
|
1052
|
+
};
|
|
794
1053
|
return module.exports;
|
|
795
1054
|
},
|
|
796
1055
|
});
|
package/lib/config.js
CHANGED
|
@@ -27,6 +27,27 @@ export const MAX_DEVICE_KEYS = 20;
|
|
|
27
27
|
/** Hard safety bound for one device key. Bark keys are short; longer input is a mistake. */
|
|
28
28
|
export const MAX_DEVICE_KEY_LENGTH = 256;
|
|
29
29
|
|
|
30
|
+
/** Default body templates for the independent browser-notification channel. */
|
|
31
|
+
export const BROWSER_BODY_TEMPLATES = Object.freeze({
|
|
32
|
+
reply: '{status} · {summary}',
|
|
33
|
+
approval: '{status} · {tool} · {reason}',
|
|
34
|
+
question: '{status} · {question}',
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
/** Config key for each browser-notification body template. */
|
|
38
|
+
export const BROWSER_TEMPLATE_KEYS = Object.freeze({
|
|
39
|
+
reply: 'browserNotifyReplyBody',
|
|
40
|
+
approval: 'browserNotifyApprovalBody',
|
|
41
|
+
question: 'browserNotifyQuestionBody',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** Placeholders accepted by each browser-notification body template. */
|
|
45
|
+
export const BROWSER_TEMPLATE_PLACEHOLDERS = Object.freeze({
|
|
46
|
+
reply: Object.freeze(['status', 'session', 'summary', 'turn', 'description']),
|
|
47
|
+
approval: Object.freeze(['status', 'session', 'tool', 'reason', 'description']),
|
|
48
|
+
question: Object.freeze(['status', 'session', 'question', 'questionCount', 'description']),
|
|
49
|
+
});
|
|
50
|
+
|
|
30
51
|
/** Every configurable key, in the order the management page renders them. */
|
|
31
52
|
export const CONFIG_KEYS = Object.freeze([
|
|
32
53
|
'enabled',
|
|
@@ -55,6 +76,19 @@ export const CONFIG_KEYS = Object.freeze([
|
|
|
55
76
|
'linkToSession',
|
|
56
77
|
'publicBaseUrl',
|
|
57
78
|
'trace',
|
|
79
|
+
'browserNotifyEnabled',
|
|
80
|
+
'browserNotifyOnReply',
|
|
81
|
+
'browserNotifyOnApproval',
|
|
82
|
+
'browserNotifyOnQuestion',
|
|
83
|
+
'browserNotifyReplyPersistent',
|
|
84
|
+
'browserNotifyApprovalPersistent',
|
|
85
|
+
'browserNotifyQuestionPersistent',
|
|
86
|
+
'browserNotifySkipForeground',
|
|
87
|
+
'browserNotifyUsePushMutes',
|
|
88
|
+
'browserNotifyReplyBody',
|
|
89
|
+
'browserNotifyApprovalBody',
|
|
90
|
+
'browserNotifyQuestionBody',
|
|
91
|
+
'browserNotifyMaxBodyLength',
|
|
58
92
|
...LEGACY_CONFIG_KEYS,
|
|
59
93
|
]);
|
|
60
94
|
|
|
@@ -86,6 +120,19 @@ export const DEFAULTS = Object.freeze({
|
|
|
86
120
|
linkToSession: true,
|
|
87
121
|
publicBaseUrl: '',
|
|
88
122
|
trace: false,
|
|
123
|
+
browserNotifyEnabled: false,
|
|
124
|
+
browserNotifyOnReply: true,
|
|
125
|
+
browserNotifyOnApproval: true,
|
|
126
|
+
browserNotifyOnQuestion: true,
|
|
127
|
+
browserNotifyReplyPersistent: false,
|
|
128
|
+
browserNotifyApprovalPersistent: true,
|
|
129
|
+
browserNotifyQuestionPersistent: true,
|
|
130
|
+
browserNotifySkipForeground: true,
|
|
131
|
+
browserNotifyUsePushMutes: true,
|
|
132
|
+
browserNotifyReplyBody: BROWSER_BODY_TEMPLATES.reply,
|
|
133
|
+
browserNotifyApprovalBody: BROWSER_BODY_TEMPLATES.approval,
|
|
134
|
+
browserNotifyQuestionBody: BROWSER_BODY_TEMPLATES.question,
|
|
135
|
+
browserNotifyMaxBodyLength: 200,
|
|
89
136
|
soundNormal: '',
|
|
90
137
|
soundImportant: '',
|
|
91
138
|
levelNormal: 'active',
|
|
@@ -384,6 +431,19 @@ function normalizeFields(raw, base, warnings) {
|
|
|
384
431
|
linkToSession: booleanOf(raw.linkToSession, base.linkToSession),
|
|
385
432
|
publicBaseUrl: normalizePublicBaseUrl(raw.publicBaseUrl, base.publicBaseUrl),
|
|
386
433
|
trace: booleanOf(raw.trace, base.trace),
|
|
434
|
+
browserNotifyEnabled: booleanOf(raw.browserNotifyEnabled, base.browserNotifyEnabled),
|
|
435
|
+
browserNotifyOnReply: booleanOf(raw.browserNotifyOnReply, base.browserNotifyOnReply),
|
|
436
|
+
browserNotifyOnApproval: booleanOf(raw.browserNotifyOnApproval, base.browserNotifyOnApproval),
|
|
437
|
+
browserNotifyOnQuestion: booleanOf(raw.browserNotifyOnQuestion, base.browserNotifyOnQuestion),
|
|
438
|
+
browserNotifyReplyPersistent: booleanOf(raw.browserNotifyReplyPersistent, base.browserNotifyReplyPersistent),
|
|
439
|
+
browserNotifyApprovalPersistent: booleanOf(raw.browserNotifyApprovalPersistent, base.browserNotifyApprovalPersistent),
|
|
440
|
+
browserNotifyQuestionPersistent: booleanOf(raw.browserNotifyQuestionPersistent, base.browserNotifyQuestionPersistent),
|
|
441
|
+
browserNotifySkipForeground: booleanOf(raw.browserNotifySkipForeground, base.browserNotifySkipForeground),
|
|
442
|
+
browserNotifyUsePushMutes: booleanOf(raw.browserNotifyUsePushMutes, base.browserNotifyUsePushMutes),
|
|
443
|
+
browserNotifyReplyBody: textOf(raw.browserNotifyReplyBody, base.browserNotifyReplyBody, 300),
|
|
444
|
+
browserNotifyApprovalBody: textOf(raw.browserNotifyApprovalBody, base.browserNotifyApprovalBody, 300),
|
|
445
|
+
browserNotifyQuestionBody: textOf(raw.browserNotifyQuestionBody, base.browserNotifyQuestionBody, 300),
|
|
446
|
+
browserNotifyMaxBodyLength: integerInRange(raw.browserNotifyMaxBodyLength, base.browserNotifyMaxBodyLength, 32, 500),
|
|
387
447
|
};
|
|
388
448
|
}
|
|
389
449
|
|
package/lib/events.js
CHANGED
|
@@ -70,11 +70,12 @@ export function turnEndKind(reason) {
|
|
|
70
70
|
* @param {object} ctx - host Cordis context.
|
|
71
71
|
* @param {object} options - bridge dependencies.
|
|
72
72
|
* @param {object} options.notifier - notifier with `reply`/`approval` methods.
|
|
73
|
+
* @param {object} [options.browserNotifier] - independent browser notification sink.
|
|
73
74
|
* @param {() => Record<string, unknown>} options.getConfig - current config.
|
|
74
75
|
* @param {{ info: Function, warn: Function, error: Function, debug: Function }} options.logger - logger.
|
|
75
76
|
* @returns {() => void} disposer.
|
|
76
77
|
*/
|
|
77
|
-
export function installSessionBridge(ctx, { notifier, getConfig, logger }) {
|
|
78
|
+
export function installSessionBridge(ctx, { notifier, browserNotifier, getConfig, logger }) {
|
|
78
79
|
/** @type {Map<string, { turn: number, text: string }>} */
|
|
79
80
|
const pending = new Map();
|
|
80
81
|
/** @type {Map<string, string>} */
|
|
@@ -129,13 +130,20 @@ export function installSessionBridge(ctx, { notifier, getConfig, logger }) {
|
|
|
129
130
|
text: record.text,
|
|
130
131
|
sessionTitle: titleOf(id),
|
|
131
132
|
});
|
|
133
|
+
browserNotifier?.reply?.({
|
|
134
|
+
session,
|
|
135
|
+
sessionId: id,
|
|
136
|
+
turn,
|
|
137
|
+
text: record.text,
|
|
138
|
+
sessionTitle: titleOf(id),
|
|
139
|
+
});
|
|
132
140
|
return;
|
|
133
141
|
}
|
|
134
142
|
|
|
135
143
|
if (event.type === 'approval/asked') {
|
|
136
144
|
const requestId = typeof event.data?.id === 'string' ? event.data.id : '';
|
|
137
145
|
if (requestId.length === 0) return;
|
|
138
|
-
|
|
146
|
+
const input = {
|
|
139
147
|
session,
|
|
140
148
|
sessionId: id,
|
|
141
149
|
requestId,
|
|
@@ -143,7 +151,9 @@ export function installSessionBridge(ctx, { notifier, getConfig, logger }) {
|
|
|
143
151
|
reason: typeof event.data?.reason === 'string' ? event.data.reason : '',
|
|
144
152
|
callId: typeof event.data?.callId === 'string' ? event.data.callId : undefined,
|
|
145
153
|
sessionTitle: titleOf(id),
|
|
146
|
-
}
|
|
154
|
+
};
|
|
155
|
+
notifier.approval(input);
|
|
156
|
+
browserNotifier?.approval?.(input);
|
|
147
157
|
}
|
|
148
158
|
};
|
|
149
159
|
|
|
@@ -180,15 +190,21 @@ export function installSessionBridge(ctx, { notifier, getConfig, logger }) {
|
|
|
180
190
|
* @param {object} ctx - host Cordis context.
|
|
181
191
|
* @param {object} options - observer dependencies.
|
|
182
192
|
* @param {object} options.notifier - notifier with `question` method.
|
|
193
|
+
* @param {object} [options.browserNotifier] - independent browser notification sink.
|
|
183
194
|
* @param {{ warn: Function }} options.logger - logger.
|
|
184
195
|
* @returns {() => void} disposer.
|
|
185
196
|
*/
|
|
186
|
-
export function installQuestionBridge(ctx, { notifier, logger }) {
|
|
197
|
+
export function installQuestionBridge(ctx, { notifier, browserNotifier, logger }) {
|
|
187
198
|
const off = ctx.on('user-questions/request', (request, next) => {
|
|
188
199
|
try {
|
|
189
200
|
notifier.question(request);
|
|
190
201
|
} catch (error) {
|
|
191
|
-
logger.warn(`bark-push:
|
|
202
|
+
logger.warn(`bark-push: 选择请求推送失败:${error?.message ?? String(error)}`);
|
|
203
|
+
}
|
|
204
|
+
try {
|
|
205
|
+
browserNotifier?.question?.(request);
|
|
206
|
+
} catch (error) {
|
|
207
|
+
logger.warn(`bark-push: 选择请求浏览器通知失败:${error?.message ?? String(error)}`);
|
|
192
208
|
}
|
|
193
209
|
return typeof next === 'function' ? next() : Promise.reject(new Error('bark-push: missing waterfall delegate'));
|
|
194
210
|
});
|
package/lib/http.js
CHANGED
|
@@ -23,6 +23,7 @@ export const ROUTES = Object.freeze({
|
|
|
23
23
|
toggle: '/api/bark-push/toggle',
|
|
24
24
|
test: '/api/bark-push/test',
|
|
25
25
|
testChannel: '/api/bark-push/test-channel',
|
|
26
|
+
browserEvents: '/api/bark-push/browser-events',
|
|
26
27
|
});
|
|
27
28
|
|
|
28
29
|
/** Maximum accepted JSON request body. */
|
|
@@ -87,6 +88,17 @@ function muteTarget(kind, id) {
|
|
|
87
88
|
return { kind, id: trimmed };
|
|
88
89
|
}
|
|
89
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Normalize one optional mute channel. Unknown values are rejected rather than
|
|
93
|
+
* silently writing to the wrong mute set.
|
|
94
|
+
* @param {unknown} value - raw channel.
|
|
95
|
+
* @returns {'push'|'browser'|null} normalized channel.
|
|
96
|
+
*/
|
|
97
|
+
function muteChannel(value) {
|
|
98
|
+
if (value === undefined || value === null) return 'push';
|
|
99
|
+
return value === 'push' || value === 'browser' ? value : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
90
102
|
/**
|
|
91
103
|
* Read one optional Cordis service without an inject requirement.
|
|
92
104
|
* @param {object} ctx - host context.
|
|
@@ -291,6 +303,10 @@ async function collectSessionSummaries(ctx, signal) {
|
|
|
291
303
|
export async function buildCatalog(ctx, snapshot, signal) {
|
|
292
304
|
const mutedWorkspaces = new Set(snapshot.mutedWorkspaces);
|
|
293
305
|
const mutedSessions = new Set(snapshot.mutedSessions);
|
|
306
|
+
const allMutedSessions = new Set([
|
|
307
|
+
...mutedSessions,
|
|
308
|
+
...(Array.isArray(snapshot.browserMutedSessions) ? snapshot.browserMutedSessions : []),
|
|
309
|
+
]);
|
|
294
310
|
const workspaces = [];
|
|
295
311
|
/** Session id -> workspace id for every Session the registry knows. */
|
|
296
312
|
const workspaceBySession = new Map();
|
|
@@ -384,7 +400,7 @@ export async function buildCatalog(ctx, snapshot, signal) {
|
|
|
384
400
|
|
|
385
401
|
// A mute outlives the row it was set on: keep archived, subagent, blank, or
|
|
386
402
|
// deleted Sessions visible here so the user can still clear that mute.
|
|
387
|
-
for (const id of
|
|
403
|
+
for (const id of allMutedSessions) {
|
|
388
404
|
if (seen.has(id)) continue;
|
|
389
405
|
const summary = summaryById.get(id);
|
|
390
406
|
const workspaceId = workspaceBySession.get(id) ?? null;
|
|
@@ -398,7 +414,7 @@ export async function buildCatalog(ctx, snapshot, signal) {
|
|
|
398
414
|
createdAt: summary?.updatedAt ?? 0,
|
|
399
415
|
updatedAt: summary?.updatedAt ?? 0,
|
|
400
416
|
running: false,
|
|
401
|
-
muted:
|
|
417
|
+
muted: mutedSessions.has(id),
|
|
402
418
|
archived: archivedSessionIds.has(id),
|
|
403
419
|
orphaned: true,
|
|
404
420
|
});
|
|
@@ -453,6 +469,8 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
453
469
|
mute: {
|
|
454
470
|
workspaces: snapshot.mutedWorkspaces,
|
|
455
471
|
sessions: snapshot.mutedSessions,
|
|
472
|
+
browserWorkspaces: snapshot.browserMutedWorkspaces ?? [],
|
|
473
|
+
browserSessions: snapshot.browserMutedSessions ?? [],
|
|
456
474
|
},
|
|
457
475
|
catalog: await buildCatalog(host, snapshot, request?.signal),
|
|
458
476
|
lastTest: snapshot.lastTest,
|
|
@@ -503,9 +521,11 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
503
521
|
}
|
|
504
522
|
const target = muteTarget(body.kind, body.id);
|
|
505
523
|
if (target === null) return json({ ok: false, error: '无效的静音目标' }, 400);
|
|
524
|
+
const channel = muteChannel(body.channel);
|
|
525
|
+
if (channel === null) return json({ ok: false, error: '无效的静音通道' }, 400);
|
|
506
526
|
if (typeof body.muted !== 'boolean') return json({ ok: false, error: 'muted 必须是布尔值' }, 400);
|
|
507
527
|
try {
|
|
508
|
-
await runtime.setMute(target.kind, target.id, body.muted);
|
|
528
|
+
await runtime.setMute(target.kind, target.id, body.muted, channel);
|
|
509
529
|
return json(await statePayload(request));
|
|
510
530
|
} catch (error) {
|
|
511
531
|
return json({ ok: false, error: error?.message ?? '保存失败' }, 500);
|
|
@@ -522,14 +542,23 @@ export function installHttpRoutes(ctx, runtime, host = ctx) {
|
|
|
522
542
|
}
|
|
523
543
|
const target = muteTarget(body.kind, body.id);
|
|
524
544
|
if (target === null) return json({ ok: false, error: '无效的静音目标' }, 400);
|
|
545
|
+
const channel = muteChannel(body.channel);
|
|
546
|
+
if (channel === null) return json({ ok: false, error: '无效的静音通道' }, 400);
|
|
525
547
|
try {
|
|
526
|
-
const result = await runtime.toggleMute(target.kind, target.id);
|
|
548
|
+
const result = await runtime.toggleMute(target.kind, target.id, channel);
|
|
527
549
|
return json({ ...(await statePayload(request)), muted: result.muted });
|
|
528
550
|
} catch (error) {
|
|
529
551
|
return json({ ok: false, error: error?.message ?? '保存失败' }, 500);
|
|
530
552
|
}
|
|
531
553
|
});
|
|
532
554
|
|
|
555
|
+
register(ROUTES.browserEvents, ['GET'], async (request) => {
|
|
556
|
+
if (typeof runtime.browserNotify?.subscribe !== 'function') {
|
|
557
|
+
return json({ ok: false, error: '浏览器通知通道不可用' }, 501);
|
|
558
|
+
}
|
|
559
|
+
return runtime.browserNotify.subscribe(request);
|
|
560
|
+
});
|
|
561
|
+
|
|
533
562
|
register(ROUTES.test, ['POST'], async (request) => {
|
|
534
563
|
if (!writeAllowed(request)) return json({ ok: false, error: 'forbidden' }, 403);
|
|
535
564
|
try {
|