dsh-reminder-notify 0.14.0 → 0.16.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/README.md +25 -0
- package/client.js +142 -1
- package/cordis.patch.yml +1 -0
- package/docs/PUBLISH.md +2 -2
- package/docs/detail.en.md +1 -0
- package/docs/detail.zh.md +1 -0
- package/index.js +229 -2
- package/package.json +1 -1
- package/publish/index.json +2 -2
package/README.md
CHANGED
|
@@ -85,3 +85,28 @@ dsh plugin --profile desktop add link:<本目录绝对路径>
|
|
|
85
85
|
- 仅 Windows(`os: ["win32"]`):卡片宿主是 WinForms。
|
|
86
86
|
- 每次弹卡片要起一个 PowerShell 进程,`Add-Type` 编译约 0.6 s 才出现(有轻微延迟,播放与动画照旧)。
|
|
87
87
|
- 卡片随 DSH 宿主进程存活;DSH 关掉时正在显示的卡片也会一起消失。
|
|
88
|
+
|
|
89
|
+
## 给别的插件用(0.15 起)
|
|
90
|
+
|
|
91
|
+
插件把整套提醒能力挂在宿主的一个服务上,别的插件不用自己起 PowerShell、也不用重复写排队与免打扰逻辑:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
const reminder = ctx.get('reminder') // 服务名固定为 reminder
|
|
95
|
+
await reminder.notify('decide', { message: '有个改动等你点头', seconds: 30 })
|
|
96
|
+
await reminder.digest({ force: true }) // 忽略「今天已发过」,立刻发一条日报
|
|
97
|
+
reminder.stats() // { total, today, byKind, byAction, last7 }
|
|
98
|
+
reminder.config() // 当前生效配置的只读副本
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
`notify()` 的 `kind` 与 `notify_user` 一致(`done` / `decide` / `raw`),其余参数同工具参数;调用同样受冷却、排队、合并、免打扰、历史记录约束。
|
|
102
|
+
|
|
103
|
+
## 出问题先自检(0.16 起)
|
|
104
|
+
|
|
105
|
+
设置面板最下面的「工程与可靠性:自检」里有两个按钮:
|
|
106
|
+
|
|
107
|
+
- **运行自检**:逐条检查系统 / PowerShell / 宿主脚本 / 提示音目录与文件 / 历史文件可写 / 面板路由 / `notify_user` / 配置可写 / 余额接口 / 宿主服务 API / 最近一次宿主退出码,✗ 会带上具体路径或原因。
|
|
108
|
+
- **弹样卡**:真的弹一张 12 秒的卡,能看到就说明宿主链路正常(比读日志快)。
|
|
109
|
+
|
|
110
|
+
宿主兜底:卡片该写结果文件却没写(被杀、被策略拦下、非零退出)时,会记一条 `host-failed` 到历史,并在 0.8 秒后**静音重试一次**(`retryHostOnce`,默认开);最近 5 次失败原因显示在自检块里。
|
|
111
|
+
|
|
112
|
+
CI 在 `.github/workflows/ci.yml`:Ubuntu 与 Windows 各跑 node 20/22,做语法检查 + 两套无窗口自检(真弹卡的自检只在本机跑,CI 没有交互桌面)。
|
package/client.js
CHANGED
|
@@ -22,6 +22,7 @@ window.__ModuleLoader__.load({
|
|
|
22
22
|
const BALANCE_ROUTE = '/dsh-reminder-notify/balance'
|
|
23
23
|
// 0.11 新增:提醒历史(GET 拿统计+最近记录,POST { mode: 'clear' } 清空)。
|
|
24
24
|
const HISTORY_ROUTE = '/dsh-reminder-notify/history'
|
|
25
|
+
const DOCTOR_ROUTE = '/dsh-reminder-notify/doctor'
|
|
25
26
|
const DIGEST_ROUTE = '/dsh-reminder-notify/digest'
|
|
26
27
|
// 下拉按扩展名分组:wav 一组、mp3 一组……组内按文件名排序,一眼能看出格式。
|
|
27
28
|
function groupAudioFiles(files) {
|
|
@@ -263,6 +264,32 @@ window.__ModuleLoader__.load({
|
|
|
263
264
|
tryShown: '已经弹出一张样卡,20 秒后自动关闭。',
|
|
264
265
|
tryQuiet: '现在在免打扰时段,样卡被拦住了(正常行为)。',
|
|
265
266
|
tryFailed: '没能弹出示意卡:',
|
|
267
|
+
retryHostOnce: '宿主失败时重试一次',
|
|
268
|
+
retryHostOnceHint: '第一次没画出卡片(比如被系统策略拦下)时,静音再弹一次。',
|
|
269
|
+
groupDoctor: '工程与可靠性:自检',
|
|
270
|
+
doctorHint: '「运行自检」逐条检查宿主路径、提示音、历史可写与各项服务;「弹样卡」会真的弹一张,验证宿主能不能画。',
|
|
271
|
+
doctorNotRun: '还没运行过自检。',
|
|
272
|
+
doctorRun: '运行自检',
|
|
273
|
+
doctorRunning: '检查中…',
|
|
274
|
+
doctorAllOk: '全部通过。',
|
|
275
|
+
doctorHasFailures: '有项目没通过,见下面的 ✗。',
|
|
276
|
+
doctorProbe: '弹样卡',
|
|
277
|
+
doctorProbeOk: '样卡已弹出,能看到就说明宿主正常。',
|
|
278
|
+
doctorProbeFailed: '自检失败:',
|
|
279
|
+
doctorLastExit: '最近退出码',
|
|
280
|
+
doctorFailures: '最近失败次数',
|
|
281
|
+
check_platform: '系统',
|
|
282
|
+
check_powershell: 'PowerShell',
|
|
283
|
+
check_host_script: '宿主脚本',
|
|
284
|
+
check_audio_dir: '提示音目录',
|
|
285
|
+
check_audio_files: '提示音文件',
|
|
286
|
+
check_history_writable: '历史文件可写',
|
|
287
|
+
check_routes: '面板路由',
|
|
288
|
+
check_tools: 'notify_user',
|
|
289
|
+
check_config_editor: '配置可写',
|
|
290
|
+
check_account: '余额接口',
|
|
291
|
+
check_service_api: '宿主服务',
|
|
292
|
+
check_host_exit: '宿主退出码',
|
|
266
293
|
speakEnabled: '朗读提醒内容',
|
|
267
294
|
buttonsEnabled: '卡片显示按钮',
|
|
268
295
|
snoozeMinutes: '「稍后提醒」延后分钟',
|
|
@@ -373,6 +400,32 @@ window.__ModuleLoader__.load({
|
|
|
373
400
|
tryShown: 'A sample card is on screen; it closes itself in 20 seconds.',
|
|
374
401
|
tryQuiet: 'Quiet hours are active, so the sample was held back (expected).',
|
|
375
402
|
tryFailed: 'Could not show the sample: ',
|
|
403
|
+
retryHostOnce: 'Retry once when the host fails',
|
|
404
|
+
retryHostOnceHint: 'If the first card never drew (blocked by policy, for example), try once more with sound off.',
|
|
405
|
+
groupDoctor: 'Reliability: self-check',
|
|
406
|
+
doctorHint: 'Run the self-check to verify the host path, audio files, writable history and every service; "probe" pops a real card to prove the host can draw.',
|
|
407
|
+
doctorNotRun: 'The self-check has not run yet.',
|
|
408
|
+
doctorRun: 'Run self-check',
|
|
409
|
+
doctorRunning: 'Checking…',
|
|
410
|
+
doctorAllOk: 'Everything passed.',
|
|
411
|
+
doctorHasFailures: 'Some checks failed — see the ✗ lines below.',
|
|
412
|
+
doctorProbe: 'Probe a card',
|
|
413
|
+
doctorProbeOk: 'A probe card is on screen; if you can see it, the host is fine.',
|
|
414
|
+
doctorProbeFailed: 'Self-check failed: ',
|
|
415
|
+
doctorLastExit: 'last exit code',
|
|
416
|
+
doctorFailures: 'recent failures',
|
|
417
|
+
check_platform: 'platform',
|
|
418
|
+
check_powershell: 'PowerShell',
|
|
419
|
+
check_host_script: 'host script',
|
|
420
|
+
check_audio_dir: 'audio directory',
|
|
421
|
+
check_audio_files: 'audio files',
|
|
422
|
+
check_history_writable: 'history file writable',
|
|
423
|
+
check_routes: 'panel routes',
|
|
424
|
+
check_tools: 'notify_user',
|
|
425
|
+
check_config_editor: 'config writable',
|
|
426
|
+
check_account: 'balance API',
|
|
427
|
+
check_service_api: 'host service API',
|
|
428
|
+
check_host_exit: 'host exit code',
|
|
376
429
|
speakEnabled: 'Speak the reminder',
|
|
377
430
|
buttonsEnabled: 'Show card buttons',
|
|
378
431
|
snoozeMinutes: 'Snooze minutes',
|
|
@@ -513,6 +566,7 @@ window.__ModuleLoader__.load({
|
|
|
513
566
|
{ key: 'position', group: 'basic', kind: 'select', options: ['br', 'center', 'tr'], optionLabels: ['positionBr', 'positionCenter', 'positionTr'] },
|
|
514
567
|
{ key: 'sound', group: 'basic', kind: 'boolean' },
|
|
515
568
|
{ key: 'cooldownMs', group: 'host', kind: 'number' },
|
|
569
|
+
{ key: 'retryHostOnce', group: 'host', kind: 'boolean' },
|
|
516
570
|
{ key: 'doneAudio', group: 'audio', kind: 'text' },
|
|
517
571
|
{ key: 'decideAudio', group: 'audio', kind: 'text' },
|
|
518
572
|
{ key: 'rawAudio', group: 'audio', kind: 'text' },
|
|
@@ -575,6 +629,7 @@ window.__ModuleLoader__.load({
|
|
|
575
629
|
seconds: 'secondsHint',
|
|
576
630
|
sound: 'soundHint',
|
|
577
631
|
cooldownMs: 'cooldownHint',
|
|
632
|
+
retryHostOnce: 'retryHostOnceHint',
|
|
578
633
|
doneAudio: 'audioHint',
|
|
579
634
|
audioDir: 'audioDirHint',
|
|
580
635
|
cardBackground: 'cardBackgroundHint',
|
|
@@ -635,6 +690,7 @@ window.__ModuleLoader__.load({
|
|
|
635
690
|
{ id: 'queue', label: 'groupQueue', groups: ['queue'] },
|
|
636
691
|
{ id: 'misc', label: 'groupAdvanced', groups: ['digest', 'history', 'balance', 'host'] },
|
|
637
692
|
{ id: 'help', label: 'groupHelp', groups: [] },
|
|
693
|
+
{ id: 'doctor', label: 'groupDoctor', groups: [] },
|
|
638
694
|
]
|
|
639
695
|
|
|
640
696
|
// 0.14:说明收进面板里,按条目折叠。
|
|
@@ -676,6 +732,8 @@ window.__ModuleLoader__.load({
|
|
|
676
732
|
const [openBlocks, setOpenBlocks] = React.useState({})
|
|
677
733
|
const [tryBusy, setTryBusy] = React.useState(null)
|
|
678
734
|
const [helpOpen, setHelpOpen] = React.useState({})
|
|
735
|
+
const [doctor, setDoctor] = React.useState(null)
|
|
736
|
+
const [doctorBusy, setDoctorBusy] = React.useState(false)
|
|
679
737
|
|
|
680
738
|
React.useEffect(() => {
|
|
681
739
|
if (typeof fetch !== 'function') {
|
|
@@ -878,6 +936,52 @@ window.__ModuleLoader__.load({
|
|
|
878
936
|
})
|
|
879
937
|
}
|
|
880
938
|
|
|
939
|
+
// 0.16 自检:把宿主路径、提示音、历史可写、各项服务逐条查一遍;顺带能弹一张样卡验证宿主。
|
|
940
|
+
function runDoctor(mode) {
|
|
941
|
+
if (doctorBusy) return
|
|
942
|
+
setDoctorBusy(true)
|
|
943
|
+
setMessage(null)
|
|
944
|
+
Promise.resolve()
|
|
945
|
+
.then(() => {
|
|
946
|
+
const init = mode === 'probe'
|
|
947
|
+
? {
|
|
948
|
+
method: 'POST',
|
|
949
|
+
headers: { 'content-type': 'application/json', accept: 'application/json' },
|
|
950
|
+
body: JSON.stringify({ mode: 'probe' }),
|
|
951
|
+
}
|
|
952
|
+
: { headers: { accept: 'application/json' } }
|
|
953
|
+
return fetch(DOCTOR_ROUTE, init)
|
|
954
|
+
})
|
|
955
|
+
.then((response) => {
|
|
956
|
+
const status = response ? response.status : 0
|
|
957
|
+
const body = response && typeof response.json === 'function' ? response.json() : Promise.resolve(null)
|
|
958
|
+
return Promise.resolve(body)
|
|
959
|
+
.catch(() => null)
|
|
960
|
+
.then((data) => {
|
|
961
|
+
if (response && response.ok && data && data.ok !== false) return data
|
|
962
|
+
const detail = data && data.error ? data.error : 'HTTP ' + status
|
|
963
|
+
throw new Error(detail)
|
|
964
|
+
})
|
|
965
|
+
})
|
|
966
|
+
.then((data) => {
|
|
967
|
+
setDoctorBusy(false)
|
|
968
|
+
if (data && data.report) setDoctor(data.report)
|
|
969
|
+
const label = typeof t === 'function' ? t : (key) => key
|
|
970
|
+
if (mode === 'probe') {
|
|
971
|
+
const card = data && data.card ? data.card : {}
|
|
972
|
+
if (card.ok === false) {
|
|
973
|
+
setMessage({ ok: false, text: label('doctorProbeFailed') + String(card.detail || card.error || '') })
|
|
974
|
+
return
|
|
975
|
+
}
|
|
976
|
+
setMessage({ ok: true, text: label('doctorProbeOk') })
|
|
977
|
+
}
|
|
978
|
+
})
|
|
979
|
+
.catch((error) => {
|
|
980
|
+
setDoctorBusy(false)
|
|
981
|
+
const prefix = typeof t === 'function' ? t('doctorProbeFailed') : 'self-check failed: '
|
|
982
|
+
setMessage({ ok: false, text: prefix + String((error && error.message) || error) })
|
|
983
|
+
})
|
|
984
|
+
}
|
|
881
985
|
// 0.13「试试看」:让宿主真的弹一张样卡(POST /preview,mode=card),
|
|
882
986
|
// 比读说明书更快知道每一档长什么样。样卡只停 20 秒,不改任何配置。
|
|
883
987
|
function tryCard(kind) {
|
|
@@ -1367,7 +1471,44 @@ window.__ModuleLoader__.load({
|
|
|
1367
1471
|
|
|
1368
1472
|
const advanced = ADV_BLOCKS.map((block) => {
|
|
1369
1473
|
const open = openBlocks[block.id] === true
|
|
1370
|
-
const children = block.id === '
|
|
1474
|
+
const children = block.id === 'doctor'
|
|
1475
|
+
? [
|
|
1476
|
+
h(
|
|
1477
|
+
'div',
|
|
1478
|
+
{ className: 'drn-help', key: 'doctor-panel' },
|
|
1479
|
+
h('div', { className: 'drn-head-intro' }, label('doctorHint')),
|
|
1480
|
+
doctor === null
|
|
1481
|
+
? h('div', { className: 'drn-hint' }, label('doctorNotRun'))
|
|
1482
|
+
: h(
|
|
1483
|
+
'div',
|
|
1484
|
+
{ className: 'drn-help' },
|
|
1485
|
+
h('div', { className: 'drn-hint' }, doctor.ok === true ? label('doctorAllOk') : label('doctorHasFailures')),
|
|
1486
|
+
(doctor.checks || []).map((check, index) =>
|
|
1487
|
+
h(
|
|
1488
|
+
'div',
|
|
1489
|
+
{ className: 'drn-hint', key: 'check-' + index },
|
|
1490
|
+
(check.ok ? '✓ ' : '✗ ') + label('check_' + String(check.id || '').replace(/-/g, '_')) + ' · ' + String(check.detail || ''),
|
|
1491
|
+
),
|
|
1492
|
+
),
|
|
1493
|
+
h('div', { className: 'drn-hint' }, label('doctorLastExit') + ' ' + String(doctor.host && doctor.host.lastExit === null ? '—' : doctor.host && doctor.host.lastExit) + ' · ' + label('doctorFailures') + ' ' + String(doctor.host ? doctor.host.failureCount : 0)),
|
|
1494
|
+
),
|
|
1495
|
+
h(
|
|
1496
|
+
'div',
|
|
1497
|
+
{ className: 'drn-pick' },
|
|
1498
|
+
h(
|
|
1499
|
+
'button',
|
|
1500
|
+
{ type: 'button', className: 'drn-btn drn-digest-btn', disabled: doctorBusy, onClick: () => runDoctor('check') },
|
|
1501
|
+
label(doctorBusy ? 'doctorRunning' : 'doctorRun'),
|
|
1502
|
+
),
|
|
1503
|
+
h(
|
|
1504
|
+
'button',
|
|
1505
|
+
{ type: 'button', className: 'drn-btn drn-digest-btn', disabled: doctorBusy, onClick: () => runDoctor('probe') },
|
|
1506
|
+
label('doctorProbe'),
|
|
1507
|
+
),
|
|
1508
|
+
),
|
|
1509
|
+
),
|
|
1510
|
+
]
|
|
1511
|
+
: block.id === 'help'
|
|
1371
1512
|
? [
|
|
1372
1513
|
h(
|
|
1373
1514
|
'div',
|
package/cordis.patch.yml
CHANGED
package/docs/PUBLISH.md
CHANGED
|
@@ -4,7 +4,7 @@ DSH 的「插件市场 → 发现」页读的是**索引 URL**:一份 JSON(
|
|
|
4
4
|
索引本身不托管代码,只给出版本从哪儿装(`git` 仓库 + 固定提交,或 npm 包名)。
|
|
5
5
|
本目录的 `index.json` 由 `scripts/build-index.mjs` 生成,**不要手改**。
|
|
6
6
|
|
|
7
|
-
## 已发布状态(2026-11-
|
|
7
|
+
## 已发布状态(2026-11-21 更新)
|
|
8
8
|
|
|
9
9
|
| 项目 | 值 |
|
|
10
10
|
| --- | --- |
|
|
@@ -12,7 +12,7 @@ DSH 的「插件市场 → 发现」页读的是**索引 URL**:一份 JSON(
|
|
|
12
12
|
| 索引地址(他人添加这个) | `https://jiangshuaiye1203.github.io/dsh-reminder-notify/publish/index.json` |
|
|
13
13
|
| 站点首页 | https://jiangshuaiye1203.github.io/dsh-reminder-notify/ |
|
|
14
14
|
| npm 包 | https://www.npmjs.com/package/dsh-reminder-notify |
|
|
15
|
-
| 当前版本 | **0.
|
|
15
|
+
| 当前版本 | **0.15.0**,装源为 **npm**(`dsh-reminder-notify@0.15.0`),不再是 git 固定提交 |
|
|
16
16
|
| Pages | Deploy from a branch,`main` / `(root)` |
|
|
17
17
|
|
|
18
18
|
发新版(npm 源):改代码 → 升 `package.json` 版本 → `scripts\publish-npm.cmd` 发布到 npm →
|
package/docs/detail.en.md
CHANGED
|
@@ -10,3 +10,4 @@ Cards are drawn by the bundled `host/ReminderHost.ps1` with the built-in PowerSh
|
|
|
10
10
|
- **Want to see it now**: press one of the three "try it" buttons; a sample card closes itself after 20 seconds.
|
|
11
11
|
- **Want it on your phone**: open push under "Digest & push", pick the format, paste your own webhook URL (WeCom bot or Server Chan), then press "Send one now".
|
|
12
12
|
- **Everything else**: folded into "How it works (expand to read)" inside the settings panel below, one topic at a time.
|
|
13
|
+
- **For other plugins**: this plugin exposes a `reminder` service (`notify` / `digest` / `stats` / `config`); see the README.
|
package/docs/detail.zh.md
CHANGED
package/index.js
CHANGED
|
@@ -101,6 +101,7 @@ export const Config = z === null ? undefined : z.object({
|
|
|
101
101
|
pushFormat: z.union(['wecom', 'serverchan', 'json', 'text']).default('wecom').description('推送格式:wecom 企业微信机器人、serverchan Server酱、json 通用 JSON、text 纯文本').volatile(),
|
|
102
102
|
pushDecide: z.boolean().default(true).description('「需要你来决定」档位也推到手机').volatile(),
|
|
103
103
|
pushDigest: z.boolean().default(true).description('日报同时推到手机').volatile(),
|
|
104
|
+
retryHostOnce: z.boolean().default(true).description('宿主第一次没画出卡片时,自动静音重试一次').volatile(),
|
|
104
105
|
dryRun: z.boolean().default(false).description('调试用:只试算参数、不真的弹窗(自检脚本会打开它)').volatile(),
|
|
105
106
|
})
|
|
106
107
|
|
|
@@ -241,6 +242,7 @@ function settingsOf(config) {
|
|
|
241
242
|
pushFormat: ['wecom', 'serverchan', 'json', 'text'].includes(c.pushFormat) ? c.pushFormat : 'wecom',
|
|
242
243
|
pushDecide: c.pushDecide !== false,
|
|
243
244
|
pushDigest: c.pushDigest !== false,
|
|
245
|
+
retryHostOnce: c.retryHostOnce !== false,
|
|
244
246
|
dryRun: c.dryRun === true,
|
|
245
247
|
}
|
|
246
248
|
}
|
|
@@ -523,11 +525,14 @@ async function showItem(item) {
|
|
|
523
525
|
queueLeft,
|
|
524
526
|
})
|
|
525
527
|
let action = 'unknown'
|
|
528
|
+
let exitCode = null
|
|
529
|
+
let hostError
|
|
526
530
|
try {
|
|
527
531
|
const { exited } = await launch(settings.powershellPath, argv, PACKAGE_ROOT)
|
|
528
|
-
await exited
|
|
529
|
-
} catch {
|
|
532
|
+
exitCode = await exited
|
|
533
|
+
} catch (error) {
|
|
530
534
|
// spawn 失败就当作没弹出来,记录一条便于排查
|
|
535
|
+
hostError = String(error?.message ?? error)
|
|
531
536
|
}
|
|
532
537
|
try {
|
|
533
538
|
const parsed = JSON.parse(fs.readFileSync(resultFile, 'utf8'))
|
|
@@ -545,12 +550,30 @@ async function showItem(item) {
|
|
|
545
550
|
} catch {
|
|
546
551
|
// 没有结果文件(比如宿主被杀)时按 unknown 记
|
|
547
552
|
}
|
|
553
|
+
// 0.16:宿主明明该写结果却没写(或非零退出)就记一次 host-failed,并在下面自动重试一次。
|
|
554
|
+
if (hostError !== undefined) {
|
|
555
|
+
action = 'host-failed'
|
|
556
|
+
noteHostRun(exitCode, action, hostError)
|
|
557
|
+
} else if (action === 'unknown' && exitCode !== null && exitCode !== 0) {
|
|
558
|
+
action = 'host-failed'
|
|
559
|
+
noteHostRun(exitCode, action, 'host exited before writing a result')
|
|
560
|
+
} else {
|
|
561
|
+
noteHostRun(exitCode, action)
|
|
562
|
+
}
|
|
548
563
|
try {
|
|
549
564
|
fs.unlinkSync(resultFile)
|
|
550
565
|
} catch {
|
|
551
566
|
// 清不掉就算了,文件名带 pid + 时间戳不会互相覆盖
|
|
552
567
|
}
|
|
553
568
|
item.action = action
|
|
569
|
+
if (action === 'host-failed' && settings.retryHostOnce && item.retried !== true) {
|
|
570
|
+
// 兜底:静音重试一次(宿主偶发被系统策略拦下时,第二次通常能画出来)。
|
|
571
|
+
const retry = setTimeout(() => {
|
|
572
|
+
queue.unshift({ ...item, retried: true, count: item.count ?? 1, holdUntil: 0, createdAt: Date.now() })
|
|
573
|
+
pump()
|
|
574
|
+
}, 800)
|
|
575
|
+
if (typeof retry.unref === 'function') retry.unref()
|
|
576
|
+
}
|
|
554
577
|
appendHistory(settings, {
|
|
555
578
|
at: Date.now(),
|
|
556
579
|
kind: item.kind,
|
|
@@ -1532,6 +1555,168 @@ function registerDigestRoute(ctx, getSettings) {
|
|
|
1532
1555
|
}, 'dsh-reminder-notify: digest route')
|
|
1533
1556
|
}
|
|
1534
1557
|
|
|
1558
|
+
/** 宿主健康:最近一次退出码 / 结果 / 失败原因,供自诊断面板与 doctor 路由读取。 */
|
|
1559
|
+
const hostHealth = { lastExit: null, lastAction: null, lastAt: 0, lastError: '', failures: [] }
|
|
1560
|
+
|
|
1561
|
+
function noteHostRun(exitCode, action, error) {
|
|
1562
|
+
hostHealth.lastExit = exitCode
|
|
1563
|
+
hostHealth.lastAction = action
|
|
1564
|
+
hostHealth.lastAt = Date.now()
|
|
1565
|
+
hostHealth.lastError = error === undefined ? '' : String(error)
|
|
1566
|
+
if (error !== undefined || action === 'host-failed') {
|
|
1567
|
+
hostHealth.failures.push({
|
|
1568
|
+
at: hostHealth.lastAt,
|
|
1569
|
+
exit: exitCode,
|
|
1570
|
+
action,
|
|
1571
|
+
error: hostHealth.lastError,
|
|
1572
|
+
})
|
|
1573
|
+
if (hostHealth.failures.length > 10) hostHealth.failures.shift()
|
|
1574
|
+
}
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
/** 在 PATH 里找一个可执行文件(Windows 上补 .exe),找不到返回 null。 */
|
|
1578
|
+
function resolveOnPath(name) {
|
|
1579
|
+
if (typeof name !== 'string' || name.trim() === '') return null
|
|
1580
|
+
const exts = process.platform === 'win32' ? ['', '.exe', '.cmd', '.bat'] : ['']
|
|
1581
|
+
const dirs = String(process.env.PATH ?? '').split(path.delimiter).filter((dir) => dir !== '')
|
|
1582
|
+
for (const dir of dirs) {
|
|
1583
|
+
for (const ext of exts) {
|
|
1584
|
+
const candidate = path.join(dir, name + ext)
|
|
1585
|
+
try {
|
|
1586
|
+
if (fs.existsSync(candidate)) return candidate
|
|
1587
|
+
} catch {
|
|
1588
|
+
// 无权访问的目录跳过
|
|
1589
|
+
}
|
|
1590
|
+
}
|
|
1591
|
+
}
|
|
1592
|
+
return null
|
|
1593
|
+
}
|
|
1594
|
+
|
|
1595
|
+
/**
|
|
1596
|
+
* 0.16 自诊断:把「要什么、在哪、通不通」逐条列出来。
|
|
1597
|
+
* 只读,不弹卡、不改配置;面板的「运行自检」和 doctor 路由都用它。
|
|
1598
|
+
*/
|
|
1599
|
+
export function doctorReport(ctx, getSettings) {
|
|
1600
|
+
const settings = getSettings()
|
|
1601
|
+
const checks = []
|
|
1602
|
+
const add = (id, ok, detail) => checks.push({ id, ok: ok === true, detail: String(detail ?? '') })
|
|
1603
|
+
|
|
1604
|
+
add('platform', process.platform === 'win32', process.platform)
|
|
1605
|
+
|
|
1606
|
+
const psName = str(settings.powershellPath, 'powershell.exe')
|
|
1607
|
+
const looksLikePath = psName.includes('/') || psName.includes('\\')
|
|
1608
|
+
const psPath = looksLikePath ? path.resolve(psName) : resolveOnPath(psName)
|
|
1609
|
+
add('powershell', psPath !== null && psPath !== undefined && fs.existsSync(psPath), psPath ?? `找不到 ${psName}`)
|
|
1610
|
+
|
|
1611
|
+
const scriptPath = path.resolve(str(settings.scriptPath, BUNDLED_HOST_SCRIPT))
|
|
1612
|
+
add('host-script', fs.existsSync(scriptPath), scriptPath)
|
|
1613
|
+
|
|
1614
|
+
const audioDir = path.resolve(str(settings.audioDir, BUNDLED_AUDIO_DIR))
|
|
1615
|
+
const doneAudio = str(settings.doneAudio, '')
|
|
1616
|
+
const decideAudio = str(settings.decideAudio, '')
|
|
1617
|
+
const audioOk = fs.existsSync(audioDir)
|
|
1618
|
+
add('audio-dir', audioOk, audioDir)
|
|
1619
|
+
if (audioOk) {
|
|
1620
|
+
const missing = [doneAudio, decideAudio]
|
|
1621
|
+
.filter((file) => file !== '')
|
|
1622
|
+
.filter((file) => !fs.existsSync(path.join(audioDir, file)))
|
|
1623
|
+
add('audio-files', missing.length === 0, missing.length === 0 ? '两个提示音都在' : '缺少 ' + missing.join('、'))
|
|
1624
|
+
}
|
|
1625
|
+
|
|
1626
|
+
// 调用方可能只给半份 settings(自检、别的插件),这里兜一层,别让诊断自己崩。
|
|
1627
|
+
let histFile = ''
|
|
1628
|
+
try {
|
|
1629
|
+
histFile = historyPathOf(settings)
|
|
1630
|
+
} catch {
|
|
1631
|
+
histFile = path.join(os.homedir(), '.dsh', 'dsh-reminder-notify', 'history.json')
|
|
1632
|
+
}
|
|
1633
|
+
let writable = false
|
|
1634
|
+
try {
|
|
1635
|
+
fs.mkdirSync(path.dirname(histFile), { recursive: true })
|
|
1636
|
+
fs.accessSync(path.dirname(histFile), fs.constants.W_OK)
|
|
1637
|
+
writable = true
|
|
1638
|
+
} catch {
|
|
1639
|
+
writable = false
|
|
1640
|
+
}
|
|
1641
|
+
add('history-writable', writable, histFile)
|
|
1642
|
+
|
|
1643
|
+
const service = (name) => (typeof ctx.get === 'function' ? ctx.get(name) : undefined)
|
|
1644
|
+
const web = service('webServer')
|
|
1645
|
+
add('routes', web !== undefined && web !== null && typeof web.register === 'function', web === undefined ? 'webServer 不可用' : 'webServer 可用')
|
|
1646
|
+
const tools = service('tools')
|
|
1647
|
+
add('tools', tools !== undefined && tools !== null && typeof tools.register === 'function', tools === undefined ? 'tools 不可用' : 'notify_user 可注册')
|
|
1648
|
+
add('config-editor', service('configEditor') !== undefined, service('configEditor') === undefined ? '面板只能读(旧宿主)' : '可写')
|
|
1649
|
+
add('account', service('deepseekAccount') !== undefined, service('deepseekAccount') === undefined ? '读不到余额(不影响卡片)' : '可读余额')
|
|
1650
|
+
add('service-api', typeof ctx.provide === 'function', typeof ctx.provide === 'function' ? "可 provide('reminder')" : '旧宿主没有 ctx.provide')
|
|
1651
|
+
|
|
1652
|
+
const lastExit = hostHealth.lastExit
|
|
1653
|
+
const hostOk = lastExit === null || lastExit === 0
|
|
1654
|
+
add('host-exit', hostOk, lastExit === null ? '还没有弹过卡片' : `最近一次退出码 ${lastExit}`)
|
|
1655
|
+
|
|
1656
|
+
return {
|
|
1657
|
+
ok: checks.every((check) => check.ok),
|
|
1658
|
+
version: packageVersion(),
|
|
1659
|
+
platform: process.platform,
|
|
1660
|
+
node: process.version,
|
|
1661
|
+
paths: { powershell: psPath ?? null, hostScript: scriptPath, audioDir, history: histFile },
|
|
1662
|
+
host: {
|
|
1663
|
+
lastExit: hostHealth.lastExit,
|
|
1664
|
+
lastAction: hostHealth.lastAction,
|
|
1665
|
+
lastAt: hostHealth.lastAt,
|
|
1666
|
+
lastError: hostHealth.lastError,
|
|
1667
|
+
failureCount: hostHealth.failures.length,
|
|
1668
|
+
failures: hostHealth.failures.slice(-5),
|
|
1669
|
+
},
|
|
1670
|
+
checks,
|
|
1671
|
+
}
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
const DOCTOR_ROUTE = '/dsh-reminder-notify/doctor'
|
|
1675
|
+
|
|
1676
|
+
function registerDoctorRoute(ctx, getSettings) {
|
|
1677
|
+
const web = typeof ctx.get === 'function' ? ctx.get('webServer') : undefined
|
|
1678
|
+
if (web === undefined || web === null || typeof web.register !== 'function') {
|
|
1679
|
+
ctx.logger?.warn?.('dsh-reminder-notify: webServer is unavailable, the doctor panel stays hidden')
|
|
1680
|
+
return
|
|
1681
|
+
}
|
|
1682
|
+
const reply = (res, status, payload) => {
|
|
1683
|
+
res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
|
|
1684
|
+
res.end(JSON.stringify(payload))
|
|
1685
|
+
}
|
|
1686
|
+
const route = {
|
|
1687
|
+
kind: 'exact',
|
|
1688
|
+
path: DOCTOR_ROUTE,
|
|
1689
|
+
handler: async (req, res) => {
|
|
1690
|
+
try {
|
|
1691
|
+
const settings = getSettings()
|
|
1692
|
+
const method = (req.method ?? 'GET').toUpperCase()
|
|
1693
|
+
if (method === 'GET') return reply(res, 200, { ok: true, report: doctorReport(ctx, getSettings) })
|
|
1694
|
+
if (method !== 'POST') return reply(res, 405, { ok: false, error: 'use GET or POST' })
|
|
1695
|
+
const body = await readJsonBody(req)
|
|
1696
|
+
const mode = body !== null && typeof body === 'object' ? str(body.mode, 'check') : 'check'
|
|
1697
|
+
if (mode === 'check') return reply(res, 200, { ok: true, report: doctorReport(ctx, getSettings) })
|
|
1698
|
+
if (mode === 'probe') {
|
|
1699
|
+
const card = await notify(
|
|
1700
|
+
'raw',
|
|
1701
|
+
{ title: '自检样卡', message: '这张卡由「运行自检」弹出,能看到就说明宿主正常。', seconds: 12 },
|
|
1702
|
+
{ ...settings, quietEnabled: false },
|
|
1703
|
+
)
|
|
1704
|
+
return reply(res, 200, { ok: true, mode: 'probe', card })
|
|
1705
|
+
}
|
|
1706
|
+
return reply(res, 400, { ok: false, error: 'unknown mode' })
|
|
1707
|
+
} catch (error) {
|
|
1708
|
+
return reply(res, 500, { ok: false, error: String(error?.message ?? error) })
|
|
1709
|
+
}
|
|
1710
|
+
},
|
|
1711
|
+
}
|
|
1712
|
+
ctx.effect(() => {
|
|
1713
|
+
const dispose = web.register(route)
|
|
1714
|
+
return () => {
|
|
1715
|
+
if (typeof dispose === 'function') dispose()
|
|
1716
|
+
}
|
|
1717
|
+
}, 'dsh-reminder-notify: doctor route')
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1535
1720
|
function registerHistoryRoute(ctx, getSettings) {
|
|
1536
1721
|
const web = typeof ctx.get === 'function' ? ctx.get('webServer') : undefined
|
|
1537
1722
|
if (web === undefined || web === null || typeof web.register !== 'function') {
|
|
@@ -1579,7 +1764,48 @@ function registerHistoryRoute(ctx, getSettings) {
|
|
|
1579
1764
|
}, 'dsh-reminder-notify: history route')
|
|
1580
1765
|
}
|
|
1581
1766
|
|
|
1767
|
+
/** Host 服务名:别的插件用 ctx.get('reminder') 拿它。 */
|
|
1768
|
+
export const SERVICE_NAME = 'reminder'
|
|
1769
|
+
|
|
1770
|
+
function packageVersion() {
|
|
1771
|
+
try {
|
|
1772
|
+
return JSON.parse(fs.readFileSync(path.join(PACKAGE_ROOT, 'package.json'), 'utf8')).version
|
|
1773
|
+
} catch {
|
|
1774
|
+
return null
|
|
1775
|
+
}
|
|
1776
|
+
}
|
|
1777
|
+
|
|
1778
|
+
/**
|
|
1779
|
+
* 0.15 起对外提供的服务:让别的插件也能发提醒、发日报、读统计,而不用自己起宿主。
|
|
1780
|
+
* 全部方法是 fire-and-forget 语义(返回 Promise,失败只在返回值里体现),不会抛给调用方。
|
|
1781
|
+
*/
|
|
1782
|
+
export function serviceOf(ctx, getSettings) {
|
|
1783
|
+
return {
|
|
1784
|
+
name: 'dsh-reminder-notify',
|
|
1785
|
+
version: packageVersion(),
|
|
1786
|
+
/** 发一张提醒卡。kind: done | decide | raw;options 同 notify_user 的参数。 */
|
|
1787
|
+
notify: (kind, options = {}) => notify(nodeKind(kind), options || {}, getSettings()),
|
|
1788
|
+
/** 立刻发一条日报(force: true 时忽略「今天已经发过」和「日报未开启」)。 */
|
|
1789
|
+
digest: (options = {}) => runDigest(ctx, getSettings, { force: (options || {}).force === true }),
|
|
1790
|
+
/** 提醒历史统计(不弹卡、不写文件)。 */
|
|
1791
|
+
stats: () => historyStats(readHistory(getSettings())),
|
|
1792
|
+
/** 当前生效的配置快照(只读副本)。 */
|
|
1793
|
+
config: () => ({ ...getSettings() }),
|
|
1794
|
+
}
|
|
1795
|
+
}
|
|
1796
|
+
|
|
1797
|
+
function nodeKind(kind) {
|
|
1798
|
+
return kind === 'decide' || kind === 'raw' ? kind : 'done'
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1582
1801
|
export function apply(ctx, config) {
|
|
1802
|
+
const getSettings = () => settingsOf(config)
|
|
1803
|
+
if (typeof ctx.provide === 'function') {
|
|
1804
|
+
// 服务在 apply 里一次性注册;ctx.provide 自己挂在本 fiber 上,插件卸载时会自动回收。
|
|
1805
|
+
ctx.provide(SERVICE_NAME, serviceOf(ctx, getSettings))
|
|
1806
|
+
} else {
|
|
1807
|
+
ctx.logger?.warn?.('dsh-reminder-notify: ctx.provide is unavailable, the reminder service was not provided')
|
|
1808
|
+
}
|
|
1583
1809
|
registerAudioRoute(ctx, () => settingsOf(config))
|
|
1584
1810
|
registerConfigRoute(ctx, () => settingsOf(config))
|
|
1585
1811
|
registerPreviewRoute(ctx, () => settingsOf(config))
|
|
@@ -1587,6 +1813,7 @@ export function apply(ctx, config) {
|
|
|
1587
1813
|
registerHistoryRoute(ctx, () => settingsOf(config))
|
|
1588
1814
|
registerDigestRoute(ctx, () => settingsOf(config))
|
|
1589
1815
|
registerDigestLoop(ctx, () => settingsOf(config))
|
|
1816
|
+
registerDoctorRoute(ctx, () => settingsOf(config))
|
|
1590
1817
|
const tools = typeof ctx.get === 'function' ? ctx.get('tools') : ctx.tools
|
|
1591
1818
|
if (tools === undefined || tools === null || typeof tools.register !== 'function') {
|
|
1592
1819
|
ctx.logger?.warn?.('dsh-reminder-notify: tools service is unavailable, notify_user was not registered')
|
package/package.json
CHANGED