dsh-notify-say 0.4.61 → 0.4.63
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/CHANGELOG.md +8 -0
- package/README.md +103 -11
- package/dsh-notify.ps1 +10 -3
- package/lib/client.js +267 -12
- package/lib/config-schema.js +15 -0
- package/lib/config-store.js +2 -1
- package/lib/index.js +77 -12
- package/lib/session-bridge.js +171 -14
- package/package.json +1 -1
- package/test/client-contract.test.mjs +24 -17
- package/test/foreground-longpoll.test.mjs +100 -0
- package/test/toast-title-jump-rule.test.mjs +132 -0
package/lib/index.js
CHANGED
|
@@ -315,7 +315,7 @@ function normalizeConfig(raw) {
|
|
|
315
315
|
for (const k of ['notifyOnComplete', 'notifyOnError', 'notifyOnBlocked', 'notifyOnAborted',
|
|
316
316
|
'notifyOnApproval', 'notifyOnQuestion', 'voice', 'subagents', 'idleGate',
|
|
317
317
|
// v0.4.2
|
|
318
|
-
'showToast', 'jumpWhenBackground', 'jumpWhenForeground',
|
|
318
|
+
'showToast', 'jumpWhenBackground', 'jumpWhenForeground', 'multiJumpRule',
|
|
319
319
|
// v0.4.3:开发开关
|
|
320
320
|
'devReload']) {
|
|
321
321
|
if (typeof raw[k] === 'boolean') out[k] = raw[k];
|
|
@@ -698,6 +698,7 @@ export function effectiveForClient(c) {
|
|
|
698
698
|
ttsRate: g.ttsRate,
|
|
699
699
|
jumpWhenBackground: g.jumpWhenBackground,
|
|
700
700
|
jumpWhenForeground: g.jumpWhenForeground,
|
|
701
|
+
multiJumpRule: g.multiJumpRule,
|
|
701
702
|
// v0.4.7 冷却:毫秒 → 秒(前端下拉用的是秒)
|
|
702
703
|
debounceSeconds: Math.round((Number(g.debounceMs) || 0) / 1000),
|
|
703
704
|
globalCooldownSeconds: Math.round((Number(g.globalCooldownMs) || 0) / 1000),
|
|
@@ -867,7 +868,9 @@ export function apply(ctx, config) {
|
|
|
867
868
|
|
|
868
869
|
// 会话状态桥实例:notify() 往里写,HTTP route 往外读(client 半侧消费)。
|
|
869
870
|
// 进程内、不落盘 —— 重启后"刚才那条通知"就该失效,这是正确语义。
|
|
870
|
-
const bridge = createSessionBridge();
|
|
871
|
+
const bridge = createSessionBridge();
|
|
872
|
+
// ⚠️ v0.4.63:长轮询的挂起响应必须在卸载时结束,
|
|
873
|
+
// 否则热重载会留下永远等不到响应的连接(2026-10-18 那次崩溃同源)。
|
|
871
874
|
|
|
872
875
|
// 配置 route:client 设置卡片读写音量/音色/开关。
|
|
873
876
|
// onChanged 立刻重读配置 —— 让"点一下即生效"成立,不必重启宿主。
|
|
@@ -980,6 +983,14 @@ export function apply(ctx, config) {
|
|
|
980
983
|
// 播报前现读配置:让 CLI/GUI 改的值立即生效,无需重载宿主。
|
|
981
984
|
refreshConfig();
|
|
982
985
|
|
|
986
|
+
// Toast 第三行:项目名(v0.4.62)。
|
|
987
|
+
//
|
|
988
|
+
// 只在"标题里没包含它"时才给 —— 否则标题已经是「优化0Deepseek…」,
|
|
989
|
+
// 再显示一遍 "0Deepseek" 就是重复。单项目用户看不到第三行,
|
|
990
|
+
// 多项目并行的用户能靠它区分。
|
|
991
|
+
let detail = projectNameOf(session);
|
|
992
|
+
if (detail && String(title || '').toLowerCase().includes(detail.toLowerCase())) detail = '';
|
|
993
|
+
|
|
983
994
|
// 记下"这条通知属于哪个会话",供 client 半侧点击后跳转。
|
|
984
995
|
// 失败绝不影响通知本身(跳会话是增强功能)。
|
|
985
996
|
try {
|
|
@@ -1076,7 +1087,7 @@ export function apply(ctx, config) {
|
|
|
1076
1087
|
];
|
|
1077
1088
|
if (vVoice) ttsArgs.push('-VoiceName', vVoice);
|
|
1078
1089
|
// TTS 自己会出声,所以 notify.ps1 只负责弹窗(-NoSound)
|
|
1079
|
-
if (wantToast) spawnNotify(notifyArgs(psKind, title, body, true));
|
|
1090
|
+
if (wantToast) spawnNotify(notifyArgs(psKind, title, body, true, false, detail));
|
|
1080
1091
|
spawnTts(ttsArgs);
|
|
1081
1092
|
return;
|
|
1082
1093
|
}
|
|
@@ -1095,7 +1106,7 @@ export function apply(ctx, config) {
|
|
|
1095
1106
|
// **不传 -Voice/-Mode**,并且 ps1 侧见到 -SystemSound 就跳过语音池
|
|
1096
1107
|
// (否则 -Kind fail 会先选中 fail 池的 wav,永远走不到系统音效)。
|
|
1097
1108
|
if (wantSound && source === 'system') {
|
|
1098
|
-
const a = baseNotifyArgs(psKind, title, body, false, !wantToast);
|
|
1109
|
+
const a = baseNotifyArgs(psKind, title, body, false, !wantToast, detail);
|
|
1099
1110
|
const explicitTone = kindOr(cfg.kindSystemSound, aKind, '');
|
|
1100
1111
|
a.push('-SystemSound', explicitTone || 'auto');
|
|
1101
1112
|
spawnNotify(a);
|
|
@@ -1111,7 +1122,7 @@ export function apply(ctx, config) {
|
|
|
1111
1122
|
const kind = audioKindOf(psKind);
|
|
1112
1123
|
const file = resolveCustomFile(kind, cfg);
|
|
1113
1124
|
if (file) {
|
|
1114
|
-
const a = baseNotifyArgs(psKind, title, body, false, !wantToast);
|
|
1125
|
+
const a = baseNotifyArgs(psKind, title, body, false, !wantToast, detail);
|
|
1115
1126
|
a.push('-CustomFile', file);
|
|
1116
1127
|
// v0.4.16:自定义音频的"标准化"是**可选**的。
|
|
1117
1128
|
//
|
|
@@ -1125,7 +1136,7 @@ export function apply(ctx, config) {
|
|
|
1125
1136
|
}
|
|
1126
1137
|
// 没配文件:不要静默无声,退到系统提示音(audio-source 的回退链已算过,
|
|
1127
1138
|
// 但这里再兜一次,防止 effectiveSource 的 hasCustomFile 判断与读取不一致)
|
|
1128
|
-
const a2 = baseNotifyArgs(psKind, title, body, false, !wantToast);
|
|
1139
|
+
const a2 = baseNotifyArgs(psKind, title, body, false, !wantToast, detail);
|
|
1129
1140
|
a2.push('-SystemSound', 'auto');
|
|
1130
1141
|
spawnNotify(a2);
|
|
1131
1142
|
return;
|
|
@@ -1134,15 +1145,17 @@ export function apply(ctx, config) {
|
|
|
1134
1145
|
// ── 其余音源(bundled)走 dsh-notify.ps1 ──────────────────────────────
|
|
1135
1146
|
// 第 5 个参数是 soundOnly(v0.4.59)。原来这里传的是 source,
|
|
1136
1147
|
// 而 notifyArgs 只认 4 个参数 —— 那个多传的值一直被静默忽略。
|
|
1137
|
-
spawnNotify(notifyArgs(psKind, title, body, !wantSound, !wantToast));
|
|
1148
|
+
spawnNotify(notifyArgs(psKind, title, body, !wantSound, !wantToast, detail));
|
|
1138
1149
|
}
|
|
1139
1150
|
|
|
1140
1151
|
/** 只有基础参数的 notify 调用(供 system 音源拼装)。 */
|
|
1141
|
-
function baseNotifyArgs(psKind, title, body, noSound, soundOnly) {
|
|
1152
|
+
function baseNotifyArgs(psKind, title, body, noSound, soundOnly, detail) {
|
|
1142
1153
|
const args = [
|
|
1143
1154
|
'-NoProfile', '-ExecutionPolicy', 'Bypass', '-WindowStyle', 'Hidden', '-File',
|
|
1144
1155
|
SCRIPT_PATH, '-Kind', psKind, '-Title', title, '-Body', body,
|
|
1145
1156
|
];
|
|
1157
|
+
// 第三行可选(v0.4.62):空就不传,ps1 侧相应不加 <text>。
|
|
1158
|
+
if (detail) args.push('-Detail', detail);
|
|
1146
1159
|
if (noSound) args.push('-NoSound');
|
|
1147
1160
|
// 只出声不弹窗(v0.4.59)。与 -NoSound 互斥,调用方保证。
|
|
1148
1161
|
if (soundOnly) args.push('-SoundOnly');
|
|
@@ -1157,11 +1170,13 @@ export function apply(ctx, config) {
|
|
|
1157
1170
|
}
|
|
1158
1171
|
|
|
1159
1172
|
/** 构造 dsh-notify.ps1 的参数。noSound=true 表示只弹窗不出声。 */
|
|
1160
|
-
function notifyArgs(psKind, title, body, noSound, soundOnly) {
|
|
1173
|
+
function notifyArgs(psKind, title, body, noSound, soundOnly, detail) {
|
|
1161
1174
|
const args = [
|
|
1162
1175
|
'-NoProfile', '-ExecutionPolicy', 'Bypass', '-WindowStyle', 'Hidden', '-File',
|
|
1163
1176
|
SCRIPT_PATH, '-Kind', psKind, '-Title', title, '-Body', body,
|
|
1164
1177
|
];
|
|
1178
|
+
// 第三行可选(v0.4.62):空就不传,ps1 侧相应不加 <text>。
|
|
1179
|
+
if (detail) args.push('-Detail', detail);
|
|
1165
1180
|
if (noSound) args.push('-NoSound');
|
|
1166
1181
|
// 只出声不弹窗(v0.4.59)。与 -NoSound 互斥,调用方保证。
|
|
1167
1182
|
if (soundOnly) args.push('-SoundOnly');
|
|
@@ -1218,6 +1233,21 @@ function hasVoiceLine(psKind) {
|
|
|
1218
1233
|
return cfg.subagents || !isSubagent(session);
|
|
1219
1234
|
}
|
|
1220
1235
|
|
|
1236
|
+
/**
|
|
1237
|
+
* 项目名(cwd 最后一段),用作 Toast 第三行(v0.4.62)。
|
|
1238
|
+
* 只在"标题里没有它"时才显示,避免重复。
|
|
1239
|
+
*/
|
|
1240
|
+
function projectNameOf(session) {
|
|
1241
|
+
try {
|
|
1242
|
+
const cwd = session && session.header && session.header.cwd;
|
|
1243
|
+
if (!cwd) return '';
|
|
1244
|
+
const parts = String(cwd).split(/[\\/]+/).filter(Boolean);
|
|
1245
|
+
return parts.length ? parts[parts.length - 1] : '';
|
|
1246
|
+
} catch {
|
|
1247
|
+
return '';
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1221
1251
|
/** 读宿主服务(非 strict:容许由其它作用域提供;任何失败都返回 undefined)。 */
|
|
1222
1252
|
function svc(name) {
|
|
1223
1253
|
try { return ctx.get(name, false); } catch { return undefined; }
|
|
@@ -1418,6 +1448,15 @@ function hasVoiceLine(psKind) {
|
|
|
1418
1448
|
gate = cfg.notifyOnAborted; psKind = 'chime';
|
|
1419
1449
|
title = 'DSH 回合提前结束'; body = '回合结束原因:' + (kind || 'unknown');
|
|
1420
1450
|
}
|
|
1451
|
+
// ── Toast 标题改用会话标题(v0.4.62)─────────────────────────────────
|
|
1452
|
+
//
|
|
1453
|
+
// 用户指出:既然点 Toast 跳不到对应会话,那至少让标题告诉用户
|
|
1454
|
+
// "是哪个会话完成了"。DSH 的侧栏标题就是首句话摘要,比「DSH 任务完成」有用。
|
|
1455
|
+
// 只用「看起来像标题」的结果 —— 拿不到真标题时 sessionTitleOf 会退回
|
|
1456
|
+
// 会话 id 前缀(如 session-2cc3),那东西当 Toast 标题毫无意义,还不如通用文案。
|
|
1457
|
+
const sTitle = sessionTitleOf(session);
|
|
1458
|
+
if (sTitle && !/^session[-_]?/i.test(sTitle) && sTitle !== '未知会话') title = sTitle;
|
|
1459
|
+
|
|
1421
1460
|
// 事件类别开关关闭 → 不播
|
|
1422
1461
|
if (!gate) {
|
|
1423
1462
|
// 必须报**真实生效的配置键**,不能从 kind 名拼。
|
|
@@ -1479,13 +1518,34 @@ function hasVoiceLine(psKind) {
|
|
|
1479
1518
|
if (id) approvalSeen.delete(id);
|
|
1480
1519
|
}
|
|
1481
1520
|
|
|
1482
|
-
/**
|
|
1521
|
+
/**
|
|
1522
|
+
* 会话标题(正文线索 + v0.4.62 起兼作 Toast 标题)。
|
|
1523
|
+
*
|
|
1524
|
+
* 三级来源(前一级拿不到就试下一级):
|
|
1525
|
+
* ① `sessionTitle` 服务 —— 宿主自己维护的标题(支持 rename),最权威;
|
|
1526
|
+
* ② `sessionProjections` 的 `title` 投影 —— 2026-10-19 实测 436 个会话全部有,
|
|
1527
|
+
* 内容通常就是用户首句摘要(「优化Deepseek Harness配置与插件」);
|
|
1528
|
+
* ③ 会话 id 前 12 位 —— 丑陋但至少能区分(旧插件 :433-439 同构)。
|
|
1529
|
+
*
|
|
1530
|
+
* ⚠️ 为什么要有 ②:① 在某些宿主版本/时机下取不到(服务缺失或尚未生成),
|
|
1531
|
+
* 而投影缓存里已经有标题了。两者都拿不到才退回 id。
|
|
1532
|
+
*/
|
|
1483
1533
|
function sessionTitleOf(session) {
|
|
1484
1534
|
try {
|
|
1485
1535
|
const svcTitle = svc('sessionTitle');
|
|
1486
1536
|
const snap = svcTitle && typeof svcTitle.get === 'function' ? svcTitle.get(session) : undefined;
|
|
1487
1537
|
if (snap && snap.title) return String(snap.title);
|
|
1488
|
-
} catch { /* 服务不可用 →
|
|
1538
|
+
} catch { /* 服务不可用 → 试下一级 */ }
|
|
1539
|
+
try {
|
|
1540
|
+
const proj = svc('sessionProjections');
|
|
1541
|
+
if (proj && typeof proj.stateOf === 'function') {
|
|
1542
|
+
const v = proj.stateOf(session, 'title');
|
|
1543
|
+
// 防御两种形状:直接字符串,或 { ver, seq, val } 行。
|
|
1544
|
+
const s = typeof v === 'string' ? v : (v && typeof v.val === 'string' ? v.val : '');
|
|
1545
|
+
const t = String(s || '').trim();
|
|
1546
|
+
if (t) return t;
|
|
1547
|
+
}
|
|
1548
|
+
} catch { /* 投影不可用 → 退回 id */ }
|
|
1489
1549
|
const k = sessionKey(session);
|
|
1490
1550
|
return k === '__unknown__' ? '未知会话' : k.slice(0, 12);
|
|
1491
1551
|
}
|
|
@@ -1596,6 +1656,9 @@ function hasVoiceLine(psKind) {
|
|
|
1596
1656
|
pendingDone.clear();
|
|
1597
1657
|
prevStatus.clear();
|
|
1598
1658
|
lastTurnEnd.clear();
|
|
1659
|
+
// v0.4.63:长轮询的挂起响应也要结束掉。不做的后果是热重载后
|
|
1660
|
+
// 旧连接永远等不到响应,客户端不会再发新的 /wait(它以为还在等)。
|
|
1661
|
+
try { bridge.disposeAllWaiters(); } catch { /* 卸载期失败不影响退出 */ }
|
|
1599
1662
|
}, 'dsh-notify-say: pending timers');
|
|
1600
1663
|
|
|
1601
1664
|
// ── 审批 waterfall:必须 next() 放行并透传返回值,绝不阻断、不改写 ──────
|
|
@@ -1675,7 +1738,9 @@ function hasVoiceLine(psKind) {
|
|
|
1675
1738
|
// (外部审阅 2026-10-18 指出。)
|
|
1676
1739
|
const isWriteRoute =
|
|
1677
1740
|
(head === 'config' && /\/set\b/.test(pathname))
|
|
1678
|
-
|| head === 'consume'
|
|
1741
|
+
|| head === 'consume'
|
|
1742
|
+
// v0.4.63:/ack 只摘一条(前台跳转后确认),同样是写操作
|
|
1743
|
+
|| head === 'ack'
|
|
1679
1744
|
|| head === 'diagnostics'
|
|
1680
1745
|
|| (head === 'voicepacks' && (/\/install\b/.test(pathname) || /\/uninstall\b/.test(pathname)))
|
|
1681
1746
|
// v0.4.57:一次性自重载会 dispose 当前实例并重新求值模块,
|
package/lib/session-bridge.js
CHANGED
|
@@ -30,6 +30,10 @@ export const ROUTE_PREFIX = '/api/dsh-notify-say';
|
|
|
30
30
|
|
|
31
31
|
/** 取最近通知会话的路径。 */
|
|
32
32
|
export const ROUTE_LAST = ROUTE_PREFIX + '/last-session';
|
|
33
|
+
/** 长轮询:有新的完成信号就返回(v0.4.63)。 */
|
|
34
|
+
export const ROUTE_WAIT = ROUTE_PREFIX + '/wait';
|
|
35
|
+
/** 只确认某一条(前台跳转后调用,避免切回来重复跳)。 */
|
|
36
|
+
export const ROUTE_ACK = ROUTE_PREFIX + '/ack';
|
|
33
37
|
|
|
34
38
|
/** 消费(读取并清空)的路径。 */
|
|
35
39
|
export const ROUTE_CONSUME = ROUTE_PREFIX + '/consume';
|
|
@@ -42,21 +46,114 @@ export const ROUTE_CONSUME = ROUTE_PREFIX + '/consume';
|
|
|
42
46
|
* @returns {{ handler: Function, setLast: Function, peek: Function }}
|
|
43
47
|
*/
|
|
44
48
|
export function createSessionBridge() {
|
|
45
|
-
/**
|
|
46
|
-
|
|
49
|
+
/**
|
|
50
|
+
* 待跳转队列(v0.4.62:原来只有一个 `last` 槽位)。
|
|
51
|
+
*
|
|
52
|
+
* 按 `at` 升序;**同一会话只保留第一次完成的时刻**。
|
|
53
|
+
* 为什么:队列的排序含义是"谁先完成的",如果重复完成就刷新 at,
|
|
54
|
+
* 它会退化成"谁最后完成的",那"跳最早错过的"这条规则就失效了。
|
|
55
|
+
*
|
|
56
|
+
* 上限 MAX_PENDING 纯防御:长时间不回来也不无限增长,超出丢最早的。
|
|
57
|
+
*
|
|
58
|
+
* @type {{ sessionId: string, at: number }[]}
|
|
59
|
+
*/
|
|
60
|
+
let pending = [];
|
|
61
|
+
|
|
62
|
+
/** 队列上限(正常使用远达不到)。 */
|
|
63
|
+
const MAX_PENDING = 20;
|
|
47
64
|
|
|
48
65
|
/** 记录一条通知对应的会话(供 client 取用)。 */
|
|
49
66
|
function setLast(sessionId, now) {
|
|
50
67
|
try {
|
|
51
68
|
const sid = typeof sessionId === 'string' ? sessionId.trim() : '';
|
|
52
69
|
if (!sid) return;
|
|
53
|
-
|
|
70
|
+
if (pending.some((e) => e.sessionId === sid)) return;
|
|
71
|
+
pending.push({ sessionId: sid, at: typeof now === 'number' ? now : Date.now() });
|
|
72
|
+
pending.sort((a, b) => a.at - b.at);
|
|
73
|
+
while (pending.length > MAX_PENDING) pending.shift();
|
|
74
|
+
// 长轮询:新信号到达,立刻唤醒所有挂着的 /wait。
|
|
75
|
+
wakeWaiters();
|
|
54
76
|
} catch { /* 通知主流程不因此中断 */ }
|
|
55
77
|
}
|
|
56
78
|
|
|
57
|
-
/**
|
|
79
|
+
/**
|
|
80
|
+
* 从 POST 请求体里读 sessionId(/ack 用)。
|
|
81
|
+
*
|
|
82
|
+
* 失败一律返回空串(不让畸形请求把 handler 打挂)。体积设上限 4KB ——
|
|
83
|
+
* 这个接口只需要一个 id,没必要读大 body。
|
|
84
|
+
*/
|
|
85
|
+
function readBodySessionId(req) {
|
|
86
|
+
return new Promise((resolve) => {
|
|
87
|
+
try {
|
|
88
|
+
if (!req || typeof req.on !== 'function') { resolve(''); return; }
|
|
89
|
+
let raw = '';
|
|
90
|
+
let over = false;
|
|
91
|
+
req.on('data', (chunk) => {
|
|
92
|
+
if (over) return;
|
|
93
|
+
raw += chunk;
|
|
94
|
+
if (raw.length > 4096) { over = true; raw = ''; }
|
|
95
|
+
});
|
|
96
|
+
req.on('end', () => {
|
|
97
|
+
if (over) { resolve(''); return; }
|
|
98
|
+
try {
|
|
99
|
+
const j = JSON.parse(raw || '{}');
|
|
100
|
+
const sid = j && typeof j.sessionId === 'string' ? j.sessionId.trim() : '';
|
|
101
|
+
resolve(sid);
|
|
102
|
+
} catch { resolve(''); }
|
|
103
|
+
});
|
|
104
|
+
req.on('error', () => resolve(''));
|
|
105
|
+
} catch { resolve(''); }
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 挂起的 /wait 响应池(长轮询)。
|
|
111
|
+
*
|
|
112
|
+
* 每项 { res, after, timer }:
|
|
113
|
+
* after = 客户端已知的最新 at,只有更新的条目才该唤醒它;
|
|
114
|
+
* timer = 25 秒兜底超时,防连接被代理/宿主悄悄掐断后客户端一直等。
|
|
115
|
+
*
|
|
116
|
+
* ⚠️ 必须在 res 的 close/error 上摘除自己 —— 否则客户端刷新或关页面会留下
|
|
117
|
+
* 死引用,长跑内存只增不减。
|
|
118
|
+
*/
|
|
119
|
+
const waiters = new Set();
|
|
120
|
+
|
|
121
|
+
/** 超时上限:25 秒(低于常见 30 秒空闲超时)。 */
|
|
122
|
+
const WAIT_TIMEOUT_MS = 25_000;
|
|
123
|
+
|
|
124
|
+
/** 结束一个挂起响应并把它从池里摘掉(幂等)。 */
|
|
125
|
+
function settleWaiter(w, code, body) {
|
|
126
|
+
if (!w || w.done) return;
|
|
127
|
+
w.done = true;
|
|
128
|
+
try { clearTimeout(w.timer); } catch { /* ignore */ }
|
|
129
|
+
waiters.delete(w);
|
|
130
|
+
try { w.send(code, body); } catch { /* 客户端可能已断开 */ }
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 有新信号时唤醒**所有**满足条件的挂起响应。
|
|
135
|
+
*
|
|
136
|
+
* 条件:队列里存在 at > w.after 的条目。用"最新的那条"返回,
|
|
137
|
+
* 这样客户端一次就能拿到当前最新的完成(无需为每条都往返一次)。
|
|
138
|
+
*/
|
|
139
|
+
function wakeWaiters() {
|
|
140
|
+
if (!waiters.size) return;
|
|
141
|
+
for (const w of [...waiters]) {
|
|
142
|
+
const newer = pending.filter((e) => e.at > w.after);
|
|
143
|
+
if (!newer.length) continue;
|
|
144
|
+
const latest = newer[newer.length - 1];
|
|
145
|
+
settleWaiter(w, 200, { sessionId: latest.sessionId, at: latest.at, sessions: newer });
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** 插件卸载/热重载:把所有挂起响应正常结束掉,避免残留句柄。 */
|
|
150
|
+
function disposeAllWaiters() {
|
|
151
|
+
for (const w of [...waiters]) settleWaiter(w, 204);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** 只看不取:返回队列里**最早**的一条(单条时即原语义)。 */
|
|
58
155
|
function peek() {
|
|
59
|
-
return
|
|
156
|
+
return pending.length ? pending[0] : null;
|
|
60
157
|
}
|
|
61
158
|
|
|
62
159
|
/**
|
|
@@ -67,7 +164,7 @@ export function createSessionBridge() {
|
|
|
67
164
|
* - `(req, res)` 位置参数风格
|
|
68
165
|
* 统一从第一个参数上取 request/response,兼容两者。
|
|
69
166
|
*/
|
|
70
|
-
function handler(...args) {
|
|
167
|
+
async function handler(...args) {
|
|
71
168
|
const req = args[0] && args[0].request ? args[0].request
|
|
72
169
|
: (args[0] && args[0].req ? args[0].req : args[0]);
|
|
73
170
|
const res = args[0] && args[0].response ? args[0].response
|
|
@@ -107,6 +204,62 @@ export function createSessionBridge() {
|
|
|
107
204
|
const head = rest.replace(/^\/+/, '').split('/')[0];
|
|
108
205
|
|
|
109
206
|
const isConsume = head === 'consume';
|
|
207
|
+
const isWait = head === 'wait';
|
|
208
|
+
const isAck = head === 'ack';
|
|
209
|
+
|
|
210
|
+
// ── /ack:只确认一条(前台跳转后用)────────────────────────────
|
|
211
|
+
//
|
|
212
|
+
// 与 /consume 的区别:consume 清空整个队列("我回来处理完了"),
|
|
213
|
+
// ack 只摘掉指定的那一条("这一条我已经跳过去看了,其余的留着")。
|
|
214
|
+
if (isAck) {
|
|
215
|
+
const method = String((req && req.method) || 'GET').toUpperCase();
|
|
216
|
+
if (method !== 'POST') {
|
|
217
|
+
if (res && typeof res.setHeader === 'function') res.setHeader('allow', 'POST');
|
|
218
|
+
send(405, { error: 'method-not-allowed', allow: 'POST' });
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
const sid = await readBodySessionId(req);
|
|
222
|
+
const before = pending.length;
|
|
223
|
+
if (sid) pending = pending.filter((e) => e.sessionId !== sid);
|
|
224
|
+
send(200, { ok: true, removed: before - pending.length, left: pending.length });
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ── /wait:长轮询(v0.4.63)────────────────────────────────────
|
|
229
|
+
//
|
|
230
|
+
// 客户端带上它已知的最新 at;有更新的就立刻返回,否则挂起。
|
|
231
|
+
// 挂起期间不占 CPU、不发新请求 —— 这就是它比定时轮询好的地方。
|
|
232
|
+
if (isWait) {
|
|
233
|
+
const qs = url.includes('?') ? url.slice(url.indexOf('?') + 1) : '';
|
|
234
|
+
const m = /(?:^|&)after=(\d+)/.exec(qs);
|
|
235
|
+
const after = m ? Number(m[1]) : 0;
|
|
236
|
+
const newer = pending.filter((e) => e.at > after);
|
|
237
|
+
if (newer.length) {
|
|
238
|
+
const latest = newer[newer.length - 1];
|
|
239
|
+
send(200, { sessionId: latest.sessionId, at: latest.at, sessions: newer });
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
// 没有新信号 → 挂起。用 send 的同一个响应对象,保证头部设置一致。
|
|
243
|
+
const w = {
|
|
244
|
+
res,
|
|
245
|
+
after,
|
|
246
|
+
done: false,
|
|
247
|
+
send,
|
|
248
|
+
timer: (() => {
|
|
249
|
+
const t = setTimeout(() => settleWaiter(w, 204), WAIT_TIMEOUT_MS);
|
|
250
|
+
// ⚠️ 必须 unref:否则一个挂着的 /wait 会吊住整个 Node 进程不退出
|
|
251
|
+
// (测试里尤其致命 —— 跑完所有用例后进程还在等)。
|
|
252
|
+
try { if (t && typeof t.unref === 'function') t.unref(); } catch { /* ignore */ }
|
|
253
|
+
return t;
|
|
254
|
+
})(),
|
|
255
|
+
};
|
|
256
|
+
waiters.add(w);
|
|
257
|
+
// 客户端断开(关页面/刷新/网络断)→ 摘除,否则死引用堆积。
|
|
258
|
+
try {
|
|
259
|
+
if (res && typeof res.once === 'function') res.once('close', () => settleWaiter(w, 204));
|
|
260
|
+
} catch { /* ignore */ }
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
110
263
|
|
|
111
264
|
if (isConsume) {
|
|
112
265
|
// ⚠️ 只接受 POST:这个动作会**清除服务端状态**,用 GET 既语义不当,
|
|
@@ -120,19 +273,23 @@ export function createSessionBridge() {
|
|
|
120
273
|
send(405, { error: 'method-not-allowed', allow: 'POST' });
|
|
121
274
|
return;
|
|
122
275
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
276
|
+
// 一次取走整个队列并清空(消费即清,避免重复跳转)。
|
|
277
|
+
// 顶层仍保留 sessionId/at(= 队首)—— 老客户端只读这两个字段,
|
|
278
|
+
// 新客户端优先读 sessions 数组(v0.4.62)。
|
|
279
|
+
const taken = pending;
|
|
280
|
+
pending = [];
|
|
281
|
+
if (!taken.length) { send(204); return; }
|
|
282
|
+
send(200, { sessions: taken, sessionId: taken[0].sessionId, at: taken[0].at });
|
|
127
283
|
return;
|
|
128
284
|
}
|
|
129
|
-
// 默认走 last-session
|
|
130
|
-
|
|
131
|
-
send(
|
|
285
|
+
// 默认走 last-session(只读,不改状态):返回**队首**,保持旧的单条形状。
|
|
286
|
+
const firstPending = pending.length ? pending[0] : null;
|
|
287
|
+
if (!firstPending) { send(204); return; }
|
|
288
|
+
send(200, firstPending);
|
|
132
289
|
} catch {
|
|
133
290
|
send(500, { error: 'internal' });
|
|
134
291
|
}
|
|
135
292
|
}
|
|
136
293
|
|
|
137
|
-
return { handler, setLast, peek };
|
|
294
|
+
return { handler, setLast, peek, disposeAllWaiters };
|
|
138
295
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-notify-say",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.63",
|
|
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",
|
|
@@ -27,30 +27,37 @@ const NOTIFY_CONFIG = join(ROOT, 'tools', 'notify-config.ps1');
|
|
|
27
27
|
|
|
28
28
|
const client = readFileSync(CLIENT, 'utf8');
|
|
29
29
|
|
|
30
|
-
// ── C27: consumeLastSession
|
|
31
|
-
test('C27: consumeLastSession 返回 {sessionId, at}
|
|
30
|
+
// ── C27: consumeLastSession 必须返回**队列**(v0.4.62;元素仍是对象而非裸字符串)──
|
|
31
|
+
test('C27: consumeLastSession 返回 [{sessionId, at}] 队列而非裸字符串', () => {
|
|
32
32
|
const fn = client.match(/function consumeLastSession\(\)[\s\S]*?\n \}/);
|
|
33
33
|
assert.ok(fn, '应能找到 consumeLastSession 定义');
|
|
34
34
|
const body = fn[0];
|
|
35
35
|
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
36
|
+
// v0.4.62:返回值是数组,但每个元素仍必须是 {sessionId, at} 对象。
|
|
37
|
+
// 沿用原防线:元素绝不能是裸字符串(那会让 target.sessionId 恒 undefined,
|
|
38
|
+
// 2026-10-18 真实 bug)。
|
|
39
|
+
assert.match(body, /list\.push\(\{ sessionId: [\s\S]*?at: /,
|
|
40
|
+
'元素必须是 {sessionId, at} 对象;裸字符串会让 target.sessionId 恒为 undefined');
|
|
41
|
+
assert.doesNotMatch(body, /list\.push\(\s*(e\.sessionId|doc\.sessionId)\s*\)/,
|
|
42
|
+
'不得把裸 id 塞进队列');
|
|
43
|
+
|
|
44
|
+
// 必须同时吃新队列与旧单条响应(升级中途两端可能一新一旧)
|
|
45
|
+
assert.match(body, /doc\.sessions/, '要优先读新字段 sessions');
|
|
46
|
+
assert.match(body, /typeof doc\.sessionId === \'string\'/, '旧形状 sessionId 也要吃');
|
|
44
47
|
});
|
|
45
48
|
|
|
46
49
|
// ── C28: 调用方与返回形状必须一致(防单边修改)──────────────────────────
|
|
47
|
-
test('C28:
|
|
48
|
-
const call = client.match(/consumeLastSession\(\)\.then\(function \(
|
|
49
|
-
assert.ok(call, '
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
assert.match(call[0],
|
|
53
|
-
|
|
50
|
+
test('C28: 调用方按队列消费,并按规则挑出 target 再跳', () => {
|
|
51
|
+
const call = client.match(/consumeLastSession\(\)\.then\(function \(list\) \{[\s\S]{0,700}/);
|
|
52
|
+
assert.ok(call, '应能找到调用点,且参数名应为 list(形状变了就得改名,防误用)');
|
|
53
|
+
|
|
54
|
+
// 空值短路必须还在 —— 没有待跳转就不该继续
|
|
55
|
+
assert.match(call[0], /if \(!list \|\| !list\.length\) return/, '应保留空值短路');
|
|
56
|
+
|
|
57
|
+
// v0.4.62:必须经 pickJumpTarget 挑目标,不能直接拿 list 当单个会话用
|
|
58
|
+
assert.match(call[0], /pickJumpTarget\(list, en\.multiJumpRule\)/,
|
|
59
|
+
'必须按 multiJumpRule 挑目标;直接跳 list 会让「多个会话」规则形同不存在');
|
|
60
|
+
assert.match(call[0], /target\.sessionId/, 'shouldJump 要用挑出来的 target');
|
|
54
61
|
});
|
|
55
62
|
|
|
56
63
|
// ── C29: jumpEnabled 必须"读不到配置就不跳" ─────────────────────────────
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* v0.4.63:前台长轮询(/wait)、单条确认(/ack)与客户端环境判据。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么这些断言值得写
|
|
5
|
+
*
|
|
6
|
+
* 这轮改动踩到两个**只有跑起来才会暴露**的坑,都必须锁住:
|
|
7
|
+
*
|
|
8
|
+
* ① `/wait` 的 25 秒兜底定时器没有 unref → 一个挂着的请求会吊住
|
|
9
|
+
* 整个 Node 进程不退出(`npm test` 跑了 10 分钟不止)。
|
|
10
|
+
* ② 客户端在挂载时无条件启动轮询 → `client-bundle.test.mjs` 真实执行
|
|
11
|
+
* factory 时发出了真 fetch + 无限 setTimeout → 同样卡死。
|
|
12
|
+
*
|
|
13
|
+
* 这两条都不是"逻辑写错",而是**生命周期没管住**,静态读代码很难发现。
|
|
14
|
+
*/
|
|
15
|
+
import { test } from 'node:test';
|
|
16
|
+
import assert from 'node:assert/strict';
|
|
17
|
+
import { readFileSync } from 'node:fs';
|
|
18
|
+
import { fileURLToPath } from 'node:url';
|
|
19
|
+
import { dirname, join } from 'node:path';
|
|
20
|
+
|
|
21
|
+
const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..');
|
|
22
|
+
const BRIDGE = readFileSync(join(ROOT, 'lib/session-bridge.js'), 'utf8');
|
|
23
|
+
const CLIENT = readFileSync(join(ROOT, 'lib/client.js'), 'utf8');
|
|
24
|
+
const INDEX = readFileSync(join(ROOT, 'lib/index.js'), 'utf8');
|
|
25
|
+
|
|
26
|
+
// ══ W1-W3:/wait 的生命周期 ═════════════════════════════════════════════
|
|
27
|
+
test('W1: /wait 的兜底定时器必须 unref(否则吊住进程)', () => {
|
|
28
|
+
assert.match(BRIDGE, /WAIT_TIMEOUT_MS/, '必须有超时常量');
|
|
29
|
+
// 从**使用处**(setTimeout 那一行)切片,不能从常量定义处切 ——
|
|
30
|
+
// 常量在文件上方,切片会落空。
|
|
31
|
+
const i = BRIDGE.indexOf('setTimeout(() => settleWaiter(w, 204), WAIT_TIMEOUT_MS)');
|
|
32
|
+
assert.ok(i > 0, '应能找到挂起定时器');
|
|
33
|
+
const seg = BRIDGE.slice(i, i + 500);
|
|
34
|
+
assert.match(seg, /typeof t\.unref === 'function'/, '必须显式 unref');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('W2: 客户端断开时要摘掉挂起项(防死引用堆积)', () => {
|
|
38
|
+
assert.match(BRIDGE, /res\.once\('close', \(\) => settleWaiter\(w, 204\)\)/,
|
|
39
|
+
'必须监听 close 并结算 waiter —— 否则刷新/关页面会留下永远不结束的响应');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test('W3: 插件卸载时结束所有挂起响应', () => {
|
|
43
|
+
assert.match(BRIDGE, /function disposeAllWaiters\(\)/, 'bridge 要提供 disposeAllWaiters');
|
|
44
|
+
assert.match(BRIDGE, /disposeAllWaiters \};|disposeAllWaiters\b.*return/, '必须导出');
|
|
45
|
+
assert.match(INDEX, /bridge\.disposeAllWaiters\(\)/,
|
|
46
|
+
'index.js 的卸载清理里必须调用 —— 否则热重载后旧连接永远等不到响应');
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// ══ W4-W5:/ack 只摘一条 ════════════════════════════════════════════════
|
|
50
|
+
test('W4: /ack 只移除指定会话,不清空队列', () => {
|
|
51
|
+
const seg = BRIDGE.slice(BRIDGE.indexOf("if (isAck)"), BRIDGE.indexOf("if (isWait)"));
|
|
52
|
+
assert.match(seg, /pending\.filter\(\(e\) => e\.sessionId !== sid\)/,
|
|
53
|
+
'必须只过滤掉那一条;像 /consume 那样整体清空会让你切回来时丢掉其余的');
|
|
54
|
+
assert.doesNotMatch(seg, /pending = \[\];/, 'ack 不得清空整个队列');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
test('W5: /ack 是写操作(要进 isWriteRoute,否则绕过 Origin 检查)', () => {
|
|
58
|
+
assert.match(INDEX, /\|\| head === 'ack'/, '/ack 会改队列,必须按写操作做来源检查');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// ══ W6-W7:客户端的运行前提 ═════════════════════════════════════════════
|
|
62
|
+
test('W6: 非浏览器环境不得启动轮询(含 node --test 直接执行 factory)', () => {
|
|
63
|
+
assert.match(CLIENT, /function canPollForeground\(\)/, '必须有环境判据');
|
|
64
|
+
const seg = CLIENT.slice(CLIENT.indexOf('function canPollForeground()'), CLIENT.indexOf('function startForegroundPoll'));
|
|
65
|
+
assert.match(seg, /typeof document === 'undefined'/, '要检查 document');
|
|
66
|
+
assert.match(seg, /typeof fetch !== 'function'/, '要检查 fetch');
|
|
67
|
+
assert.match(seg, /typeof AbortController !== 'function'/, '要检查 AbortController(长轮询要能中止)');
|
|
68
|
+
assert.match(CLIENT, /if \(!canPollForeground\(\)\) return/,
|
|
69
|
+
'startForegroundPoll 必须先过这道判据,否则测试里会发真请求');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test('W7: 循环有代际校验(stop 之后不能再发请求)', () => {
|
|
73
|
+
assert.match(CLIENT, /var myGen = pollGen/, '要记录代际');
|
|
74
|
+
const n = (CLIENT.match(/myGen !== pollGen/g) || []).length;
|
|
75
|
+
assert.ok(n >= 3, '至少三处代际校验(入口/返回后/失败后),实际 ' + n);
|
|
76
|
+
assert.match(CLIENT, /pollGen \+= 1/, 'stop 时递增代际');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// ══ W8-W9:配置缓存与生效 ═══════════════════════════════════════════════
|
|
80
|
+
test('W8: 轮询不每轮打 /config(用缓存)', () => {
|
|
81
|
+
assert.match(CLIENT, /var enCache = null/, '要有配置缓存');
|
|
82
|
+
assert.match(CLIENT, /var ready = enCache \? Promise\.resolve\(enCache\) : refreshJumpConfig\(\)/,
|
|
83
|
+
'有缓存就用缓存,不能每轮都发 /config');
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('W9: 开关关着时定时探活,打开后无需刷新页面', () => {
|
|
87
|
+
assert.match(CLIENT, /IDLE_RECHECK_MS/, '要有探活间隔');
|
|
88
|
+
const seg = CLIENT.slice(CLIENT.indexOf('if (!en || !en.jumpWhenForeground)'), CLIENT.indexOf('var hidden =', CLIENT.indexOf('if (!en || !en.jumpWhenForeground)')));
|
|
89
|
+
assert.match(seg, /setTimeout/, '关着时必须定时回来看,不能直接 return 掉');
|
|
90
|
+
assert.match(CLIENT, /dsh-notify-say:config-changed/,
|
|
91
|
+
'GUI 保存后要广播事件作废缓存,否则改了开关要刷新页面才生效');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// ══ W10:前后台两条路径不重复跳 ═════════════════════════════════════════
|
|
95
|
+
test('W10: 前台跳转后 ack,避免切回来再跳一次', () => {
|
|
96
|
+
const seg = CLIENT.slice(CLIENT.indexOf('if (doc && doc.sessionId)'), CLIENT.indexOf('if (doc && doc.sessionId)') + 700);
|
|
97
|
+
assert.match(seg, /ackSession\(doc\.sessionId\)/, '前台跳完必须 ack');
|
|
98
|
+
assert.match(seg, /at > lastSeenAt/, '要靠 at 去重,同一条不能反复跳');
|
|
99
|
+
assert.match(seg, /lastSeenAt = at/, '跳过后要推进 lastSeenAt');
|
|
100
|
+
});
|