@lukstei/dsh-client-notify 0.0.0-stage → 0.2.2-rc.5.4
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 +132 -2
- package/cordis.patch.yml +3 -0
- package/dist/client.js +314 -0
- package/dist/index.js +43 -0
- package/package.json +80 -3
- package/src/client/badge.ts +8 -0
- package/src/client/contract.ts +12 -0
- package/src/client/index.ts +358 -0
- package/src/client/notify.ts +114 -0
- package/src/client/sound.ts +72 -0
- package/src/index.ts +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Lukas Steinbrecher
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,133 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @lukstei/dsh-client-notify
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## Summary
|
|
4
|
+
|
|
5
|
+
Alerts you when an agent stops or needs input. It shows a desktop notification, plays a short synthesized chime, and sets the installed PWA's app-icon badge. For anyone who leaves the DSH tab in the background.
|
|
6
|
+
|
|
7
|
+
## Table of Contents
|
|
8
|
+
|
|
9
|
+
- [Use this package](#use-this-package)
|
|
10
|
+
- [Understand the implementation](#understand-the-implementation)
|
|
11
|
+
- [Further Exploration](#further-exploration)
|
|
12
|
+
- [Model Experience](#model-experience)
|
|
13
|
+
- [Known Limitations and Deferred Work](#known-limitations-and-deferred-work)
|
|
14
|
+
- [Dev Note](#dev-note)
|
|
15
|
+
|
|
16
|
+
-----
|
|
17
|
+
|
|
18
|
+
<a id="use-this-package"></a>
|
|
19
|
+
## Use this package
|
|
20
|
+
|
|
21
|
+
```yaml
|
|
22
|
+
- insert:
|
|
23
|
+
- id: client-notify
|
|
24
|
+
name: '@lukstei/dsh-client-notify'
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
No config. The package has a host part and a client part:
|
|
28
|
+
|
|
29
|
+
- Host (`inject: webServer`): serves the Service Worker script at `/sw.js` and `/tsctl/sw.js` with `service-worker-allowed: /` and `cache-control: no-cache`.
|
|
30
|
+
- Client (`platform: web`, `immediately: true`): needs `remote`, `remote.session`, `sessions`, `uiSession`.
|
|
31
|
+
|
|
32
|
+
### Plugin dependencies
|
|
33
|
+
|
|
34
|
+
Server-side and client-side entries.
|
|
35
|
+
|
|
36
|
+
**Server-side** (`inject: webServer`):
|
|
37
|
+
|
|
38
|
+
| Plugin / service | Required? | Provided by | Effect without it |
|
|
39
|
+
|---|---|---|---|
|
|
40
|
+
| `webServer` | Required | @deepseek-ai/dsh-host-webserver | Plugin stays pending; never applies. `/sw.js` and `/tsctl/sw.js` are not served, so the Service Worker cannot register. |
|
|
41
|
+
|
|
42
|
+
**Client-side** (`inject`: `remote`, `remote.session`, `sessions`, `uiSession`):
|
|
43
|
+
|
|
44
|
+
| Plugin / service | Required? | Provided by | Effect without it |
|
|
45
|
+
|---|---|---|---|
|
|
46
|
+
| `remote`, `remote.session` | Required | @deepseek-ai/dsh-api-remotes / @deepseek-ai/dsh-api-session-controller | Plugin stays pending; never applies. No notifications. |
|
|
47
|
+
| `sessions`, `uiSession` | Required | @deepseek-ai/dsh-api-session-controller, @deepseek-ai/dsh-client-ui-session | Plugin stays pending; never applies. |
|
|
48
|
+
|
|
49
|
+
Provides the client service `appBadge`.
|
|
50
|
+
|
|
51
|
+
Used by: [`dsh-client-kanban`](../dsh-client-kanban/README.md) (optional `appBadge` consumer).
|
|
52
|
+
|
|
53
|
+
### Triggers
|
|
54
|
+
|
|
55
|
+
| Event | Notification text | Sound |
|
|
56
|
+
|---|---|---|
|
|
57
|
+
| Session stops running | `Agent stopped` | pop chime |
|
|
58
|
+
| Pending approval (`approval` kind, `acp-permission:` key, or an approval question) | `Tool approval requested: <tool name or first question>` | alert chime |
|
|
59
|
+
| Pending question | `Question: <first question>` | alert chime |
|
|
60
|
+
| Any other pending interaction kind except `plan-review` | `Action required: <kind>` | alert chime |
|
|
61
|
+
|
|
62
|
+
Rules:
|
|
63
|
+
|
|
64
|
+
- Stop notifications are debounced by 3 s. A session that starts running again inside that window stays silent, so workflow steps and chained turns do not alert.
|
|
65
|
+
- A stop notifies only if the session appended a `user/message` or `assistant/message` within the last 60 s. Runs that append no chat message (command-driven work) stay silent. A failed events read fails open and notifies.
|
|
66
|
+
- A session with running subagents does not notify. When the last subagent stops, the parent is checked with the same debounce.
|
|
67
|
+
- Interactions from subagent sessions never notify. Each interaction key notifies once.
|
|
68
|
+
- On desktop, nothing is shown while the tab has focus and is visible (`document.hasFocus() && !document.hidden`). The chime still plays. On touch devices (`pointer: coarse` or `maxTouchPoints > 0`) this check is skipped.
|
|
69
|
+
- A notification needs `Notification.permission === 'granted'`. The chime plays regardless.
|
|
70
|
+
|
|
71
|
+
### Permission and Service Worker
|
|
72
|
+
|
|
73
|
+
On start the client registers `/sw.js` (scope `/`, falling back to `sw.js`). If notification permission is `default`, it requests permission on the first `click`, `touchend` or `keydown`. Notifications go through the Service Worker registration (`showNotification`) when one is ready within 300 ms, else `new Notification()`. Clicking a notification focuses an existing DSH window or opens `/`.
|
|
74
|
+
|
|
75
|
+
### App icon badge
|
|
76
|
+
|
|
77
|
+
The badge (`navigator.setAppBadge`) shows sessions with a pending interaction plus recent review sessions. It is cleared at 0 and on unload. [`@lukstei/dsh-client-kanban`](../dsh-client-kanban/README.md) supplies the review part through `appBadge.setRecentReviewCount(n)` (`IAppBadge`, from `@lukstei/dsh-client-notify/client-contract`). Without Kanban the badge counts pending interactions only. The browser rejects the call when the app is not installed or permission is missing; the error is ignored. iOS shows the badge only after notification permission is granted.
|
|
78
|
+
|
|
79
|
+
-----
|
|
80
|
+
|
|
81
|
+
<a id="understand-the-implementation"></a>
|
|
82
|
+
## Understand the implementation
|
|
83
|
+
|
|
84
|
+
<details>
|
|
85
|
+
<summary>Implementation internals — click to expand</summary>
|
|
86
|
+
|
|
87
|
+
| File | Role |
|
|
88
|
+
|---|---|
|
|
89
|
+
| [src/index.ts](src/index.ts) | Host: `SERVICE_WORKER_SCRIPT` and the two exact routes |
|
|
90
|
+
| [src/client/index.ts](src/client/index.ts) | Client: stop debounce, interaction watcher, `appBadge` provider |
|
|
91
|
+
| [src/client/notify.ts](src/client/notify.ts) | Service Worker registration, permission lifecycle, notification display |
|
|
92
|
+
| [src/client/sound.ts](src/client/sound.ts) | Web Audio chimes |
|
|
93
|
+
| [src/client/badge.ts](src/client/badge.ts) | `syncAppBadge` |
|
|
94
|
+
| [src/client/contract.ts](src/client/contract.ts) | `IAppBadge` and the `Context.appBadge` augmentation |
|
|
95
|
+
|
|
96
|
+
Chimes are sine oscillators, no audio files. The pop chime is 791 Hz with a fast pitch drop and about 70 ms decay. The alert chime is 587.33 Hz then 880 Hz, 120 ms apart. One `AudioContext` is created lazily, reused, and resumed when suspended. Audio errors are swallowed.
|
|
97
|
+
|
|
98
|
+
Stop detection listens to `api-session/status` on `remote`. Pending interactions come from `uiSession.sessionStatus`. Notification icon is `/favicon.svg`.
|
|
99
|
+
|
|
100
|
+
The Service Worker script handles `install` (`skipWaiting`), `activate` (`clients.claim`) and `notificationclick`. Persisted state: none.
|
|
101
|
+
|
|
102
|
+
</details>
|
|
103
|
+
|
|
104
|
+
-----
|
|
105
|
+
|
|
106
|
+
<a id="further-exploration"></a>
|
|
107
|
+
## Further Exploration
|
|
108
|
+
|
|
109
|
+
- Upstream: [DSH module graph](https://github.com/deepseek-ai/deepseek-harness/blob/master/docs/module-graph.md).
|
|
110
|
+
- [`@lukstei/dsh-client-pwa`](../dsh-client-pwa/README.md): PWA manifest and icons for installed-app badges.
|
|
111
|
+
- [`@lukstei/dsh-client-favicon`](../dsh-client-favicon/README.md): tab favicon status.
|
|
112
|
+
|
|
113
|
+
<a id="model-experience"></a>
|
|
114
|
+
## Model Experience
|
|
115
|
+
|
|
116
|
+
No model-visible surface. This package adds no tools, prompt text, or context to the model.
|
|
117
|
+
|
|
118
|
+
<a id="known-limitations-and-deferred-work"></a>
|
|
119
|
+
## Known Limitations and Deferred Work
|
|
120
|
+
|
|
121
|
+
- The Service Worker routes are fixed to `/sw.js` and `/tsctl/sw.js`. Other reverse-proxy prefixes are not served.
|
|
122
|
+
- Notification permission needs a user gesture, so no notification is shown before the first click, tap or key press.
|
|
123
|
+
- `plan-review` interactions do not notify.
|
|
124
|
+
|
|
125
|
+
<a id="dev-note"></a>
|
|
126
|
+
### Dev Note
|
|
127
|
+
|
|
128
|
+
<details>
|
|
129
|
+
<summary>Working context for maintainers — click to expand</summary>
|
|
130
|
+
|
|
131
|
+
`pnpm run verify` runs Biome, build, typecheck and tests (specs live beside the sources in `src/`).
|
|
132
|
+
|
|
133
|
+
</details>
|
package/cordis.patch.yml
ADDED
package/dist/client.js
ADDED
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: "@lukstei/dsh-client-notify",
|
|
3
|
+
factory: (require) => {
|
|
4
|
+
var module = { exports: {} };
|
|
5
|
+
var exports = module.exports;
|
|
6
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
7
|
+
// src/client/badge.ts
|
|
8
|
+
/** Sets the installed-PWA app-icon badge; a count of 0 clears it. */
|
|
9
|
+
function syncAppBadge(count) {
|
|
10
|
+
if (typeof navigator === "undefined" || !("setAppBadge" in navigator)) return;
|
|
11
|
+
(count > 0 ? navigator.setAppBadge(count) : navigator.clearAppBadge()).catch(() => {});
|
|
12
|
+
}
|
|
13
|
+
// src/client/sound.ts
|
|
14
|
+
let sharedContext;
|
|
15
|
+
function audioContext() {
|
|
16
|
+
const AudioCtx = window.AudioContext || window.webkitAudioContext;
|
|
17
|
+
if (!AudioCtx) return void 0;
|
|
18
|
+
sharedContext ??= new AudioCtx();
|
|
19
|
+
if (sharedContext.state === "suspended") sharedContext.resume().catch(() => {});
|
|
20
|
+
return sharedContext;
|
|
21
|
+
}
|
|
22
|
+
function playPopChime() {
|
|
23
|
+
try {
|
|
24
|
+
const ctx = audioContext();
|
|
25
|
+
if (!ctx) return;
|
|
26
|
+
const notes = [791];
|
|
27
|
+
const startTime = ctx.currentTime;
|
|
28
|
+
notes.forEach((freq, i) => {
|
|
29
|
+
const osc = ctx.createOscillator();
|
|
30
|
+
const gain = ctx.createGain();
|
|
31
|
+
const t = startTime + i * .12;
|
|
32
|
+
osc.type = "sine";
|
|
33
|
+
osc.frequency.setValueAtTime(freq, t);
|
|
34
|
+
osc.frequency.exponentialRampToValueAtTime(freq * .22, t + .05);
|
|
35
|
+
gain.gain.setValueAtTime(0, t);
|
|
36
|
+
gain.gain.linearRampToValueAtTime(.11, t + .01);
|
|
37
|
+
gain.gain.exponentialRampToValueAtTime(1e-4, t + .07);
|
|
38
|
+
osc.connect(gain).connect(ctx.destination);
|
|
39
|
+
osc.start(t);
|
|
40
|
+
osc.stop(t + .12);
|
|
41
|
+
});
|
|
42
|
+
} catch {}
|
|
43
|
+
}
|
|
44
|
+
function playAlertChime() {
|
|
45
|
+
try {
|
|
46
|
+
const ctx = audioContext();
|
|
47
|
+
if (!ctx) return;
|
|
48
|
+
const notes = [587.33, 880];
|
|
49
|
+
const startTime = ctx.currentTime;
|
|
50
|
+
notes.forEach((freq, i) => {
|
|
51
|
+
const osc = ctx.createOscillator();
|
|
52
|
+
const gain = ctx.createGain();
|
|
53
|
+
const t = startTime + i * .12;
|
|
54
|
+
osc.type = "sine";
|
|
55
|
+
osc.frequency.setValueAtTime(freq, t);
|
|
56
|
+
gain.gain.setValueAtTime(0, t);
|
|
57
|
+
gain.gain.linearRampToValueAtTime(.12, t + .01);
|
|
58
|
+
gain.gain.exponentialRampToValueAtTime(1e-4, t + .11);
|
|
59
|
+
osc.connect(gain).connect(ctx.destination);
|
|
60
|
+
osc.start(t);
|
|
61
|
+
osc.stop(t + .12);
|
|
62
|
+
});
|
|
63
|
+
} catch {}
|
|
64
|
+
}
|
|
65
|
+
// src/client/notify.ts
|
|
66
|
+
function registerServiceWorker() {
|
|
67
|
+
if (typeof navigator === "undefined" || !("serviceWorker" in navigator)) return;
|
|
68
|
+
navigator.serviceWorker.register("/sw.js", { scope: "/" }).catch(() => {
|
|
69
|
+
navigator.serviceWorker.register("sw.js").catch(() => {});
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function requestNotificationPermission() {
|
|
73
|
+
if (typeof window === "undefined") return;
|
|
74
|
+
registerServiceWorker();
|
|
75
|
+
if (!("Notification" in window)) return;
|
|
76
|
+
if (window.Notification.permission !== "default") return;
|
|
77
|
+
const onRequest = () => {
|
|
78
|
+
if (window.Notification.permission === "default") window.Notification.requestPermission().then((permission) => {
|
|
79
|
+
if (permission !== "default") {
|
|
80
|
+
window.removeEventListener("click", onRequest);
|
|
81
|
+
window.removeEventListener("touchend", onRequest);
|
|
82
|
+
window.removeEventListener("keydown", onRequest);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
window.addEventListener("click", onRequest);
|
|
87
|
+
window.addEventListener("touchend", onRequest);
|
|
88
|
+
window.addEventListener("keydown", onRequest);
|
|
89
|
+
}
|
|
90
|
+
function constructNotification(title, options) {
|
|
91
|
+
try {
|
|
92
|
+
const notification = new window.Notification(title, options);
|
|
93
|
+
notification.onclick = () => {
|
|
94
|
+
window.focus();
|
|
95
|
+
notification.close();
|
|
96
|
+
};
|
|
97
|
+
} catch {}
|
|
98
|
+
}
|
|
99
|
+
function showBrowserNotification(title, message, sound) {
|
|
100
|
+
if (typeof window === "undefined") return;
|
|
101
|
+
if (!(typeof window.matchMedia === "function" && window.matchMedia("(pointer: coarse)").matches || typeof navigator !== "undefined" && (navigator.maxTouchPoints ?? 0) > 0) && document.hasFocus() && !document.hidden) return;
|
|
102
|
+
if ("Notification" in window && window.Notification.permission === "granted") {
|
|
103
|
+
const options = {
|
|
104
|
+
body: message,
|
|
105
|
+
icon: "/favicon.svg"
|
|
106
|
+
};
|
|
107
|
+
if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
|
|
108
|
+
const resolveSwReg = async () => {
|
|
109
|
+
const reg = await navigator.serviceWorker.getRegistration();
|
|
110
|
+
if (reg && "showNotification" in reg) return reg;
|
|
111
|
+
return Promise.race([navigator.serviceWorker.ready, new Promise((resolve) => setTimeout(() => resolve(void 0), 300))]);
|
|
112
|
+
};
|
|
113
|
+
resolveSwReg().then((reg) => {
|
|
114
|
+
if (reg && "showNotification" in reg) {
|
|
115
|
+
reg.showNotification(title, options);
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
constructNotification(title, options);
|
|
119
|
+
}).catch(() => {
|
|
120
|
+
constructNotification(title, options);
|
|
121
|
+
});
|
|
122
|
+
} else constructNotification(title, options);
|
|
123
|
+
}
|
|
124
|
+
if (sound === "alert") playAlertChime();
|
|
125
|
+
else playPopChime();
|
|
126
|
+
}
|
|
127
|
+
// src/client/index.ts
|
|
128
|
+
const inject = [
|
|
129
|
+
"remote",
|
|
130
|
+
"remote.session",
|
|
131
|
+
"sessions",
|
|
132
|
+
"uiSession"
|
|
133
|
+
];
|
|
134
|
+
const STOP_NOTIFICATION_DEBOUNCE_MS = 3e3;
|
|
135
|
+
const RECENT_MESSAGE_WINDOW_MS = 6e4;
|
|
136
|
+
/**
|
|
137
|
+
* Reports whether the session appended a user or assistant message inside the
|
|
138
|
+
* recent-message window.
|
|
139
|
+
*
|
|
140
|
+
* edge case: command-driven work (workflow steps, scheduled scripts) appends no
|
|
141
|
+
* chat message to the parent, so a run that produced no message stays silent by
|
|
142
|
+
* design.
|
|
143
|
+
*/
|
|
144
|
+
async function hasRecentMessage(ctx, sessionId) {
|
|
145
|
+
const now = Date.now();
|
|
146
|
+
try {
|
|
147
|
+
const result = await ctx.remote.session.events({
|
|
148
|
+
address: {
|
|
149
|
+
kind: "session",
|
|
150
|
+
sessionId
|
|
151
|
+
},
|
|
152
|
+
maxMessages: 1
|
|
153
|
+
});
|
|
154
|
+
if (!result.ok) {
|
|
155
|
+
console.warn("dsh-client-notify: session events read failed", result.error);
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
const records = result.value.records;
|
|
159
|
+
for (let i = records.length - 1; i >= 0; i--) {
|
|
160
|
+
const event = records[i].event;
|
|
161
|
+
if (event.type === "user/message" || event.type === "assistant/message") return event.time >= now - RECENT_MESSAGE_WINDOW_MS;
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
console.warn("dsh-client-notify: session events read threw", error);
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
/** @internal */
|
|
170
|
+
function getSessionAlert(interaction) {
|
|
171
|
+
const hasApprovalQuestion = "questions" in interaction && Array.isArray(interaction.questions) && interaction.questions.some((q) => {
|
|
172
|
+
if (typeof q !== "object" || q === null) return false;
|
|
173
|
+
if ("id" in q && typeof q.id === "string" && q.id.startsWith("acp-permission:")) return true;
|
|
174
|
+
if ("intent" in q && typeof q.intent === "object" && q.intent !== null) {
|
|
175
|
+
const intent = q.intent;
|
|
176
|
+
return "kind" in intent && intent.kind === "approval";
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
});
|
|
180
|
+
if (interaction.kind === "approval" || interaction.key.startsWith("acp-permission:") || hasApprovalQuestion) return "permission";
|
|
181
|
+
if (interaction.kind === "question") return "question";
|
|
182
|
+
if (interaction.kind !== "plan-review") return "error";
|
|
183
|
+
}
|
|
184
|
+
function apply(ctx) {
|
|
185
|
+
requestNotificationPermission();
|
|
186
|
+
const sessions = ctx.sessions;
|
|
187
|
+
const remote = ctx.remote;
|
|
188
|
+
const sessionStatus = ctx.uiSession.sessionStatus;
|
|
189
|
+
const runningSessions = /* @__PURE__ */ new Set();
|
|
190
|
+
const stopTimers = /* @__PURE__ */ new Map();
|
|
191
|
+
function isSubagent(sessionId) {
|
|
192
|
+
if (sessions.subagentAddress(sessionId) !== void 0) return true;
|
|
193
|
+
const summary = sessions.list.getSnapshot().byId[sessionId];
|
|
194
|
+
if (summary !== void 0 && (summary.origin === "subagent" || summary.parentId !== void 0)) return true;
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
function hasActiveSubagents(parentId) {
|
|
198
|
+
const snapshot = sessions.list.getSnapshot();
|
|
199
|
+
for (const summary of Object.values(snapshot.byId)) if (summary && summary.origin === "subagent" && summary.parentId === parentId && summary.running) return true;
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
ctx.effect(() => {
|
|
203
|
+
const unregister = remote.$on("api-session/status", (sessionId, running) => {
|
|
204
|
+
if (running) {
|
|
205
|
+
const pendingTimer = stopTimers.get(sessionId);
|
|
206
|
+
if (pendingTimer !== void 0) {
|
|
207
|
+
clearTimeout(pendingTimer);
|
|
208
|
+
stopTimers.delete(sessionId);
|
|
209
|
+
}
|
|
210
|
+
runningSessions.add(sessionId);
|
|
211
|
+
if (isSubagent(sessionId)) {
|
|
212
|
+
const parentId = sessions.list.getSnapshot().byId[sessionId]?.parentId;
|
|
213
|
+
if (parentId) {
|
|
214
|
+
const parentTimer = stopTimers.get(parentId);
|
|
215
|
+
if (parentTimer !== void 0) {
|
|
216
|
+
clearTimeout(parentTimer);
|
|
217
|
+
stopTimers.delete(parentId);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
if (runningSessions.delete(sessionId)) {
|
|
224
|
+
if (isSubagent(sessionId)) {
|
|
225
|
+
const parentId = sessions.list.getSnapshot().byId[sessionId]?.parentId;
|
|
226
|
+
if (parentId && !runningSessions.has(parentId) && !hasActiveSubagents(parentId) && !stopTimers.has(parentId)) {
|
|
227
|
+
const timer = setTimeout(async () => {
|
|
228
|
+
stopTimers.delete(parentId);
|
|
229
|
+
if (runningSessions.has(parentId) || hasActiveSubagents(parentId)) return;
|
|
230
|
+
if (await hasRecentMessage(ctx, parentId) && !runningSessions.has(parentId) && !hasActiveSubagents(parentId)) showBrowserNotification("DeepSeek Harness", "Agent stopped", "stop");
|
|
231
|
+
}, STOP_NOTIFICATION_DEBOUNCE_MS);
|
|
232
|
+
stopTimers.set(parentId, timer);
|
|
233
|
+
}
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (hasActiveSubagents(sessionId)) return;
|
|
237
|
+
const timer = setTimeout(async () => {
|
|
238
|
+
stopTimers.delete(sessionId);
|
|
239
|
+
if (runningSessions.has(sessionId) || hasActiveSubagents(sessionId)) return;
|
|
240
|
+
if (await hasRecentMessage(ctx, sessionId) && !runningSessions.has(sessionId) && !hasActiveSubagents(sessionId)) showBrowserNotification("DeepSeek Harness", "Agent stopped", "stop");
|
|
241
|
+
}, STOP_NOTIFICATION_DEBOUNCE_MS);
|
|
242
|
+
stopTimers.set(sessionId, timer);
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
return () => {
|
|
246
|
+
unregister();
|
|
247
|
+
for (const timer of stopTimers.values()) clearTimeout(timer);
|
|
248
|
+
stopTimers.clear();
|
|
249
|
+
};
|
|
250
|
+
}, "dsh-client-notify: api-session/status listener");
|
|
251
|
+
const seenInteractions = /* @__PURE__ */ new Set();
|
|
252
|
+
ctx.effect(() => {
|
|
253
|
+
const unsubscribe = sessionStatus.subscribe(() => {
|
|
254
|
+
const snapshot = sessionStatus.getSnapshot();
|
|
255
|
+
const activeKeys = /* @__PURE__ */ new Set();
|
|
256
|
+
for (const [sessionId, status] of snapshot) {
|
|
257
|
+
const interaction = status.pendingInteraction;
|
|
258
|
+
if (interaction === void 0) continue;
|
|
259
|
+
activeKeys.add(interaction.key);
|
|
260
|
+
if (seenInteractions.has(interaction.key)) continue;
|
|
261
|
+
seenInteractions.add(interaction.key);
|
|
262
|
+
if (isSubagent(sessionId)) continue;
|
|
263
|
+
const alertKind = getSessionAlert(interaction);
|
|
264
|
+
if (alertKind === void 0) continue;
|
|
265
|
+
if (alertKind === "permission") {
|
|
266
|
+
let toolName;
|
|
267
|
+
if ("toolName" in interaction && typeof interaction.toolName === "string") toolName = interaction.toolName;
|
|
268
|
+
else if ("questions" in interaction && Array.isArray(interaction.questions) && interaction.questions.length > 0 && typeof interaction.questions[0] === "object" && interaction.questions[0] !== null && "question" in interaction.questions[0] && typeof interaction.questions[0].question === "string") toolName = interaction.questions[0].question;
|
|
269
|
+
else toolName = "tool";
|
|
270
|
+
showBrowserNotification("DeepSeek Harness", `Tool approval requested: ${toolName}`, "alert");
|
|
271
|
+
} else if (alertKind === "question") {
|
|
272
|
+
const questions = "questions" in interaction && Array.isArray(interaction.questions) ? interaction.questions : [];
|
|
273
|
+
showBrowserNotification("DeepSeek Harness", `Question: ${questions.length > 0 && typeof questions[0] === "object" && questions[0] !== null && "question" in questions[0] && typeof questions[0].question === "string" ? questions[0].question : "Question asked"}`, "alert");
|
|
274
|
+
} else if (alertKind === "error") showBrowserNotification("DeepSeek Harness", `Action required: ${interaction.kind}`, "alert");
|
|
275
|
+
}
|
|
276
|
+
for (const key of seenInteractions) if (!activeKeys.has(key)) seenInteractions.delete(key);
|
|
277
|
+
});
|
|
278
|
+
return () => {
|
|
279
|
+
unsubscribe();
|
|
280
|
+
};
|
|
281
|
+
}, "dsh-client-notify: uiSession status subscription");
|
|
282
|
+
let recentReview = 0;
|
|
283
|
+
let lastBadge = 0;
|
|
284
|
+
const updateBadge = () => {
|
|
285
|
+
let alerts = 0;
|
|
286
|
+
for (const status of sessionStatus.getSnapshot().values()) if (status.pendingInteraction !== void 0) alerts++;
|
|
287
|
+
const total = alerts + recentReview;
|
|
288
|
+
if (total === lastBadge) return;
|
|
289
|
+
lastBadge = total;
|
|
290
|
+
syncAppBadge(total);
|
|
291
|
+
};
|
|
292
|
+
ctx.provide("appBadge", { setRecentReviewCount: (count) => {
|
|
293
|
+
recentReview = count;
|
|
294
|
+
updateBadge();
|
|
295
|
+
} });
|
|
296
|
+
ctx.effect(() => {
|
|
297
|
+
updateBadge();
|
|
298
|
+
const unsubscribe = sessionStatus.subscribe(updateBadge);
|
|
299
|
+
return () => {
|
|
300
|
+
unsubscribe();
|
|
301
|
+
recentReview = 0;
|
|
302
|
+
lastBadge = 0;
|
|
303
|
+
syncAppBadge(0);
|
|
304
|
+
};
|
|
305
|
+
}, "dsh-client-notify: app icon badge");
|
|
306
|
+
}
|
|
307
|
+
exports.STOP_NOTIFICATION_DEBOUNCE_MS = STOP_NOTIFICATION_DEBOUNCE_MS;
|
|
308
|
+
exports.apply = apply;
|
|
309
|
+
exports.getSessionAlert = getSessionAlert;
|
|
310
|
+
exports.hasRecentMessage = hasRecentMessage;
|
|
311
|
+
exports.inject = inject;
|
|
312
|
+
return module.exports;
|
|
313
|
+
}
|
|
314
|
+
});
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
const name = "dsh-client-notify";
|
|
3
|
+
const inject = ["webServer"];
|
|
4
|
+
const SERVICE_WORKER_SCRIPT = `
|
|
5
|
+
self.addEventListener('install', (event) => {
|
|
6
|
+
self.skipWaiting();
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
self.addEventListener('activate', (event) => {
|
|
10
|
+
event.waitUntil(self.clients.claim());
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
self.addEventListener('notificationclick', (event) => {
|
|
14
|
+
event.notification.close();
|
|
15
|
+
event.waitUntil(
|
|
16
|
+
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
|
|
17
|
+
for (const client of clientList) {
|
|
18
|
+
if ('focus' in client) {
|
|
19
|
+
return client.focus();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
if (self.clients.openWindow) {
|
|
23
|
+
return self.clients.openWindow('/');
|
|
24
|
+
}
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
});
|
|
28
|
+
`.trim();
|
|
29
|
+
function apply(ctx) {
|
|
30
|
+
for (const path of ["/sw.js", "/tsctl/sw.js"]) ctx.effect(() => ctx.webServer.register({
|
|
31
|
+
kind: "exact",
|
|
32
|
+
path,
|
|
33
|
+
handler: (_req, res) => {
|
|
34
|
+
res.writeHead(200, {
|
|
35
|
+
"content-type": "application/javascript; charset=utf-8",
|
|
36
|
+
"cache-control": "no-cache",
|
|
37
|
+
"service-worker-allowed": "/"
|
|
38
|
+
});
|
|
39
|
+
res.end(SERVICE_WORKER_SCRIPT);
|
|
40
|
+
}
|
|
41
|
+
}), `dsh-client-notify: service worker route (${path})`);
|
|
42
|
+
}
|
|
43
|
+
export { SERVICE_WORKER_SCRIPT, apply, inject, name };
|
package/package.json
CHANGED
|
@@ -1,6 +1,83 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lukstei/dsh-client-notify",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.2-rc.5.4",
|
|
4
|
+
"description": "Client-side browser desktop notification plugin for DeepSeek Harness",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./src/index.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./client": "./dist/client.js",
|
|
14
|
+
"./package.json": "./package.json",
|
|
15
|
+
"./client-contract": "./src/client/contract.ts"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"dist",
|
|
20
|
+
"README.md",
|
|
21
|
+
"cordis.patch.yml",
|
|
22
|
+
"!src/**/*.spec.*",
|
|
23
|
+
"!src/**/*.test.*",
|
|
24
|
+
"!src/**/*.stories.*",
|
|
25
|
+
"!src/**/stories",
|
|
26
|
+
"!src/**/*.e2e.*",
|
|
27
|
+
"!src/**/__snapshots__",
|
|
28
|
+
"!src/**/fixtures"
|
|
29
|
+
],
|
|
30
|
+
"dsh": {
|
|
31
|
+
"bundle": {
|
|
32
|
+
"patch": "./cordis.patch.yml"
|
|
33
|
+
},
|
|
34
|
+
"client": {
|
|
35
|
+
"platform": "web",
|
|
36
|
+
"immediately": true,
|
|
37
|
+
"inject": [
|
|
38
|
+
"@deepseek-ai/dsh-api-remotes",
|
|
39
|
+
"@deepseek-ai/dsh-api-session-controller",
|
|
40
|
+
"@deepseek-ai/dsh-client-ui-session"
|
|
41
|
+
]
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@deepseek-ai/cordis": "4.1.0",
|
|
46
|
+
"@deepseek-ai/dsh-api-remotes": "0.2.2-rc.5",
|
|
47
|
+
"@deepseek-ai/dsh-api-session-controller": "0.2.2-rc.5",
|
|
48
|
+
"@deepseek-ai/dsh-client-ui-session": "0.2.2-rc.5",
|
|
49
|
+
"@deepseek-ai/dsh-host-webserver": "0.2.2-rc.5",
|
|
50
|
+
"@deepseek-ai/dsh-session": "0.2.2-rc.5",
|
|
51
|
+
"@types/node": "^26.0.0",
|
|
52
|
+
"tsdown": "^0.23.0",
|
|
53
|
+
"typescript": "^7.0.2",
|
|
54
|
+
"vitest": "5.0.5"
|
|
55
|
+
},
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"@deepseek-ai/cordis": "*",
|
|
58
|
+
"@deepseek-ai/dsh-api-remotes": "*",
|
|
59
|
+
"@deepseek-ai/dsh-api-session-controller": "*",
|
|
60
|
+
"@deepseek-ai/dsh-client-ui-session": "*",
|
|
61
|
+
"@deepseek-ai/dsh-host-webserver": "*",
|
|
62
|
+
"@deepseek-ai/dsh-session": "*"
|
|
63
|
+
},
|
|
64
|
+
"license": "MIT",
|
|
65
|
+
"repository": {
|
|
66
|
+
"type": "git",
|
|
67
|
+
"url": "git+https://github.com/lukstei/dsh-plugins.git",
|
|
68
|
+
"directory": "packages/dsh-client-notify"
|
|
69
|
+
},
|
|
70
|
+
"publishConfig": {
|
|
71
|
+
"access": "public"
|
|
72
|
+
},
|
|
73
|
+
"scripts": {
|
|
74
|
+
"build": "tsdown --config tsdown.config.ts --logLevel error",
|
|
75
|
+
"dev": "tsdown --config tsdown.config.ts --watch",
|
|
76
|
+
"check": "biome check --reporter=concise .",
|
|
77
|
+
"typecheck": "tsc --noEmit",
|
|
78
|
+
"test": "vitest run --reporter=minimal",
|
|
79
|
+
"test:watch": "vitest",
|
|
80
|
+
"test:update": "vitest run -u",
|
|
81
|
+
"verify": "pnpm --silent run check && pnpm --silent run build && pnpm --silent run typecheck && pnpm --silent run test"
|
|
82
|
+
}
|
|
6
83
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Sets the installed-PWA app-icon badge; a count of 0 clears it. */
|
|
2
|
+
export function syncAppBadge(count: number): void {
|
|
3
|
+
if (typeof navigator === "undefined" || !("setAppBadge" in navigator)) return;
|
|
4
|
+
// edge case: the browser rejects when the app is not installed or permission is missing.
|
|
5
|
+
const result =
|
|
6
|
+
count > 0 ? navigator.setAppBadge(count) : navigator.clearAppBadge();
|
|
7
|
+
result.catch(() => {});
|
|
8
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type {} from "@deepseek-ai/cordis";
|
|
2
|
+
|
|
3
|
+
export interface IAppBadge {
|
|
4
|
+
/** Sessions in review that were active recently; added to pending-interaction sessions for the app-icon badge. */
|
|
5
|
+
readonly setRecentReviewCount: (count: number) => void;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
declare module "@deepseek-ai/cordis" {
|
|
9
|
+
interface Context {
|
|
10
|
+
appBadge: IAppBadge;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,358 @@
|
|
|
1
|
+
import type { Context as ClientContext } from "@deepseek-ai/cordis";
|
|
2
|
+
import type {} from "@deepseek-ai/dsh-api-remotes/client";
|
|
3
|
+
import type { SessionSummary } from "@deepseek-ai/dsh-api-session-controller/client";
|
|
4
|
+
import type {} from "@deepseek-ai/dsh-api-session-controller/remote";
|
|
5
|
+
import type { SessionPendingInteraction } from "@deepseek-ai/dsh-client-ui-session/client";
|
|
6
|
+
import type { SessionId } from "@deepseek-ai/dsh-session/types";
|
|
7
|
+
import { syncAppBadge } from "./badge.ts";
|
|
8
|
+
import type {} from "./contract.ts";
|
|
9
|
+
import {
|
|
10
|
+
requestNotificationPermission,
|
|
11
|
+
showBrowserNotification,
|
|
12
|
+
} from "./notify.ts";
|
|
13
|
+
|
|
14
|
+
export const inject = [
|
|
15
|
+
"remote",
|
|
16
|
+
"remote.session",
|
|
17
|
+
"sessions",
|
|
18
|
+
"uiSession",
|
|
19
|
+
] as const;
|
|
20
|
+
|
|
21
|
+
export const STOP_NOTIFICATION_DEBOUNCE_MS = 3000;
|
|
22
|
+
const RECENT_MESSAGE_WINDOW_MS = 60_000;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Reports whether the session appended a user or assistant message inside the
|
|
26
|
+
* recent-message window.
|
|
27
|
+
*
|
|
28
|
+
* edge case: command-driven work (workflow steps, scheduled scripts) appends no
|
|
29
|
+
* chat message to the parent, so a run that produced no message stays silent by
|
|
30
|
+
* design.
|
|
31
|
+
*/
|
|
32
|
+
export async function hasRecentMessage(
|
|
33
|
+
ctx: ClientContext,
|
|
34
|
+
sessionId: SessionId,
|
|
35
|
+
): Promise<boolean> {
|
|
36
|
+
const now = Date.now();
|
|
37
|
+
// A failed read must not swallow a real stop notification: fail open and warn.
|
|
38
|
+
try {
|
|
39
|
+
const result = await ctx.remote.session.events({
|
|
40
|
+
address: { kind: "session", sessionId },
|
|
41
|
+
maxMessages: 1,
|
|
42
|
+
});
|
|
43
|
+
if (!result.ok) {
|
|
44
|
+
console.warn(
|
|
45
|
+
"dsh-client-notify: session events read failed",
|
|
46
|
+
result.error,
|
|
47
|
+
);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
const records = result.value.records;
|
|
51
|
+
for (let i = records.length - 1; i >= 0; i--) {
|
|
52
|
+
const event = records[i].event;
|
|
53
|
+
if (event.type === "user/message" || event.type === "assistant/message") {
|
|
54
|
+
return event.time >= now - RECENT_MESSAGE_WINDOW_MS;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
} catch (error) {
|
|
58
|
+
console.warn("dsh-client-notify: session events read threw", error);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
type SessionAlertKind = "permission" | "question" | "error";
|
|
66
|
+
|
|
67
|
+
/** @internal */
|
|
68
|
+
export function getSessionAlert(
|
|
69
|
+
interaction: SessionPendingInteraction,
|
|
70
|
+
): SessionAlertKind | undefined {
|
|
71
|
+
const hasApprovalQuestion =
|
|
72
|
+
"questions" in interaction &&
|
|
73
|
+
Array.isArray(interaction.questions) &&
|
|
74
|
+
interaction.questions.some((q: unknown) => {
|
|
75
|
+
if (typeof q !== "object" || q === null) return false;
|
|
76
|
+
if (
|
|
77
|
+
"id" in q &&
|
|
78
|
+
typeof q.id === "string" &&
|
|
79
|
+
q.id.startsWith("acp-permission:")
|
|
80
|
+
) {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
if ("intent" in q && typeof q.intent === "object" && q.intent !== null) {
|
|
84
|
+
const intent = q.intent;
|
|
85
|
+
return "kind" in intent && intent.kind === "approval";
|
|
86
|
+
}
|
|
87
|
+
return false;
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
interaction.kind === "approval" ||
|
|
92
|
+
interaction.key.startsWith("acp-permission:") ||
|
|
93
|
+
hasApprovalQuestion
|
|
94
|
+
) {
|
|
95
|
+
return "permission";
|
|
96
|
+
}
|
|
97
|
+
if (interaction.kind === "question") {
|
|
98
|
+
return "question";
|
|
99
|
+
}
|
|
100
|
+
if (interaction.kind !== "plan-review") {
|
|
101
|
+
return "error";
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function apply(ctx: ClientContext): void {
|
|
107
|
+
requestNotificationPermission();
|
|
108
|
+
|
|
109
|
+
const sessions = ctx.sessions;
|
|
110
|
+
const remote = ctx.remote;
|
|
111
|
+
const sessionStatus = ctx.uiSession.sessionStatus;
|
|
112
|
+
|
|
113
|
+
const runningSessions = new Set<string>();
|
|
114
|
+
const stopTimers = new Map<SessionId, ReturnType<typeof setTimeout>>();
|
|
115
|
+
|
|
116
|
+
function isSubagent(sessionId: SessionId): boolean {
|
|
117
|
+
if (sessions.subagentAddress(sessionId) !== undefined) return true;
|
|
118
|
+
const summary: SessionSummary | undefined =
|
|
119
|
+
sessions.list.getSnapshot().byId[sessionId];
|
|
120
|
+
if (
|
|
121
|
+
summary !== undefined &&
|
|
122
|
+
(summary.origin === "subagent" || summary.parentId !== undefined)
|
|
123
|
+
) {
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function hasActiveSubagents(parentId: SessionId): boolean {
|
|
130
|
+
const snapshot = sessions.list.getSnapshot();
|
|
131
|
+
for (const summary of Object.values<SessionSummary>(snapshot.byId)) {
|
|
132
|
+
if (
|
|
133
|
+
summary &&
|
|
134
|
+
summary.origin === "subagent" &&
|
|
135
|
+
summary.parentId === parentId &&
|
|
136
|
+
summary.running
|
|
137
|
+
) {
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
ctx.effect(() => {
|
|
145
|
+
const unregister = remote.$on(
|
|
146
|
+
"api-session/status",
|
|
147
|
+
(sessionId: SessionId, running: boolean) => {
|
|
148
|
+
if (running) {
|
|
149
|
+
const pendingTimer = stopTimers.get(sessionId);
|
|
150
|
+
if (pendingTimer !== undefined) {
|
|
151
|
+
clearTimeout(pendingTimer);
|
|
152
|
+
stopTimers.delete(sessionId);
|
|
153
|
+
}
|
|
154
|
+
runningSessions.add(sessionId);
|
|
155
|
+
|
|
156
|
+
if (isSubagent(sessionId)) {
|
|
157
|
+
const parentId =
|
|
158
|
+
sessions.list.getSnapshot().byId[sessionId]?.parentId;
|
|
159
|
+
if (parentId) {
|
|
160
|
+
const parentTimer = stopTimers.get(parentId);
|
|
161
|
+
if (parentTimer !== undefined) {
|
|
162
|
+
clearTimeout(parentTimer);
|
|
163
|
+
stopTimers.delete(parentId);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (runningSessions.delete(sessionId)) {
|
|
171
|
+
if (isSubagent(sessionId)) {
|
|
172
|
+
const parentId =
|
|
173
|
+
sessions.list.getSnapshot().byId[sessionId]?.parentId;
|
|
174
|
+
if (
|
|
175
|
+
parentId &&
|
|
176
|
+
!runningSessions.has(parentId) &&
|
|
177
|
+
!hasActiveSubagents(parentId) &&
|
|
178
|
+
!stopTimers.has(parentId)
|
|
179
|
+
) {
|
|
180
|
+
const timer = setTimeout(async () => {
|
|
181
|
+
stopTimers.delete(parentId);
|
|
182
|
+
if (
|
|
183
|
+
runningSessions.has(parentId) ||
|
|
184
|
+
hasActiveSubagents(parentId)
|
|
185
|
+
) {
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
const shouldNotify = await hasRecentMessage(ctx, parentId);
|
|
189
|
+
if (
|
|
190
|
+
shouldNotify &&
|
|
191
|
+
!runningSessions.has(parentId) &&
|
|
192
|
+
!hasActiveSubagents(parentId)
|
|
193
|
+
) {
|
|
194
|
+
showBrowserNotification(
|
|
195
|
+
"DeepSeek Harness",
|
|
196
|
+
"Agent stopped",
|
|
197
|
+
"stop",
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
}, STOP_NOTIFICATION_DEBOUNCE_MS);
|
|
201
|
+
stopTimers.set(parentId, timer);
|
|
202
|
+
}
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
if (hasActiveSubagents(sessionId)) return;
|
|
207
|
+
|
|
208
|
+
// Workflows and chained prompt turns briefly settle to idle between steps
|
|
209
|
+
// before immediately dispatching the next turn. Debounce stopping notifications
|
|
210
|
+
// so intermediate workflow steps remain silent and only the true cessation of work alerts.
|
|
211
|
+
const timer = setTimeout(async () => {
|
|
212
|
+
stopTimers.delete(sessionId);
|
|
213
|
+
if (
|
|
214
|
+
runningSessions.has(sessionId) ||
|
|
215
|
+
hasActiveSubagents(sessionId)
|
|
216
|
+
) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
const shouldNotify = await hasRecentMessage(ctx, sessionId);
|
|
220
|
+
if (
|
|
221
|
+
shouldNotify &&
|
|
222
|
+
!runningSessions.has(sessionId) &&
|
|
223
|
+
!hasActiveSubagents(sessionId)
|
|
224
|
+
) {
|
|
225
|
+
showBrowserNotification(
|
|
226
|
+
"DeepSeek Harness",
|
|
227
|
+
"Agent stopped",
|
|
228
|
+
"stop",
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}, STOP_NOTIFICATION_DEBOUNCE_MS);
|
|
232
|
+
stopTimers.set(sessionId, timer);
|
|
233
|
+
}
|
|
234
|
+
},
|
|
235
|
+
);
|
|
236
|
+
|
|
237
|
+
return () => {
|
|
238
|
+
unregister();
|
|
239
|
+
for (const timer of stopTimers.values()) {
|
|
240
|
+
clearTimeout(timer);
|
|
241
|
+
}
|
|
242
|
+
stopTimers.clear();
|
|
243
|
+
};
|
|
244
|
+
}, "dsh-client-notify: api-session/status listener");
|
|
245
|
+
|
|
246
|
+
const seenInteractions = new Set<string>();
|
|
247
|
+
|
|
248
|
+
ctx.effect(() => {
|
|
249
|
+
const unsubscribe = sessionStatus.subscribe(() => {
|
|
250
|
+
const snapshot = sessionStatus.getSnapshot();
|
|
251
|
+
const activeKeys = new Set<string>();
|
|
252
|
+
|
|
253
|
+
for (const [sessionId, status] of snapshot) {
|
|
254
|
+
const interaction = status.pendingInteraction;
|
|
255
|
+
if (interaction === undefined) continue;
|
|
256
|
+
|
|
257
|
+
activeKeys.add(interaction.key);
|
|
258
|
+
if (seenInteractions.has(interaction.key)) continue;
|
|
259
|
+
seenInteractions.add(interaction.key);
|
|
260
|
+
|
|
261
|
+
if (isSubagent(sessionId)) continue;
|
|
262
|
+
|
|
263
|
+
const alertKind = getSessionAlert(interaction);
|
|
264
|
+
if (alertKind === undefined) continue;
|
|
265
|
+
|
|
266
|
+
if (alertKind === "permission") {
|
|
267
|
+
let toolName: string;
|
|
268
|
+
if (
|
|
269
|
+
"toolName" in interaction &&
|
|
270
|
+
typeof interaction.toolName === "string"
|
|
271
|
+
) {
|
|
272
|
+
toolName = interaction.toolName;
|
|
273
|
+
} else if (
|
|
274
|
+
"questions" in interaction &&
|
|
275
|
+
Array.isArray(interaction.questions) &&
|
|
276
|
+
interaction.questions.length > 0 &&
|
|
277
|
+
typeof interaction.questions[0] === "object" &&
|
|
278
|
+
interaction.questions[0] !== null &&
|
|
279
|
+
"question" in interaction.questions[0] &&
|
|
280
|
+
typeof interaction.questions[0].question === "string"
|
|
281
|
+
) {
|
|
282
|
+
toolName = interaction.questions[0].question;
|
|
283
|
+
} else {
|
|
284
|
+
toolName = "tool";
|
|
285
|
+
}
|
|
286
|
+
showBrowserNotification(
|
|
287
|
+
"DeepSeek Harness",
|
|
288
|
+
`Tool approval requested: ${toolName}`,
|
|
289
|
+
"alert",
|
|
290
|
+
);
|
|
291
|
+
} else if (alertKind === "question") {
|
|
292
|
+
const questions =
|
|
293
|
+
"questions" in interaction && Array.isArray(interaction.questions)
|
|
294
|
+
? interaction.questions
|
|
295
|
+
: [];
|
|
296
|
+
const questionText =
|
|
297
|
+
questions.length > 0 &&
|
|
298
|
+
typeof questions[0] === "object" &&
|
|
299
|
+
questions[0] !== null &&
|
|
300
|
+
"question" in questions[0] &&
|
|
301
|
+
typeof questions[0].question === "string"
|
|
302
|
+
? questions[0].question
|
|
303
|
+
: "Question asked";
|
|
304
|
+
showBrowserNotification(
|
|
305
|
+
"DeepSeek Harness",
|
|
306
|
+
`Question: ${questionText}`,
|
|
307
|
+
"alert",
|
|
308
|
+
);
|
|
309
|
+
} else if (alertKind === "error") {
|
|
310
|
+
showBrowserNotification(
|
|
311
|
+
"DeepSeek Harness",
|
|
312
|
+
`Action required: ${interaction.kind}`,
|
|
313
|
+
"alert",
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
for (const key of seenInteractions) {
|
|
319
|
+
if (!activeKeys.has(key)) {
|
|
320
|
+
seenInteractions.delete(key);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
return () => {
|
|
326
|
+
unsubscribe();
|
|
327
|
+
};
|
|
328
|
+
}, "dsh-client-notify: uiSession status subscription");
|
|
329
|
+
|
|
330
|
+
let recentReview = 0;
|
|
331
|
+
let lastBadge = 0;
|
|
332
|
+
const updateBadge = () => {
|
|
333
|
+
let alerts = 0;
|
|
334
|
+
for (const status of sessionStatus.getSnapshot().values()) {
|
|
335
|
+
if (status.pendingInteraction !== undefined) alerts++;
|
|
336
|
+
}
|
|
337
|
+
const total = alerts + recentReview;
|
|
338
|
+
if (total === lastBadge) return;
|
|
339
|
+
lastBadge = total;
|
|
340
|
+
syncAppBadge(total);
|
|
341
|
+
};
|
|
342
|
+
ctx.provide("appBadge", {
|
|
343
|
+
setRecentReviewCount: (count) => {
|
|
344
|
+
recentReview = count;
|
|
345
|
+
updateBadge();
|
|
346
|
+
},
|
|
347
|
+
});
|
|
348
|
+
ctx.effect(() => {
|
|
349
|
+
updateBadge();
|
|
350
|
+
const unsubscribe = sessionStatus.subscribe(updateBadge);
|
|
351
|
+
return () => {
|
|
352
|
+
unsubscribe();
|
|
353
|
+
recentReview = 0;
|
|
354
|
+
lastBadge = 0;
|
|
355
|
+
syncAppBadge(0);
|
|
356
|
+
};
|
|
357
|
+
}, "dsh-client-notify: app icon badge");
|
|
358
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type NotificationSound,
|
|
3
|
+
playAlertChime,
|
|
4
|
+
playPopChime,
|
|
5
|
+
} from "./sound.ts";
|
|
6
|
+
|
|
7
|
+
export function registerServiceWorker(): void {
|
|
8
|
+
if (typeof navigator === "undefined" || !("serviceWorker" in navigator))
|
|
9
|
+
return;
|
|
10
|
+
navigator.serviceWorker.register("/sw.js", { scope: "/" }).catch(() => {
|
|
11
|
+
navigator.serviceWorker.register("sw.js").catch(() => {});
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function requestNotificationPermission(): void {
|
|
16
|
+
if (typeof window === "undefined") return;
|
|
17
|
+
|
|
18
|
+
registerServiceWorker();
|
|
19
|
+
|
|
20
|
+
if (!("Notification" in window)) return;
|
|
21
|
+
if (window.Notification.permission !== "default") return;
|
|
22
|
+
|
|
23
|
+
const onRequest = (): void => {
|
|
24
|
+
if (window.Notification.permission === "default") {
|
|
25
|
+
void window.Notification.requestPermission().then((permission) => {
|
|
26
|
+
if (permission !== "default") {
|
|
27
|
+
window.removeEventListener("click", onRequest);
|
|
28
|
+
window.removeEventListener("touchend", onRequest);
|
|
29
|
+
window.removeEventListener("keydown", onRequest);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
window.addEventListener("click", onRequest);
|
|
36
|
+
window.addEventListener("touchend", onRequest);
|
|
37
|
+
window.addEventListener("keydown", onRequest);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function constructNotification(
|
|
41
|
+
title: string,
|
|
42
|
+
options: NotificationOptions,
|
|
43
|
+
): void {
|
|
44
|
+
try {
|
|
45
|
+
const notification = new window.Notification(title, options);
|
|
46
|
+
notification.onclick = () => {
|
|
47
|
+
window.focus();
|
|
48
|
+
notification.close();
|
|
49
|
+
};
|
|
50
|
+
} catch {
|
|
51
|
+
// Suppressed when platform forbids new Notification() (e.g. mobile/PWA requiring ServiceWorker)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function showBrowserNotification(
|
|
56
|
+
title: string,
|
|
57
|
+
message: string,
|
|
58
|
+
sound: NotificationSound,
|
|
59
|
+
): void {
|
|
60
|
+
if (typeof window === "undefined") return;
|
|
61
|
+
|
|
62
|
+
const isMobile =
|
|
63
|
+
(typeof window.matchMedia === "function" &&
|
|
64
|
+
window.matchMedia("(pointer: coarse)").matches) ||
|
|
65
|
+
(typeof navigator !== "undefined" && (navigator.maxTouchPoints ?? 0) > 0);
|
|
66
|
+
|
|
67
|
+
// Suppress desktop notification if user is actively in the tab on desktop
|
|
68
|
+
if (!isMobile && document.hasFocus() && !document.hidden) return;
|
|
69
|
+
|
|
70
|
+
if (
|
|
71
|
+
"Notification" in window &&
|
|
72
|
+
window.Notification.permission === "granted"
|
|
73
|
+
) {
|
|
74
|
+
const options: NotificationOptions = {
|
|
75
|
+
body: message,
|
|
76
|
+
icon: "/favicon.svg",
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
if (typeof navigator !== "undefined" && "serviceWorker" in navigator) {
|
|
80
|
+
const resolveSwReg = async (): Promise<
|
|
81
|
+
ServiceWorkerRegistration | undefined
|
|
82
|
+
> => {
|
|
83
|
+
const reg = await navigator.serviceWorker.getRegistration();
|
|
84
|
+
if (reg && "showNotification" in reg) return reg;
|
|
85
|
+
return Promise.race([
|
|
86
|
+
navigator.serviceWorker.ready,
|
|
87
|
+
new Promise<undefined>((resolve) =>
|
|
88
|
+
setTimeout(() => resolve(undefined), 300),
|
|
89
|
+
),
|
|
90
|
+
]);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
void resolveSwReg()
|
|
94
|
+
.then((reg) => {
|
|
95
|
+
if (reg && "showNotification" in reg) {
|
|
96
|
+
void reg.showNotification(title, options);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
constructNotification(title, options);
|
|
100
|
+
})
|
|
101
|
+
.catch(() => {
|
|
102
|
+
constructNotification(title, options);
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
constructNotification(title, options);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (sound === "alert") {
|
|
110
|
+
playAlertChime();
|
|
111
|
+
} else {
|
|
112
|
+
playPopChime();
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
export type NotificationSound = "stop" | "alert";
|
|
2
|
+
|
|
3
|
+
let sharedContext: AudioContext | undefined;
|
|
4
|
+
|
|
5
|
+
function audioContext(): AudioContext | undefined {
|
|
6
|
+
const AudioCtx =
|
|
7
|
+
window.AudioContext ||
|
|
8
|
+
(window as unknown as { webkitAudioContext: typeof AudioContext })
|
|
9
|
+
.webkitAudioContext;
|
|
10
|
+
if (!AudioCtx) return undefined;
|
|
11
|
+
sharedContext ??= new AudioCtx();
|
|
12
|
+
// edge case: Firefox hands back a suspended context; notes scheduled on a frozen clock never fire
|
|
13
|
+
if (sharedContext.state === "suspended") {
|
|
14
|
+
void sharedContext.resume().catch(() => {});
|
|
15
|
+
}
|
|
16
|
+
return sharedContext;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function playPopChime(): void {
|
|
20
|
+
try {
|
|
21
|
+
const ctx = audioContext();
|
|
22
|
+
if (!ctx) return;
|
|
23
|
+
const notes = [791];
|
|
24
|
+
const startTime = ctx.currentTime;
|
|
25
|
+
|
|
26
|
+
notes.forEach((freq, i) => {
|
|
27
|
+
const osc = ctx.createOscillator();
|
|
28
|
+
const gain = ctx.createGain();
|
|
29
|
+
const t = startTime + i * 0.12;
|
|
30
|
+
|
|
31
|
+
osc.type = "sine";
|
|
32
|
+
osc.frequency.setValueAtTime(freq, t);
|
|
33
|
+
osc.frequency.exponentialRampToValueAtTime(freq * 0.22, t + 0.05);
|
|
34
|
+
gain.gain.setValueAtTime(0, t);
|
|
35
|
+
gain.gain.linearRampToValueAtTime(0.11, t + 0.01);
|
|
36
|
+
gain.gain.exponentialRampToValueAtTime(0.0001, t + 0.07);
|
|
37
|
+
|
|
38
|
+
osc.connect(gain).connect(ctx.destination);
|
|
39
|
+
osc.start(t);
|
|
40
|
+
osc.stop(t + 0.12);
|
|
41
|
+
});
|
|
42
|
+
} catch {
|
|
43
|
+
// Suppressed when browser policy blocks audio before user interaction
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function playAlertChime(): void {
|
|
48
|
+
try {
|
|
49
|
+
const ctx = audioContext();
|
|
50
|
+
if (!ctx) return;
|
|
51
|
+
const notes = [587.33, 880];
|
|
52
|
+
const startTime = ctx.currentTime;
|
|
53
|
+
|
|
54
|
+
notes.forEach((freq, i) => {
|
|
55
|
+
const osc = ctx.createOscillator();
|
|
56
|
+
const gain = ctx.createGain();
|
|
57
|
+
const t = startTime + i * 0.12;
|
|
58
|
+
|
|
59
|
+
osc.type = "sine";
|
|
60
|
+
osc.frequency.setValueAtTime(freq, t);
|
|
61
|
+
gain.gain.setValueAtTime(0, t);
|
|
62
|
+
gain.gain.linearRampToValueAtTime(0.12, t + 0.01);
|
|
63
|
+
gain.gain.exponentialRampToValueAtTime(0.0001, t + 0.11);
|
|
64
|
+
|
|
65
|
+
osc.connect(gain).connect(ctx.destination);
|
|
66
|
+
osc.start(t);
|
|
67
|
+
osc.stop(t + 0.12);
|
|
68
|
+
});
|
|
69
|
+
} catch {
|
|
70
|
+
// Suppressed when browser policy blocks audio before user interaction
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
2
|
+
import type { WebRoute } from "@deepseek-ai/dsh-host-webserver";
|
|
3
|
+
|
|
4
|
+
export type { WebRoute };
|
|
5
|
+
|
|
6
|
+
export const name = "dsh-client-notify";
|
|
7
|
+
export const inject = ["webServer"] as const;
|
|
8
|
+
|
|
9
|
+
export const SERVICE_WORKER_SCRIPT = `
|
|
10
|
+
self.addEventListener('install', (event) => {
|
|
11
|
+
self.skipWaiting();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
self.addEventListener('activate', (event) => {
|
|
15
|
+
event.waitUntil(self.clients.claim());
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
self.addEventListener('notificationclick', (event) => {
|
|
19
|
+
event.notification.close();
|
|
20
|
+
event.waitUntil(
|
|
21
|
+
self.clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => {
|
|
22
|
+
for (const client of clientList) {
|
|
23
|
+
if ('focus' in client) {
|
|
24
|
+
return client.focus();
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (self.clients.openWindow) {
|
|
28
|
+
return self.clients.openWindow('/');
|
|
29
|
+
}
|
|
30
|
+
})
|
|
31
|
+
);
|
|
32
|
+
});
|
|
33
|
+
`.trim();
|
|
34
|
+
|
|
35
|
+
export function apply(ctx: Context): void {
|
|
36
|
+
for (const path of ["/sw.js", "/tsctl/sw.js"]) {
|
|
37
|
+
ctx.effect(
|
|
38
|
+
() =>
|
|
39
|
+
ctx.webServer.register({
|
|
40
|
+
kind: "exact",
|
|
41
|
+
path,
|
|
42
|
+
handler: (_req, res) => {
|
|
43
|
+
res.writeHead(200, {
|
|
44
|
+
"content-type": "application/javascript; charset=utf-8",
|
|
45
|
+
"cache-control": "no-cache",
|
|
46
|
+
"service-worker-allowed": "/",
|
|
47
|
+
});
|
|
48
|
+
res.end(SERVICE_WORKER_SCRIPT);
|
|
49
|
+
},
|
|
50
|
+
}),
|
|
51
|
+
`dsh-client-notify: service worker route (${path})`,
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
}
|