@tavlo-dsh/dsh-plugin-hooks 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +29 -2
- package/cordis.patch.yml +4 -0
- package/icon.svg +5 -0
- package/index.js +793 -0
- package/lib/client.js +5 -0
- package/lib/client.js.LEGAL.txt +4 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +72 -4
- package/src/config-store.js +804 -0
- package/src/default-scripts.js +193 -0
- package/src/dsh-hook-bridge.js +999 -0
- package/src/event-hub.js +198 -0
- package/src/hook-catalog.js +359 -0
- package/src/notification-dispatcher.js +486 -0
- package/src/script-runner.js +671 -0
- package/src/sound-manager.js +403 -0
- package/src/system-notifier.js +549 -0
- package/src/third-party-providers.js +1137 -0
|
@@ -0,0 +1,549 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-platform OS system notifications (WinRT/WSL/macOS/Linux) and Windows taskbar attention flashing.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { spawn as nodeSpawn } from 'node:child_process'
|
|
6
|
+
import { readFileSync } from 'node:fs'
|
|
7
|
+
|
|
8
|
+
const MAX_TITLE_CHARS = 200
|
|
9
|
+
const MAX_BODY_CHARS = 1000
|
|
10
|
+
const DEFAULT_FLASH_COUNT = 5
|
|
11
|
+
const MIN_FLASH_COUNT = 1
|
|
12
|
+
const MAX_FLASH_COUNT = 20
|
|
13
|
+
const DEFAULT_TIMEOUT_MS = 10_000
|
|
14
|
+
const MAX_STDIO_BYTES = 4096
|
|
15
|
+
const WSL_POWERSHELL_FALLBACK = '/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe'
|
|
16
|
+
|
|
17
|
+
const PS_TOAST_SCRIPT = `
|
|
18
|
+
$ErrorActionPreference = 'Stop'
|
|
19
|
+
$title = [string]$env:DSH_TOAST_TITLE
|
|
20
|
+
$body = [string]$env:DSH_TOAST_BODY
|
|
21
|
+
$silent = ($env:DSH_TOAST_SILENT -eq '1' -or $env:DSH_TOAST_SILENT -eq 'true')
|
|
22
|
+
if ([string]::IsNullOrWhiteSpace($title)) { exit 0 }
|
|
23
|
+
$escTitle = [System.Security.SecurityElement]::Escape($title)
|
|
24
|
+
$escBody = [System.Security.SecurityElement]::Escape($body)
|
|
25
|
+
$audio = if ($silent) { '<audio silent="true"/>' } else { '' }
|
|
26
|
+
$xmlText = "<toast><visual><binding template=\`"ToastGeneric\`"><text>$escTitle</text><text>$escBody</text></binding></visual>$audio</toast>"
|
|
27
|
+
try {
|
|
28
|
+
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
|
|
29
|
+
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
|
|
30
|
+
$doc = New-Object Windows.Data.Xml.Dom.XmlDocument
|
|
31
|
+
$doc.LoadXml($xmlText)
|
|
32
|
+
$toast = New-Object Windows.UI.Notifications.ToastNotification $doc
|
|
33
|
+
$appIds = @(
|
|
34
|
+
'{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe',
|
|
35
|
+
'DSH'
|
|
36
|
+
)
|
|
37
|
+
foreach ($aid in $appIds) {
|
|
38
|
+
try {
|
|
39
|
+
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($aid)
|
|
40
|
+
$notifier.Show($toast)
|
|
41
|
+
Write-Output 'WINRT_OK'
|
|
42
|
+
exit 0
|
|
43
|
+
} catch {}
|
|
44
|
+
}
|
|
45
|
+
} catch {}
|
|
46
|
+
try {
|
|
47
|
+
Add-Type -AssemblyName System.Windows.Forms
|
|
48
|
+
Add-Type -AssemblyName System.Drawing
|
|
49
|
+
$ni = New-Object System.Windows.Forms.NotifyIcon
|
|
50
|
+
$ni.Icon = [System.Drawing.SystemIcons]::Information
|
|
51
|
+
$ni.BalloonTipTitle = $title
|
|
52
|
+
$ni.BalloonTipText = if ([string]::IsNullOrEmpty($body)) { ' ' } else { $body }
|
|
53
|
+
$ni.Visible = $true
|
|
54
|
+
$ni.ShowBalloonTip(5000)
|
|
55
|
+
Start-Sleep -Milliseconds 250
|
|
56
|
+
$ni.Dispose()
|
|
57
|
+
Write-Output 'BALLOON_OK'
|
|
58
|
+
exit 0
|
|
59
|
+
} catch {
|
|
60
|
+
exit 0
|
|
61
|
+
}
|
|
62
|
+
`.trim()
|
|
63
|
+
|
|
64
|
+
const PS_FLASH_SCRIPT = `
|
|
65
|
+
$ErrorActionPreference = 'Stop'
|
|
66
|
+
$mode = [string]$env:DSH_FLASH_MODE
|
|
67
|
+
if ([string]::IsNullOrWhiteSpace($mode)) { $mode = 'finite' }
|
|
68
|
+
$countRaw = 0
|
|
69
|
+
[void][int]::TryParse([string]$env:DSH_FLASH_COUNT, [ref]$countRaw)
|
|
70
|
+
if ($countRaw -lt 1) { $countRaw = 5 }
|
|
71
|
+
if ($countRaw -gt 20) { $countRaw = 20 }
|
|
72
|
+
$targetPid = 0
|
|
73
|
+
[void][int]::TryParse([string]$env:DSH_TARGET_PID, [ref]$targetPid)
|
|
74
|
+
$hint = [string]$env:DSH_WINDOW_HINT
|
|
75
|
+
if ([string]::IsNullOrWhiteSpace($hint)) { $hint = 'DSH' }
|
|
76
|
+
|
|
77
|
+
$code = @'
|
|
78
|
+
using System;
|
|
79
|
+
using System.Diagnostics;
|
|
80
|
+
using System.Runtime.InteropServices;
|
|
81
|
+
using System.Text;
|
|
82
|
+
|
|
83
|
+
public static class DshTaskbarAttention {
|
|
84
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
85
|
+
public struct FLASHWINFO {
|
|
86
|
+
public uint cbSize;
|
|
87
|
+
public IntPtr hwnd;
|
|
88
|
+
public uint dwFlags;
|
|
89
|
+
public uint uCount;
|
|
90
|
+
public uint dwTimeout;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public const uint FLASHW_STOP = 0;
|
|
94
|
+
public const uint FLASHW_TRAY = 2;
|
|
95
|
+
public const uint FLASHW_TIMERNOFG = 12;
|
|
96
|
+
|
|
97
|
+
[DllImport("user32.dll")]
|
|
98
|
+
[return: MarshalAs(UnmanagedType.Bool)]
|
|
99
|
+
public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);
|
|
100
|
+
|
|
101
|
+
[DllImport("user32.dll")]
|
|
102
|
+
[return: MarshalAs(UnmanagedType.Bool)]
|
|
103
|
+
private static extern bool IsWindowVisible(IntPtr hWnd);
|
|
104
|
+
|
|
105
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
|
|
106
|
+
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
|
|
107
|
+
|
|
108
|
+
[DllImport("user32.dll")]
|
|
109
|
+
private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
|
|
110
|
+
|
|
111
|
+
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
|
|
112
|
+
|
|
113
|
+
[DllImport("user32.dll")]
|
|
114
|
+
[return: MarshalAs(UnmanagedType.Bool)]
|
|
115
|
+
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
|
116
|
+
|
|
117
|
+
public static IntPtr FindTargetWindow(int targetPid, string hint) {
|
|
118
|
+
if (targetPid > 0) {
|
|
119
|
+
try {
|
|
120
|
+
var proc = Process.GetProcessById(targetPid);
|
|
121
|
+
if (proc.MainWindowHandle != IntPtr.Zero && IsWindowVisible(proc.MainWindowHandle)) {
|
|
122
|
+
return proc.MainWindowHandle;
|
|
123
|
+
}
|
|
124
|
+
} catch {}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
IntPtr pidHwnd = IntPtr.Zero;
|
|
128
|
+
IntPtr hintHwnd = IntPtr.Zero;
|
|
129
|
+
string[] hints = string.IsNullOrWhiteSpace(hint)
|
|
130
|
+
? new[] { "DSH", "Tavlo", "DeepSeek" }
|
|
131
|
+
: new[] { hint, "DSH", "Tavlo", "DeepSeek" };
|
|
132
|
+
|
|
133
|
+
EnumWindows((hwnd, lParam) => {
|
|
134
|
+
if (!IsWindowVisible(hwnd)) return true;
|
|
135
|
+
if (targetPid > 0 && pidHwnd == IntPtr.Zero) {
|
|
136
|
+
uint winPid;
|
|
137
|
+
GetWindowThreadProcessId(hwnd, out winPid);
|
|
138
|
+
if (winPid == (uint)targetPid) {
|
|
139
|
+
pidHwnd = hwnd;
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (hintHwnd == IntPtr.Zero) {
|
|
144
|
+
var sb = new StringBuilder(512);
|
|
145
|
+
if (GetWindowText(hwnd, sb, sb.Capacity) > 0) {
|
|
146
|
+
string title = sb.ToString();
|
|
147
|
+
foreach (string h in hints) {
|
|
148
|
+
if (!string.IsNullOrEmpty(h) && title.IndexOf(h, StringComparison.OrdinalIgnoreCase) >= 0) {
|
|
149
|
+
hintHwnd = hwnd;
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
return true;
|
|
156
|
+
}, IntPtr.Zero);
|
|
157
|
+
|
|
158
|
+
return pidHwnd != IntPtr.Zero ? pidHwnd : hintHwnd;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
public static string Apply(int targetPid, string hint, string mode, uint count) {
|
|
162
|
+
IntPtr hwnd = FindTargetWindow(targetPid, hint);
|
|
163
|
+
if (hwnd == IntPtr.Zero) return "NO_HWND";
|
|
164
|
+
var info = new FLASHWINFO {
|
|
165
|
+
cbSize = (uint)Marshal.SizeOf(typeof(FLASHWINFO)),
|
|
166
|
+
hwnd = hwnd,
|
|
167
|
+
dwTimeout = 0
|
|
168
|
+
};
|
|
169
|
+
if (mode == "stop") {
|
|
170
|
+
info.dwFlags = FLASHW_STOP;
|
|
171
|
+
info.uCount = 0;
|
|
172
|
+
} else if (mode == "untilFocused") {
|
|
173
|
+
info.dwFlags = FLASHW_TRAY | FLASHW_TIMERNOFG;
|
|
174
|
+
info.uCount = uint.MaxValue;
|
|
175
|
+
} else {
|
|
176
|
+
info.dwFlags = FLASHW_TRAY;
|
|
177
|
+
info.uCount = count;
|
|
178
|
+
}
|
|
179
|
+
FlashWindowEx(ref info);
|
|
180
|
+
return "FLASHED";
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
'@
|
|
184
|
+
if (-not ('DshTaskbarAttention' -as [type])) {
|
|
185
|
+
Add-Type -TypeDefinition $code -Language CSharp
|
|
186
|
+
}
|
|
187
|
+
Write-Output ([DshTaskbarAttention]::Apply($targetPid, $hint, $mode, [uint32]$countRaw))
|
|
188
|
+
`.trim()
|
|
189
|
+
|
|
190
|
+
let cachedScrubbedParentEnv = null
|
|
191
|
+
|
|
192
|
+
async function getScrubbedEnv(baseEnv = process.env) {
|
|
193
|
+
if (baseEnv !== process.env) {
|
|
194
|
+
const env = {}
|
|
195
|
+
for (const [key, value] of Object.entries(baseEnv || {})) {
|
|
196
|
+
if (
|
|
197
|
+
value !== undefined &&
|
|
198
|
+
!/KEY|PASSWORD|SECRET|TOKEN/i.test(key) &&
|
|
199
|
+
!key.toUpperCase().startsWith('DSH_')
|
|
200
|
+
) {
|
|
201
|
+
env[key] = value
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return env
|
|
205
|
+
}
|
|
206
|
+
if (!cachedScrubbedParentEnv) {
|
|
207
|
+
try {
|
|
208
|
+
const mod = await import('@deepseek-ai/dsh-subprocess')
|
|
209
|
+
if (typeof mod?.scrubbedParentEnv === 'function') {
|
|
210
|
+
cachedScrubbedParentEnv = mod.scrubbedParentEnv
|
|
211
|
+
}
|
|
212
|
+
} catch {
|
|
213
|
+
// Fallback when dependencies are not installed yet.
|
|
214
|
+
}
|
|
215
|
+
if (!cachedScrubbedParentEnv) {
|
|
216
|
+
cachedScrubbedParentEnv = () => {
|
|
217
|
+
const env = {}
|
|
218
|
+
for (const [key, value] of Object.entries(process.env)) {
|
|
219
|
+
if (
|
|
220
|
+
value !== undefined &&
|
|
221
|
+
!/KEY|PASSWORD|SECRET|TOKEN/i.test(key) &&
|
|
222
|
+
!key.toUpperCase().startsWith('DSH_')
|
|
223
|
+
) {
|
|
224
|
+
env[key] = value
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return env
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return cachedScrubbedParentEnv()
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function stripAndTruncate(raw, maxCodepoints) {
|
|
235
|
+
const clean = (raw == null ? '' : String(raw))
|
|
236
|
+
.replace(/\r\n?/g, '\n')
|
|
237
|
+
.replace(/[\x00-\x08\x0b-\x1f\x7f]/g, '')
|
|
238
|
+
const codepoints = Array.from(clean)
|
|
239
|
+
if (codepoints.length <= maxCodepoints) return clean
|
|
240
|
+
return codepoints.slice(0, maxCodepoints).join('')
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Detect whether the current Linux environment is running inside WSL.
|
|
245
|
+
* @param {{ platform?: string, env?: NodeJS.ProcessEnv, procVersion?: string }} [opts]
|
|
246
|
+
* @returns {boolean}
|
|
247
|
+
*/
|
|
248
|
+
export function isWsl({ platform = process.platform, env = process.env, procVersion } = {}) {
|
|
249
|
+
if (platform !== 'linux') return false
|
|
250
|
+
if (env?.WSL_DISTRO_NAME || env?.WSL_INTEROP) return true
|
|
251
|
+
if (typeof procVersion === 'string') {
|
|
252
|
+
return /microsoft/i.test(procVersion)
|
|
253
|
+
}
|
|
254
|
+
try {
|
|
255
|
+
const content = readFileSync('/proc/version', 'utf8')
|
|
256
|
+
return /microsoft/i.test(content)
|
|
257
|
+
} catch {
|
|
258
|
+
return false
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Escape XML special characters and strip NUL / invalid control characters.
|
|
264
|
+
* @param {string} str
|
|
265
|
+
* @returns {string}
|
|
266
|
+
*/
|
|
267
|
+
export function escapeXml(str) {
|
|
268
|
+
const clean = (str == null ? '' : String(str))
|
|
269
|
+
.replace(/\r\n?/g, '\n')
|
|
270
|
+
.replace(/[\x00-\x08\x0b-\x1f\x7f]/g, '')
|
|
271
|
+
return clean
|
|
272
|
+
.replace(/&/g, '&')
|
|
273
|
+
.replace(/</g, '<')
|
|
274
|
+
.replace(/>/g, '>')
|
|
275
|
+
.replace(/"/g, '"')
|
|
276
|
+
.replace(/'/g, ''')
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Build an XML-escaped ToastGeneric payload for Windows notifications.
|
|
281
|
+
* @param {{ title?: string, body?: string, silent?: boolean }} params
|
|
282
|
+
* @returns {string}
|
|
283
|
+
*/
|
|
284
|
+
export function buildWindowsToastCommandPayload({ title = '', body = '', silent = false } = {}) {
|
|
285
|
+
const safeTitle = stripAndTruncate(title, MAX_TITLE_CHARS)
|
|
286
|
+
const safeBody = stripAndTruncate(body, MAX_BODY_CHARS)
|
|
287
|
+
const audioNode = silent ? '<audio silent="true"/>' : ''
|
|
288
|
+
return `<toast><visual><binding template="ToastGeneric"><text>${escapeXml(safeTitle)}</text><text>${escapeXml(safeBody)}</text></binding></visual>${audioNode}</toast>`
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
function runSpawn(spawnImpl, cmd, args, options, timeoutMs = DEFAULT_TIMEOUT_MS) {
|
|
292
|
+
return new Promise((resolve) => {
|
|
293
|
+
let settled = false
|
|
294
|
+
let exitFallbackTimer = null
|
|
295
|
+
const stdoutChunks = []
|
|
296
|
+
const stderrChunks = []
|
|
297
|
+
let stdoutLen = 0
|
|
298
|
+
let stderrLen = 0
|
|
299
|
+
|
|
300
|
+
let child
|
|
301
|
+
try {
|
|
302
|
+
child = spawnImpl(cmd, args, options)
|
|
303
|
+
} catch (error) {
|
|
304
|
+
resolve({ ok: false, stdout: '', stderr: '', error: error?.message || 'spawn failed', spawnError: true })
|
|
305
|
+
return
|
|
306
|
+
}
|
|
307
|
+
if (!child || typeof child.once !== 'function') {
|
|
308
|
+
resolve({ ok: false, stdout: '', stderr: '', error: 'Invalid child process handle', spawnError: true })
|
|
309
|
+
return
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const timer = setTimeout(() => {
|
|
313
|
+
try {
|
|
314
|
+
child.kill?.('SIGKILL')
|
|
315
|
+
} catch {
|
|
316
|
+
try {
|
|
317
|
+
child.kill?.()
|
|
318
|
+
} catch {
|
|
319
|
+
// Ignore kill errors
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
finish({ ok: false, stdout: '', stderr: '', error: 'timeout' })
|
|
323
|
+
}, timeoutMs)
|
|
324
|
+
timer.unref?.()
|
|
325
|
+
|
|
326
|
+
function collect(stream, chunks, getLen, setLen) {
|
|
327
|
+
if (!stream || typeof stream.on !== 'function') return
|
|
328
|
+
stream.on('data', (chunk) => {
|
|
329
|
+
const len = getLen()
|
|
330
|
+
if (len >= MAX_STDIO_BYTES) return
|
|
331
|
+
const buf = Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk))
|
|
332
|
+
const slice = buf.subarray(0, MAX_STDIO_BYTES - len)
|
|
333
|
+
chunks.push(slice)
|
|
334
|
+
setLen(len + slice.length)
|
|
335
|
+
})
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
collect(child.stdout, stdoutChunks, () => stdoutLen, (n) => { stdoutLen = n })
|
|
339
|
+
collect(child.stderr, stderrChunks, () => stderrLen, (n) => { stderrLen = n })
|
|
340
|
+
|
|
341
|
+
function finish(result) {
|
|
342
|
+
if (settled) return
|
|
343
|
+
settled = true
|
|
344
|
+
clearTimeout(timer)
|
|
345
|
+
if (exitFallbackTimer) clearTimeout(exitFallbackTimer)
|
|
346
|
+
resolve(result)
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
child.once('error', (error) => {
|
|
350
|
+
finish({
|
|
351
|
+
ok: false,
|
|
352
|
+
stdout: Buffer.concat(stdoutChunks).toString('utf8').trim(),
|
|
353
|
+
stderr: Buffer.concat(stderrChunks).toString('utf8').trim(),
|
|
354
|
+
error: error?.message || 'process error',
|
|
355
|
+
spawnError: error?.code === 'ENOENT',
|
|
356
|
+
})
|
|
357
|
+
})
|
|
358
|
+
|
|
359
|
+
const complete = (code, signal) => {
|
|
360
|
+
const stdout = Buffer.concat(stdoutChunks).toString('utf8').trim()
|
|
361
|
+
const stderr = Buffer.concat(stderrChunks).toString('utf8').replace(/\s+/g, ' ').trim()
|
|
362
|
+
if (code === 0) {
|
|
363
|
+
finish({ ok: true, stdout, stderr })
|
|
364
|
+
} else {
|
|
365
|
+
finish({
|
|
366
|
+
ok: false,
|
|
367
|
+
stdout,
|
|
368
|
+
stderr,
|
|
369
|
+
error: stderr || (signal ? `signal ${signal}` : `exit code ${code}`),
|
|
370
|
+
})
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
child.once('close', complete)
|
|
375
|
+
child.once('exit', (code, signal) => {
|
|
376
|
+
if (settled) return
|
|
377
|
+
exitFallbackTimer = setTimeout(() => complete(code, signal), 25)
|
|
378
|
+
exitFallbackTimer.unref?.()
|
|
379
|
+
})
|
|
380
|
+
})
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Send a native OS notification across Windows, WSL, macOS, and Linux.
|
|
385
|
+
* Never throws; returns structured delivery metadata.
|
|
386
|
+
*/
|
|
387
|
+
export async function sendSystemNotification({
|
|
388
|
+
title,
|
|
389
|
+
body,
|
|
390
|
+
urgency = 'normal',
|
|
391
|
+
silent = false,
|
|
392
|
+
platform = process.platform,
|
|
393
|
+
env = process.env,
|
|
394
|
+
spawnImpl = nodeSpawn,
|
|
395
|
+
} = {}) {
|
|
396
|
+
const safeTitle = stripAndTruncate(title, MAX_TITLE_CHARS).trim()
|
|
397
|
+
if (!safeTitle) {
|
|
398
|
+
return { ok: false, channel: 'none', error: 'Notification title is required.' }
|
|
399
|
+
}
|
|
400
|
+
const safeBody = stripAndTruncate(body ?? '', MAX_BODY_CHARS)
|
|
401
|
+
const wsl = isWsl({ platform, env })
|
|
402
|
+
const scrubbedEnv = await getScrubbedEnv(env)
|
|
403
|
+
|
|
404
|
+
if (platform === 'win32' || wsl) {
|
|
405
|
+
const channel = wsl ? 'wsl' : 'winrt'
|
|
406
|
+
const psArgs = ['-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-Command', PS_TOAST_SCRIPT]
|
|
407
|
+
const psOpts = {
|
|
408
|
+
env: {
|
|
409
|
+
...scrubbedEnv,
|
|
410
|
+
DSH_TOAST_TITLE: safeTitle,
|
|
411
|
+
DSH_TOAST_BODY: safeBody,
|
|
412
|
+
DSH_TOAST_SILENT: silent ? '1' : '0',
|
|
413
|
+
},
|
|
414
|
+
windowsHide: true,
|
|
415
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
416
|
+
}
|
|
417
|
+
let res = await runSpawn(spawnImpl, 'powershell.exe', psArgs, psOpts)
|
|
418
|
+
if (!res.ok && wsl && res.spawnError) {
|
|
419
|
+
res = await runSpawn(spawnImpl, WSL_POWERSHELL_FALLBACK, psArgs, psOpts)
|
|
420
|
+
}
|
|
421
|
+
return res.ok ? { ok: true, channel } : { ok: false, channel, error: res.error || 'Toast failed' }
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (platform === 'darwin') {
|
|
425
|
+
const res = await runSpawn(
|
|
426
|
+
spawnImpl,
|
|
427
|
+
'osascript',
|
|
428
|
+
['-e', 'on run argv\ndisplay notification (item 2 of argv) with title (item 1 of argv)\nend run', safeTitle, safeBody],
|
|
429
|
+
{
|
|
430
|
+
env: scrubbedEnv,
|
|
431
|
+
windowsHide: true,
|
|
432
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
433
|
+
},
|
|
434
|
+
)
|
|
435
|
+
return res.ok
|
|
436
|
+
? { ok: true, channel: 'osascript' }
|
|
437
|
+
: { ok: false, channel: 'osascript', error: res.error || 'osascript failed' }
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
if (platform === 'linux') {
|
|
441
|
+
const level = urgency === 'critical' ? 'critical' : 'normal'
|
|
442
|
+
const res = await runSpawn(
|
|
443
|
+
spawnImpl,
|
|
444
|
+
'notify-send',
|
|
445
|
+
['--app-name=DSH', `--urgency=${level}`, '--', safeTitle, safeBody],
|
|
446
|
+
{
|
|
447
|
+
env: scrubbedEnv,
|
|
448
|
+
windowsHide: true,
|
|
449
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
450
|
+
},
|
|
451
|
+
)
|
|
452
|
+
return res.ok
|
|
453
|
+
? { ok: true, channel: 'notify-send' }
|
|
454
|
+
: { ok: false, channel: 'notify-send', error: res.error || 'notify-send failed' }
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
return { ok: false, channel: 'none', error: `Unsupported platform: ${platform}` }
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async function runWindowsTaskbarFlash({
|
|
461
|
+
mode,
|
|
462
|
+
flashCount,
|
|
463
|
+
targetPid,
|
|
464
|
+
windowTitleHint,
|
|
465
|
+
platform,
|
|
466
|
+
spawnImpl,
|
|
467
|
+
}) {
|
|
468
|
+
if (platform !== 'win32') {
|
|
469
|
+
return { ok: true, skipped: true }
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
const parsedCount = Number(flashCount)
|
|
473
|
+
const clampedCount = Number.isFinite(parsedCount)
|
|
474
|
+
? Math.min(MAX_FLASH_COUNT, Math.max(MIN_FLASH_COUNT, Math.round(parsedCount)))
|
|
475
|
+
: DEFAULT_FLASH_COUNT
|
|
476
|
+
|
|
477
|
+
const normalizedMode =
|
|
478
|
+
mode === 'stop' ? 'stop' : mode === 'untilFocused' ? 'untilFocused' : 'finite'
|
|
479
|
+
|
|
480
|
+
const parsedPid = Number(targetPid)
|
|
481
|
+
const safePid = Number.isInteger(parsedPid) && parsedPid > 0 ? parsedPid : 0
|
|
482
|
+
const safeHint = stripAndTruncate(windowTitleHint || 'DSH', 120).trim() || 'DSH'
|
|
483
|
+
const scrubbedEnv = await getScrubbedEnv()
|
|
484
|
+
|
|
485
|
+
const res = await runSpawn(
|
|
486
|
+
spawnImpl,
|
|
487
|
+
'powershell.exe',
|
|
488
|
+
['-NoProfile', '-NonInteractive', '-WindowStyle', 'Hidden', '-Command', PS_FLASH_SCRIPT],
|
|
489
|
+
{
|
|
490
|
+
env: {
|
|
491
|
+
...scrubbedEnv,
|
|
492
|
+
DSH_FLASH_MODE: normalizedMode,
|
|
493
|
+
DSH_FLASH_COUNT: normalizedMode === 'stop' ? '0' : String(clampedCount),
|
|
494
|
+
DSH_TARGET_PID: String(safePid),
|
|
495
|
+
DSH_WINDOW_HINT: safeHint,
|
|
496
|
+
},
|
|
497
|
+
windowsHide: true,
|
|
498
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
499
|
+
},
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
if (!res.ok) {
|
|
503
|
+
return { ok: false, error: res.error || 'FlashWindowEx failed' }
|
|
504
|
+
}
|
|
505
|
+
if (res.stdout === 'NO_HWND') {
|
|
506
|
+
return { ok: true, skipped: true }
|
|
507
|
+
}
|
|
508
|
+
return { ok: true }
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/**
|
|
512
|
+
* Flash the Windows taskbar icon when DSH is unfocused.
|
|
513
|
+
*/
|
|
514
|
+
export async function flashTaskbar({
|
|
515
|
+
mode = 'finite',
|
|
516
|
+
flashCount = DEFAULT_FLASH_COUNT,
|
|
517
|
+
targetPid = process.ppid,
|
|
518
|
+
windowTitleHint = 'DSH',
|
|
519
|
+
platform = process.platform,
|
|
520
|
+
spawnImpl = nodeSpawn,
|
|
521
|
+
} = {}) {
|
|
522
|
+
return runWindowsTaskbarFlash({
|
|
523
|
+
mode: mode === 'untilFocused' ? 'untilFocused' : 'finite',
|
|
524
|
+
flashCount,
|
|
525
|
+
targetPid,
|
|
526
|
+
windowTitleHint,
|
|
527
|
+
platform,
|
|
528
|
+
spawnImpl,
|
|
529
|
+
})
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Stop any active Windows taskbar attention flashing (`FLASHW_STOP`).
|
|
534
|
+
*/
|
|
535
|
+
export async function clearTaskbarAttention({
|
|
536
|
+
targetPid = process.ppid,
|
|
537
|
+
windowTitleHint = 'DSH',
|
|
538
|
+
platform = process.platform,
|
|
539
|
+
spawnImpl = nodeSpawn,
|
|
540
|
+
} = {}) {
|
|
541
|
+
return runWindowsTaskbarFlash({
|
|
542
|
+
mode: 'stop',
|
|
543
|
+
flashCount: 0,
|
|
544
|
+
targetPid,
|
|
545
|
+
windowTitleHint,
|
|
546
|
+
platform,
|
|
547
|
+
spawnImpl,
|
|
548
|
+
})
|
|
549
|
+
}
|