@goodandready/dsh-messenger-gateway 0.4.30 → 0.4.31
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 +6 -1
- package/lib/adapters/index.js +1 -0
- package/lib/adapters/telegram.js +4 -0
- package/lib/commands.js +4 -0
- package/lib/config.js +4 -0
- package/lib/gateway-commands.js +109 -0
- package/lib/gateway.js +8 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,7 +48,8 @@ Talk to your Harness agent from Telegram: text, voice, photos, documents, inline
|
|
|
48
48
|
- Configurable proxy support: HTTP, HTTPS, SOCKS5, and SOCKS5h (`telegram.proxyUrl`)
|
|
49
49
|
- Custom Bot API root URL (`telegram.apiRoot`) for reverse proxies and local Bot API servers
|
|
50
50
|
- Multi-transport support: Telegram, Discord (Webhooks & Bot API), and Slack (Webhooks & Bot API)
|
|
51
|
-
- Allowlist + pairing codes
|
|
51
|
+
- Allowlist + pairing codes (with configurable `allowlistMode`: `open` | `closed`)
|
|
52
|
+
- Conversation switching directly from chat (`/sessions` and `/resume <n|id>`)
|
|
52
53
|
- Per-user or per-chat sessions (`sessionScope`)
|
|
53
54
|
- Forum topics as separate sessions
|
|
54
55
|
- Named sessions from chat metadata (`agent.sessionTitleFromChat`: `TG · @username` or `TG · Group › topic N`)
|
|
@@ -105,6 +106,8 @@ Spoken replies use Telegram `sendVoice`. If TTS returns MP3 (or other non-Opus a
|
|
|
105
106
|
| `/start` `/help` | Help |
|
|
106
107
|
| `/whoami` | Your Telegram user id |
|
|
107
108
|
| `/new` | New agent session |
|
|
109
|
+
| `/sessions` | List recent messenger conversations |
|
|
110
|
+
| `/resume <n|id>` | Switch chat to previous session |
|
|
108
111
|
| `/stop` | Abort the current turn |
|
|
109
112
|
| `/model` | Interactive 2-step model picker (providers → models) or `/model <prov> <mod>` |
|
|
110
113
|
| `/status` | Gateway status and session counters |
|
|
@@ -140,6 +143,8 @@ In addition to Telegram, outbound messages can be dispatched to Discord and Slac
|
|
|
140
143
|
## Configuration notes
|
|
141
144
|
|
|
142
145
|
- `sessionScope`: `user` (default) or `chat` — how group chats isolate sessions
|
|
146
|
+
- `allowlistMode`: `open` (default) or `closed` — in `closed` mode, an empty allowlist admits nobody (fail-closed)
|
|
147
|
+
- `sessionList`: `true` (default) — enables `/sessions` and `/resume` commands in chat
|
|
143
148
|
- `sessionTitleFromChat`: `false` (default) — name each new session after its originating chat (`TG · @user`, `TG · Group › topic N`) instead of generic generated titles
|
|
144
149
|
- `voiceMode`: `mirror` / `always` / `off` — when to speak replies (also `/voice`)
|
|
145
150
|
- `tts.enabled` / `tts.maxChars` — TTS gate and length cap
|
package/lib/adapters/index.js
CHANGED
|
@@ -10,6 +10,7 @@ export default function createAdapters(deps) {
|
|
|
10
10
|
list.push(new TelegramAdapter({
|
|
11
11
|
botToken: tg.botToken,
|
|
12
12
|
allowedUserIds: tg.allowedUserIds,
|
|
13
|
+
allowlistMode: tg.allowlistMode || 'open',
|
|
13
14
|
timeoutSeconds: tg.pollTimeoutSeconds,
|
|
14
15
|
pollIntervalMs: tg.pollIntervalMs,
|
|
15
16
|
commands: tg.commands,
|
package/lib/adapters/telegram.js
CHANGED
|
@@ -40,6 +40,7 @@ export class TelegramAdapter {
|
|
|
40
40
|
this.name = 'telegram'
|
|
41
41
|
this.token = String(opts.botToken || '').trim()
|
|
42
42
|
this.allowedUserIds = (opts.allowedUserIds || []).map(Number).filter((n) => Number.isFinite(n))
|
|
43
|
+
this.allowlistMode = opts.allowlistMode === 'closed' ? 'closed' : 'open'
|
|
43
44
|
this.timeoutSeconds = Number(opts.timeoutSeconds) || 50
|
|
44
45
|
this.pollIntervalMs = Number(opts.pollIntervalMs) || 500
|
|
45
46
|
this.media = opts.media || {}
|
|
@@ -382,6 +383,9 @@ export class TelegramAdapter {
|
|
|
382
383
|
|
|
383
384
|
allowed(userId) {
|
|
384
385
|
if (typeof this.isUserAllowed === 'function') return this.isUserAllowed(userId)
|
|
386
|
+
if (this.allowlistMode === 'closed') {
|
|
387
|
+
return this.allowedUserIds.length > 0 && this.allowedUserIds.includes(Number(userId))
|
|
388
|
+
}
|
|
385
389
|
return this.allowedUserIds.length === 0 || this.allowedUserIds.includes(Number(userId))
|
|
386
390
|
}
|
|
387
391
|
|
package/lib/commands.js
CHANGED
|
@@ -3,6 +3,8 @@ export const DEFAULT_TELEGRAM_COMMANDS = [
|
|
|
3
3
|
{ command: 'start', description: 'Connect and show greeting' },
|
|
4
4
|
{ command: 'help', description: 'Show commands help' },
|
|
5
5
|
{ command: 'new', description: 'Start a new agent session' },
|
|
6
|
+
{ command: 'sessions', description: 'List recent messenger sessions' },
|
|
7
|
+
{ command: 'resume', description: 'Resume session: /resume <n|id>' },
|
|
6
8
|
{ command: 'whoami', description: 'Show your messenger user ID' },
|
|
7
9
|
{ command: 'stop', description: 'Interrupt current turn' },
|
|
8
10
|
{ command: 'pair', description: 'Approve pairing code: /pair CODE' },
|
|
@@ -101,6 +103,8 @@ export const HELP_TEXT = [
|
|
|
101
103
|
'💬 <b>Session & Chat:</b>',
|
|
102
104
|
'• /help — show this command reference',
|
|
103
105
|
'• /new — start fresh session',
|
|
106
|
+
'• /sessions — list recent conversations',
|
|
107
|
+
'• /resume <n|id> — resume previous session',
|
|
104
108
|
'• /stop — interrupt current response',
|
|
105
109
|
'• /model — interactive model selector (/model list)',
|
|
106
110
|
'• /role [name] — switch persona or role (/role list)',
|
package/lib/config.js
CHANGED
|
@@ -22,6 +22,8 @@ export const PluginConfig = z.object({
|
|
|
22
22
|
enabled: z.boolean().default(false),
|
|
23
23
|
botToken: z.string().role('secret').default(''),
|
|
24
24
|
allowedUserIds: z.array(z.number()).default([]),
|
|
25
|
+
allowlistMode: z.union([z.const('open'), z.const('closed')]).default('open')
|
|
26
|
+
.description('Allowlist mode: open admits all users when list is empty; closed refuses unknown users when list is empty'),
|
|
25
27
|
pollTimeoutSeconds: z.number().default(50),
|
|
26
28
|
pollIntervalMs: z.number().default(500),
|
|
27
29
|
commands: z.array(z.object({
|
|
@@ -129,6 +131,8 @@ export const PluginConfig = z.object({
|
|
|
129
131
|
.description('In groups: user = per-user session; chat = shared session for the chat/topic'),
|
|
130
132
|
sessionTitleFromChat: z.boolean().default(false)
|
|
131
133
|
.description('Name each new session after its chat (TG · @user, TG · Group › topic N) instead of leaving the generated title'),
|
|
134
|
+
sessionList: z.boolean().default(true)
|
|
135
|
+
.description('Enable /sessions and /resume commands to list and switch between recent conversations'),
|
|
132
136
|
}).volatile(),
|
|
133
137
|
})
|
|
134
138
|
|
package/lib/gateway-commands.js
CHANGED
|
@@ -496,6 +496,115 @@ export async function handleGatewayCommand(gw, key, text, input) {
|
|
|
496
496
|
}
|
|
497
497
|
|
|
498
498
|
|
|
499
|
+
|
|
500
|
+
if (cmd === '/sessions') {
|
|
501
|
+
if (gw.config.agent?.sessionList === false) {
|
|
502
|
+
return reply('Session switching is disabled by configuration.')
|
|
503
|
+
}
|
|
504
|
+
const sessionQuery = gw.ctx.get?.('sessionQuery') ?? gw.ctx.sessionQuery
|
|
505
|
+
if (!sessionQuery?.listSessions) {
|
|
506
|
+
return reply('Session listing service is not available on this host.')
|
|
507
|
+
}
|
|
508
|
+
try {
|
|
509
|
+
const records = await sessionQuery.listSessions()
|
|
510
|
+
const filtered = (records || [])
|
|
511
|
+
.filter((r) => {
|
|
512
|
+
const id = String(r?.header?.id || '')
|
|
513
|
+
return id.startsWith('msgw-') && !r?.header?.archived
|
|
514
|
+
})
|
|
515
|
+
.slice(0, 10)
|
|
516
|
+
if (filtered.length === 0) {
|
|
517
|
+
return reply('No recent messenger sessions found.')
|
|
518
|
+
}
|
|
519
|
+
const items = await Promise.all(
|
|
520
|
+
filtered.map(async (r) => {
|
|
521
|
+
const id = r.header.id
|
|
522
|
+
let title = ''
|
|
523
|
+
try {
|
|
524
|
+
const titleSnap = await sessionQuery.readTitle(id)
|
|
525
|
+
title = titleSnap?.title || titleSnap?.value || ''
|
|
526
|
+
} catch { /* best effort */ }
|
|
527
|
+
if (!title) title = r.header.title || id
|
|
528
|
+
const cwd = r.header.cwd || r.header.meta?.cwd || ''
|
|
529
|
+
const dir = cwd ? cwd.split(/[\\/]/).filter(Boolean).slice(-2).join('/') : ''
|
|
530
|
+
return { id, title, dir }
|
|
531
|
+
})
|
|
532
|
+
)
|
|
533
|
+
gw.recentSessions.set(key, items)
|
|
534
|
+
const lines = ['📋 <b>Recent Sessions:</b>', '']
|
|
535
|
+
items.forEach((item, index) => {
|
|
536
|
+
const dirSuffix = item.dir ? ` (<i>${item.dir}</i>)` : ''
|
|
537
|
+
lines.push(`${index + 1}. <b>${item.title}</b>${dirSuffix}\n <code>/resume ${index + 1}</code>`)
|
|
538
|
+
})
|
|
539
|
+
lines.push('', 'Switch using <code>/resume <number></code>')
|
|
540
|
+
return reply(lines.join('\n'))
|
|
541
|
+
} catch (err) {
|
|
542
|
+
gw.logger?.warn?.(`/sessions failed: ${err?.message || err}`)
|
|
543
|
+
return reply(`Failed to list sessions: ${err?.message || err}`)
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
if (cmd === '/resume') {
|
|
548
|
+
if (gw.config.agent?.sessionList === false) {
|
|
549
|
+
return reply('Session switching is disabled by configuration.')
|
|
550
|
+
}
|
|
551
|
+
const chat = gw.chats.get(key)
|
|
552
|
+
if (chat?.turnActive) {
|
|
553
|
+
return reply('Cannot switch session while a turn is in progress. Use /stop first.')
|
|
554
|
+
}
|
|
555
|
+
const rawArg = text.replace(/^\/resume(?:\s+|$)/i, '').trim()
|
|
556
|
+
if (!rawArg) {
|
|
557
|
+
return reply('Usage: /resume <number> or /resume <sessionId>\nUse /sessions to list available conversations.')
|
|
558
|
+
}
|
|
559
|
+
let targetSessionId = ''
|
|
560
|
+
const num = parseInt(rawArg, 10)
|
|
561
|
+
if (Number.isFinite(num) && num >= 1) {
|
|
562
|
+
const items = gw.recentSessions?.get(key) || []
|
|
563
|
+
const chosen = items[num - 1]
|
|
564
|
+
if (!chosen) {
|
|
565
|
+
return reply(`Session number ${num} not found. Run /sessions first to view the numbered list.`)
|
|
566
|
+
}
|
|
567
|
+
targetSessionId = chosen.id
|
|
568
|
+
} else if (rawArg.startsWith('msgw-')) {
|
|
569
|
+
targetSessionId = rawArg
|
|
570
|
+
} else {
|
|
571
|
+
return reply(`Invalid session reference "${rawArg}". Provide a number from /sessions or a session ID starting with msgw-.`)
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
const currentId = String(chat?.agent?.session?.id || '')
|
|
575
|
+
if (currentId === targetSessionId) {
|
|
576
|
+
return reply(`Already connected to session ${targetSessionId}.`)
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
if (chat) {
|
|
580
|
+
if (chat.abort) chat.abort.abort()
|
|
581
|
+
chat.pendingMedia = []
|
|
582
|
+
gw.chats.delete(key)
|
|
583
|
+
if (currentId) {
|
|
584
|
+
gw.sessionToChat.delete(currentId)
|
|
585
|
+
gw.sessionToThread.delete(currentId)
|
|
586
|
+
}
|
|
587
|
+
const target = chat.target || {}
|
|
588
|
+
gw.threadToSession.delete(`${target.chatId ?? input.chatId}:${target.threadId ?? input.threadId ?? 0}`)
|
|
589
|
+
setImmediate(() => {
|
|
590
|
+
try {
|
|
591
|
+
chat.dispose?.().catch?.((e) => gw.logger?.warn?.(`/resume dispose: ${e?.message || e}`))
|
|
592
|
+
} catch (e) {
|
|
593
|
+
gw.logger?.warn?.(`/resume dispose: ${e?.message || e}`)
|
|
594
|
+
}
|
|
595
|
+
})
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
try {
|
|
599
|
+
const newChat = await gw.createChat(key, input, targetSessionId)
|
|
600
|
+
gw.chats.set(key, newChat)
|
|
601
|
+
return reply(`Resumed session: ${targetSessionId}`)
|
|
602
|
+
} catch (err) {
|
|
603
|
+
gw.logger?.warn?.(`/resume createChat failed: ${err?.message || err}`)
|
|
604
|
+
return reply(`Failed to resume session ${targetSessionId}: ${err?.message || err}`)
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
499
608
|
if (cmd === '/new') {
|
|
500
609
|
const chat = gw.chats.get(key)
|
|
501
610
|
if (!chat) return reply(t('msg.no_active_session', {}, locale))
|
package/lib/gateway.js
CHANGED
|
@@ -201,6 +201,10 @@ export class Gateway {
|
|
|
201
201
|
|
|
202
202
|
isUserAllowed(userId) {
|
|
203
203
|
const ids = this.effectiveAllowedIds()
|
|
204
|
+
const mode = this.tg()?.allowlistMode || 'open'
|
|
205
|
+
if (mode === 'closed') {
|
|
206
|
+
return ids.length > 0 && ids.includes(Number(userId))
|
|
207
|
+
}
|
|
204
208
|
return ids.length === 0 || ids.includes(Number(userId))
|
|
205
209
|
}
|
|
206
210
|
|
|
@@ -300,6 +304,9 @@ export class Gateway {
|
|
|
300
304
|
this.adapterList = this.adapterList.filter((item) => item !== adapter)
|
|
301
305
|
if (this.tgAdapter === adapter) this.tgAdapter = undefined
|
|
302
306
|
}
|
|
307
|
+
if ((this.tg()?.allowlistMode || 'open') === 'open' && this.effectiveAllowedIds().length === 0) {
|
|
308
|
+
this.ctx.logger?.warn?.('[dsh-messenger-gateway] telegram allowlist is empty in "open" mode: the bot will accept messages from any Telegram user.')
|
|
309
|
+
}
|
|
303
310
|
for (const adapter of adapters) {
|
|
304
311
|
// Регистрируем адаптер ДО `await adapter.start()`: старт адаптера — это
|
|
305
312
|
// сетевые вызовы (getMe/setMyCommands/deleteWebhook), и если за это время
|
|
@@ -667,7 +674,7 @@ export class Gateway {
|
|
|
667
674
|
await registerSessionInWorkspace(this.ctx, targetSessionId, cwd, this.ctx.logger).catch(() => {})
|
|
668
675
|
// Opt-in: name the session after the chat that opened it, so a bot serving
|
|
669
676
|
// several chats keeps an unambiguous session list.
|
|
670
|
-
if (agentCfg.sessionTitleFromChat === true) {
|
|
677
|
+
if (!existingSessionId && agentCfg.sessionTitleFromChat === true) {
|
|
671
678
|
try {
|
|
672
679
|
const titleService = this.ctx.get?.('sessionTitle') ?? this.ctx.sessionTitle
|
|
673
680
|
const label = chatLabel({
|
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.31",
|
|
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",
|