@goodandready/dsh-messenger-gateway 0.4.43 → 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 +1 -0
- package/lib/adapters/telegram.js +16 -10
- package/lib/gateway-commands.js +16 -2
- package/lib/gateway.js +0 -0
- package/lib/index.js +89 -50
- package/lib/outbox.js +0 -0
- package/lib/settings-bridge.js +9 -0
- package/lib/telegram-errors.js +0 -0
- package/lib/text.js +50 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -154,6 +154,7 @@ 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
|
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.js
CHANGED
|
File without changes
|
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
|
File without changes
|
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/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",
|