dsh-notification-focus 0.0.0-stage → 0.3.0
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 +22 -0
- package/README.md +160 -3
- package/THIRD_PARTY_NOTICES.md +78 -0
- package/approval-actions.js +496 -0
- package/assets/icon-transparent.png +0 -0
- package/assets/icon.ico +0 -0
- package/client.js +988 -0
- package/cordis.patch.yml +9 -0
- package/history-store.js +212 -0
- package/idle-prompt.js +188 -0
- package/index.js +2064 -0
- package/install-aumid.ps1 +225 -0
- package/install-notify-protocol.ps1 +42 -0
- package/notify-action-handler.ps1 +151 -0
- package/notify-action-hidden.vbs +26 -0
- package/package.json +77 -6
- package/pause-actions.js +274 -0
- package/play-file.ps1 +67 -0
- package/preview-sound.ps1 +62 -0
- package/speak.ps1 +59 -0
- package/state-store.js +344 -0
- package/uninstall-aumid.ps1 +107 -0
- package/uninstall-notify-protocol.ps1 +19 -0
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
// origin: opencode-agent/hy4-flash-free | 2026-11-27T09:09:18+08:00 | approval-notification actions: one-time bound tokens, reject/open/approve ops, strict XML/URL builders
|
|
2
|
+
|
|
3
|
+
// Approval toast actions. HISTORY: this module used to be navigation-only and
|
|
4
|
+
// explicitly refused to grant. On 2026-11-27 the USER explicitly authorized
|
|
5
|
+
// overriding that red line: a real in-toast "approve once" action now exists.
|
|
6
|
+
// Consequences of that authorization (do not "fix" them back):
|
|
7
|
+
// - approve now produces an allowed-once decision in the Host;
|
|
8
|
+
// - a forged/spoofed approve click is therefore accepted -- the original
|
|
9
|
+
// forensic objection (approval/asked carries no tool arguments) is waived
|
|
10
|
+
// by binding the token to askedId+callId+sessionId+toolName+argsHash;
|
|
11
|
+
// - this module NEVER calls ctx.approval.decide() or ctx.approval.request();
|
|
12
|
+
// the Host resolves the outcome inside the existing approval waterfall.
|
|
13
|
+
// Supported ops:
|
|
14
|
+
// - reject: FAIL-DIRECTION op validated by a one-time token (2026-11-26)
|
|
15
|
+
// - open: dsh://open navigation only, focuses DSH for in-app confirmation
|
|
16
|
+
// - approve: allowed-once, one-time token, TTL <= 120 s, strict 5-field binding
|
|
17
|
+
//
|
|
18
|
+
// Source-forensic basis (app.asar, read-only, 2026-11-26):
|
|
19
|
+
// @deepseek-ai/dsh-user-approval/src/types.ts:62-76 ApprovalRequestEvent
|
|
20
|
+
// carries agent / toolName / callId? / reason? / displayReason? / signal?
|
|
21
|
+
// -- NO tool arguments. The user waived this objection on 2026-11-27.
|
|
22
|
+
// @deepseek-ai/dsh-user-approval/src/index.ts:293-318 decide() is PRIVATE,
|
|
23
|
+
// dispatches ctx.waterfall('approval/request', ...) and normalises any
|
|
24
|
+
// non-vocabulary answer to 'unavailable'. No public 'approval/decide' API.
|
|
25
|
+
// README.md:166 / docs/subsystems/approval.md:53 state the request omits tool
|
|
26
|
+
// arguments; the Host must supply them from the tool/call event by callId.
|
|
27
|
+
|
|
28
|
+
import { randomBytes as nodeRandomBytes } from 'node:crypto'
|
|
29
|
+
|
|
30
|
+
/** Legacy navigation op. Frozen: validateApprovalAction still only accepts this. */
|
|
31
|
+
export const OP_OPEN_APPROVAL = 'open-approval'
|
|
32
|
+
export const ALLOWED_OPS = Object.freeze([OP_OPEN_APPROVAL])
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 2026-11-27 用户明确授权:通知内真就地批准。
|
|
36
|
+
* APPROVE_OP is intentionally NOT part of ALLOWED_OPS: legacy navigation
|
|
37
|
+
* validation stays closed; approve has its own validator with a 5-field binding.
|
|
38
|
+
*/
|
|
39
|
+
export const APPROVE_OP = 'approve'
|
|
40
|
+
export const APPROVE_TOKEN_TTL_MS = 120 * 1000
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* One-time token lifetime: 30 minutes.
|
|
44
|
+
*
|
|
45
|
+
* It used to be 2 minutes, which made the whole in-toast reject button useless for
|
|
46
|
+
* the user it is meant for. Measured on the real machine 2026-11-26: the approval
|
|
47
|
+
* fired while the user was away, they came back and clicked "拒绝", and by then the
|
|
48
|
+
* token had expired -- the click silently did nothing and the app card stayed open.
|
|
49
|
+
*
|
|
50
|
+
* A longer window grants nothing extra: this token can only ever produce 'rejected'
|
|
51
|
+
* (never a grant), it is bound to exactly one askedId, and approval/decided revokes
|
|
52
|
+
* it immediately (`revokeKey`). The TTL is only a backstop for approvals that are
|
|
53
|
+
* never decided at all, so it should match a realistic "away, come back, click" gap.
|
|
54
|
+
*/
|
|
55
|
+
export const MAX_TTL_MS = 30 * 60 * 1000
|
|
56
|
+
export const DEFAULT_TTL_MS = MAX_TTL_MS
|
|
57
|
+
|
|
58
|
+
/** Callback used by a validated action. Navigation only -- never an outcome. */
|
|
59
|
+
export const NAVIGATION_TARGET = Object.freeze({ kind: 'open-approval-card' })
|
|
60
|
+
|
|
61
|
+
const TOKEN_RE = /^[A-Za-z0-9_-]{16,128}$/
|
|
62
|
+
const KEY_RE = /^[A-Za-z0-9_.:-]{1,200}$/
|
|
63
|
+
const OP_RE = /^[a-z][a-z0-9-]{0,31}$/
|
|
64
|
+
const TOOL_NAME_RE = /^[A-Za-z0-9_.-]{1,80}$/
|
|
65
|
+
const ARGS_HASH_RE = /^[a-f0-9]{16}$/
|
|
66
|
+
const MAX_ACTIONS = 1
|
|
67
|
+
const APPROVE_BINDING_FIELDS = Object.freeze(['callId', 'sessionId', 'toolName', 'argsHash'])
|
|
68
|
+
|
|
69
|
+
/** Strict URL shape shared with the PowerShell protocol handler. */
|
|
70
|
+
const NOTIFY_URL_RE = /^dshnotify:\/\/act\?op=([^&#/?]+)&token=([^&#/?]+)&t=([^&#/?]+)$/
|
|
71
|
+
|
|
72
|
+
function isPlainObject(value) {
|
|
73
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function validKey(key) {
|
|
77
|
+
return typeof key === 'string' && KEY_RE.test(key)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function validTokenShape(token) {
|
|
81
|
+
return typeof token === 'string' && TOKEN_RE.test(token)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function randomToken(randomBytes) {
|
|
85
|
+
const buf = randomBytes(24)
|
|
86
|
+
if (!buf || typeof buf.toString !== 'function') return ''
|
|
87
|
+
return buf.toString('base64url')
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Strict own-key-set + strictly-equal-values comparison (no coercion, no prototypes). */
|
|
91
|
+
function sameBinding(a, b) {
|
|
92
|
+
if (!isPlainObject(a) || !isPlainObject(b)) return false
|
|
93
|
+
const ak = Reflect.ownKeys(a)
|
|
94
|
+
const bk = Reflect.ownKeys(b)
|
|
95
|
+
if (ak.length !== bk.length) return false
|
|
96
|
+
for (const k of ak) {
|
|
97
|
+
if (!Object.prototype.hasOwnProperty.call(b, k)) return false
|
|
98
|
+
if (a[k] !== b[k]) return false
|
|
99
|
+
}
|
|
100
|
+
return true
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Exact shape required for an approve binding: five fields, no extras. */
|
|
104
|
+
function validApproveBinding(binding) {
|
|
105
|
+
if (!isPlainObject(binding)) return false
|
|
106
|
+
const keys = Reflect.ownKeys(binding)
|
|
107
|
+
if (keys.length !== APPROVE_BINDING_FIELDS.length) return false
|
|
108
|
+
if (!APPROVE_BINDING_FIELDS.every((f) => Object.prototype.hasOwnProperty.call(binding, f))) return false
|
|
109
|
+
return validKey(binding.callId)
|
|
110
|
+
&& validKey(binding.sessionId)
|
|
111
|
+
&& TOOL_NAME_RE.test(binding.toolName)
|
|
112
|
+
&& ARGS_HASH_RE.test(binding.argsHash)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Create an isolated, testable token store.
|
|
117
|
+
*
|
|
118
|
+
* `now` and `randomBytes` are injectable so tests are deterministic. Tokens
|
|
119
|
+
* are one-time: consume() deletes them on success, and every failure path
|
|
120
|
+
* returns false without throwing.
|
|
121
|
+
*/
|
|
122
|
+
export function createApprovalActionStore(options = {}) {
|
|
123
|
+
const cfg = isPlainObject(options) ? options : {}
|
|
124
|
+
const now = typeof cfg.now === 'function' ? cfg.now : Date.now
|
|
125
|
+
const randomBytes = typeof cfg.randomBytes === 'function' ? cfg.randomBytes : nodeRandomBytes
|
|
126
|
+
const defaultTtl = Number.isFinite(cfg.ttlMs) ? Math.min(MAX_TTL_MS, Math.max(1, cfg.ttlMs)) : DEFAULT_TTL_MS
|
|
127
|
+
const maxEntries = Number.isFinite(cfg.maxEntries) ? Math.max(1, Math.min(4096, cfg.maxEntries)) : 512
|
|
128
|
+
/** token -> { key, expiresAt, binding? } (binding added 2026-11-27 for approve) */
|
|
129
|
+
const entries = new Map()
|
|
130
|
+
|
|
131
|
+
function prune() {
|
|
132
|
+
const t = Number(now())
|
|
133
|
+
for (const [token, rec] of entries) {
|
|
134
|
+
if (!Number.isFinite(rec.expiresAt) || t >= rec.expiresAt) entries.delete(token)
|
|
135
|
+
}
|
|
136
|
+
while (entries.size > maxEntries) {
|
|
137
|
+
const oldest = entries.keys().next().value
|
|
138
|
+
if (oldest === undefined) break
|
|
139
|
+
entries.delete(oldest)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Issue a random token bound to one approval key.
|
|
145
|
+
* Optional third argument `binding` (2026-11-27) is stored by value and later
|
|
146
|
+
* compared field-by-field by consume(). Returns '' on bad input.
|
|
147
|
+
*/
|
|
148
|
+
function issue(key, ttlMs, binding) {
|
|
149
|
+
try {
|
|
150
|
+
if (!validKey(key)) return ''
|
|
151
|
+
prune()
|
|
152
|
+
const ttl = ttlMs === undefined ? defaultTtl : Number(ttlMs)
|
|
153
|
+
if (!Number.isFinite(ttl) || ttl <= 0) return ''
|
|
154
|
+
const token = randomToken(randomBytes)
|
|
155
|
+
if (!validTokenShape(token) || entries.has(token)) return ''
|
|
156
|
+
const expiresAt = Number(now()) + Math.min(MAX_TTL_MS, ttl)
|
|
157
|
+
if (!Number.isFinite(expiresAt)) return ''
|
|
158
|
+
const storedBinding = isPlainObject(binding) ? { ...binding } : undefined
|
|
159
|
+
entries.set(token, { key, expiresAt, binding: storedBinding })
|
|
160
|
+
return token
|
|
161
|
+
} catch {
|
|
162
|
+
return '' // fail-closed: never throw into a cordis fiber
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Consume a token. Returns true only for a fresh, unexpired, key-matching token.
|
|
168
|
+
* Optional third argument `binding` must match the stored binding exactly
|
|
169
|
+
* (same own keys, strictly equal values); a mismatch keeps the token usable.
|
|
170
|
+
*/
|
|
171
|
+
function consume(token, key, binding) {
|
|
172
|
+
try {
|
|
173
|
+
if (!validTokenShape(token) || !validKey(key)) return false
|
|
174
|
+
const rec = entries.get(token)
|
|
175
|
+
if (!rec) return false
|
|
176
|
+
const t = Number(now())
|
|
177
|
+
if (!Number.isFinite(t) || !Number.isFinite(rec.expiresAt) || t >= rec.expiresAt) {
|
|
178
|
+
entries.delete(token) // expired: burn it
|
|
179
|
+
return false
|
|
180
|
+
}
|
|
181
|
+
if (rec.key !== key) return false // bound to a different approval: reject, keep token
|
|
182
|
+
if (binding !== undefined && !sameBinding(rec.binding, binding)) return false // keep token
|
|
183
|
+
entries.delete(token) // single use -- replay from here on is always false
|
|
184
|
+
return true
|
|
185
|
+
} catch {
|
|
186
|
+
return false
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Revoke all tokens for a key (called on approval/decided). */
|
|
191
|
+
function revokeKey(key) {
|
|
192
|
+
try {
|
|
193
|
+
if (!validKey(key)) return 0
|
|
194
|
+
let n = 0
|
|
195
|
+
for (const [token, rec] of entries) {
|
|
196
|
+
if (rec.key === key) { entries.delete(token); n++ }
|
|
197
|
+
}
|
|
198
|
+
return n
|
|
199
|
+
} catch {
|
|
200
|
+
return 0
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return {
|
|
205
|
+
issue,
|
|
206
|
+
consume,
|
|
207
|
+
revokeKey,
|
|
208
|
+
prune,
|
|
209
|
+
get size() { return entries.size },
|
|
210
|
+
/**
|
|
211
|
+
* TEST / DIAGNOSTIC VIEW ONLY -- contains live one-time tokens.
|
|
212
|
+
* NEVER write this to a log, toast body, model context or HTTP response.
|
|
213
|
+
*/
|
|
214
|
+
snapshot() {
|
|
215
|
+
const out = []
|
|
216
|
+
for (const [token, rec] of entries) {
|
|
217
|
+
out.push({ token, key: rec.key, expiresAt: rec.expiresAt, binding: rec.binding })
|
|
218
|
+
}
|
|
219
|
+
return out
|
|
220
|
+
},
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// Module-level default store. Production callers (index.js) should create their
|
|
225
|
+
// own store and pass it explicitly; these helpers are convenience wrappers.
|
|
226
|
+
const defaultStore = createApprovalActionStore()
|
|
227
|
+
|
|
228
|
+
export function issueApprovalToken(key, ttlMs, deps = {}) {
|
|
229
|
+
const store = isPlainObject(deps) && deps.store ? deps.store : defaultStore
|
|
230
|
+
return store.issue(key, ttlMs)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function consumeApprovalToken(token, key, deps = {}) {
|
|
234
|
+
const store = isPlainObject(deps) && deps.store ? deps.store : defaultStore
|
|
235
|
+
return store.consume(token, key)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Authoritative validation. Returns a normalized navigation descriptor or a
|
|
240
|
+
* fail-closed rejection. It NEVER returns an ApprovalOutcome and never
|
|
241
|
+
* grants a capability. The token is consumed only on full success.
|
|
242
|
+
*/
|
|
243
|
+
export function validateApprovalAction(action, deps = {}) {
|
|
244
|
+
try {
|
|
245
|
+
if (!isPlainObject(action)) return { ok: false, reason: 'malformed-action' }
|
|
246
|
+
const { op, token, key } = action
|
|
247
|
+
if (typeof op !== 'string' || !OP_RE.test(op) || !ALLOWED_OPS.includes(op)) {
|
|
248
|
+
return { ok: false, reason: 'op-not-allowed' }
|
|
249
|
+
}
|
|
250
|
+
if (!validTokenShape(token)) return { ok: false, reason: 'bad-token-shape' }
|
|
251
|
+
if (!validKey(key)) return { ok: false, reason: 'bad-key' }
|
|
252
|
+
const store = isPlainObject(deps) && deps.store ? deps.store : defaultStore
|
|
253
|
+
if (!store.consume(token, key)) return { ok: false, reason: 'token-invalid' }
|
|
254
|
+
return { ok: true, op, key, navigate: NAVIGATION_TARGET }
|
|
255
|
+
} catch {
|
|
256
|
+
return { ok: false, reason: 'rejected' }
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* 2026-11-27 用户授权的 approve 校验:令牌必须同时绑定
|
|
262
|
+
* askedId(key) + callId + sessionId + toolName + argsHash,TTL 由签发方限制到
|
|
263
|
+
* <=120s,单次消费。成功只返回 {ok, op, key, binding}:没有 navigate/outcome,
|
|
264
|
+
* 不产生 allowed-once 之外的任何文案,也绝不调用 decide/request。
|
|
265
|
+
* 永不 throw。
|
|
266
|
+
*/
|
|
267
|
+
export function validateApproveAction(action, deps = {}) {
|
|
268
|
+
try {
|
|
269
|
+
if (!isPlainObject(action)) return { ok: false, reason: 'malformed-action' }
|
|
270
|
+
const { op, token, key, callId, sessionId, toolName, argsHash } = action
|
|
271
|
+
if (op !== APPROVE_OP) return { ok: false, reason: 'op-not-allowed' }
|
|
272
|
+
if (!validTokenShape(token)) return { ok: false, reason: 'bad-token-shape' }
|
|
273
|
+
if (!validKey(key)) return { ok: false, reason: 'bad-key' }
|
|
274
|
+
const binding = { callId, sessionId, toolName, argsHash }
|
|
275
|
+
if (!validApproveBinding(binding)) return { ok: false, reason: 'bad-binding' }
|
|
276
|
+
const store = isPlainObject(deps) && deps.store ? deps.store : defaultStore
|
|
277
|
+
if (!store.consume(token, key, binding)) return { ok: false, reason: 'token-invalid' }
|
|
278
|
+
return { ok: true, op: APPROVE_OP, key, binding }
|
|
279
|
+
} catch {
|
|
280
|
+
return { ok: false, reason: 'rejected' }
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/** Build the (navigation-only) action list for one approval. */
|
|
285
|
+
export function buildApprovalActions({ askedId, token, label = 'Open approval in DSH' } = {}) {
|
|
286
|
+
try {
|
|
287
|
+
if (!validKey(askedId) || !validTokenShape(token)) return Object.freeze([])
|
|
288
|
+
if (typeof label !== 'string' || label.length === 0 || label.length > 40) return Object.freeze([])
|
|
289
|
+
return Object.freeze([Object.freeze({
|
|
290
|
+
op: OP_OPEN_APPROVAL,
|
|
291
|
+
token,
|
|
292
|
+
key: askedId,
|
|
293
|
+
label,
|
|
294
|
+
})])
|
|
295
|
+
} catch {
|
|
296
|
+
return Object.freeze([])
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** Escape every character that could break a double-quoted XML attribute. */
|
|
301
|
+
export function escapeXmlAttribute(value) {
|
|
302
|
+
return String(value ?? '')
|
|
303
|
+
.replace(/&/g, '&')
|
|
304
|
+
.replace(/</g, '<')
|
|
305
|
+
.replace(/>/g, '>')
|
|
306
|
+
.replace(/"/g, '"')
|
|
307
|
+
.replace(/'/g, ''')
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Build the Windows toast <actions> fragment.
|
|
312
|
+
*
|
|
313
|
+
* The fragment is ALREADY XML-escaped by this function: the integration must
|
|
314
|
+
* inject it verbatim and must NOT run SecurityElement::Escape over it.
|
|
315
|
+
* Returns '' (no actions at all) on any anomaly -- fail closed.
|
|
316
|
+
*/
|
|
317
|
+
export function buildActionXml(actions) {
|
|
318
|
+
try {
|
|
319
|
+
if (!Array.isArray(actions) || actions.length === 0 || actions.length > MAX_ACTIONS) return ''
|
|
320
|
+
let xml = '<actions>'
|
|
321
|
+
for (const action of actions) {
|
|
322
|
+
if (!isPlainObject(action)) return ''
|
|
323
|
+
const { op, token, key, label } = action
|
|
324
|
+
if (typeof op !== 'string' || !OP_RE.test(op) || !ALLOWED_OPS.includes(op)) return ''
|
|
325
|
+
if (!validTokenShape(token) || !validKey(key)) return ''
|
|
326
|
+
if (typeof label !== 'string' || label.length === 0 || label.length > 40) return ''
|
|
327
|
+
// Canonical query order is part of the protocol contract.
|
|
328
|
+
const query = 'op=' + encodeURIComponent(op)
|
|
329
|
+
+ '&token=' + encodeURIComponent(token)
|
|
330
|
+
+ '&t=' + encodeURIComponent(key)
|
|
331
|
+
const uri = 'dshnotify://act?' + query
|
|
332
|
+
xml += '<action activationType="protocol" arguments="' + escapeXmlAttribute(uri)
|
|
333
|
+
+ '" content="' + escapeXmlAttribute(label) + '"/>'
|
|
334
|
+
}
|
|
335
|
+
return xml + '</actions>'
|
|
336
|
+
} catch {
|
|
337
|
+
return ''
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* FINAL DESIGN (after adversarial audit 2026-11-26):
|
|
343
|
+
* The in-toast approval button is a plain navigation action using the ALREADY
|
|
344
|
+
* VERIFIED `dsh://open` protocol. No custom protocol, no handler process, no
|
|
345
|
+
* HTTP loopback (which would be rejected 401/403 by requestRejection), and no
|
|
346
|
+
* capability token is needed: focusing the DSH window grants nothing.
|
|
347
|
+
*
|
|
348
|
+
* DSH's `open-url` handler only matches `dsh://open` and calls
|
|
349
|
+
* focusPrimaryWindow() -- it CANNOT jump to a specific approval card. The label
|
|
350
|
+
* therefore says "回到 DSH 处理", not "打开审批卡片".
|
|
351
|
+
*/
|
|
352
|
+
export const FOCUS_PROTOCOL_URL = Object.freeze('dsh://open')
|
|
353
|
+
|
|
354
|
+
export function buildFocusActionXml({ label = '回到 DSH 处理' } = {}) {
|
|
355
|
+
try {
|
|
356
|
+
if (typeof label !== 'string' || label.length === 0 || label.length > 40) return ''
|
|
357
|
+
return '<actions><action activationType="protocol" arguments="'
|
|
358
|
+
+ escapeXmlAttribute(FOCUS_PROTOCOL_URL)
|
|
359
|
+
+ '" content="' + escapeXmlAttribute(label) + '"/></actions>'
|
|
360
|
+
} catch {
|
|
361
|
+
return '' // fail closed
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* 2026-11-26 用户授权的最小失败方向 op。2026-11-27 起 approve 是独立 op,
|
|
367
|
+
* 不修改 reject 的任何既有语义与导出。
|
|
368
|
+
*/
|
|
369
|
+
export const REJECT_OP = 'reject'
|
|
370
|
+
|
|
371
|
+
export function validateRejectAction(action, deps = {}) {
|
|
372
|
+
try {
|
|
373
|
+
if (!isPlainObject(action)) return { ok: false, reason: 'malformed-action' }
|
|
374
|
+
const { op, token, key } = action
|
|
375
|
+
if (op !== REJECT_OP) return { ok: false, reason: 'op-not-allowed' }
|
|
376
|
+
if (!validTokenShape(token)) return { ok: false, reason: 'bad-token-shape' }
|
|
377
|
+
if (!validKey(key)) return { ok: false, reason: 'bad-key' }
|
|
378
|
+
const store = isPlainObject(deps) && deps.store ? deps.store : defaultStore
|
|
379
|
+
if (!store.consume(token, key)) return { ok: false, reason: 'token-invalid' }
|
|
380
|
+
return { ok: true, op: REJECT_OP, key } // 无 grant 语义
|
|
381
|
+
} catch {
|
|
382
|
+
return { ok: false, reason: 'rejected' }
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* 决策按钮 XML。基础两按钮始终是 reject + dsh://open(聚焦,不授权)。
|
|
388
|
+
* 2026-11-27 用户授权:仅当 approveToken 形状合法且 binding 五字段全部合法时,
|
|
389
|
+
* 才追加第三个按钮 approve(一次性、<=120s、Host 端兑现 allowed-once)。
|
|
390
|
+
* 任何 approve 相关的异常都只省略该按钮,绝不丢掉 reject+focus(grant fail-closed)。
|
|
391
|
+
* 返回值已 XML 转义,Host 直接注入;label <=20 字,URL <=2048,按钮 <=5。
|
|
392
|
+
*/
|
|
393
|
+
export function buildDecisionActionXml({
|
|
394
|
+
askedId,
|
|
395
|
+
token,
|
|
396
|
+
approveToken,
|
|
397
|
+
binding,
|
|
398
|
+
rejectLabel = '拒绝',
|
|
399
|
+
approveLabel = '⚠ 批准一次',
|
|
400
|
+
focusLabel = '去 DSH 确认',
|
|
401
|
+
} = {}) {
|
|
402
|
+
try {
|
|
403
|
+
if (!validKey(askedId) || !validTokenShape(token)) return ''
|
|
404
|
+
const validLabel = (s) => typeof s === 'string' && s.length > 0 && s.length <= 20
|
|
405
|
+
if (!validLabel(rejectLabel) || !validLabel(approveLabel) || !validLabel(focusLabel)) return ''
|
|
406
|
+
const action = (uri, label) => '<action activationType="protocol" arguments="'
|
|
407
|
+
+ escapeXmlAttribute(uri) + '" content="' + escapeXmlAttribute(label) + '"/>'
|
|
408
|
+
const rejectUri = 'dshnotify://act?op=' + encodeURIComponent(REJECT_OP)
|
|
409
|
+
+ '&t=' + encodeURIComponent(token) + '&k=' + encodeURIComponent(askedId)
|
|
410
|
+
|
|
411
|
+
// Approve is strictly opt-in: any anomaly here only removes this button.
|
|
412
|
+
let approveUri = ''
|
|
413
|
+
try {
|
|
414
|
+
if (validTokenShape(approveToken) && validApproveBinding(binding)) {
|
|
415
|
+
const candidate = 'dshnotify://act?op=' + encodeURIComponent(APPROVE_OP)
|
|
416
|
+
+ '&t=' + encodeURIComponent(approveToken)
|
|
417
|
+
+ '&k=' + encodeURIComponent(askedId)
|
|
418
|
+
+ '&c=' + encodeURIComponent(binding.callId)
|
|
419
|
+
+ '&s=' + encodeURIComponent(binding.sessionId)
|
|
420
|
+
+ '&n=' + encodeURIComponent(binding.toolName)
|
|
421
|
+
+ '&h=' + encodeURIComponent(binding.argsHash)
|
|
422
|
+
if (candidate.length <= 2048) approveUri = candidate
|
|
423
|
+
}
|
|
424
|
+
} catch {
|
|
425
|
+
approveUri = ''
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const buttons = [action(rejectUri, rejectLabel), action(FOCUS_PROTOCOL_URL, focusLabel)]
|
|
429
|
+
if (approveUri) buttons.push(action(approveUri, approveLabel))
|
|
430
|
+
// Hard cap: never emit more than five buttons, even if this builder changes.
|
|
431
|
+
return '<actions>' + buttons.slice(0, 5).join('') + '</actions>'
|
|
432
|
+
} catch {
|
|
433
|
+
return '' // fail closed:任何异常都不产生按钮
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* ⚠️ HISTORICAL / TEST-ONLY — DO NOT WIRE INTO PRODUCTION.
|
|
439
|
+
* This parser only accepts the obsolete `op=open-approval&token=&t=` shape and
|
|
440
|
+
* rejects every real button URL we emit (`op=reject|approve|open`, `t=`, `k=`).
|
|
441
|
+
* It is exported solely because legacy tests assert its shape; production uses
|
|
442
|
+
* createApprovalActionStore + validateRejectAction/validateApproveAction and
|
|
443
|
+
* the action-file channel. Wiring this in would silently invalidate all buttons.
|
|
444
|
+
*/
|
|
445
|
+
export function parseNotifyUrl(url) {
|
|
446
|
+
try {
|
|
447
|
+
if (typeof url !== 'string' || url.length > 512) return null
|
|
448
|
+
const m = NOTIFY_URL_RE.exec(url)
|
|
449
|
+
if (!m) return null
|
|
450
|
+
const op = decodeURIComponent(m[1])
|
|
451
|
+
const token = decodeURIComponent(m[2])
|
|
452
|
+
const key = decodeURIComponent(m[3])
|
|
453
|
+
if (typeof op !== 'string' || !OP_RE.test(op)) return null
|
|
454
|
+
// Approval-specific defense in depth: Host validation stays authoritative,
|
|
455
|
+
// but this parser must never hand a grant-style op back to a caller.
|
|
456
|
+
if (!ALLOWED_OPS.includes(op)) return null
|
|
457
|
+
if (!validTokenShape(token) || !validKey(key)) return null
|
|
458
|
+
return { op, token, key }
|
|
459
|
+
} catch {
|
|
460
|
+
return null
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Optional body helper: a FIXED hint, deliberately accepting no model input.
|
|
466
|
+
* Tool arguments and model-generated justification are never passed to toast
|
|
467
|
+
* actions or URLs by this module.
|
|
468
|
+
*/
|
|
469
|
+
export function formatApprovalBody() {
|
|
470
|
+
return 'Open the approval card in DSH to review the tool call before deciding.'
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
export default {
|
|
474
|
+
OP_OPEN_APPROVAL,
|
|
475
|
+
ALLOWED_OPS,
|
|
476
|
+
APPROVE_OP,
|
|
477
|
+
APPROVE_TOKEN_TTL_MS,
|
|
478
|
+
MAX_TTL_MS,
|
|
479
|
+
NAVIGATION_TARGET,
|
|
480
|
+
createApprovalActionStore,
|
|
481
|
+
issueApprovalToken,
|
|
482
|
+
consumeApprovalToken,
|
|
483
|
+
validateApprovalAction,
|
|
484
|
+
validateApproveAction,
|
|
485
|
+
REJECT_OP,
|
|
486
|
+
validateRejectAction,
|
|
487
|
+
buildDecisionActionXml,
|
|
488
|
+
buildApprovalActions,
|
|
489
|
+
buildActionXml,
|
|
490
|
+
FOCUS_PROTOCOL_URL,
|
|
491
|
+
buildFocusActionXml,
|
|
492
|
+
parseNotifyUrl,
|
|
493
|
+
escapeXmlAttribute,
|
|
494
|
+
formatApprovalBody,
|
|
495
|
+
}
|
|
496
|
+
|
|
Binary file
|
package/assets/icon.ico
ADDED
|
Binary file
|