dsh-qqbot-plus 0.0.0-stage → 0.1.2
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 +188 -0
- package/CHANGELOG_EN.md +188 -0
- package/LICENSE +21 -0
- package/README.md +281 -2
- package/README_EN.md +248 -0
- package/cordis.patch.yml +163 -0
- package/cordis.yml +112 -0
- package/install.sh +108 -0
- package/lib/alarms.js +371 -0
- package/lib/client.js +2411 -0
- package/lib/commands/auth.d.ts +18 -0
- package/lib/commands/auth.d.ts.map +1 -0
- package/lib/commands/auth.js +15 -0
- package/lib/commands/auth.js.map +1 -0
- package/lib/commands/help.d.ts +10 -0
- package/lib/commands/help.d.ts.map +1 -0
- package/lib/commands/help.js +39 -0
- package/lib/commands/help.js.map +1 -0
- package/lib/commands/index.d.ts +15 -0
- package/lib/commands/index.d.ts.map +1 -0
- package/lib/commands/index.js +30 -0
- package/lib/commands/index.js.map +1 -0
- package/lib/commands/misc.d.ts +21 -0
- package/lib/commands/misc.d.ts.map +1 -0
- package/lib/commands/misc.js +93 -0
- package/lib/commands/misc.js.map +1 -0
- package/lib/commands/model.d.ts +6 -0
- package/lib/commands/model.d.ts.map +1 -0
- package/lib/commands/model.js +63 -0
- package/lib/commands/model.js.map +1 -0
- package/lib/commands/new.js +17 -0
- package/lib/commands/preset.d.ts +8 -0
- package/lib/commands/preset.d.ts.map +1 -0
- package/lib/commands/preset.js +56 -0
- package/lib/commands/preset.js.map +1 -0
- package/lib/commands/session.d.ts +14 -0
- package/lib/commands/session.d.ts.map +1 -0
- package/lib/commands/session.js +34 -0
- package/lib/commands/session.js.map +1 -0
- package/lib/commands/status.d.ts +7 -0
- package/lib/commands/status.d.ts.map +1 -0
- package/lib/commands/status.js +26 -0
- package/lib/commands/status.js.map +1 -0
- package/lib/commands/types.d.ts +21 -0
- package/lib/commands/types.d.ts.map +1 -0
- package/lib/commands/types.js +2 -0
- package/lib/commands/types.js.map +1 -0
- package/lib/config.d.ts +95 -0
- package/lib/config.js +88 -0
- package/lib/features/answer-parser.d.ts +9 -0
- package/lib/features/answer-parser.d.ts.map +1 -0
- package/lib/features/answer-parser.js +23 -0
- package/lib/features/answer-parser.js.map +1 -0
- package/lib/features/approval-channel.d.ts +94 -0
- package/lib/features/approval-channel.d.ts.map +1 -0
- package/lib/features/approval-channel.js +158 -0
- package/lib/features/approval-channel.js.map +1 -0
- package/lib/features/approval-renderer.d.ts +18 -0
- package/lib/features/approval-renderer.d.ts.map +1 -0
- package/lib/features/approval-renderer.js +52 -0
- package/lib/features/approval-renderer.js.map +1 -0
- package/lib/features/button-utils.d.ts +34 -0
- package/lib/features/button-utils.d.ts.map +1 -0
- package/lib/features/button-utils.js +27 -0
- package/lib/features/button-utils.js.map +1 -0
- package/lib/features/history-store.d.ts +8 -0
- package/lib/features/history-store.d.ts.map +1 -0
- package/lib/features/history-store.js +23 -0
- package/lib/features/history-store.js.map +1 -0
- package/lib/features/question-channel.d.ts +96 -0
- package/lib/features/question-channel.d.ts.map +1 -0
- package/lib/features/question-channel.js +234 -0
- package/lib/features/question-channel.js.map +1 -0
- package/lib/features/question-renderer.d.ts +20 -0
- package/lib/features/question-renderer.d.ts.map +1 -0
- package/lib/features/question-renderer.js +70 -0
- package/lib/features/question-renderer.js.map +1 -0
- package/lib/fishcheck/measure.js +230 -0
- package/lib/fishcheck/rules.js +99 -0
- package/lib/fishmeme/plugin.js +605 -0
- package/lib/fishmeme/routes.js +141 -0
- package/lib/fishmemory/plugin.js +1052 -0
- package/lib/gateway/bootstrap.d.ts +12 -0
- package/lib/gateway/bootstrap.d.ts.map +1 -0
- package/lib/gateway/bootstrap.js +200 -0
- package/lib/gateway/bootstrap.js.map +1 -0
- package/lib/gateway/index.d.ts +3 -0
- package/lib/gateway/index.d.ts.map +1 -0
- package/lib/gateway/index.js +3 -0
- package/lib/gateway/index.js.map +1 -0
- package/lib/gateway/middleware-setup.d.ts +12 -0
- package/lib/gateway/middleware-setup.d.ts.map +1 -0
- package/lib/gateway/middleware-setup.js +82 -0
- package/lib/gateway/middleware-setup.js.map +1 -0
- package/lib/host/adapter.d.ts +48 -0
- package/lib/host/adapter.d.ts.map +1 -0
- package/lib/host/adapter.js +127 -0
- package/lib/host/adapter.js.map +1 -0
- package/lib/humanlike/ENV_PROMPT.md +28 -0
- package/lib/humanlike/affinity.js +316 -0
- package/lib/humanlike/learn.js +343 -0
- package/lib/humanlike/life.js +899 -0
- package/lib/humanlike/mind.js +323 -0
- package/lib/humanlike/plugin.js +587 -0
- package/lib/humanlike/reach.js +119 -0
- package/lib/humanlike/real.js +276 -0
- package/lib/humanlike/rules.js +67 -0
- package/lib/humanlike/settings-api.js +159 -0
- package/lib/humanlike/settings.js +370 -0
- package/lib/humanlike/together.js +251 -0
- package/lib/identity/paths.js +27 -0
- package/lib/identity/persona.js +75 -0
- package/lib/identity/portrait.js +25 -0
- package/lib/identity/runtime.js +78 -0
- package/lib/index.d.ts +15 -0
- package/lib/index.js +117 -0
- package/lib/media/draft-tool.js +442 -0
- package/lib/media/draft-tool.js.bak-to-182027 +389 -0
- package/lib/media/media-cleaner.d.ts +19 -0
- package/lib/media/media-cleaner.d.ts.map +1 -0
- package/lib/media/media-cleaner.js +81 -0
- package/lib/media/media-cleaner.js.map +1 -0
- package/lib/media/say-tool.js +72 -0
- package/lib/media/send-file-tool.d.ts +12 -0
- package/lib/media/send-file-tool.d.ts.map +1 -0
- package/lib/media/send-file-tool.js +122 -0
- package/lib/media/send-file-tool.js.map +1 -0
- package/lib/media/send-media.d.ts +61 -0
- package/lib/media/send-media.d.ts.map +1 -0
- package/lib/media/send-media.js +104 -0
- package/lib/media/send-media.js.map +1 -0
- package/lib/media/vision-tool.d.ts +22 -0
- package/lib/media/vision-tool.d.ts.map +1 -0
- package/lib/media/vision-tool.js +288 -0
- package/lib/media/vision-tool.js.map +1 -0
- package/lib/middleware/attachment.d.ts +11 -0
- package/lib/middleware/attachment.d.ts.map +1 -0
- package/lib/middleware/attachment.js +30 -0
- package/lib/middleware/attachment.js.map +1 -0
- package/lib/middleware/question-answer.d.ts +10 -0
- package/lib/middleware/question-answer.d.ts.map +1 -0
- package/lib/middleware/question-answer.js +19 -0
- package/lib/middleware/question-answer.js.map +1 -0
- package/lib/model/index.d.ts +10 -0
- package/lib/model/index.d.ts.map +1 -0
- package/lib/model/index.js +9 -0
- package/lib/model/index.js.map +1 -0
- package/lib/model/model-resolver.d.ts +102 -0
- package/lib/model/model-resolver.d.ts.map +1 -0
- package/lib/model/model-resolver.js +206 -0
- package/lib/model/model-resolver.js.map +1 -0
- package/lib/model/prefs-store.d.ts +50 -0
- package/lib/model/prefs-store.d.ts.map +1 -0
- package/lib/model/prefs-store.js +215 -0
- package/lib/model/prefs-store.js.map +1 -0
- package/lib/model/settings-reader.d.ts +19 -0
- package/lib/model/settings-reader.d.ts.map +1 -0
- package/lib/model/settings-reader.js +76 -0
- package/lib/model/settings-reader.js.map +1 -0
- package/lib/model/types.d.ts +15 -0
- package/lib/model/types.d.ts.map +1 -0
- package/lib/model/types.js +5 -0
- package/lib/model/types.js.map +1 -0
- package/lib/resume-check.js +122 -0
- package/lib/resume-tool.js +29 -0
- package/lib/rules.js +57 -0
- package/lib/session/idle-evictor.d.ts +18 -0
- package/lib/session/idle-evictor.d.ts.map +1 -0
- package/lib/session/idle-evictor.js +31 -0
- package/lib/session/idle-evictor.js.map +1 -0
- package/lib/session/index.d.ts +9 -0
- package/lib/session/index.d.ts.map +1 -0
- package/lib/session/index.js +8 -0
- package/lib/session/index.js.map +1 -0
- package/lib/session/pick-agent.js +116 -0
- package/lib/session/session-id.d.ts +5 -0
- package/lib/session/session-id.d.ts.map +1 -0
- package/lib/session/session-id.js +15 -0
- package/lib/session/session-id.js.map +1 -0
- package/lib/session/session-manager.d.ts +138 -0
- package/lib/session/session-manager.d.ts.map +1 -0
- package/lib/session/session-manager.js +1157 -0
- package/lib/session/session-manager.js.bak-target +1081 -0
- package/lib/session/session-manager.js.bak-title-20261118 +1119 -0
- package/lib/session/session-manager.js.map +1 -0
- package/lib/session/types.d.ts +162 -0
- package/lib/session/types.d.ts.map +1 -0
- package/lib/session/types.js +2 -0
- package/lib/session/types.js.map +1 -0
- package/lib/setup/bind-routes.d.ts +78 -0
- package/lib/setup/bind-routes.d.ts.map +1 -0
- package/lib/setup/bind-routes.js +588 -0
- package/lib/setup/bind-routes.js.bak-join-163944 +761 -0
- package/lib/setup/bind-routes.js.map +1 -0
- package/lib/setup/binding-service.d.ts +86 -0
- package/lib/setup/binding-service.d.ts.map +1 -0
- package/lib/setup/binding-service.js +175 -0
- package/lib/setup/binding-service.js.map +1 -0
- package/lib/setup/index.d.ts +50 -0
- package/lib/setup/index.d.ts.map +1 -0
- package/lib/setup/index.js +163 -0
- package/lib/setup/index.js.map +1 -0
- package/lib/setup/persist.d.ts +42 -0
- package/lib/setup/persist.d.ts.map +1 -0
- package/lib/setup/persist.js +344 -0
- package/lib/setup/persist.js.map +1 -0
- package/lib/setup/presenters.d.ts +40 -0
- package/lib/setup/presenters.d.ts.map +1 -0
- package/lib/setup/presenters.js +84 -0
- package/lib/setup/presenters.js.map +1 -0
- package/lib/setup/qr-svg.d.ts +9 -0
- package/lib/setup/qr-svg.d.ts.map +1 -0
- package/lib/setup/qr-svg.js +33 -0
- package/lib/setup/qr-svg.js.map +1 -0
- package/lib/shared/bind-protocol.d.ts +42 -0
- package/lib/shared/bind-protocol.d.ts.map +1 -0
- package/lib/shared/bind-protocol.js +33 -0
- package/lib/shared/bind-protocol.js.map +1 -0
- package/lib/shared/dirs.d.ts +10 -0
- package/lib/shared/dirs.d.ts.map +1 -0
- package/lib/shared/dirs.js +20 -0
- package/lib/shared/dirs.js.map +1 -0
- package/lib/shared/http-body.js +43 -0
- package/lib/shared/index.d.ts +8 -0
- package/lib/shared/index.d.ts.map +1 -0
- package/lib/shared/index.js +8 -0
- package/lib/shared/index.js.map +1 -0
- package/lib/shared/json-file.js +34 -0
- package/lib/shared/orders.js +26 -0
- package/lib/shared/scope.d.ts +14 -0
- package/lib/shared/scope.d.ts.map +1 -0
- package/lib/shared/scope.js +12 -0
- package/lib/shared/scope.js.map +1 -0
- package/lib/shared/send-helper.d.ts +11 -0
- package/lib/shared/send-helper.d.ts.map +1 -0
- package/lib/shared/send-helper.js +13 -0
- package/lib/shared/send-helper.js.map +1 -0
- package/lib/shared/utils.d.ts +31 -0
- package/lib/shared/utils.d.ts.map +1 -0
- package/lib/shared/utils.js +228 -0
- package/lib/shared/utils.js.map +1 -0
- package/lib/tool-guard.js +74 -0
- package/lib/transport/attachment.d.ts +41 -0
- package/lib/transport/attachment.d.ts.map +1 -0
- package/lib/transport/attachment.js +191 -0
- package/lib/transport/attachment.js.map +1 -0
- package/lib/transport/chunker.d.ts +14 -0
- package/lib/transport/chunker.d.ts.map +1 -0
- package/lib/transport/chunker.js +78 -0
- package/lib/transport/chunker.js.map +1 -0
- package/lib/transport/deliverables.d.ts +20 -0
- package/lib/transport/deliverables.d.ts.map +1 -0
- package/lib/transport/deliverables.js +72 -0
- package/lib/transport/deliverables.js.map +1 -0
- package/lib/transport/events.d.ts +117 -0
- package/lib/transport/events.d.ts.map +1 -0
- package/lib/transport/events.js +112 -0
- package/lib/transport/events.js.map +1 -0
- package/lib/transport/inbound.d.ts +11 -0
- package/lib/transport/inbound.d.ts.map +1 -0
- package/lib/transport/inbound.js +511 -0
- package/lib/transport/inbound.js.map +1 -0
- package/lib/transport/index.d.ts +10 -0
- package/lib/transport/index.d.ts.map +1 -0
- package/lib/transport/index.js +10 -0
- package/lib/transport/index.js.map +1 -0
- package/lib/transport/msgid-cache.d.ts +23 -0
- package/lib/transport/msgid-cache.d.ts.map +1 -0
- package/lib/transport/msgid-cache.js +57 -0
- package/lib/transport/msgid-cache.js.map +1 -0
- package/lib/transport/notify.js +147 -0
- package/lib/transport/outbound-buffer.d.ts +42 -0
- package/lib/transport/outbound-buffer.d.ts.map +1 -0
- package/lib/transport/outbound-buffer.js +68 -0
- package/lib/transport/outbound-buffer.js.map +1 -0
- package/lib/transport/outbound.d.ts +42 -0
- package/lib/transport/outbound.d.ts.map +1 -0
- package/lib/transport/outbound.js +183 -0
- package/lib/transport/outbound.js.map +1 -0
- package/lib/transport/reply-limiter.d.ts +31 -0
- package/lib/transport/reply-limiter.d.ts.map +1 -0
- package/lib/transport/reply-limiter.js +50 -0
- package/lib/transport/reply-limiter.js.map +1 -0
- package/lib/transport/reply-target.d.ts +32 -0
- package/lib/transport/reply-target.d.ts.map +1 -0
- package/lib/transport/reply-target.js +64 -0
- package/lib/transport/reply-target.js.map +1 -0
- package/lib/transport/streaming-writer.d.ts +55 -0
- package/lib/transport/streaming-writer.d.ts.map +1 -0
- package/lib/transport/streaming-writer.js +122 -0
- package/lib/transport/streaming-writer.js.map +1 -0
- package/lib/transport/tool-presenter.d.ts +37 -0
- package/lib/transport/tool-presenter.d.ts.map +1 -0
- package/lib/transport/tool-presenter.js +116 -0
- package/lib/transport/tool-presenter.js.map +1 -0
- package/lib/turn-check.js +85 -0
- package/lib/types.d.ts +39 -0
- package/lib/types.js +5 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +127 -4
package/lib/alarms.js
ADDED
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 唤醒器(闹钟):alarm_set / alarm_list / alarm_cancel + 每秒心跳。
|
|
3
|
+
*
|
|
4
|
+
* 从 Coopanion 的 core/alarms.ts 搬过来(时间换算与文案照抄,别自己重写)。
|
|
5
|
+
* dsh 没有宿主事件总线,叫醒只能走 agent.followup;
|
|
6
|
+
* 心跳每秒一次,但**先把 agent 挑到 idle 再 takeDue**,否则到点那条被删了却发不出去。
|
|
7
|
+
*
|
|
8
|
+
* @module dsh-qqbot-plus/alarms
|
|
9
|
+
*/
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs'
|
|
11
|
+
import { randomUUID } from 'node:crypto'
|
|
12
|
+
import { homedir } from 'node:os'
|
|
13
|
+
import { join } from 'node:path'
|
|
14
|
+
import { pickAgent } from './session/pick-agent.js'
|
|
15
|
+
import { writeJsonAtomic } from './shared/json-file.js'
|
|
16
|
+
|
|
17
|
+
const STATE_DIR = join(homedir(), '.dsh', 'dsh-qqbot-plus')
|
|
18
|
+
const STATE_FILE = join(STATE_DIR, 'alarms.json')
|
|
19
|
+
const LOG_FILE = join(STATE_DIR, 'alarms.log')
|
|
20
|
+
const LOG_MAX_BYTES = 1024 * 1024
|
|
21
|
+
const TICK_MS = 1000
|
|
22
|
+
const MISSED_AFTER_MS = 60_000
|
|
23
|
+
const TIMEZONE = 'Asia/Shanghai'
|
|
24
|
+
|
|
25
|
+
/** 往 alarms.log 追加一行——宿主 console 看不见,出问题只能靠这个。
|
|
26
|
+
* 超过 1MB 就把旧文件改名成 alarms.log.1(只留一代),不会无限长下去。 */
|
|
27
|
+
function note (line) {
|
|
28
|
+
try {
|
|
29
|
+
if (!existsSync(STATE_DIR)) mkdirSync(STATE_DIR, { recursive: true })
|
|
30
|
+
try {
|
|
31
|
+
if (statSync(LOG_FILE).size > LOG_MAX_BYTES) renameSync(LOG_FILE, `${LOG_FILE}.1`)
|
|
32
|
+
} catch {}
|
|
33
|
+
writeFileSync(LOG_FILE, `${new Date().toISOString()} 唤醒器 ${line}\n`, { flag: 'a' })
|
|
34
|
+
} catch {}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const zh = {
|
|
38
|
+
minutesTooFew: 'in_minutes 至少是 1',
|
|
39
|
+
noTime: '要给 at 或 in_minutes',
|
|
40
|
+
past: (at) => `${at} 已经过去了`,
|
|
41
|
+
badAt: (got) => `at 应为 HH:MM 或 YYYY-MM-DD HH:MM,收到 ${got}`,
|
|
42
|
+
due: (setAt, id, daily, note) => `[唤醒器] 你在 ${setAt} 设的唤醒到了(${id}${daily ? ',每天' : ''}):${note}`,
|
|
43
|
+
missed: (when) => `原定 ${when},那时应用没开着,现在才送到。`,
|
|
44
|
+
listLine: (id, when, clock, note) => `- ${id}:${when}${clock ? `(每天 ${clock})` : ''} ${note}`,
|
|
45
|
+
noteEmpty: '[alarm_set 没执行] note 不能为空。',
|
|
46
|
+
setFailed: (why) => `[alarm_set 没执行] ${why}。`,
|
|
47
|
+
set: (id, when, clock) => `已设 ${id}:${when}${clock ? `,之后每天 ${clock}` : ''}。`,
|
|
48
|
+
none: '没有设着的唤醒器。',
|
|
49
|
+
missing: (id) => `[alarm_cancel 没执行] 没有 ${id} 这个唤醒器。`,
|
|
50
|
+
cancelled: (id) => `已取消 ${id}。`,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** UTC 减本地,分钟,`timezone` 在 `d` 那一刻。 */
|
|
54
|
+
function offsetMinutes (timezone, d) {
|
|
55
|
+
const p = Object.fromEntries(new Intl.DateTimeFormat('en-US', {
|
|
56
|
+
timeZone: timezone, hourCycle: 'h23', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit',
|
|
57
|
+
}).formatToParts(d).map((x) => [x.type, x.value]))
|
|
58
|
+
const local = Date.UTC(+p.year, +p.month - 1, +p.day, +p.hour, +p.minute, +p.second)
|
|
59
|
+
return (d.getTime() - d.getMilliseconds() - local) / 60_000
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** `timezone` 里某面钟的时刻对应的 epoch ms;被夏令时跳过的钟点往后顺延一个缺口。 */
|
|
63
|
+
function localToEpoch (timezone, y, mo, d, h, mi) {
|
|
64
|
+
const guess = Date.UTC(y, mo - 1, d, h, mi)
|
|
65
|
+
const first = guess + offsetMinutes(timezone, new Date(guess)) * 60_000
|
|
66
|
+
const second = guess + offsetMinutes(timezone, new Date(first)) * 60_000
|
|
67
|
+
const wall = second - offsetMinutes(timezone, new Date(second)) * 60_000
|
|
68
|
+
return wall === guess ? second : Math.max(first, second)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function localDate (timezone, ms) {
|
|
72
|
+
const p = Object.fromEntries(new Intl.DateTimeFormat('en-US', {
|
|
73
|
+
timeZone: timezone, year: 'numeric', month: '2-digit', day: '2-digit',
|
|
74
|
+
}).formatToParts(new Date(ms)).map((x) => [x.type, x.value]))
|
|
75
|
+
return { year: +p.year, month: +p.month, day: +p.day }
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function nextClock (timezone, now, hour, minute, withinMinute = 0) {
|
|
79
|
+
const { year, month, day } = localDate(timezone, now)
|
|
80
|
+
const at = localToEpoch(timezone, year, month, day, hour, minute) + withinMinute
|
|
81
|
+
return at > now ? at : localToEpoch(timezone, year, month, day + 1, hour, minute) + withinMinute
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** `MM-DD HH:MM`,`timezone` 里。 */
|
|
85
|
+
function localLabel (timezone, ms) {
|
|
86
|
+
const p = Object.fromEntries(new Intl.DateTimeFormat('en-US', {
|
|
87
|
+
timeZone: timezone, hourCycle: 'h23', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit',
|
|
88
|
+
}).formatToParts(new Date(ms)).map((x) => [x.type, x.value]))
|
|
89
|
+
return `${p.month}-${p.day} ${p.hour}:${p.minute}`
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** `at`(HH:MM 取下一个这样的点,或 YYYY-MM-DD HH:MM)或 `inMinutes` 对应的到点时刻,或给不出的原因。 */
|
|
93
|
+
function dueTime (timezone, now, at, inMinutes) {
|
|
94
|
+
const t = zh
|
|
95
|
+
if (typeof inMinutes === 'number') {
|
|
96
|
+
if (!(inMinutes >= 1)) return t.minutesTooFew
|
|
97
|
+
return now + Math.round(inMinutes * 60_000)
|
|
98
|
+
}
|
|
99
|
+
if (typeof at !== 'string') return t.noTime
|
|
100
|
+
const full = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{1,2}):(\d{2})$/.exec(at.trim())
|
|
101
|
+
if (full) {
|
|
102
|
+
const [, y, mo, d, h, mi] = full.map(Number)
|
|
103
|
+
if (mo < 1 || mo > 12 || d < 1 || d > new Date(Date.UTC(y, mo, 0)).getUTCDate() || h > 23 || mi > 59) return t.badAt(JSON.stringify(at))
|
|
104
|
+
const ms = localToEpoch(timezone, y, mo, d, h, mi)
|
|
105
|
+
return ms > now ? ms : t.past(at)
|
|
106
|
+
}
|
|
107
|
+
const clock = /^(\d{1,2}):(\d{2})$/.exec(at.trim())
|
|
108
|
+
if (!clock || +clock[1] > 23 || +clock[2] > 59) return t.badAt(JSON.stringify(at))
|
|
109
|
+
return nextClock(timezone, now, +clock[1], +clock[2])
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function validAlarm (a) {
|
|
113
|
+
return a && typeof a === 'object' &&
|
|
114
|
+
typeof a.id === 'string' && typeof a.note === 'string' && typeof a.daily === 'boolean' &&
|
|
115
|
+
typeof a.at === 'number' && Number.isFinite(a.at) && typeof a.setAt === 'number' && Number.isFinite(a.setAt)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function restoreSaved (value) {
|
|
119
|
+
const saved = value && typeof value === 'object' ? value : {}
|
|
120
|
+
const records = Array.isArray(saved.alarms) ? saved.alarms : []
|
|
121
|
+
const alarms = records.filter(validAlarm)
|
|
122
|
+
if (alarms.length < records.length) {
|
|
123
|
+
// 坏记录丢掉,其余留着;只打日志,不炸。
|
|
124
|
+
note(`丢掉 ${records.length - alarms.length} 条坏记录`)
|
|
125
|
+
}
|
|
126
|
+
const next = Number.isSafeInteger(saved.next) && saved.next >= 1 ? saved.next : 1
|
|
127
|
+
return { next, alarms }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const TOOL_DECLS = [
|
|
131
|
+
{
|
|
132
|
+
name: 'alarm_set',
|
|
133
|
+
description: '给自己设一个唤醒器:到点你会被一条带着你写的 note 的事件叫醒。设了以后用 alarm_list 看还剩哪些、alarm_cancel 撤掉不要的——dsh 这边没有设置窗口可看。用它准时提醒主人、在某个钟点去做一件事,或者过一阵回来看一眼。应用关着的时候错过的,下次启动时补投,并说明原定时间。注意:那一轮正忙时不会打断,等空下来下一跳才送,可能晚几十秒。',
|
|
134
|
+
parameters: {
|
|
135
|
+
type: 'object',
|
|
136
|
+
properties: {
|
|
137
|
+
at: { type: 'string', description: "本地钟点(宿主时区):HH:MM(今天过了就算明天),或 YYYY-MM-DD HH:MM。被夏令时跳过的钟点往后顺延一个缺口。给这个或 in_minutes。" },
|
|
138
|
+
in_minutes: { type: 'number', minimum: 1, description: '从现在起几分钟后。' },
|
|
139
|
+
note: { type: 'string', description: '到点要做什么、为什么设这一条——写给你那时候的自己看。' },
|
|
140
|
+
daily: { type: 'boolean', description: 'true = 每天这个本地钟点都叫一次,直到 alarm_cancel;夏令时变了也跟着走。' },
|
|
141
|
+
},
|
|
142
|
+
required: ['note'],
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
name: 'alarm_list',
|
|
147
|
+
description: '列出你设着的唤醒器:id、时间(本地时间)、note。',
|
|
148
|
+
parameters: { type: 'object', properties: {} },
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
name: 'alarm_cancel',
|
|
152
|
+
description: '取消一个唤醒器;每天的会整个停掉。',
|
|
153
|
+
parameters: {
|
|
154
|
+
type: 'object',
|
|
155
|
+
properties: { id: { type: 'string', description: 'alarm_set 回执或 alarm_list 里的 id,形如 a3。' } },
|
|
156
|
+
required: ['id'],
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
]
|
|
160
|
+
|
|
161
|
+
export class Alarms {
|
|
162
|
+
constructor (file = STATE_FILE, timezone = TIMEZONE, now = Date.now) {
|
|
163
|
+
this.file = file
|
|
164
|
+
this.timezone = timezone
|
|
165
|
+
this.now = now
|
|
166
|
+
this.saved = { next: 1, alarms: [] }
|
|
167
|
+
this.load()
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
load () {
|
|
171
|
+
try {
|
|
172
|
+
if (!existsSync(this.file)) return
|
|
173
|
+
const raw = JSON.parse(readFileSync(this.file, 'utf8'))
|
|
174
|
+
this.saved = restoreSaved(raw)
|
|
175
|
+
} catch (error) {
|
|
176
|
+
note(`读 alarms.json 失败 ${error instanceof Error ? error.message : String(error)}`)
|
|
177
|
+
this.saved = { next: 1, alarms: [] }
|
|
178
|
+
}
|
|
179
|
+
// 老记录缺 clock 的,从 at 反推出来(每天那条要用它重新排期)。
|
|
180
|
+
for (const alarm of this.saved.alarms) {
|
|
181
|
+
if (alarm.daily && !/^(?:[01]\d|2[0-3]):[0-5]\d$/.test(String(alarm.clock ?? ''))) {
|
|
182
|
+
alarm.clock = localLabel(this.timezone, alarm.at).slice(-5)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
save () {
|
|
188
|
+
try {
|
|
189
|
+
if (!existsSync(STATE_DIR)) mkdirSync(STATE_DIR, { recursive: true })
|
|
190
|
+
// 原子写:闹钟文件写坏一半,所有闹钟就一起没了。
|
|
191
|
+
writeJsonAtomic(this.file, this.saved)
|
|
192
|
+
return true
|
|
193
|
+
} catch (error) {
|
|
194
|
+
note(`写 alarms.json 失败 ${error instanceof Error ? error.message : String(error)}`)
|
|
195
|
+
return false
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** 把 `now` 已经到点的那些取下来(每天的重新排到第二天的同一个钟点),各自带上「是不是错过了」。 */
|
|
200
|
+
takeDue (now = this.now()) {
|
|
201
|
+
const due = this.saved.alarms.filter((a) => a.at <= now)
|
|
202
|
+
if (!due.length) return []
|
|
203
|
+
this.saved.alarms = this.saved.alarms.filter((a) => a.at > now)
|
|
204
|
+
for (const a of due) {
|
|
205
|
+
if (!a.daily) continue
|
|
206
|
+
const [hour, minute] = a.clock.split(':').map(Number)
|
|
207
|
+
const at = nextClock(this.timezone, now, hour, minute, a.at % 60_000)
|
|
208
|
+
this.saved.alarms.push({ ...a, at })
|
|
209
|
+
}
|
|
210
|
+
this.save()
|
|
211
|
+
return due.map((alarm) => ({ alarm, missed: now - alarm.at > MISSED_AFTER_MS }))
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** 设着的,最近的那个在前。 */
|
|
215
|
+
list () {
|
|
216
|
+
return this.saved.alarms.slice().sort((a, b) => a.at - b.at)
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** 取掉 `id`;没有就回 null。 */
|
|
220
|
+
cancel (id) {
|
|
221
|
+
const alarm = this.saved.alarms.find((a) => a.id === id)
|
|
222
|
+
if (!alarm) return null
|
|
223
|
+
this.saved.alarms = this.saved.alarms.filter((a) => a !== alarm)
|
|
224
|
+
this.save()
|
|
225
|
+
return alarm
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
dueText (alarm, missed) {
|
|
229
|
+
const t = zh
|
|
230
|
+
return [
|
|
231
|
+
t.due(localLabel(this.timezone, alarm.setAt), alarm.id, alarm.daily, alarm.note),
|
|
232
|
+
...(missed ? [t.missed(localLabel(this.timezone, alarm.at))] : []),
|
|
233
|
+
].join('\n')
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 三个工具绑好 handler 交出去。
|
|
238
|
+
* `handlers` 里返回 `{ text, failed }`,由调用方转成工具回执。
|
|
239
|
+
*/
|
|
240
|
+
tools (now = this.now) {
|
|
241
|
+
const tz = this.timezone
|
|
242
|
+
const listText = () => this.list().map((a) => zh.listLine(a.id, localLabel(tz, a.at), a.daily ? a.clock : undefined, a.note)).join('\n')
|
|
243
|
+
const run = {
|
|
244
|
+
alarm_set: (args = {}) => {
|
|
245
|
+
const noteText = typeof args.note === 'string' ? args.note.trim() : ''
|
|
246
|
+
if (!noteText) return { text: zh.noteEmpty, failed: true }
|
|
247
|
+
const time = now()
|
|
248
|
+
const at = dueTime(tz, time, args.at, args.in_minutes)
|
|
249
|
+
if (typeof at === 'string') return { text: zh.setFailed(at), failed: true }
|
|
250
|
+
const alarm = { id: `a${this.saved.next++}`, at, note: noteText, daily: args.daily === true, setAt: time }
|
|
251
|
+
if (alarm.daily) {
|
|
252
|
+
alarm.clock = typeof args.in_minutes !== 'number' && typeof args.at === 'string'
|
|
253
|
+
? args.at.trim().match(/\d{1,2}:\d{2}$/)[0].padStart(5, '0')
|
|
254
|
+
: localLabel(tz, at).slice(-5)
|
|
255
|
+
}
|
|
256
|
+
this.saved.alarms.push(alarm)
|
|
257
|
+
this.save()
|
|
258
|
+
return { text: zh.set(alarm.id, localLabel(tz, at), alarm.clock), alarm }
|
|
259
|
+
},
|
|
260
|
+
alarm_list: () => ({ text: this.saved.alarms.length ? listText() : zh.none }),
|
|
261
|
+
alarm_cancel: (args = {}) => {
|
|
262
|
+
if (!this.cancel(args.id)) return { text: zh.missing(JSON.stringify(args.id)), failed: true }
|
|
263
|
+
return { text: zh.cancelled(String(args.id)) }
|
|
264
|
+
},
|
|
265
|
+
}
|
|
266
|
+
return { run, decls: TOOL_DECLS }
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** 进程内共用一份:工具写、心跳取,两边得看同一份内存,不然刚设的会被旧快照盖掉。 */
|
|
271
|
+
let shared = null
|
|
272
|
+
export function alarmsOf () {
|
|
273
|
+
if (!shared) shared = new Alarms(STATE_FILE, TIMEZONE)
|
|
274
|
+
return shared
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 把三个工具挂到 tools 服务上。
|
|
279
|
+
*
|
|
280
|
+
* 回执形状照 `lib/media/draft-tool.js` 的 register:`output.schema` 是 string,`render` 取 .text。
|
|
281
|
+
*/
|
|
282
|
+
export function registerAlarmTools (ctx, alarms, logger) {
|
|
283
|
+
const tools = ctx.get?.('tools') ?? ctx.tools
|
|
284
|
+
if (!tools?.register) {
|
|
285
|
+
logger?.warn?.('im-qqbot: tools 服务不可用,alarm_set/alarm_list/alarm_cancel 未注册')
|
|
286
|
+
return false
|
|
287
|
+
}
|
|
288
|
+
const { run, decls } = alarms.tools()
|
|
289
|
+
for (const decl of decls) {
|
|
290
|
+
tools.register({
|
|
291
|
+
name: decl.name,
|
|
292
|
+
description: decl.description,
|
|
293
|
+
parameters: decl.parameters,
|
|
294
|
+
output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: String(value) }] },
|
|
295
|
+
async execute (args = {}) {
|
|
296
|
+
try {
|
|
297
|
+
const result = run[decl.name]?.(args) ?? { text: `[bad input] ${decl.name} 没有这个动作`, failed: true }
|
|
298
|
+
return result.text
|
|
299
|
+
} catch (error) {
|
|
300
|
+
logger?.warn?.(`im-qqbot: ${decl.name} 出错 ${error instanceof Error ? error.message : String(error)}`)
|
|
301
|
+
return `出错了:${error instanceof Error ? error.message : String(error)}`
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
})
|
|
305
|
+
}
|
|
306
|
+
logger?.info?.('im-qqbot: 唤醒器工具已注册(alarm_set · alarm_list · alarm_cancel)')
|
|
307
|
+
return true
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* 每秒一跳:先挑到 idle 的会话再 takeDue,然后把到点的每一条投进去。
|
|
312
|
+
*
|
|
313
|
+
* 挑不到或正忙就**什么都不取**——取下来又发不出去,那条就丢了(她踩过)。
|
|
314
|
+
*/
|
|
315
|
+
export function startAlarms ({ ctx, agents, logger, timezone = TIMEZONE } = {}) {
|
|
316
|
+
const alarms = alarmsOf()
|
|
317
|
+
let busy = false
|
|
318
|
+
|
|
319
|
+
async function tick () {
|
|
320
|
+
if (busy) return
|
|
321
|
+
busy = true
|
|
322
|
+
try {
|
|
323
|
+
const agent = await pickAgent(agents)
|
|
324
|
+
if (!agent || agent.status !== 'idle') return
|
|
325
|
+
const due = alarms.takeDue(Date.now())
|
|
326
|
+
if (!due.length) return
|
|
327
|
+
// 先把这一批全投完,投不出去的攒起来最后一起放回去。
|
|
328
|
+
// 一条失败就 break 会把同批剩下的那些既没投也没写回,等于静默丢掉。
|
|
329
|
+
const failed = []
|
|
330
|
+
for (const { alarm, missed } of due) {
|
|
331
|
+
const text = alarms.dueText(alarm, missed)
|
|
332
|
+
try {
|
|
333
|
+
// 必须 await:followup 返回 Promise,不 await 的话失败会变成
|
|
334
|
+
// unhandledRejection(能掀掉主进程),catch 也接不到。
|
|
335
|
+
await agent.followup({
|
|
336
|
+
id: randomUUID(),
|
|
337
|
+
role: 'user',
|
|
338
|
+
content: [{ type: 'text', text }],
|
|
339
|
+
source: { kind: 'qq-alarm', id: alarm.id },
|
|
340
|
+
})
|
|
341
|
+
note(`叫醒 ${alarm.id}${missed ? '(错过补投)' : ''}`)
|
|
342
|
+
logger?.info?.(`唤醒器:叫醒 ${alarm.id}`)
|
|
343
|
+
} catch (error) {
|
|
344
|
+
failed.push(alarm)
|
|
345
|
+
note(`投递失败 ${alarm.id} ${error instanceof Error ? error.message : String(error)}`)
|
|
346
|
+
logger?.warn?.(`唤醒器:投递失败 ${error instanceof Error ? error.message : String(error)}`)
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (failed.length) {
|
|
350
|
+
// 放回去,下一跳再试;别丢。
|
|
351
|
+
alarms.saved.alarms.push(...failed)
|
|
352
|
+
alarms.save()
|
|
353
|
+
}
|
|
354
|
+
} catch (error) {
|
|
355
|
+
note(`tick 异常 ${error instanceof Error ? error.message : String(error)}`)
|
|
356
|
+
logger?.warn?.(`唤醒器 tick 异常 ${error instanceof Error ? error.message : String(error)}`)
|
|
357
|
+
} finally {
|
|
358
|
+
busy = false
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
const timer = setInterval(() => { void tick() }, TICK_MS)
|
|
363
|
+
if (typeof timer.unref === 'function') timer.unref()
|
|
364
|
+
|
|
365
|
+
return {
|
|
366
|
+
alarms,
|
|
367
|
+
tick,
|
|
368
|
+
stop: () => clearInterval(timer),
|
|
369
|
+
status: () => ({ file: STATE_FILE, count: alarms.list().length, alarms: alarms.list() }),
|
|
370
|
+
}
|
|
371
|
+
}
|