@game25011/dsh-notify-toast 0.0.0-stage → 0.4.2
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/AI-DISCLOSURE.md +29 -0
- package/CHANGELOG.md +87 -0
- package/LICENSE +22 -0
- package/README.en.md +290 -0
- package/README.md +308 -3
- package/THIRD_PARTY_NOTICES.md +22 -0
- package/assets/sounds/bubble.ogg +0 -0
- package/cordis.patch.yml +5 -0
- package/docs/INTERFACE.md +180 -0
- package/docs/RELEASING.md +282 -0
- package/docs/TEST-REPORT.md +183 -0
- package/docs/assets/native-toast.png +0 -0
- package/dsh-plugin.json +14 -0
- package/icon.svg +17 -0
- package/lib/all-done.js +126 -0
- package/lib/client.js +3486 -0
- package/lib/config.js +420 -0
- package/lib/goal-summary.js +438 -0
- package/lib/index.js +1411 -0
- package/lib/native-focus.js +350 -0
- package/lib/native-toast-helper.cs +206 -0
- package/lib/native-toast.js +372 -0
- package/lib/native-toast.ps1 +72 -0
- package/lib/taskbar-badge-helper.cs +126 -0
- package/lib/taskbar-badge-main.cs +43 -0
- package/lib/taskbar-badge.js +347 -0
- package/lib/taskbar-badge.ps1 +59 -0
- package/lib/text.js +174 -0
- package/locale/en.json +17 -0
- package/locale/zh.json +17 -0
- package/package.json +76 -6
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-notify-toast — Windows taskbar overlay badge manager.
|
|
3
|
+
*
|
|
4
|
+
* The badge is a per-window taskbar overlay icon. The Host cannot call
|
|
5
|
+
* Electron's BrowserWindow API from a plugin, so a tiny PowerShell/C# helper
|
|
6
|
+
* calls ITaskbarList3::SetOverlayIcon on the DSH main window. The helper is
|
|
7
|
+
* one-shot: Windows keeps the rendered overlay after the process exits.
|
|
8
|
+
*
|
|
9
|
+
* Updates are debounced and serialized so a late completion cannot overwrite
|
|
10
|
+
* a newer focus-clear.
|
|
11
|
+
*/
|
|
12
|
+
import { spawn } from 'node:child_process'
|
|
13
|
+
import { createHash } from 'node:crypto'
|
|
14
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync } from 'node:fs'
|
|
15
|
+
import { dirname, join } from 'node:path'
|
|
16
|
+
import { fileURLToPath } from 'node:url'
|
|
17
|
+
import { listDesktopWindowCandidates } from './native-focus.js'
|
|
18
|
+
|
|
19
|
+
const UPDATE_DEBOUNCE_MS = 120
|
|
20
|
+
const HELPER_TIMEOUT_MS = 12000
|
|
21
|
+
const COMPILE_TIMEOUT_MS = 20000
|
|
22
|
+
const MAX_BADGE_COUNT = 99
|
|
23
|
+
const CACHE_KEEP = 8
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {object} [options]
|
|
27
|
+
* @param {string} [options.scriptPath] absolute path to taskbar-badge.ps1
|
|
28
|
+
* @param {string} [options.powershellPath] Windows PowerShell executable
|
|
29
|
+
* @param {Function} [options.spawnProcess] child_process.spawn-compatible
|
|
30
|
+
* @param {(message: string, error?: unknown) => void} [options.logWarn]
|
|
31
|
+
* @returns {{setCount: (count: number) => void, warm: () => void, dispose: () => void, pendingCount: () => number}}
|
|
32
|
+
*/
|
|
33
|
+
export function createTaskbarBadgeManager(options = {}) {
|
|
34
|
+
const {
|
|
35
|
+
scriptPath = join(dirname(fileURLToPath(import.meta.url)), 'taskbar-badge.ps1'),
|
|
36
|
+
sourcePath = join(dirname(fileURLToPath(import.meta.url)), 'taskbar-badge-helper.cs'),
|
|
37
|
+
mainSourcePath = join(dirname(fileURLToPath(import.meta.url)), 'taskbar-badge-main.cs'),
|
|
38
|
+
powershellPath = join(process.env.WINDIR || 'C:\\Windows', 'System32', 'WindowsPowerShell', 'v1.0', 'powershell.exe'),
|
|
39
|
+
cscPath = join(process.env.WINDIR || 'C:\\Windows', 'Microsoft.NET', 'Framework64', 'v4.0.30319', 'csc.exe'),
|
|
40
|
+
cacheRoot = join(process.env.LOCALAPPDATA || '', 'dsh-notify-toast', 'native-cache'),
|
|
41
|
+
spawnProcess = spawn,
|
|
42
|
+
logWarn = () => {},
|
|
43
|
+
} = options
|
|
44
|
+
|
|
45
|
+
let disposed = false
|
|
46
|
+
let desired = 0
|
|
47
|
+
let applied = -1
|
|
48
|
+
let timer = null
|
|
49
|
+
let running = false
|
|
50
|
+
let rerun = false
|
|
51
|
+
let lastHandle = null
|
|
52
|
+
let child = null
|
|
53
|
+
let warmChild = null
|
|
54
|
+
let executablePath = null
|
|
55
|
+
let executablePromise = null
|
|
56
|
+
|
|
57
|
+
function normalize(count) {
|
|
58
|
+
const value = Number(count)
|
|
59
|
+
if (!Number.isFinite(value)) return 0
|
|
60
|
+
return Math.max(0, Math.min(MAX_BADGE_COUNT, Math.trunc(value)))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function findWindowHandle() {
|
|
64
|
+
try {
|
|
65
|
+
const candidates = listDesktopWindowCandidates()
|
|
66
|
+
if (!Array.isArray(candidates) || candidates.length === 0) return null
|
|
67
|
+
const handle = candidates[0] && candidates[0].handle
|
|
68
|
+
if (handle === undefined || handle === null || String(handle) === '0') return null
|
|
69
|
+
lastHandle = String(handle)
|
|
70
|
+
return lastHandle
|
|
71
|
+
} catch (error) {
|
|
72
|
+
logWarn('cannot enumerate the DSH taskbar window', error)
|
|
73
|
+
return lastHandle
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function clearTimer() {
|
|
78
|
+
if (timer === null) return
|
|
79
|
+
try {
|
|
80
|
+
clearTimeout(timer)
|
|
81
|
+
} catch {
|
|
82
|
+
/* already gone */
|
|
83
|
+
}
|
|
84
|
+
timer = null
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function pruneCache(keepPath) {
|
|
88
|
+
try {
|
|
89
|
+
const names = readdirSync(cacheRoot).filter((name) => /^[a-f0-9]+\.badge\.(exe|dll)$/i.test(name))
|
|
90
|
+
if (names.length <= CACHE_KEEP) return
|
|
91
|
+
const entries = names.map((name) => {
|
|
92
|
+
const path = join(cacheRoot, name)
|
|
93
|
+
let mtime = 0
|
|
94
|
+
try { mtime = statSync(path).mtimeMs } catch { /* ignore */ }
|
|
95
|
+
return { path, mtime }
|
|
96
|
+
}).sort((left, right) => right.mtime - left.mtime)
|
|
97
|
+
for (const entry of entries) {
|
|
98
|
+
if (entry.path === keepPath) continue
|
|
99
|
+
try { rmSync(entry.path, { force: true }) } catch { /* ignore */ }
|
|
100
|
+
if (entries.filter((candidate) => existsSync(candidate.path)).length <= CACHE_KEEP) break
|
|
101
|
+
}
|
|
102
|
+
} catch {
|
|
103
|
+
/* cache pruning is best-effort */
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Compile the tiny C# badge writer once into a cached standalone EXE. This
|
|
109
|
+
* removes Windows PowerShell (~50 MB transient) from the hot path. If the
|
|
110
|
+
* compiler or EXE is unavailable, runHelper() remains the PowerShell
|
|
111
|
+
* fallback and the feature still works.
|
|
112
|
+
*/
|
|
113
|
+
function ensureExecutable() {
|
|
114
|
+
if (executablePromise) return executablePromise
|
|
115
|
+
executablePromise = (async () => {
|
|
116
|
+
try {
|
|
117
|
+
if (!existsSync(sourcePath) || !existsSync(mainSourcePath) || !existsSync(cscPath)) return null
|
|
118
|
+
const hash = createHash('sha256')
|
|
119
|
+
.update(readFileSync(sourcePath))
|
|
120
|
+
.update(readFileSync(mainSourcePath))
|
|
121
|
+
.digest('hex')
|
|
122
|
+
.slice(0, 32)
|
|
123
|
+
const exe = join(cacheRoot, hash + '.badge.exe')
|
|
124
|
+
if (existsSync(exe)) {
|
|
125
|
+
executablePath = exe
|
|
126
|
+
return exe
|
|
127
|
+
}
|
|
128
|
+
mkdirSync(cacheRoot, { recursive: true })
|
|
129
|
+
const framework = dirname(cscPath)
|
|
130
|
+
const temp = exe + '.tmp-' + process.pid + '-' + Date.now()
|
|
131
|
+
const refs = ['System.dll', 'System.Drawing.dll', 'System.Runtime.dll']
|
|
132
|
+
.map((name) => join(framework, name))
|
|
133
|
+
const result = await runProcess(cscPath, [
|
|
134
|
+
'/nologo', '/target:exe', '/out:' + temp,
|
|
135
|
+
...refs.map((ref) => '/r:' + ref),
|
|
136
|
+
sourcePath, mainSourcePath,
|
|
137
|
+
], COMPILE_TIMEOUT_MS, true)
|
|
138
|
+
if (!result.ok || !existsSync(temp)) {
|
|
139
|
+
try { rmSync(temp, { force: true }) } catch { /* ignore */ }
|
|
140
|
+
logWarn('taskbar badge exe compilation failed', result.error)
|
|
141
|
+
return null
|
|
142
|
+
}
|
|
143
|
+
try {
|
|
144
|
+
renameSync(temp, exe)
|
|
145
|
+
} catch (error) {
|
|
146
|
+
try { rmSync(temp, { force: true }) } catch { /* ignore */ }
|
|
147
|
+
if (!existsSync(exe)) {
|
|
148
|
+
logWarn('taskbar badge exe rename failed', error)
|
|
149
|
+
return null
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
pruneCache(exe)
|
|
153
|
+
executablePath = exe
|
|
154
|
+
return exe
|
|
155
|
+
} catch (error) {
|
|
156
|
+
logWarn('taskbar badge exe setup failed', error)
|
|
157
|
+
return null
|
|
158
|
+
}
|
|
159
|
+
})()
|
|
160
|
+
return executablePromise
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function runProcess(file, args, timeoutMs, collectStderr) {
|
|
164
|
+
return new Promise((resolve) => {
|
|
165
|
+
let settled = false
|
|
166
|
+
let proc = null
|
|
167
|
+
let deadline = null
|
|
168
|
+
let stderr = ''
|
|
169
|
+
const finish = (ok, error) => {
|
|
170
|
+
if (settled) return
|
|
171
|
+
settled = true
|
|
172
|
+
if (deadline) {
|
|
173
|
+
try { clearTimeout(deadline) } catch { /* ignore */ }
|
|
174
|
+
}
|
|
175
|
+
resolve({ ok, error: error || '' })
|
|
176
|
+
}
|
|
177
|
+
try {
|
|
178
|
+
proc = spawnProcess(file, args, { windowsHide: true, stdio: ['ignore', 'ignore', collectStderr ? 'pipe' : 'ignore'] })
|
|
179
|
+
} catch (error) {
|
|
180
|
+
finish(false, error)
|
|
181
|
+
return
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
if (collectStderr) {
|
|
185
|
+
proc.stderr?.on?.('data', (chunk) => {
|
|
186
|
+
stderr += String(chunk || '')
|
|
187
|
+
if (stderr.length > 4000) stderr = stderr.slice(-4000)
|
|
188
|
+
})
|
|
189
|
+
}
|
|
190
|
+
proc.on?.('error', (error) => finish(false, error))
|
|
191
|
+
proc.on?.('close', (code) => finish(code === 0, code === 0 ? '' : (stderr || ('exit ' + code))))
|
|
192
|
+
} catch (error) {
|
|
193
|
+
finish(false, error)
|
|
194
|
+
return
|
|
195
|
+
}
|
|
196
|
+
deadline = setTimeout(() => {
|
|
197
|
+
try { proc.kill() } catch { /* ignore */ }
|
|
198
|
+
finish(false, new Error('taskbar badge process timed out'))
|
|
199
|
+
}, timeoutMs)
|
|
200
|
+
})
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async function runBadge(hwnd, count) {
|
|
204
|
+
const exe = await ensureExecutable()
|
|
205
|
+
if (exe) {
|
|
206
|
+
const result = await runProcess(exe, ['-Hwnd', String(hwnd), '-Count', String(count)], HELPER_TIMEOUT_MS, true)
|
|
207
|
+
if (result.ok) return true
|
|
208
|
+
logWarn('standalone taskbar badge helper failed; falling back to PowerShell', result.error)
|
|
209
|
+
}
|
|
210
|
+
return runHelper(hwnd, count)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function runHelper(hwnd, count, warmOnly = false) {
|
|
214
|
+
return new Promise((resolve) => {
|
|
215
|
+
let settled = false
|
|
216
|
+
let proc = null
|
|
217
|
+
let timerForChild = null
|
|
218
|
+
const finish = (ok, error) => {
|
|
219
|
+
if (settled) return
|
|
220
|
+
settled = true
|
|
221
|
+
try {
|
|
222
|
+
if (timerForChild) clearTimeout(timerForChild)
|
|
223
|
+
} catch {
|
|
224
|
+
/* ignore */
|
|
225
|
+
}
|
|
226
|
+
if (child === proc) child = null
|
|
227
|
+
if (warmChild === proc) warmChild = null
|
|
228
|
+
if (!ok && error && !disposed) logWarn('taskbar badge helper failed', error)
|
|
229
|
+
resolve(ok)
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
const args = ['-NoLogo', '-NoProfile', '-NonInteractive', '-STA', '-ExecutionPolicy', 'Bypass', '-File', scriptPath]
|
|
233
|
+
if (warmOnly) {
|
|
234
|
+
args.push('-WarmOnly')
|
|
235
|
+
} else {
|
|
236
|
+
args.push('-Hwnd', String(hwnd), '-Count', String(count))
|
|
237
|
+
}
|
|
238
|
+
proc = spawnProcess(powershellPath, args, { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
|
|
239
|
+
} catch (error) {
|
|
240
|
+
finish(false, error)
|
|
241
|
+
return
|
|
242
|
+
}
|
|
243
|
+
if (warmOnly) warmChild = proc
|
|
244
|
+
else child = proc
|
|
245
|
+
let stderr = ''
|
|
246
|
+
try {
|
|
247
|
+
proc.stdout?.on?.('data', () => {})
|
|
248
|
+
proc.stderr?.on?.('data', (chunk) => {
|
|
249
|
+
stderr += String(chunk || '')
|
|
250
|
+
if (stderr.length > 4000) stderr = stderr.slice(-4000)
|
|
251
|
+
})
|
|
252
|
+
proc.on?.('error', (error) => finish(false, error))
|
|
253
|
+
proc.on?.('close', (code) => finish(code === 0, code === 0 ? null : stderr || ('exit ' + code)))
|
|
254
|
+
} catch (error) {
|
|
255
|
+
finish(false, error)
|
|
256
|
+
return
|
|
257
|
+
}
|
|
258
|
+
timerForChild = setTimeout(() => {
|
|
259
|
+
try {
|
|
260
|
+
proc.kill()
|
|
261
|
+
} catch {
|
|
262
|
+
/* ignore */
|
|
263
|
+
}
|
|
264
|
+
finish(false, new Error('taskbar badge helper timed out'))
|
|
265
|
+
}, HELPER_TIMEOUT_MS)
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async function applyLatest() {
|
|
270
|
+
if (disposed || running) {
|
|
271
|
+
rerun = true
|
|
272
|
+
return
|
|
273
|
+
}
|
|
274
|
+
if (!process || process.platform !== 'win32') return
|
|
275
|
+
const target = findWindowHandle()
|
|
276
|
+
if (!target) return
|
|
277
|
+
running = true
|
|
278
|
+
try {
|
|
279
|
+
do {
|
|
280
|
+
rerun = false
|
|
281
|
+
const count = desired
|
|
282
|
+
if (count === applied) break
|
|
283
|
+
const hwnd = findWindowHandle() || target
|
|
284
|
+
const ok = await runBadge(hwnd, count)
|
|
285
|
+
if (ok) applied = count
|
|
286
|
+
if (disposed) break
|
|
287
|
+
} while (rerun || desired !== applied)
|
|
288
|
+
} finally {
|
|
289
|
+
running = false
|
|
290
|
+
if (!disposed && (rerun || desired !== applied)) {
|
|
291
|
+
// A newer request arrived while the helper was running.
|
|
292
|
+
scheduleApply()
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function scheduleApply() {
|
|
298
|
+
clearTimer()
|
|
299
|
+
if (disposed) return
|
|
300
|
+
timer = setTimeout(() => {
|
|
301
|
+
timer = null
|
|
302
|
+
void applyLatest()
|
|
303
|
+
}, UPDATE_DEBOUNCE_MS)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function setCount(count) {
|
|
307
|
+
if (disposed) return
|
|
308
|
+
desired = normalize(count)
|
|
309
|
+
scheduleApply()
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
function warm() {
|
|
313
|
+
if (disposed || process.platform !== 'win32') return
|
|
314
|
+
void ensureExecutable().then((exe) => {
|
|
315
|
+
if (!exe && !warmChild) void runHelper(0, 0, true)
|
|
316
|
+
})
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function dispose() {
|
|
320
|
+
if (disposed) return
|
|
321
|
+
const hadBadge = applied > 0 || desired > 0
|
|
322
|
+
desired = 0
|
|
323
|
+
clearTimer()
|
|
324
|
+
disposed = true
|
|
325
|
+
try {
|
|
326
|
+
if (child) child.kill()
|
|
327
|
+
} catch {
|
|
328
|
+
/* ignore */
|
|
329
|
+
}
|
|
330
|
+
try {
|
|
331
|
+
if (warmChild) warmChild.kill()
|
|
332
|
+
} catch {
|
|
333
|
+
/* ignore */
|
|
334
|
+
}
|
|
335
|
+
if (hadBadge && lastHandle) {
|
|
336
|
+
// Best-effort final clear. Do not wait: the Host may be tearing down.
|
|
337
|
+
void runHelper(lastHandle, 0)
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return {
|
|
342
|
+
setCount,
|
|
343
|
+
warm,
|
|
344
|
+
dispose,
|
|
345
|
+
pendingCount: () => desired,
|
|
346
|
+
}
|
|
347
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
param(
|
|
2
|
+
[long]$Hwnd = 0,
|
|
3
|
+
[int]$Count = 0,
|
|
4
|
+
[switch]$WarmOnly
|
|
5
|
+
)
|
|
6
|
+
|
|
7
|
+
$ErrorActionPreference = 'Stop'
|
|
8
|
+
$source = Join-Path $PSScriptRoot 'taskbar-badge-helper.cs'
|
|
9
|
+
if (-not (Test-Path $source)) { throw "taskbar badge helper source missing: $source" }
|
|
10
|
+
$sha = [Security.Cryptography.SHA256]::Create()
|
|
11
|
+
try {
|
|
12
|
+
$hashBytes = $sha.ComputeHash([IO.File]::ReadAllBytes($source))
|
|
13
|
+
} finally {
|
|
14
|
+
$sha.Dispose()
|
|
15
|
+
}
|
|
16
|
+
$hash = -join ($hashBytes | ForEach-Object { $_.ToString('x2') })
|
|
17
|
+
$cacheRoot = Join-Path $env:LOCALAPPDATA 'dsh-notify-toast\native-cache'
|
|
18
|
+
$dll = Join-Path $cacheRoot ($hash + '.badge.dll')
|
|
19
|
+
|
|
20
|
+
function New-BadgeDll {
|
|
21
|
+
New-Item -ItemType Directory -Path $cacheRoot -Force | Out-Null
|
|
22
|
+
$csc = Join-Path $env:WINDIR 'Microsoft.NET\Framework64\v4.0.30319\csc.exe'
|
|
23
|
+
if (-not (Test-Path $csc)) { throw "csc not found: $csc" }
|
|
24
|
+
$framework = Join-Path $env:WINDIR 'Microsoft.NET\Framework64\v4.0.30319'
|
|
25
|
+
$refs = @(
|
|
26
|
+
(Join-Path $framework 'System.dll'),
|
|
27
|
+
(Join-Path $framework 'System.Drawing.dll'),
|
|
28
|
+
(Join-Path $framework 'System.Runtime.dll')
|
|
29
|
+
)
|
|
30
|
+
foreach ($ref in $refs) { if (-not (Test-Path $ref)) { throw "reference missing: $ref" } }
|
|
31
|
+
$temp = $dll + '.tmp-' + [guid]::NewGuid().ToString('N')
|
|
32
|
+
$cscArgs = @('/nologo', '/target:library', ('/out:' + $temp))
|
|
33
|
+
foreach ($ref in $refs) { $cscArgs += ('/r:' + $ref) }
|
|
34
|
+
$cscArgs += $source
|
|
35
|
+
$cscOutput = & $csc @cscArgs 2>&1 | Out-String
|
|
36
|
+
if ($LASTEXITCODE -ne 0 -or -not (Test-Path $temp)) {
|
|
37
|
+
Remove-Item -Force -ErrorAction SilentlyContinue $temp
|
|
38
|
+
throw ('taskbar badge helper compilation failed: ' + $cscOutput)
|
|
39
|
+
}
|
|
40
|
+
Move-Item -Force -Path $temp -Destination $dll
|
|
41
|
+
return $dll
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (-not (Test-Path $dll)) { $dll = New-BadgeDll }
|
|
45
|
+
try {
|
|
46
|
+
Add-Type -Path $dll
|
|
47
|
+
} catch {
|
|
48
|
+
Remove-Item -Force -ErrorAction SilentlyContinue $dll
|
|
49
|
+
$dll = New-BadgeDll
|
|
50
|
+
Add-Type -Path $dll
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if ($WarmOnly) {
|
|
54
|
+
Write-Output 'warm'
|
|
55
|
+
exit 0
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
[DshTaskbarBadge]::Set([IntPtr]$Hwnd, [int]$Count)
|
|
59
|
+
Write-Output ('badge:' + $Count)
|
package/lib/text.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-notify-toast — pure text helpers for the Host half.
|
|
3
|
+
*
|
|
4
|
+
* These helpers turn model/tool output into the short, single-line bodies a
|
|
5
|
+
* Windows toast can show. They are deliberately side-effect free and exported
|
|
6
|
+
* for `node --test` (see test/text.test.mjs).
|
|
7
|
+
*
|
|
8
|
+
* Character accounting follows docs/INTERFACE.md: lengths are Unicode
|
|
9
|
+
* code points (`Array.from(text).length`), never UTF-16 code units.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/** Common named/numeric HTML entities produced by models that escape markup. */
|
|
13
|
+
const NAMED_ENTITIES = {
|
|
14
|
+
amp: '&',
|
|
15
|
+
lt: '<',
|
|
16
|
+
gt: '>',
|
|
17
|
+
quot: '"',
|
|
18
|
+
apos: "'",
|
|
19
|
+
nbsp: ' ',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Decode the small HTML entity subset that shows up in assistant text. */
|
|
23
|
+
function decodeEntities(text) {
|
|
24
|
+
return text
|
|
25
|
+
.replace(/&(#x?[0-9a-fA-F]+|[a-zA-Z]+);/g, (match, body) => {
|
|
26
|
+
if (body[0] === '#') {
|
|
27
|
+
const hex = body[1] === 'x' || body[1] === 'X'
|
|
28
|
+
const code = Number.parseInt(hex ? body.slice(2) : body.slice(1), hex ? 16 : 10)
|
|
29
|
+
if (Number.isFinite(code) && code >= 0 && code <= 0x10ffff) {
|
|
30
|
+
try {
|
|
31
|
+
return String.fromCodePoint(code)
|
|
32
|
+
} catch {
|
|
33
|
+
return match
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return match
|
|
37
|
+
}
|
|
38
|
+
return NAMED_ENTITIES[body.toLowerCase()] ?? match
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Remove Markdown/markup decoration and collapse whitespace into one line.
|
|
44
|
+
*
|
|
45
|
+
* - fenced code blocks are removed (both ``` and ~~~, closed or unterminated);
|
|
46
|
+
* - inline code keeps its content but loses the backticks;
|
|
47
|
+
* - headings, block quotes, list bullets, tables and horizontal rules lose
|
|
48
|
+
* their markers; links keep the label, images are dropped;
|
|
49
|
+
* - emphasis/strikethrough markers are removed; raw HTML tags are dropped and
|
|
50
|
+
* common entities decoded;
|
|
51
|
+
* - the result has every whitespace run collapsed to one space (no newlines).
|
|
52
|
+
*
|
|
53
|
+
* @param {unknown} text raw text.
|
|
54
|
+
* @returns {string} cleaned, single-line text ('', never null/undefined, for
|
|
55
|
+
* non-string or empty input).
|
|
56
|
+
*/
|
|
57
|
+
export function cleanMarkdownText(text) {
|
|
58
|
+
if (typeof text !== 'string' || text.length === 0) return ''
|
|
59
|
+
|
|
60
|
+
let out = text.replace(/\r\n?/g, '\n')
|
|
61
|
+
|
|
62
|
+
// Fenced code blocks first: their content must never become notification text.
|
|
63
|
+
out = out.replace(/```[^\n]*\n[\s\S]*?```/g, ' ')
|
|
64
|
+
out = out.replace(/~~~[^\n]*\n[\s\S]*?~~~/g, ' ')
|
|
65
|
+
// Unterminated fence: drop everything that follows it.
|
|
66
|
+
out = out.replace(/```[\s\S]*$/g, ' ')
|
|
67
|
+
out = out.replace(/~~~[\s\S]*$/g, ' ')
|
|
68
|
+
|
|
69
|
+
out = out.replace(/<!--[\s\S]*?-->/g, ' ')
|
|
70
|
+
// Images are noise for a toast; links keep their label. One level of
|
|
71
|
+
// parentheses is allowed inside the URL target (e.g. Wikipedia links).
|
|
72
|
+
out = out.replace(/!\[[^\]\n]*\]\((?:[^()\n]|\([^()\n]*\))*\)/g, ' ')
|
|
73
|
+
out = out.replace(/\[([^\]\n]+)\]\((?:[^()\n]|\([^()\n]*\))*\)/g, '$1')
|
|
74
|
+
// Inline code: keep the payload, drop the backticks.
|
|
75
|
+
out = out.replace(/`{1,2}([^`\n]+)`{1,2}/g, '$1')
|
|
76
|
+
out = out.replace(/`/g, '')
|
|
77
|
+
|
|
78
|
+
// Line-leading Markdown markers.
|
|
79
|
+
out = out.replace(/^[ \t]{0,3}#{1,6}[ \t]+/gm, '')
|
|
80
|
+
out = out.replace(/[ \t]+#{1,6}[ \t]*$/gm, '')
|
|
81
|
+
out = out.replace(/^[ \t]{0,3}>[ \t]?/gm, '')
|
|
82
|
+
out = out.replace(/^[ \t]{0,3}(?:(?:\*[ \t]*){3,}|(?:-[ \t]*){3,}|(?:_[ \t]*){3,})$/gm, '')
|
|
83
|
+
out = out.replace(/^[ \t]{0,3}(?:[-*+]|\d{1,9}[.)])[ \t]+/gm, '')
|
|
84
|
+
|
|
85
|
+
// Tables: separator rows disappear, remaining cell pipes become spaces.
|
|
86
|
+
out = out.replace(/^[ \t]*\|?[ \t]*:?-{2,}:?[ \t]*(?:\|[ \t]*:?-{2,}:?[ \t]*)+\|?[ \t]*$/gm, ' ')
|
|
87
|
+
out = out.replace(/\|/g, ' ')
|
|
88
|
+
|
|
89
|
+
// Markup tags (kept after link handling so `<https://...>` link wrappers die too).
|
|
90
|
+
out = out.replace(/<[^>]+>/g, ' ')
|
|
91
|
+
|
|
92
|
+
// Emphasis markers: paired forms first, then stray single markers that are
|
|
93
|
+
// adjacent to whitespace so identifiers such as `file_name` survive intact.
|
|
94
|
+
out = out.replace(/\*\*([^*\n]+)\*\*/g, '$1')
|
|
95
|
+
out = out.replace(/__([^_\n]+)__/g, '$1')
|
|
96
|
+
out = out.replace(/~~([^~\n]+)~~/g, '$1')
|
|
97
|
+
out = out.replace(/\*\*+/g, '')
|
|
98
|
+
out = out.replace(/~~/g, '')
|
|
99
|
+
out = out.replace(/(^|[\s(])\*(?=\S)/g, '$1')
|
|
100
|
+
out = out.replace(/(\S)\*(?=[\s).,!?:;]|$)/g, '$1')
|
|
101
|
+
out = out.replace(/(^|[\s(])_(?=\S)/g, '$1')
|
|
102
|
+
out = out.replace(/(\S)_(?=[\s).,!?:;]|$)/g, '$1')
|
|
103
|
+
|
|
104
|
+
out = decodeEntities(out)
|
|
105
|
+
return out.replace(/\s+/g, ' ').trim()
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Return the first non-empty paragraph of a Markdown-ish text, cleaned.
|
|
110
|
+
*
|
|
111
|
+
* Paragraph boundaries are the blank lines of the *raw* text, so fenced code
|
|
112
|
+
* or decorative headers that clean to nothing fall through to the next block.
|
|
113
|
+
*
|
|
114
|
+
* @param {unknown} text raw text.
|
|
115
|
+
* @returns {string|null} cleaned first paragraph, or null when nothing survives.
|
|
116
|
+
*/
|
|
117
|
+
export function firstParagraph(text) {
|
|
118
|
+
if (typeof text !== 'string' || text.trim() === '') return null
|
|
119
|
+
const normalized = text.replace(/\r\n?/g, '\n')
|
|
120
|
+
for (const block of normalized.split(/\n[ \t]*\n/)) {
|
|
121
|
+
const cleaned = cleanMarkdownText(block)
|
|
122
|
+
if (cleaned) return cleaned
|
|
123
|
+
}
|
|
124
|
+
const cleaned = cleanMarkdownText(normalized)
|
|
125
|
+
return cleaned || null
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Extract the first text paragraph from assistant-message content blocks.
|
|
130
|
+
*
|
|
131
|
+
* @param {unknown} contentBlocks `event.data.message.content`-shaped array.
|
|
132
|
+
* @returns {string|null} cleaned first paragraph, or null when the message has
|
|
133
|
+
* no non-empty `text` block.
|
|
134
|
+
*/
|
|
135
|
+
export function extractFirstTextParagraph(contentBlocks) {
|
|
136
|
+
if (!Array.isArray(contentBlocks)) return null
|
|
137
|
+
const parts = []
|
|
138
|
+
for (const block of contentBlocks) {
|
|
139
|
+
if (!block || block.type !== 'text' || typeof block.text !== 'string') continue
|
|
140
|
+
if (block.text.trim()) parts.push(block.text)
|
|
141
|
+
}
|
|
142
|
+
if (parts.length === 0) return null
|
|
143
|
+
return firstParagraph(parts.join('\n\n'))
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Collapse every whitespace run (including newlines) into one space and trim.
|
|
148
|
+
*
|
|
149
|
+
* @param {unknown} text raw text.
|
|
150
|
+
* @returns {string} single-line text; '' for non-string input.
|
|
151
|
+
*/
|
|
152
|
+
export function toSingleLine(text) {
|
|
153
|
+
if (typeof text !== 'string' || text.length === 0) return ''
|
|
154
|
+
return text.replace(/\s+/g, ' ').trim()
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Truncate to at most `n` Unicode code points, marking the cut with '…'.
|
|
159
|
+
*
|
|
160
|
+
* The ellipsis is counted in the budget: the result never has more than `n`
|
|
161
|
+
* code points. A limit of 0 returns ''; a limit of 1 returns '…'.
|
|
162
|
+
*
|
|
163
|
+
* @param {unknown} text raw text.
|
|
164
|
+
* @param {number} n maximum code points in the result.
|
|
165
|
+
* @returns {string} possibly truncated text.
|
|
166
|
+
*/
|
|
167
|
+
export function truncateCodepoints(text, n) {
|
|
168
|
+
if (typeof text !== 'string' || text.length === 0) return ''
|
|
169
|
+
const limit = typeof n === 'number' && Number.isFinite(n) ? Math.max(0, Math.floor(n)) : 0
|
|
170
|
+
const points = Array.from(text)
|
|
171
|
+
if (points.length <= limit) return text
|
|
172
|
+
if (limit <= 0) return ''
|
|
173
|
+
return points.slice(0, limit - 1).join('') + '…'
|
|
174
|
+
}
|
package/locale/en.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "Lightweight Native Notifications",
|
|
4
|
+
"description": "Lightweight native Windows notifications for DSH: background alerts for questions, approvals, and root-session completion, plus a taskbar unread badge. Silent while focused; no resident service."
|
|
5
|
+
},
|
|
6
|
+
"nav": "Notifications",
|
|
7
|
+
"notifyQuestionTitle": "DeepSeek has a question for you",
|
|
8
|
+
"notifyApprovalTitle": "DeepSeek needs your approval",
|
|
9
|
+
"notifyCompleteTitle": "DeepSeek finished a step",
|
|
10
|
+
"notifyCompleteMergedTitle": "DeepSeek pushed {count} steps in one go",
|
|
11
|
+
"notifyCompleteManyTitle": "DeepSeek went all out and pushed {count} steps",
|
|
12
|
+
"notifyAllDoneTitle": "All tasks are done",
|
|
13
|
+
"notifyTestTitle": "DSH notification test",
|
|
14
|
+
"notifyTestBody": "This is a test notification. Click to return to DSH.",
|
|
15
|
+
"fallbackBody": "Click to view",
|
|
16
|
+
"fallbackApprovalBody": "A tool call needs approval"
|
|
17
|
+
}
|
package/locale/zh.json
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"meta": {
|
|
3
|
+
"title": "轻量原生通知",
|
|
4
|
+
"description": "DSH 在后台时的轻量 Windows 原生通知:提醒提问、授权与根会话完成,并在任务栏显示未读根会话角标;聚焦时不打扰,不常驻后台。"
|
|
5
|
+
},
|
|
6
|
+
"nav": "通知",
|
|
7
|
+
"notifyQuestionTitle": "DeepSeek 想问你一个问题",
|
|
8
|
+
"notifyApprovalTitle": "DeepSeek 需要你的授权",
|
|
9
|
+
"notifyCompleteTitle": "DeepSeek 搞定了一小步",
|
|
10
|
+
"notifyCompleteMergedTitle": "DeepSeek 一口气推进了 {count} 步",
|
|
11
|
+
"notifyCompleteManyTitle": "DeepSeek 火力全开,连推了 {count} 步",
|
|
12
|
+
"notifyAllDoneTitle": "所有任务均已完成",
|
|
13
|
+
"notifyTestTitle": "DSH 通知测试",
|
|
14
|
+
"notifyTestBody": "这是一条测试通知,点击可回到 DSH。",
|
|
15
|
+
"fallbackBody": "点击查看",
|
|
16
|
+
"fallbackApprovalBody": "需要批准工具调用"
|
|
17
|
+
}
|