@goodandready/dsh-messenger-gateway 0.4.42 → 0.4.44
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 +3 -0
- package/lib/adapters/telegram.js +16 -10
- package/lib/gateway-commands.js +16 -2
- package/lib/gateway-turn.js +0 -0
- package/lib/gateway.js +28 -8
- package/lib/index.js +89 -50
- package/lib/outbox.js +135 -33
- package/lib/settings-bridge.js +9 -0
- package/lib/stream.js +0 -0
- package/lib/telegram-errors.js +0 -0
- package/lib/text.js +50 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -154,9 +154,12 @@ In addition to Telegram, outbound messages can be dispatched to Discord and Slac
|
|
|
154
154
|
- Delivered updates are deduplicated across polling and webhook transports and the poll offset survives restarts — commands never execute twice
|
|
155
155
|
- Artifact SVG entering the dashboard store is sanitized (script blocks, event handlers and `javascript:` URLs stripped)
|
|
156
156
|
- Pending interactive asks are bounded (200 concurrent) and the ask timeout is clamped to 10 minutes
|
|
157
|
+
- Long replies are chunked by raw markdown (fence-aware) so no HTML tag is ever cut at a message boundary; `/voice summary` and `/keyboard` persist through the settings writer
|
|
157
158
|
- `telegram.apiRoot` with plain `http` is only accepted for loopback hosts (a local Bot API server); the webhook URL is logged without its secret path
|
|
158
159
|
- `webhooks.enabled: false` removes the messenger write routes entirely; GET `/update` and `/messenger` require trusted callers
|
|
159
160
|
- `webAppEnabled: false` takes the whole Mini App surface down (dashboard and every `/api/*` answer 404) and clears the menu button
|
|
161
|
+
- The outbox queue (alerts, cron, autonomous messages) is multi-writer safe: all mutations take an exclusive lock and reload on-disk state, so two gateway processes sharing a home neither lose nor duplicate jobs; DLQ alerts bypass the queue and are rate-limited per chat
|
|
162
|
+
- Note: turn replies to the user are streamed directly and are not covered by the outbox retry guarantee (by design — streaming edits cannot be replayed)
|
|
160
163
|
- Inline keyboard `replyMarkup` sent via `/messenger/send` is schema-validated (button text/actions, sizes) before delivery
|
|
161
164
|
- All state files (pairing, outbox, voice prefs) are written with `0600` permissions inside `0700` directories
|
|
162
165
|
- `sessionList`: `true` (default) — enables `/sessions` and `/resume` commands in chat
|
package/lib/adapters/telegram.js
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
cacheName,
|
|
8
8
|
extOf, saveToCache, safeName, TELEGRAM_MAX_DOC_BYTES,
|
|
9
9
|
} from '../media.js'
|
|
10
|
-
import { splitText } from '../text.js'
|
|
10
|
+
import { splitText, splitMarkdownForTelegram } from '../text.js'
|
|
11
11
|
import { prepareTelegramText } from '../telegram-format.js'
|
|
12
12
|
import { normalizeTelegramCommands } from '../commands.js'
|
|
13
13
|
import { normalizeThreadId, telegramThreadParams } from '../topics.js'
|
|
@@ -784,19 +784,25 @@ export class TelegramAdapter {
|
|
|
784
784
|
}
|
|
785
785
|
|
|
786
786
|
async sendFormattedMessage(chatId, replyTo, text, payload, threadId, replyMarkup) {
|
|
787
|
-
const { text: formatted, parseMode } = this.formatOutgoingText(text, payload)
|
|
788
|
-
const chunks = splitText(formatted, TELEGRAM_MAX)
|
|
789
|
-
const plainChunks = splitText(text, TELEGRAM_MAX)
|
|
790
787
|
const effectiveMarkup = replyMarkup !== undefined
|
|
791
788
|
? replyMarkup
|
|
792
789
|
: (this.quickActions && !threadId
|
|
793
790
|
? buildQuickActionsKeyboard(this.webAppUrl)
|
|
794
791
|
: (!threadId ? REMOVE_REPLY_KEYBOARD : undefined))
|
|
795
|
-
|
|
792
|
+
// Chunk the RAW markdown so that every Telegram message carries complete,
|
|
793
|
+
// unclosed-tag-free HTML (#229): splitting after the HTML conversion could
|
|
794
|
+
// cut a tag at the chunk boundary.
|
|
795
|
+
const rawChunks = splitMarkdownForTelegram(
|
|
796
|
+
text,
|
|
797
|
+
TELEGRAM_MAX,
|
|
798
|
+
(s) => this.formatOutgoingText(s, payload).text,
|
|
799
|
+
)
|
|
800
|
+
for (let i = 0; i < rawChunks.length; i++) {
|
|
801
|
+
const { text: formatted, parseMode } = this.formatOutgoingText(rawChunks[i], payload)
|
|
796
802
|
const params = {
|
|
797
803
|
chat_id: chatId,
|
|
798
|
-
text:
|
|
799
|
-
reply_to_message_id: replyTo,
|
|
804
|
+
text: formatted,
|
|
805
|
+
reply_to_message_id: i === 0 ? replyTo : undefined,
|
|
800
806
|
reply_markup: i === 0 ? effectiveMarkup : undefined,
|
|
801
807
|
...telegramThreadParams(threadId),
|
|
802
808
|
}
|
|
@@ -816,8 +822,8 @@ export class TelegramAdapter {
|
|
|
816
822
|
try {
|
|
817
823
|
await this.call('sendMessage', {
|
|
818
824
|
chat_id: chatId,
|
|
819
|
-
text:
|
|
820
|
-
reply_to_message_id: replyTo,
|
|
825
|
+
text: rawChunks[i],
|
|
826
|
+
reply_to_message_id: i === 0 ? replyTo : undefined,
|
|
821
827
|
reply_markup: i === 0 ? effectiveMarkup : undefined,
|
|
822
828
|
...telegramThreadParams(threadId),
|
|
823
829
|
})
|
|
@@ -826,7 +832,7 @@ export class TelegramAdapter {
|
|
|
826
832
|
this.logger?.warn?.(`telegram plain send topic gone (thread ${threadId}), fallback main chat: ${err2.message}`)
|
|
827
833
|
await this.call('sendMessage', {
|
|
828
834
|
chat_id: chatId,
|
|
829
|
-
text:
|
|
835
|
+
text: rawChunks[i],
|
|
830
836
|
reply_to_message_id: replyTo,
|
|
831
837
|
reply_markup: i === 0 ? effectiveMarkup : undefined,
|
|
832
838
|
})
|
package/lib/gateway-commands.js
CHANGED
|
@@ -862,9 +862,15 @@ export async function handleGatewayCommand(gw, key, text, input) {
|
|
|
862
862
|
if (sub === 'summary') {
|
|
863
863
|
const val = parts[2]?.toLowerCase()
|
|
864
864
|
if (val === 'on' || val === 'off') {
|
|
865
|
+
const on = val === 'on'
|
|
865
866
|
if (!gw.config.tts) gw.config.tts = {}
|
|
866
|
-
gw.config.tts.voiceSummary =
|
|
867
|
-
|
|
867
|
+
gw.config.tts.voiceSummary = on
|
|
868
|
+
// Persist through the settings writer (#222) so /voice summary
|
|
869
|
+
// survives config syncs and /config reports the live state.
|
|
870
|
+
gw.hooks?.persistVoiceSummary?.(on).catch((e) => {
|
|
871
|
+
gw.logger?.debug?.('persistVoiceSummary:', e?.message || e)
|
|
872
|
+
})
|
|
873
|
+
return reply(`Voice summary (TL;DR): ${on ? 'enabled' : 'disabled'}`)
|
|
868
874
|
}
|
|
869
875
|
const state = gw.config.tts?.voiceSummary ? 'on' : 'off'
|
|
870
876
|
return reply(`Voice summary (TL;DR): ${state}\nToggle: <code>/voice summary on|off</code>`)
|
|
@@ -940,12 +946,20 @@ export async function handleGatewayCommand(gw, key, text, input) {
|
|
|
940
946
|
const tgAdapter = gw.getAdapter('telegram')
|
|
941
947
|
if (sub === 'on') {
|
|
942
948
|
if (tgAdapter) tgAdapter.quickActions = true
|
|
949
|
+
// Persist through the settings writer (#222) so /keyboard on|off
|
|
950
|
+
// survives config syncs and /config reports the live state.
|
|
951
|
+
gw.hooks?.persistQuickActions?.(true)?.catch((e) => {
|
|
952
|
+
gw.logger?.debug?.('persistQuickActions:', e?.message || e)
|
|
953
|
+
})
|
|
943
954
|
return reply('Quick action keyboard enabled.', {
|
|
944
955
|
replyMarkup: buildQuickActionsKeyboard(),
|
|
945
956
|
})
|
|
946
957
|
}
|
|
947
958
|
if (sub === 'off') {
|
|
948
959
|
if (tgAdapter) tgAdapter.quickActions = false
|
|
960
|
+
gw.hooks?.persistQuickActions?.(false)?.catch((e) => {
|
|
961
|
+
gw.logger?.debug?.('persistQuickActions:', e?.message || e)
|
|
962
|
+
})
|
|
949
963
|
return reply('Quick action keyboard disabled.', {
|
|
950
964
|
replyMarkup: REMOVE_REPLY_KEYBOARD,
|
|
951
965
|
})
|
package/lib/gateway-turn.js
CHANGED
|
File without changes
|
package/lib/gateway.js
CHANGED
|
@@ -48,6 +48,8 @@ export class Gateway {
|
|
|
48
48
|
this.chats = new Map()
|
|
49
49
|
// Single-flight chat creation (#215): key -> in-flight createChat promise
|
|
50
50
|
this.chatCreating = new Map()
|
|
51
|
+
// DLQ alert rate limiting (#226): chatId -> last alert timestamp
|
|
52
|
+
this.dlqAlertAt = new Map()
|
|
51
53
|
this.sessionToChat = new Map()
|
|
52
54
|
this.sessionToThread = new Map()
|
|
53
55
|
this.threadToSession = new Map()
|
|
@@ -76,12 +78,7 @@ export class Gateway {
|
|
|
76
78
|
await this.messengerSend(job.target, job.payload)
|
|
77
79
|
},
|
|
78
80
|
onDlq: (job, err) => {
|
|
79
|
-
this.
|
|
80
|
-
code: 'OUTBOX_DLQ',
|
|
81
|
-
message: `Outbox message failed permanently to ${job.target?.chatId}: ${err.message}`,
|
|
82
|
-
chatId: job.target?.chatId,
|
|
83
|
-
threadId: job.target?.threadId,
|
|
84
|
-
}).catch((e) => this.recordApiFailure('outbox.dlq_alert', e))
|
|
81
|
+
this.notifyDlq(job, err)
|
|
85
82
|
},
|
|
86
83
|
logger: this.logger,
|
|
87
84
|
})
|
|
@@ -227,15 +224,38 @@ export class Gateway {
|
|
|
227
224
|
return out
|
|
228
225
|
}
|
|
229
226
|
|
|
230
|
-
|
|
227
|
+
// DLQ alert delivery (#226): sent DIRECTLY (bypassing the outbox — alerting
|
|
228
|
+
// about the queue through the queue is the recursion) and deduplicated per
|
|
229
|
+
// chat (one alert per chat per 60s) so a permanently failing chat does not
|
|
230
|
+
// generate an alert storm.
|
|
231
|
+
notifyDlq(job, err) {
|
|
232
|
+
const chatId = String(job.target?.chatId || '')
|
|
233
|
+
const now = Date.now()
|
|
234
|
+
const last = this.dlqAlertAt.get(chatId) || 0
|
|
235
|
+
if (now - last < 60_000) {
|
|
236
|
+
this.logger?.warn?.(`[outbox] DLQ alert for ${chatId} suppressed (rate limit): ${err?.message || err}`)
|
|
237
|
+
return
|
|
238
|
+
}
|
|
239
|
+
this.dlqAlertAt.set(chatId, now)
|
|
240
|
+
this.sendAlert('error', {
|
|
241
|
+
code: 'OUTBOX_DLQ',
|
|
242
|
+
message: `Outbox message failed permanently to ${job.target?.chatId}: ${err.message}`,
|
|
243
|
+
chatId: job.target?.chatId,
|
|
244
|
+
threadId: job.target?.threadId,
|
|
245
|
+
}, { bypassOutbox: true }).catch((e) => this.recordApiFailure('outbox.dlq_alert', e))
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async sendAlert(type, payload = {}, opts = {}) {
|
|
231
249
|
try {
|
|
232
250
|
const target = resolveAlertTarget(this)
|
|
233
251
|
if (!target) return
|
|
252
|
+
if (this.config.telegram?.alerts?.enabled === false) return
|
|
234
253
|
const allowedEvents = this.config.telegram?.alerts?.events || ['error', 'pairing']
|
|
235
254
|
if (type !== 'status' && !allowedEvents.includes(type)) return
|
|
236
255
|
|
|
237
256
|
const text = formatAlertMessage(type, payload)
|
|
238
|
-
|
|
257
|
+
// bypassOutbox: DLQ alerts must not re-enter the queue they report on (#226)
|
|
258
|
+
await this.sendToMessenger(target, { text }, opts.bypassOutbox ? { reliable: false } : opts)
|
|
239
259
|
} catch (err) {
|
|
240
260
|
this.ctx.logger?.warn?.(`sendAlert (${type}): ${err.message}`)
|
|
241
261
|
}
|
package/lib/index.js
CHANGED
|
@@ -266,35 +266,65 @@ export function apply(ctx, config) {
|
|
|
266
266
|
gateway = new Gateway(ctx, effective, {
|
|
267
267
|
persistAllowedUserIds: async (ids) => {
|
|
268
268
|
if (!settingsApi?.isReady?.()) return
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
269
|
+
await settingsApi.writeExclusive(async (api) => {
|
|
270
|
+
const cur = source()
|
|
271
|
+
await api.replace(Config({
|
|
272
|
+
...cur,
|
|
273
|
+
telegram: { ...cur.telegram, allowedUserIds: ids },
|
|
274
|
+
}))
|
|
275
|
+
})
|
|
274
276
|
},
|
|
275
277
|
persistHome: async ({ chatId, threadId }) => {
|
|
276
278
|
if (!settingsApi?.isReady?.()) throw new Error('settings not ready')
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
279
|
+
await settingsApi.writeExclusive(async (api) => {
|
|
280
|
+
const cur = source()
|
|
281
|
+
await api.replace(Config({
|
|
282
|
+
...cur,
|
|
283
|
+
telegram: { ...cur.telegram, homeChatId: chatId, homeThreadId: threadId || 0 },
|
|
284
|
+
}))
|
|
285
|
+
})
|
|
282
286
|
},
|
|
283
287
|
persistHomes: async (nextTelegram) => {
|
|
284
288
|
if (!settingsApi?.isReady?.()) throw new Error('settings not ready')
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
289
|
+
await settingsApi.writeExclusive(async (api) => {
|
|
290
|
+
const cur = source()
|
|
291
|
+
await api.replace(Config({
|
|
292
|
+
...cur,
|
|
293
|
+
telegram: { ...cur.telegram, ...nextTelegram },
|
|
294
|
+
}))
|
|
295
|
+
})
|
|
290
296
|
},
|
|
291
297
|
persistAgentModel: async ({ provider, model }) => {
|
|
292
298
|
if (!settingsApi?.isReady?.()) return
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
299
|
+
await settingsApi.writeExclusive(async (api) => {
|
|
300
|
+
const cur = source()
|
|
301
|
+
await api.replace(Config({
|
|
302
|
+
...cur,
|
|
303
|
+
agent: { ...cur.agent, provider, model },
|
|
304
|
+
}))
|
|
305
|
+
})
|
|
306
|
+
},
|
|
307
|
+
// Runtime toggles from chat commands (#222): /voice summary and
|
|
308
|
+
// /keyboard on|off now survive config syncs like every other writer.
|
|
309
|
+
persistVoiceSummary: async (on) => {
|
|
310
|
+
if (!settingsApi?.isReady?.()) return
|
|
311
|
+
await settingsApi.writeExclusive(async (api) => {
|
|
312
|
+
const cur = source()
|
|
313
|
+
await api.replace(Config({
|
|
314
|
+
...cur,
|
|
315
|
+
tts: { ...cur.tts, voiceSummary: on },
|
|
316
|
+
}))
|
|
317
|
+
})
|
|
318
|
+
},
|
|
319
|
+
persistQuickActions: async (on) => {
|
|
320
|
+
if (!settingsApi?.isReady?.()) return
|
|
321
|
+
await settingsApi.writeExclusive(async (api) => {
|
|
322
|
+
const cur = source()
|
|
323
|
+
await api.replace(Config({
|
|
324
|
+
...cur,
|
|
325
|
+
telegram: { ...cur.telegram, quickActions: on },
|
|
326
|
+
}))
|
|
327
|
+
})
|
|
298
328
|
},
|
|
299
329
|
})
|
|
300
330
|
gateway.start().catch((err) => ctx.logger?.warn?.(`dsh-messenger-gateway: ${err.message}`))
|
|
@@ -321,6 +351,11 @@ export function apply(ctx, config) {
|
|
|
321
351
|
if (!api) throw new Error('settings not ready')
|
|
322
352
|
return api.update(patch)
|
|
323
353
|
},
|
|
354
|
+
writeExclusive: (fn) => {
|
|
355
|
+
const api = settingsBridge.getSettingsApi()
|
|
356
|
+
if (!api) throw new Error('settings not ready')
|
|
357
|
+
return api.writeExclusive(fn)
|
|
358
|
+
},
|
|
324
359
|
isReady: () => Boolean(settingsBridge.getSettingsApi()),
|
|
325
360
|
}
|
|
326
361
|
|
|
@@ -690,37 +725,41 @@ export function apply(ctx, config) {
|
|
|
690
725
|
let payload = parsedConfig.data
|
|
691
726
|
if (payload && typeof payload.config === 'object') payload = payload.config
|
|
692
727
|
try {
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
728
|
+
// Serialized through the settings write mutex (#221): concurrent PUT
|
|
729
|
+
// /config and chat-command persists no longer lose updates.
|
|
730
|
+
await settingsApi.writeExclusive(async (api) => {
|
|
731
|
+
const cur = source()
|
|
732
|
+
const nextTg = { ...cur.telegram, ...(payload.telegram || {}) }
|
|
733
|
+
if (payload.telegram && !String(payload.telegram.botToken || '').trim()) nextTg.botToken = cur.telegram.botToken
|
|
734
|
+
// proxyUrl is a secret: empty value or an echoed mask means "keep the
|
|
735
|
+
// stored proxy" (#200)
|
|
736
|
+
if (payload.telegram && Object.prototype.hasOwnProperty.call(payload.telegram, 'proxyUrl')) {
|
|
737
|
+
const incoming = String(payload.telegram.proxyUrl || '').trim()
|
|
738
|
+
if (!incoming || incoming.includes(':***@') || incoming === maskProxyUrl(cur.telegram.proxyUrl || '')) {
|
|
739
|
+
nextTg.proxyUrl = cur.telegram.proxyUrl
|
|
740
|
+
}
|
|
702
741
|
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
})
|
|
742
|
+
if (payload.telegram && Object.prototype.hasOwnProperty.call(payload.telegram, 'webhookSecret') && !String(payload.telegram.webhookSecret || '').trim()) {
|
|
743
|
+
nextTg.webhookSecret = cur.telegram.webhookSecret
|
|
744
|
+
}
|
|
745
|
+
if (payload.telegram?.notifyBridge) {
|
|
746
|
+
nextTg.notifyBridge = { ...cur.telegram.notifyBridge, ...payload.telegram.notifyBridge }
|
|
747
|
+
}
|
|
748
|
+
const nextDiscord = { ...(cur.discord || {}), ...(payload.discord || {}) }
|
|
749
|
+
if (payload.discord && !String(payload.discord.botToken || '').trim()) nextDiscord.botToken = cur.discord?.botToken
|
|
750
|
+
const nextSlack = { ...(cur.slack || {}), ...(payload.slack || {}) }
|
|
751
|
+
if (payload.slack && !String(payload.slack.botToken || '').trim()) nextSlack.botToken = cur.slack?.botToken
|
|
752
|
+
await api.replace(Config({
|
|
753
|
+
...cur,
|
|
754
|
+
...payload,
|
|
755
|
+
telegram: nextTg,
|
|
756
|
+
discord: nextDiscord,
|
|
757
|
+
slack: nextSlack,
|
|
758
|
+
tts: { ...cur.tts, ...(payload.tts || {}) },
|
|
759
|
+
agent: { ...cur.agent, ...(payload.agent || {}) },
|
|
760
|
+
media: { ...cur.media, ...(payload.media || {}) },
|
|
761
|
+
}))
|
|
762
|
+
})
|
|
724
763
|
sync()
|
|
725
764
|
writeJson(res, 200, { ok: true, config: publicConfig(source(), getGw(), ctx) })
|
|
726
765
|
} catch (err) {
|
package/lib/outbox.js
CHANGED
|
@@ -1,21 +1,67 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Outbound Transactional Queue (Outbox DLQ) with Offline Recovery & Backoff (#155).
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Guarantee scope (#217): the queue covers alerts, cron prompts and
|
|
5
|
+
* autonomous messages — everything sent through sendToMessenger. Turn
|
|
6
|
+
* replies to the user are streamed directly (adapter.sendReply) because
|
|
7
|
+
* streaming edits cannot be replayed from a queue; that limitation is
|
|
8
|
+
* documented in DESIGN.md.
|
|
9
|
+
*
|
|
10
|
+
* Single-writer safety (#218): every mutation (enqueue / flush / retry /
|
|
11
|
+
* clear) takes an exclusive lock file scoped to the queue path and reloads
|
|
12
|
+
* the on-disk state inside the lock — the disk copy is the source of truth,
|
|
13
|
+
* so two gateway processes sharing a home neither lose nor duplicate jobs.
|
|
14
|
+
*
|
|
6
15
|
* - Atomic persistent disk queue (~/.dsh/storages/outbox-queue.json).
|
|
7
16
|
* - Exponential backoff with jitter and Telegram 429 `retry_after` awareness.
|
|
8
17
|
* - Dead Letter Queue (DLQ) for items failing after max retries or terminal errors.
|
|
9
18
|
* - Recovery of pending uncommitted outbound jobs on gateway startup.
|
|
10
19
|
*/
|
|
11
20
|
|
|
12
|
-
import { existsSync, readFileSync } from 'node:fs'
|
|
13
|
-
import { join } from 'node:path'
|
|
21
|
+
import { existsSync, readFileSync, openSync, writeSync, closeSync, unlinkSync, statSync, mkdirSync } from 'node:fs'
|
|
22
|
+
import { join, dirname } from 'node:path'
|
|
14
23
|
import { homedir } from 'node:os'
|
|
15
24
|
import { randomUUID } from 'node:crypto'
|
|
16
25
|
import { writeJsonAtomic } from './storage-atomic.js'
|
|
17
26
|
import { isTopicGoneError } from './telegram-errors.js'
|
|
18
27
|
|
|
28
|
+
const LOCK_STALE_MS = 30_000
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Exclusive lock file scoped to the queue (#218). Returns false when another
|
|
32
|
+
* writer holds a fresh lock; steals locks older than LOCK_STALE_MS (crashed
|
|
33
|
+
* writer). The caller must releaseLock() when it returns true.
|
|
34
|
+
*/
|
|
35
|
+
export function acquireLock(lockPath, staleMs = LOCK_STALE_MS) {
|
|
36
|
+
mkdirSync(dirname(lockPath), { recursive: true, mode: 0o700 })
|
|
37
|
+
try {
|
|
38
|
+
const fd = openSync(lockPath, 'wx')
|
|
39
|
+
try { writeSync(fd, String(process.pid)) } catch { /* best-effort */ }
|
|
40
|
+
closeSync(fd)
|
|
41
|
+
return true
|
|
42
|
+
} catch (err) {
|
|
43
|
+
if (err.code !== 'EEXIST') throw err
|
|
44
|
+
}
|
|
45
|
+
let ageMs = 0
|
|
46
|
+
try {
|
|
47
|
+
ageMs = Date.now() - statSync(lockPath).mtimeMs
|
|
48
|
+
} catch { ageMs = staleMs + 1 }
|
|
49
|
+
if (ageMs < staleMs) return false
|
|
50
|
+
try { unlinkSync(lockPath) } catch { return false }
|
|
51
|
+
try {
|
|
52
|
+
const fd = openSync(lockPath, 'wx')
|
|
53
|
+
try { writeSync(fd, String(process.pid)) } catch { /* best-effort */ }
|
|
54
|
+
closeSync(fd)
|
|
55
|
+
return true
|
|
56
|
+
} catch {
|
|
57
|
+
return false
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function releaseLock(lockPath) {
|
|
62
|
+
try { unlinkSync(lockPath) } catch { /* already gone */ }
|
|
63
|
+
}
|
|
64
|
+
|
|
19
65
|
export function resolveOutboxStoragePath(baseDir) {
|
|
20
66
|
if (baseDir) return join(baseDir, 'outbox-queue.json')
|
|
21
67
|
const dshHome = (typeof process !== 'undefined' && process.env?.DSH_HOME) || ''
|
|
@@ -79,16 +125,30 @@ export function createOutboxQueue(options = {}) {
|
|
|
79
125
|
let pending = []
|
|
80
126
|
let dlq = []
|
|
81
127
|
let timer = null
|
|
82
|
-
let isFlushing = false
|
|
83
128
|
let isStopped = false
|
|
129
|
+
const lockPath = `${storagePath}.lock`
|
|
84
130
|
|
|
85
|
-
function
|
|
131
|
+
function loadDiskIntoMemory() {
|
|
86
132
|
try {
|
|
87
133
|
if (existsSync(storagePath)) {
|
|
88
134
|
const raw = readFileSync(storagePath, 'utf8')
|
|
89
135
|
const data = JSON.parse(raw)
|
|
90
|
-
|
|
91
|
-
|
|
136
|
+
// Merge (#218): adopt jobs added to disk by another writer; drop our
|
|
137
|
+
// in-memory copies that another writer has already removed (it sent
|
|
138
|
+
// or DLQ'd them under the lock). In-memory runtime state (backoff
|
|
139
|
+
// nextRetryAt) for jobs still on disk is preserved.
|
|
140
|
+
const diskPending = Array.isArray(data.pending) ? data.pending : []
|
|
141
|
+
const diskDlq = Array.isArray(data.dlq) ? data.dlq : []
|
|
142
|
+
const diskPendingIds = new Set(diskPending.map((j) => j.id))
|
|
143
|
+
for (const j of diskPending) {
|
|
144
|
+
if (!pending.some((m) => m.id === j.id)) pending.push(j)
|
|
145
|
+
}
|
|
146
|
+
pending = pending.filter((m) => diskPendingIds.has(m.id))
|
|
147
|
+
const diskDlqIds = new Set(diskDlq.map((j) => j.id))
|
|
148
|
+
for (const j of diskDlq) {
|
|
149
|
+
if (!dlq.some((m) => m.id === j.id)) dlq.push(j)
|
|
150
|
+
}
|
|
151
|
+
dlq = dlq.filter((m) => diskDlqIds.has(m.id))
|
|
92
152
|
if (pending.length > 0 && logger) {
|
|
93
153
|
logger.info?.(`[outbox] recovered ${pending.length} pending outbound jobs from disk`)
|
|
94
154
|
}
|
|
@@ -100,7 +160,7 @@ export function createOutboxQueue(options = {}) {
|
|
|
100
160
|
}
|
|
101
161
|
}
|
|
102
162
|
|
|
103
|
-
|
|
163
|
+
loadDiskIntoMemory()
|
|
104
164
|
|
|
105
165
|
async function persist() {
|
|
106
166
|
try {
|
|
@@ -112,17 +172,36 @@ export function createOutboxQueue(options = {}) {
|
|
|
112
172
|
}
|
|
113
173
|
}
|
|
114
174
|
|
|
175
|
+
const PID = process.pid
|
|
176
|
+
const CLAIM_MS = 60_000
|
|
177
|
+
|
|
115
178
|
async function flush() {
|
|
116
|
-
if (
|
|
179
|
+
if (isStopped) return
|
|
117
180
|
if (!pending.length) return
|
|
118
|
-
|
|
181
|
+
// Brief retry loop: enqueue()'s void flush may transiently hold the lock.
|
|
182
|
+
let lk = false
|
|
183
|
+
for (let i = 0; i < 3 && !lk; i++) {
|
|
184
|
+
lk = acquireLock(lockPath)
|
|
185
|
+
if (!lk) await new Promise((r) => setTimeout(r, 50))
|
|
186
|
+
}
|
|
187
|
+
if (!lk) return // genuinely busy: the lock holder will process everything
|
|
119
188
|
const now = Date.now()
|
|
120
189
|
let dirty = false
|
|
121
190
|
|
|
122
191
|
try {
|
|
192
|
+
// Adopt jobs enqueued by another writer while we didn't hold the lock;
|
|
193
|
+
// keep our in-memory runtime state (nextRetryAt backoff) for known ids.
|
|
194
|
+
loadDiskIntoMemory()
|
|
195
|
+
|
|
123
196
|
const readyJobs = pending.filter((j) => (j.nextRetryAt || 0) <= now)
|
|
124
197
|
for (const job of readyJobs) {
|
|
125
198
|
if (isStopped) break
|
|
199
|
+
// Job claim (#218): a job locked by another live writer is skipped —
|
|
200
|
+
// claims expire after CLAIM_MS so a crashed writer's jobs are adopted.
|
|
201
|
+
if (job.claimedBy && job.claimedBy !== PID && job.claimUntil > now) continue
|
|
202
|
+
job.claimedBy = PID
|
|
203
|
+
job.claimUntil = now + CLAIM_MS
|
|
204
|
+
dirty = true
|
|
126
205
|
try {
|
|
127
206
|
if (typeof senderFn !== 'function') {
|
|
128
207
|
throw new Error('outbox senderFn not configured')
|
|
@@ -160,6 +239,7 @@ export function createOutboxQueue(options = {}) {
|
|
|
160
239
|
const retryAfter = parseRetryAfter(err)
|
|
161
240
|
const delayMs = computeOutboxBackoffMs(job.attempts, retryAfter)
|
|
162
241
|
job.nextRetryAt = Date.now() + delayMs
|
|
242
|
+
job.claimUntil = 0 // backoff: release the claim for the retry window
|
|
163
243
|
dirty = true
|
|
164
244
|
if (logger) {
|
|
165
245
|
logger.warn?.(`[outbox] job ${job.id} failed (attempt ${job.attempts}/${max}), retrying in ${Math.round(delayMs / 1000)}s: ${job.lastError}`)
|
|
@@ -168,10 +248,10 @@ export function createOutboxQueue(options = {}) {
|
|
|
168
248
|
}
|
|
169
249
|
}
|
|
170
250
|
} finally {
|
|
171
|
-
isFlushing = false
|
|
172
251
|
if (dirty) {
|
|
173
252
|
await persist()
|
|
174
253
|
}
|
|
254
|
+
releaseLock(lockPath)
|
|
175
255
|
}
|
|
176
256
|
}
|
|
177
257
|
|
|
@@ -187,8 +267,17 @@ export function createOutboxQueue(options = {}) {
|
|
|
187
267
|
nextRetryAt: Date.now(),
|
|
188
268
|
lastError: null,
|
|
189
269
|
}
|
|
190
|
-
|
|
191
|
-
|
|
270
|
+
while (!acquireLock(lockPath)) {
|
|
271
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
272
|
+
}
|
|
273
|
+
try {
|
|
274
|
+
// Merge under the lock (#218): adopt jobs another instance enqueued.
|
|
275
|
+
loadDiskIntoMemory()
|
|
276
|
+
pending.push(item)
|
|
277
|
+
await persist()
|
|
278
|
+
} finally {
|
|
279
|
+
releaseLock(lockPath)
|
|
280
|
+
}
|
|
192
281
|
void flush()
|
|
193
282
|
return item
|
|
194
283
|
}
|
|
@@ -221,31 +310,44 @@ export function createOutboxQueue(options = {}) {
|
|
|
221
310
|
|
|
222
311
|
async function retryDlq() {
|
|
223
312
|
if (!dlq.length) return 0
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
313
|
+
while (!acquireLock(lockPath)) {
|
|
314
|
+
await new Promise((r) => setTimeout(r, 100))
|
|
315
|
+
}
|
|
316
|
+
let count = 0
|
|
317
|
+
try {
|
|
318
|
+
count = dlq.length
|
|
319
|
+
for (const item of dlq) {
|
|
320
|
+
pending.push({
|
|
321
|
+
id: item.id || randomUUID(),
|
|
322
|
+
type: item.type,
|
|
323
|
+
target: item.target,
|
|
324
|
+
payload: item.payload,
|
|
325
|
+
attempts: 0,
|
|
326
|
+
maxAttempts: defaultMaxAttempts,
|
|
327
|
+
createdAt: Date.now(),
|
|
328
|
+
nextRetryAt: Date.now(),
|
|
329
|
+
lastError: null,
|
|
330
|
+
})
|
|
331
|
+
}
|
|
332
|
+
dlq = []
|
|
333
|
+
await persist()
|
|
334
|
+
} finally {
|
|
335
|
+
releaseLock(lockPath)
|
|
237
336
|
}
|
|
238
|
-
dlq = []
|
|
239
|
-
await persist()
|
|
240
337
|
await flush()
|
|
241
338
|
return count
|
|
242
339
|
}
|
|
243
340
|
|
|
244
341
|
async function clearDlq() {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
342
|
+
if (!acquireLock(lockPath)) return 0
|
|
343
|
+
try {
|
|
344
|
+
const count = dlq.length
|
|
345
|
+
dlq = []
|
|
346
|
+
await persist()
|
|
347
|
+
return count
|
|
348
|
+
} finally {
|
|
349
|
+
releaseLock(lockPath)
|
|
350
|
+
}
|
|
249
351
|
}
|
|
250
352
|
|
|
251
353
|
return {
|
package/lib/settings-bridge.js
CHANGED
|
@@ -96,10 +96,19 @@ export function setupSettings(ctx, { namespace, Config, resolveConfig, baseConfi
|
|
|
96
96
|
return currentConfig
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
+
let writeChain = Promise.resolve()
|
|
99
100
|
settingsApi = {
|
|
100
101
|
get: () => source(),
|
|
101
102
|
replace: async (next) => persist(next),
|
|
102
103
|
update: async (patch) => persist({ ...source(), ...patch }),
|
|
104
|
+
// Serialized write mutex (#221): concurrent read-modify-write writers
|
|
105
|
+
// (PUT /config, chat persist hooks) chain through this queue and each
|
|
106
|
+
// re-reads the current config at execution time, so no update is lost.
|
|
107
|
+
writeExclusive: (fn) => {
|
|
108
|
+
const run = writeChain.then(() => fn(settingsApi))
|
|
109
|
+
writeChain = run.then(() => {}, () => {})
|
|
110
|
+
return run
|
|
111
|
+
},
|
|
103
112
|
watch: (cb) => {
|
|
104
113
|
if (typeof cb === 'function') watchers.add(cb)
|
|
105
114
|
const unwatchService = typeof service.watch === 'function' ? service.watch(cb) : null
|
package/lib/stream.js
CHANGED
|
File without changes
|
package/lib/telegram-errors.js
CHANGED
|
File without changes
|
package/lib/text.js
CHANGED
|
@@ -14,6 +14,56 @@ export function splitText(text, maxLen) {
|
|
|
14
14
|
return chunks
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Split raw markdown into chunks whose FORMATTED rendering (measured by
|
|
19
|
+
* `measure`) stays within `budget`, so no HTML tag is ever cut at a chunk
|
|
20
|
+
* boundary (#229). Fence-aware: lines inside ``` fences always stay with
|
|
21
|
+
* their block. A single oversized unit (huge fenceless line) is hard-split.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} text raw markdown
|
|
24
|
+
* @param {number} budget max allowed length of the FORMATTED chunk
|
|
25
|
+
* @param {(s: string) => string} measure renders raw markdown to its
|
|
26
|
+
* Telegram HTML form (used for length measurement)
|
|
27
|
+
* @returns {string[]} raw markdown chunks (format each separately)
|
|
28
|
+
*/
|
|
29
|
+
export function splitMarkdownForTelegram(text, budget, measure) {
|
|
30
|
+
const raw = String(text ?? '')
|
|
31
|
+
if (measure(raw).length <= budget) return [raw]
|
|
32
|
+
const chunks = []
|
|
33
|
+
let cur = ''
|
|
34
|
+
let fence = false
|
|
35
|
+
|
|
36
|
+
const flushCur = () => {
|
|
37
|
+
const trimmed = cur.replace(/^\n+/, '').replace(/\n+$/, '')
|
|
38
|
+
if (trimmed) chunks.push(trimmed)
|
|
39
|
+
cur = ''
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
for (const line of raw.split('\n')) {
|
|
43
|
+
if (line.trimStart().startsWith('```')) fence = !fence
|
|
44
|
+
const candidate = cur ? cur + '\n' + line : line
|
|
45
|
+
if (fence || measure(candidate).length <= budget) {
|
|
46
|
+
// Inside a fence we must keep the block intact even over budget; the
|
|
47
|
+
// pathological single-fence-megablock case is hard-split below.
|
|
48
|
+
cur = candidate
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
flushCur()
|
|
52
|
+
cur = line
|
|
53
|
+
if (measure(cur).length > budget) {
|
|
54
|
+
// Single long line without fences: hard-split at word boundaries.
|
|
55
|
+
while (measure(cur).length > budget) {
|
|
56
|
+
let cut = cur.lastIndexOf(' ', budget)
|
|
57
|
+
if (cut < budget / 2) cut = budget
|
|
58
|
+
chunks.push(cur.slice(0, cut).trim())
|
|
59
|
+
cur = cur.slice(cut).trim()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
flushCur()
|
|
64
|
+
return chunks.length > 0 ? chunks : [raw]
|
|
65
|
+
}
|
|
66
|
+
|
|
17
67
|
export function assistantText(message) {
|
|
18
68
|
return (message?.content || [])
|
|
19
69
|
.filter((block) => block.type === 'text')
|
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.44",
|
|
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",
|