@goodandready/dsh-messenger-gateway 0.4.3 → 0.4.4
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/lib/adapters/telegram-inbound.js +141 -141
- package/lib/adapters/telegram.js +596 -596
- package/lib/alerts.js +105 -105
- package/lib/api-health.js +38 -38
- package/lib/commands.js +126 -126
- package/lib/forum-mirror.js +105 -105
- package/lib/gateway-callbacks.js +187 -187
- package/lib/gateway-commands.js +584 -584
- package/lib/gateway-turn.js +288 -285
- package/lib/gateway.js +8 -10
- package/lib/index.js +540 -540
- package/lib/scheduler.js +169 -169
- package/lib/telegram-errors.js +0 -0
- package/package.json +1 -1
package/lib/scheduler.js
CHANGED
|
@@ -1,170 +1,170 @@
|
|
|
1
|
-
import { readFile } from 'node:fs/promises'
|
|
2
|
-
import { randomUUID } from 'node:crypto'
|
|
3
|
-
import { writeJsonAtomic } from './storage-atomic.js'
|
|
4
|
-
|
|
5
|
-
export function parseRelativeTime(input) {
|
|
6
|
-
if (!input) return null
|
|
7
|
-
const raw = String(input).trim().toLowerCase()
|
|
8
|
-
const match = /^(\d+)\s*(s|sec|m|min|h|hr|d|day|秒|分|小时|天)?$/i.exec(raw)
|
|
9
|
-
if (!match) return null
|
|
10
|
-
|
|
11
|
-
const val = parseInt(match[1], 10)
|
|
12
|
-
if (!Number.isFinite(val) || val <= 0) return null
|
|
13
|
-
|
|
14
|
-
const unit = (match[2] || 'm').toLowerCase()
|
|
15
|
-
if (['s', 'sec', '秒'].includes(unit)) return val * 1000
|
|
16
|
-
if (['m', 'min', '分'].includes(unit)) return val * 60 * 1000
|
|
17
|
-
if (['h', 'hr', '小时'].includes(unit)) return val * 3600 * 1000
|
|
18
|
-
if (['d', 'day', '天'].includes(unit)) return val * 86400 * 1000
|
|
19
|
-
|
|
20
|
-
return null
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function formatRemaining(ms, locale = 'en') {
|
|
24
|
-
const isZh = String(locale || '').toLowerCase().startsWith('zh')
|
|
25
|
-
if (ms <= 0) return isZh ? '刚刚' : 'now'
|
|
26
|
-
const sec = Math.ceil(ms / 1000)
|
|
27
|
-
if (sec < 60) return isZh ? `${sec}秒` : `${sec}s`
|
|
28
|
-
const min = Math.ceil(sec / 60)
|
|
29
|
-
if (min < 60) return isZh ? `${min}分` : `${min}m`
|
|
30
|
-
const hr = Math.floor(min / 60)
|
|
31
|
-
const remMin = min % 60
|
|
32
|
-
if (isZh) return remMin ? `${hr}小时 ${remMin}分` : `${hr}小时`
|
|
33
|
-
return remMin ? `${hr}h ${remMin}m` : `${hr}h`
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export function createScheduler(filePath, onDue, options = {}) {
|
|
37
|
-
const logger = options?.logger || console
|
|
38
|
-
let tasks = []
|
|
39
|
-
let loaded = false
|
|
40
|
-
let timer = null
|
|
41
|
-
|
|
42
|
-
async function load() {
|
|
43
|
-
try {
|
|
44
|
-
const raw = await readFile(filePath, 'utf8')
|
|
45
|
-
tasks = JSON.parse(raw)
|
|
46
|
-
} catch (err) {
|
|
47
|
-
if (err?.code !== 'ENOENT') {
|
|
48
|
-
logger?.debug?.('[dsh-messenger-gateway] scheduler load error:', err?.message || err)
|
|
49
|
-
}
|
|
50
|
-
tasks = []
|
|
51
|
-
}
|
|
52
|
-
loaded = true
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
const RETENTION_MS = 7 * 86400 * 1000
|
|
56
|
-
|
|
57
|
-
async function save() {
|
|
58
|
-
if (!filePath) return
|
|
59
|
-
try {
|
|
60
|
-
const now = Date.now()
|
|
61
|
-
tasks = tasks.filter((t) => {
|
|
62
|
-
if (t.status === 'pending') return true
|
|
63
|
-
const finishTime = t.firedAt || t.createdAt || 0
|
|
64
|
-
return (now - finishTime) < RETENTION_MS
|
|
65
|
-
})
|
|
66
|
-
await writeJsonAtomic(filePath, tasks)
|
|
67
|
-
} catch (err) {
|
|
68
|
-
logger?.warn?.('[dsh-messenger-gateway] scheduler save failed:', err?.message || err)
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
async function checkDue() {
|
|
73
|
-
if (!loaded) await load()
|
|
74
|
-
const now = Date.now()
|
|
75
|
-
const due = tasks.filter((t) => t.status === 'pending' && t.dueAt <= now)
|
|
76
|
-
|
|
77
|
-
for (const task of due) {
|
|
78
|
-
if (task.recurring && Number(task.intervalMs) > 0) {
|
|
79
|
-
task.dueAt = now + Number(task.intervalMs)
|
|
80
|
-
task.lastFiredAt = now
|
|
81
|
-
} else {
|
|
82
|
-
task.status = 'fired'
|
|
83
|
-
task.firedAt = now
|
|
84
|
-
}
|
|
85
|
-
if (typeof onDue === 'function') {
|
|
86
|
-
try {
|
|
87
|
-
await onDue(task)
|
|
88
|
-
} catch (err) {
|
|
89
|
-
logger?.warn?.('[dsh-messenger-gateway] scheduler task execution failed:', err?.message || err)
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
if (due.length > 0) {
|
|
95
|
-
await save()
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function start(intervalMs = 5000) {
|
|
100
|
-
if (timer) clearInterval(timer)
|
|
101
|
-
timer = setInterval(() => {
|
|
102
|
-
checkDue().catch(() => {})
|
|
103
|
-
}, intervalMs)
|
|
104
|
-
timer.unref?.()
|
|
105
|
-
checkDue().catch(() => {})
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function stop() {
|
|
109
|
-
if (timer) {
|
|
110
|
-
clearInterval(timer)
|
|
111
|
-
timer = null
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
async function schedule(taskData) {
|
|
116
|
-
if (!loaded) await load()
|
|
117
|
-
const id = taskData.id || randomUUID().slice(0, 8)
|
|
118
|
-
const task = {
|
|
119
|
-
id,
|
|
120
|
-
platform: taskData.platform || 'telegram',
|
|
121
|
-
chatId: taskData.chatId,
|
|
122
|
-
threadId: taskData.threadId || 0,
|
|
123
|
-
userId: taskData.userId,
|
|
124
|
-
text: taskData.text,
|
|
125
|
-
prompt: taskData.prompt || taskData.text || '',
|
|
126
|
-
dueAt: taskData.dueAt,
|
|
127
|
-
createdAt: Date.now(),
|
|
128
|
-
status: 'pending',
|
|
129
|
-
recurring: Boolean(taskData.recurring),
|
|
130
|
-
intervalMs: taskData.intervalMs || 0,
|
|
131
|
-
cronExpr: taskData.cronExpr || '',
|
|
132
|
-
}
|
|
133
|
-
tasks.push(task)
|
|
134
|
-
await save()
|
|
135
|
-
return task
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async function list(chatId) {
|
|
139
|
-
if (!loaded) await load()
|
|
140
|
-
const now = Date.now()
|
|
141
|
-
return tasks.filter((t) => (chatId ? t.chatId === chatId : true) && t.status === 'pending' && t.dueAt > now)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
async function listRecurring(chatId) {
|
|
145
|
-
if (!loaded) await load()
|
|
146
|
-
return tasks.filter((t) => (chatId ? t.chatId === chatId : true) && t.status === 'pending' && t.recurring)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
async function cancel(id, chatId) {
|
|
150
|
-
if (!loaded) await load()
|
|
151
|
-
const task = tasks.find((t) => t.id === id && (chatId ? t.chatId === chatId : true) && t.status === 'pending')
|
|
152
|
-
if (!task) return false
|
|
153
|
-
task.status = 'cancelled'
|
|
154
|
-
await save()
|
|
155
|
-
return true
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
return {
|
|
159
|
-
load,
|
|
160
|
-
save,
|
|
161
|
-
start,
|
|
162
|
-
stop,
|
|
163
|
-
schedule,
|
|
164
|
-
list,
|
|
165
|
-
listRecurring,
|
|
166
|
-
cancel,
|
|
167
|
-
checkDue,
|
|
168
|
-
getTasks: () => tasks,
|
|
169
|
-
}
|
|
1
|
+
import { readFile } from 'node:fs/promises'
|
|
2
|
+
import { randomUUID } from 'node:crypto'
|
|
3
|
+
import { writeJsonAtomic } from './storage-atomic.js'
|
|
4
|
+
|
|
5
|
+
export function parseRelativeTime(input) {
|
|
6
|
+
if (!input) return null
|
|
7
|
+
const raw = String(input).trim().toLowerCase()
|
|
8
|
+
const match = /^(\d+)\s*(s|sec|m|min|h|hr|d|day|秒|分|小时|天)?$/i.exec(raw)
|
|
9
|
+
if (!match) return null
|
|
10
|
+
|
|
11
|
+
const val = parseInt(match[1], 10)
|
|
12
|
+
if (!Number.isFinite(val) || val <= 0) return null
|
|
13
|
+
|
|
14
|
+
const unit = (match[2] || 'm').toLowerCase()
|
|
15
|
+
if (['s', 'sec', '秒'].includes(unit)) return val * 1000
|
|
16
|
+
if (['m', 'min', '分'].includes(unit)) return val * 60 * 1000
|
|
17
|
+
if (['h', 'hr', '小时'].includes(unit)) return val * 3600 * 1000
|
|
18
|
+
if (['d', 'day', '天'].includes(unit)) return val * 86400 * 1000
|
|
19
|
+
|
|
20
|
+
return null
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function formatRemaining(ms, locale = 'en') {
|
|
24
|
+
const isZh = String(locale || '').toLowerCase().startsWith('zh')
|
|
25
|
+
if (ms <= 0) return isZh ? '刚刚' : 'now'
|
|
26
|
+
const sec = Math.ceil(ms / 1000)
|
|
27
|
+
if (sec < 60) return isZh ? `${sec}秒` : `${sec}s`
|
|
28
|
+
const min = Math.ceil(sec / 60)
|
|
29
|
+
if (min < 60) return isZh ? `${min}分` : `${min}m`
|
|
30
|
+
const hr = Math.floor(min / 60)
|
|
31
|
+
const remMin = min % 60
|
|
32
|
+
if (isZh) return remMin ? `${hr}小时 ${remMin}分` : `${hr}小时`
|
|
33
|
+
return remMin ? `${hr}h ${remMin}m` : `${hr}h`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function createScheduler(filePath, onDue, options = {}) {
|
|
37
|
+
const logger = options?.logger || console
|
|
38
|
+
let tasks = []
|
|
39
|
+
let loaded = false
|
|
40
|
+
let timer = null
|
|
41
|
+
|
|
42
|
+
async function load() {
|
|
43
|
+
try {
|
|
44
|
+
const raw = await readFile(filePath, 'utf8')
|
|
45
|
+
tasks = JSON.parse(raw)
|
|
46
|
+
} catch (err) {
|
|
47
|
+
if (err?.code !== 'ENOENT') {
|
|
48
|
+
logger?.debug?.('[dsh-messenger-gateway] scheduler load error:', err?.message || err)
|
|
49
|
+
}
|
|
50
|
+
tasks = []
|
|
51
|
+
}
|
|
52
|
+
loaded = true
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const RETENTION_MS = 7 * 86400 * 1000
|
|
56
|
+
|
|
57
|
+
async function save() {
|
|
58
|
+
if (!filePath) return
|
|
59
|
+
try {
|
|
60
|
+
const now = Date.now()
|
|
61
|
+
tasks = tasks.filter((t) => {
|
|
62
|
+
if (t.status === 'pending') return true
|
|
63
|
+
const finishTime = t.firedAt || t.createdAt || 0
|
|
64
|
+
return (now - finishTime) < RETENTION_MS
|
|
65
|
+
})
|
|
66
|
+
await writeJsonAtomic(filePath, tasks)
|
|
67
|
+
} catch (err) {
|
|
68
|
+
logger?.warn?.('[dsh-messenger-gateway] scheduler save failed:', err?.message || err)
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function checkDue() {
|
|
73
|
+
if (!loaded) await load()
|
|
74
|
+
const now = Date.now()
|
|
75
|
+
const due = tasks.filter((t) => t.status === 'pending' && t.dueAt <= now)
|
|
76
|
+
|
|
77
|
+
for (const task of due) {
|
|
78
|
+
if (task.recurring && Number(task.intervalMs) > 0) {
|
|
79
|
+
task.dueAt = now + Number(task.intervalMs)
|
|
80
|
+
task.lastFiredAt = now
|
|
81
|
+
} else {
|
|
82
|
+
task.status = 'fired'
|
|
83
|
+
task.firedAt = now
|
|
84
|
+
}
|
|
85
|
+
if (typeof onDue === 'function') {
|
|
86
|
+
try {
|
|
87
|
+
await onDue(task)
|
|
88
|
+
} catch (err) {
|
|
89
|
+
logger?.warn?.('[dsh-messenger-gateway] scheduler task execution failed:', err?.message || err)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (due.length > 0) {
|
|
95
|
+
await save()
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function start(intervalMs = 5000) {
|
|
100
|
+
if (timer) clearInterval(timer)
|
|
101
|
+
timer = setInterval(() => {
|
|
102
|
+
checkDue().catch(() => {})
|
|
103
|
+
}, intervalMs)
|
|
104
|
+
timer.unref?.()
|
|
105
|
+
checkDue().catch(() => {})
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function stop() {
|
|
109
|
+
if (timer) {
|
|
110
|
+
clearInterval(timer)
|
|
111
|
+
timer = null
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async function schedule(taskData) {
|
|
116
|
+
if (!loaded) await load()
|
|
117
|
+
const id = taskData.id || randomUUID().slice(0, 8)
|
|
118
|
+
const task = {
|
|
119
|
+
id,
|
|
120
|
+
platform: taskData.platform || 'telegram',
|
|
121
|
+
chatId: taskData.chatId,
|
|
122
|
+
threadId: taskData.threadId || 0,
|
|
123
|
+
userId: taskData.userId,
|
|
124
|
+
text: taskData.text,
|
|
125
|
+
prompt: taskData.prompt || taskData.text || '',
|
|
126
|
+
dueAt: taskData.dueAt,
|
|
127
|
+
createdAt: Date.now(),
|
|
128
|
+
status: 'pending',
|
|
129
|
+
recurring: Boolean(taskData.recurring),
|
|
130
|
+
intervalMs: taskData.intervalMs || 0,
|
|
131
|
+
cronExpr: taskData.cronExpr || '',
|
|
132
|
+
}
|
|
133
|
+
tasks.push(task)
|
|
134
|
+
await save()
|
|
135
|
+
return task
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async function list(chatId) {
|
|
139
|
+
if (!loaded) await load()
|
|
140
|
+
const now = Date.now()
|
|
141
|
+
return tasks.filter((t) => (chatId ? t.chatId === chatId : true) && t.status === 'pending' && t.dueAt > now)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async function listRecurring(chatId) {
|
|
145
|
+
if (!loaded) await load()
|
|
146
|
+
return tasks.filter((t) => (chatId ? t.chatId === chatId : true) && t.status === 'pending' && t.recurring)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function cancel(id, chatId) {
|
|
150
|
+
if (!loaded) await load()
|
|
151
|
+
const task = tasks.find((t) => t.id === id && (chatId ? t.chatId === chatId : true) && t.status === 'pending')
|
|
152
|
+
if (!task) return false
|
|
153
|
+
task.status = 'cancelled'
|
|
154
|
+
await save()
|
|
155
|
+
return true
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
load,
|
|
160
|
+
save,
|
|
161
|
+
start,
|
|
162
|
+
stop,
|
|
163
|
+
schedule,
|
|
164
|
+
list,
|
|
165
|
+
listRecurring,
|
|
166
|
+
cancel,
|
|
167
|
+
checkDue,
|
|
168
|
+
getTasks: () => tasks,
|
|
169
|
+
}
|
|
170
170
|
}
|
package/lib/telegram-errors.js
CHANGED
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@goodandready/dsh-messenger-gateway",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"description": "Telegram messenger bridge for DeepSeek Harness: sessions, steer, homes, inline asks, notify bridge, and optional TTS voice notes.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|