dsh-notify-say 0.4.79 → 0.4.81

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.
@@ -0,0 +1,128 @@
1
+ <#
2
+ .SYNOPSIS
3
+ DSH 通知插件 —— 自定义协议处理器(Toast 精准跳转)。
4
+
5
+ .DESCRIPTION
6
+ 当用户点击 Toast 时,Windows 会调起本脚本并传入完整 URL:
7
+
8
+ dshnotify://jump?session=session-abc12345
9
+
10
+ 本脚本做两件事:
11
+ 1. 把 sessionId 写到插件的 /pending-jump 端点(前端据此精确跳转)
12
+ 2. 调 dsh://open 把 DSH 窗口拉到前台(复用官方唤窗,不自己操作窗口)
13
+
14
+ 为什么需要它:点 Toast 走 dsh://open 时 DSH 本体是精确匹配(只认
15
+ "dsh://open" 和 "dsh://open/"),无法携带参数;自有协议才能精确知道
16
+ 「用户点的是哪个会话」。多会话同时有通知时,这点是决定性的。
17
+
18
+ 安全:只接受 dshnotify://jump?session=<id> 形态,session 必须匹配
19
+ 字母数字/下划线/连字符(4-128 位);不合法就静默退出,不执行其它内容。
20
+
21
+ 注意:本文件必须带 UTF-8 BOM —— 否则 PowerShell 5.1 会把中文读成
22
+ 乱码然后语法报错(实测踩到)。
23
+ #>
24
+ param(
25
+ [Parameter(Mandatory = $false)]
26
+ [string]$Url = ''
27
+ )
28
+
29
+ $ErrorActionPreference = 'SilentlyContinue'
30
+
31
+ # ── 1. 解析并校验 URL ─────────────────────────────────────────────────────
32
+ if ([string]::IsNullOrWhiteSpace($Url)) { exit 0 }
33
+
34
+ $m = [regex]::Match($Url, '^dshnotify://jump/?(?:\?(.*))?$', 'IgnoreCase')
35
+ if (-not $m.Success) { exit 0 }
36
+
37
+ $query = $m.Groups[1].Value
38
+ $sid = ''
39
+ $sm = [regex]::Match($query, '(?:^|&)session=([^&]+)')
40
+ if ($sm.Success) {
41
+ $sid = [System.Uri]::UnescapeDataString($sm.Groups[1].Value)
42
+ }
43
+
44
+ # 会话 id 白名单:DSH 的是 session-<uuid>;也容许纯 uuid / 字母数字
45
+ if ($sid -and ($sid -notmatch '^[A-Za-z0-9_-]{4,128}$')) { $sid = '' }
46
+
47
+ # ── 2. 找插件端口 ─────────────────────────────────────────────────────────
48
+ # 优先级:环境变量 > %LOCALAPPDATA% 下的 port.txt > 逐个探测常见端口
49
+ $port = 0
50
+ if ($env:DSH_NOTIFY_PORT -match '^\d+$') { $port = [int]$env:DSH_NOTIFY_PORT }
51
+
52
+ if ($port -le 0) {
53
+ $cfgPath = Join-Path $env:LOCALAPPDATA 'dsh-notify-say\port.txt'
54
+ if (Test-Path -LiteralPath $cfgPath) {
55
+ $t = (Get-Content -LiteralPath $cfgPath -Raw).Trim()
56
+ if ($t -match '^\d+$') { $port = [int]$t }
57
+ }
58
+ }
59
+
60
+ # ── 3. 写 pending-jump(精确的会话 id)───────────────────────────────────
61
+ if ($sid) {
62
+ $bodyObj = @{
63
+ sessionId = $sid
64
+ at = [DateTimeOffset]::UtcNow.ToUnixTimeMilliseconds()
65
+ } | ConvertTo-Json -Compress
66
+
67
+ # 候选端口来源(按可靠性排序):
68
+ # ① port.txt —— 插件在注册路由时写入,**唯一可靠来源**
69
+ # ② 扫描本机正在监听的端口,挑能响应我们路由的那个
70
+ # ③ 几个历史见过的端口(兜底)
71
+ $candidates = @()
72
+ if ($port -gt 0) { $candidates += $port }
73
+
74
+ # ② 动态发现:找本机正在监听、且能响应 /version 的端口。
75
+ # 2026-10-20 踩坑:DSH 每次启动端口都变,写死候选必然漏。
76
+ try {
77
+ $listening = @()
78
+ $tcp = Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue |
79
+ Where-Object { $_.LocalAddress -in @('127.0.0.1', '0.0.0.0', '::1', '::') } |
80
+ Select-Object -ExpandProperty LocalPort -Unique
81
+ foreach ($p in $tcp) {
82
+ if ($candidates -contains $p) { continue }
83
+ if ($p -lt 1024 -or $p -gt 65535) { continue }
84
+ $listening += $p
85
+ }
86
+ # 只保留"像 DSH"的(能响应我们的 version 端点)
87
+ foreach ($p in $listening) {
88
+ try {
89
+ $v = Invoke-RestMethod -Uri ("http://127.0.0.1:" + $p + "/api/dsh-notify-say/version") -TimeoutSec 1
90
+ if ($v -and $v.ok) { $candidates += $p }
91
+ } catch { continue }
92
+ }
93
+ } catch { }
94
+
95
+ # ③ 历史端口兜底
96
+ foreach ($p in @(59525, 53523, 50061, 19387)) {
97
+ if ($candidates -notcontains $p) { $candidates += $p }
98
+ }
99
+
100
+ foreach ($p in $candidates) {
101
+ try {
102
+ $null = Invoke-RestMethod -Uri ("http://127.0.0.1:" + $p + "/api/dsh-notify-say/pending-jump") `
103
+ -Method Post -ContentType 'application/json' -Body $bodyObj -TimeoutSec 2 `
104
+ -Headers @{ 'Origin' = ("http://127.0.0.1:" + $p) }
105
+ break
106
+ } catch { continue }
107
+ }
108
+ }
109
+
110
+ # ── 4. 拉起 DSH 窗口(复用官方协议,不自己操作窗口)──────────────────────
111
+ #
112
+ # ⚠️ 2026-10-20:`Start-Process 'dsh://open'` 会**弹一个命令行窗口** ——
113
+ # 它走 ShellExecute,控制台子进程可见。用户实测「点 Toast 后拉起一个
114
+ # 命令行窗口」,就是这行造成的。
115
+ #
116
+ # 修法:
117
+ # · 优先用 explorer.exe 转发 shell 协议(最干净,不产生可见控制台)
118
+ # · 兜底才直接 Start-Process,并加 -WindowStyle Hidden
119
+ try {
120
+ $explorer = Join-Path $env:SystemRoot 'explorer.exe'
121
+ if (Test-Path -LiteralPath $explorer) {
122
+ Start-Process -FilePath $explorer -ArgumentList 'dsh://open' -WindowStyle Hidden | Out-Null
123
+ } else {
124
+ Start-Process 'dsh://open' -WindowStyle Hidden | Out-Null
125
+ }
126
+ } catch {
127
+ try { Start-Process 'dsh://open' -WindowStyle Hidden | Out-Null } catch { }
128
+ }
@@ -0,0 +1,177 @@
1
+ ' DSH 通知插件 —— 协议处理器包装(v0.4.78)
2
+ '
3
+ ' ## 为什么用 VBS 而不是直接调 powershell.exe
4
+ '
5
+ ' 1. **不弹命令行窗口**
6
+ ' wscript.exe 是 GUI 子系统程序,本身不创建控制台。
7
+ ' 而 `powershell.exe -WindowStyle Hidden` 在协议调起场景下**仍会闪窗/
8
+ ' 留下可见控制台**(Windows 已知行为:-WindowStyle 只影响 PS 自己的
9
+ ' 宿主窗口,不改父进程传给子进程的 console)。
10
+ ' 2026-10-20 用户实测多次报告"点 Toast 打开一个命令行窗口"。
11
+ '
12
+ ' 2. **启动更快**
13
+ ' 不走 PowerShell 冷启动(约 300–800ms),VBS 几毫秒。
14
+ '
15
+ ' 3. **参数传递可靠**
16
+ ' 协议调起时 Windows 传给 "%1" 的是完整 URL;VBS 的 WScript.Arguments
17
+ ' 能原样拿到,不需要经过 powershell 的参数解析(那层会吃掉引号/& 之类)。
18
+ '
19
+ ' ## 它做什么
20
+ '
21
+ ' ① 解析 dshnotify://jump?session=<id>
22
+ ' ② 找插件端口(port.txt,或扫描能响应 /version 的端口)
23
+ ' ③ POST /api/dsh-notify-say/pending-jump(带 Origin 头,过 http-guard)
24
+ ' ④ 调 dsh://open 把 DSH 唤到前台(用 Run 的第二参数 0 = 隐藏窗口)
25
+ '
26
+ ' ## 安全
27
+ '
28
+ ' 只接受 dshnotify://jump?session=<白名单字符>,其它一律静默退出。
29
+
30
+ Option Explicit
31
+
32
+ Dim url, sid, port, candidates, i, ok, logPath, fso
33
+
34
+ Set fso = CreateObject("Scripting.FileSystemObject")
35
+
36
+ ' ── 诊断日志(放在 %TEMP%,与插件主日志同目录)──────────────────────────
37
+ Sub LogLine(msg)
38
+ On Error Resume Next
39
+ Dim ts, dir, f
40
+ dir = fso.GetSpecialFolder(2).Path & "\dsh-notify-say"
41
+ If Not fso.FolderExists(dir) Then fso.CreateFolder(dir)
42
+ logPath = dir & "\jump-handler.log"
43
+ Set ts = fso.OpenTextFile(logPath, 8, True)
44
+ ts.WriteLine Now & " " & msg
45
+ ts.Close
46
+ On Error GoTo 0
47
+ End Sub
48
+
49
+ ' ── 1. 取参数 ─────────────────────────────────────────────────────────────
50
+ If WScript.Arguments.Count = 0 Then
51
+ LogLine "[skip] no args"
52
+ WScript.Quit 0
53
+ End If
54
+
55
+ url = WScript.Arguments(0)
56
+ LogLine "[recv] """ & url & """"
57
+
58
+ ' ── 2. 解析 session ──────────────────────────────────────────────────────
59
+ sid = ""
60
+ Dim m
61
+ Set m = New RegExp
62
+ m.IgnoreCase = True
63
+ m.Pattern = "^dshnotify://jump/?(\?(.*))?$"
64
+ If Not m.Test(url) Then
65
+ LogLine "[skip] url not matched"
66
+ WScript.Quit 0
67
+ End If
68
+
69
+ Dim query
70
+ query = ""
71
+ Set m = New RegExp
72
+ m.IgnoreCase = True
73
+ m.Pattern = "\?(.*)$"
74
+ If m.Test(url) Then query = m.Execute(url)(0).SubMatches(0)
75
+
76
+ Set m = New RegExp
77
+ m.IgnoreCase = True
78
+ m.Pattern = "(^|&)session=([^&]+)"
79
+ If m.Test(query) Then
80
+ sid = m.Execute(query)(0).SubMatches(1)
81
+ End If
82
+
83
+ ' 白名单:只接受字母数字/下划线/连字符(4-128 位)
84
+ If sid <> "" Then
85
+ Set m = New RegExp
86
+ m.Pattern = "^[A-Za-z0-9_-]{4,128}$"
87
+ If Not m.Test(sid) Then
88
+ LogLine "[skip] bad session id: """ & sid & """"
89
+ sid = ""
90
+ End If
91
+ End If
92
+
93
+ LogLine "[parse] sid=" & sid
94
+ If sid = "" Then WScript.Quit 0
95
+
96
+ ' ── 3. 找端口 ────────────────────────────────────────────────────────────
97
+ ' 优先 port.txt(插件在注册路由时写入)
98
+ port = 0
99
+ On Error Resume Next
100
+ Dim ts2, txt
101
+ Dim shEnv, localAppData, p1
102
+ Set shEnv = CreateObject("WScript.Shell")
103
+ localAppData = shEnv.ExpandEnvironmentStrings("%LOCALAPPDATA%")
104
+ If localAppData = "" Or InStr(localAppData, "%") > 0 Then
105
+ ' 兜底:TEMP 的上一级就是 LOCALAPPDATA
106
+ localAppData = fso.GetSpecialFolder(2).ParentFolder.Path
107
+ End If
108
+ p1 = localAppData & "\dsh-notify-say\port.txt"
109
+ LogLine "[port] looking at " & p1
110
+ If fso.FileExists(p1) Then
111
+ Set ts2 = fso.OpenTextFile(p1, 1)
112
+ txt = Trim(ts2.ReadAll)
113
+ ts2.Close
114
+ If IsNumeric(txt) Then port = CLng(txt)
115
+ LogLine "[port] from port.txt=" & port
116
+ Else
117
+ LogLine "[port] port.txt not found"
118
+ End If
119
+ On Error GoTo 0
120
+
121
+ ' 候选端口:port.txt 优先,再补常见/历史端口
122
+ candidates = ""
123
+ If port > 0 Then candidates = CStr(port)
124
+ Dim extra
125
+ extra = Array("55510", "58163", "59525", "53523", "50061", "19387")
126
+ For i = 0 To UBound(extra)
127
+ If InStr("," & candidates & ",", "," & extra(i) & ",") = 0 Then
128
+ If candidates = "" Then
129
+ candidates = extra(i)
130
+ Else
131
+ candidates = candidates & "," & extra(i)
132
+ End If
133
+ End If
134
+ Next
135
+
136
+ ' ── 4. POST pending-jump ────────────────────────────────────────────────
137
+ Dim ports
138
+ ports = Split(candidates, ",")
139
+ ok = False
140
+
141
+ For i = 0 To UBound(ports)
142
+ If Not ok Then
143
+ Dim px, body, httpResp
144
+ px = ports(i)
145
+ If IsNumeric(px) And CLng(px) > 0 Then
146
+ On Error Resume Next
147
+ body = "{""sessionId"":""" & sid & """,""at"":" & CStr(Now * 86400000) & "}"
148
+ ' 正确的毫秒时间戳(VBS 没有 Date.now,用 Timer 不够,用 DateDiff)
149
+ Dim epochMs
150
+ epochMs = DateDiff("s", #1/1/1970#, Now()) * 1000
151
+ body = "{""sessionId"":""" & sid & """,""at"":" & CStr(epochMs) & "}"
152
+
153
+ Set httpResp = CreateObject("MSXML2.XMLHTTP.6.0")
154
+ httpResp.Open "POST", "http://127.0.0.1:" & px & "/api/dsh-notify-say/pending-jump", False
155
+ httpResp.setRequestHeader "Origin", "http://127.0.0.1:" & px
156
+ httpResp.setRequestHeader "Content-Type", "application/json"
157
+ httpResp.Send body
158
+ If httpResp.Status = 200 Then
159
+ LogLine "[posted] port=" & px & " status=" & httpResp.Status
160
+ ok = True
161
+ Else
162
+ LogLine "[post-fail] port=" & px & " status=" & httpResp.Status
163
+ End If
164
+ On Error GoTo 0
165
+ End If
166
+ End If
167
+ Next
168
+
169
+ If Not ok Then LogLine "[warn] no port accepted the POST"
170
+
171
+ ' ── 5. 唤窗(第二参数 0 = 隐藏窗口)────────────────────────────────────
172
+ On Error Resume Next
173
+ Dim sh
174
+ Set sh = CreateObject("WScript.Shell")
175
+ sh.Run "cmd /c start """" dsh://open", 0, False
176
+ LogLine "[open] dsh://open"
177
+ On Error GoTo 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-notify-say",
3
- "version": "0.4.79",
3
+ "version": "0.4.81",
4
4
  "description": "Desktop notifications for DSH Desktop that actually speak: Windows TTS out of the box (zero download), VOICEVOX voice packs optional, a selectable system-sound fallback, and an idle gate that waits for running subagents before saying the turn is done. Includes a settings-page GUI (sound source, per-event overrides, jump-back-to-session, cooldowns) and a self hot-reload for development.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,6 +25,8 @@
25
25
  "sounds",
26
26
  "dsh-notify.ps1",
27
27
  "tts-speak.ps1",
28
+ "jump-handler.vbs",
29
+ "jump-handler.ps1",
28
30
  "cordis.patch.yml",
29
31
  "tools",
30
32
  "README.md",