@goodandready/dsh-messenger-gateway 0.3.13 → 0.3.14

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/gateway.js CHANGED
@@ -174,11 +174,10 @@ export class Gateway {
174
174
  this.ctx.logger?.warn?.(`dsh-messenger-gateway: ${adapter.name}: ${err.message}`)
175
175
  }
176
176
  }
177
- const idleMs = Number(this.config.agent?.idleTimeoutMs) || 0
178
- if (idleMs > 0) {
179
- this.idleTimer = setInterval(() => this.reapIdle(), Math.min(idleMs, 60_000))
180
- this.idleTimer.unref?.()
181
- }
177
+ const rawIdle = Number(this.config.agent?.idleTimeoutMs)
178
+ const idleMs = Number.isFinite(rawIdle) && rawIdle > 0 ? rawIdle : 86_400_000
179
+ this.idleTimer = setInterval(() => this.reapIdle(), Math.min(idleMs, 60_000))
180
+ this.idleTimer.unref?.()
182
181
  this.scheduler.start()
183
182
  }
184
183
 
@@ -565,30 +564,37 @@ export class Gateway {
565
564
  if (cmd === '/start') return reply('Шлюз подключён. Пишите сообщение агенту. /help — команды.', { replyMarkup: REMOVE_REPLY_KEYBOARD })
566
565
  if (cmd === '/help') {
567
566
  return reply([
568
- 'Команды:',
569
- '/help — справка',
570
- '/new — новая сессия',
571
- '/whoamiваш id',
572
- '/stopпрервать текущий ответ',
573
- '/statusстатус шлюза',
574
- '/model — текущая модель; /model provider model — сменить',
575
- '/role [name] — персоны и роли агента (/role list)',
576
- '/skills / /toolsсписок инструментов и навыков',
577
- '/fork — ответвление текущей сессии',
578
- '/export — выгрузка истории диалога в Markdown',
579
- '/rewind [N] — откат последних N сообщений',
580
- '/files [dir] менеджер файлов в рабочей папке агента',
581
- '/get <path>скачать файл из рабочей папки в Telegram',
582
- '/pair CODEодобрить пользователя',
583
- '/sethome [name]этот чат = home (имя опционально)',
584
- '/homeсписок home',
585
- '/setalert — назначить этот чат каналом алертов',
586
- '/alert [test] — статус канала алертов и тест',
587
- '/remind <время> <текст> напоминание (/remind 10m текст, /remind list)',
588
- '/voice on|off|status голосовые ответы',
589
- '/tts on|off|statusозвучка в этом чате',
590
- '/mute / /unmuteзаглушить уведомления в этот чат',
591
- '/keyboard on|off — быстрая клавиатура',
567
+ '📖 <b>Команды бота:</b>',
568
+ '',
569
+ '💬 <b>Диалог:</b>',
570
+ '/helpэта справка',
571
+ '/newновая сессия',
572
+ '/stopпрервать текущий ответ',
573
+ '/model — интерактивный выбор модели (/model list)',
574
+ '/role [name] — персоны и роли агента (/role list)',
575
+ ' /rewind [N]откат последних N ходов',
576
+ '/fork — форк сессии в новую ветку',
577
+ '/export — выгрузка истории в Markdown',
578
+ '',
579
+ '🛠️ <b>Инструменты и файлы:</b>',
580
+ '/skills / /tools список активных инструментов',
581
+ '/files [dir]проводник рабочей папки',
582
+ '/get <path>скачать файл в Telegram',
583
+ '/remind <время> <текст> напоминание (/remind 10m текст)',
584
+ '',
585
+ '⚙️ <b>Настройки:</b>',
586
+ '/statusстатус шлюза и модели',
587
+ '/topсистемные ресурсы (RAM, uptime)',
588
+ '/keyboard on|off — быстрые кнопки',
589
+ ' /voice on|off|statusголосовые ответы',
590
+ '/tts on|off|statusозвучка в этом чате',
591
+ '• /mute / /unmute — заглушить уведомления в этот чат',
592
+ '',
593
+ '🔒 <b>Доступ и каналы:</b>',
594
+ '• /whoami — ваш Telegram id',
595
+ '• /pair CODE — одобрить код сопряжения',
596
+ '• /sethome [name] — привязать домашний чат',
597
+ '• /setalert — назначить канал алертов',
592
598
  ].join('\n'))
593
599
  }
594
600
  if (cmd === '/role' || cmd === '/persona') {
@@ -1318,8 +1324,8 @@ export class Gateway {
1318
1324
  }
1319
1325
 
1320
1326
  reapIdle() {
1321
- const timeout = Number(this.config.agent?.idleTimeoutMs) || 0
1322
- if (timeout <= 0) return
1327
+ const rawTimeout = Number(this.config.agent?.idleTimeoutMs)
1328
+ const timeout = Number.isFinite(rawTimeout) && rawTimeout > 0 ? rawTimeout : 86_400_000
1323
1329
  const now = Date.now()
1324
1330
  for (const [key, chat] of this.chats) {
1325
1331
  if (!chat.turnActive && now - chat.lastUsed > timeout) {
package/lib/pairing.js CHANGED
@@ -1,5 +1,5 @@
1
- import { randomBytes } from 'node:crypto'
2
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'node:fs'
1
+ import { randomBytes, randomUUID } from 'node:crypto'
2
+ import { readFileSync, writeFileSync, mkdirSync, existsSync, renameSync, unlinkSync } from 'node:fs'
3
3
  import { dirname } from 'node:path'
4
4
 
5
5
  const ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
@@ -11,6 +11,19 @@ export function generatePairingCode(length = 8) {
11
11
  return out
12
12
  }
13
13
 
14
+ function writeJsonAtomicSync(filePath, data) {
15
+ const dir = dirname(filePath)
16
+ mkdirSync(dir, { recursive: true })
17
+ const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
18
+ try {
19
+ writeFileSync(tmpPath, JSON.stringify(data, null, 2), 'utf8')
20
+ renameSync(tmpPath, filePath)
21
+ } catch (err) {
22
+ try { unlinkSync(tmpPath) } catch {}
23
+ throw err
24
+ }
25
+ }
26
+
14
27
  export function createPairingStore(filePath) {
15
28
  const state = { pending: {}, /** @type {number[]} */ approved: [] }
16
29
  if (filePath && existsSync(filePath)) {
@@ -22,8 +35,9 @@ export function createPairingStore(filePath) {
22
35
  }
23
36
  const persist = () => {
24
37
  if (!filePath) return
25
- mkdirSync(dirname(filePath), { recursive: true })
26
- writeFileSync(filePath, JSON.stringify({ approved: state.approved, pending: state.pending }, null, 2))
38
+ try {
39
+ writeJsonAtomicSync(filePath, { approved: state.approved, pending: state.pending })
40
+ } catch {}
27
41
  }
28
42
  const prune = () => {
29
43
  const now = Date.now()
@@ -107,4 +121,4 @@ export function createPairingStore(filePath) {
107
121
  return { ok: true, userId: row.userId, username: row.username }
108
122
  },
109
123
  }
110
- }
124
+ }
package/lib/scheduler.js CHANGED
@@ -1,6 +1,6 @@
1
- import { readFile, writeFile, mkdir } from 'node:fs/promises'
2
- import { dirname } from 'node:path'
1
+ import { readFile } from 'node:fs/promises'
3
2
  import { randomUUID } from 'node:crypto'
3
+ import { writeJsonAtomic } from './storage-atomic.js'
4
4
 
5
5
  export function parseRelativeTime(input) {
6
6
  if (!input) return null
@@ -49,6 +49,7 @@ export function createScheduler(filePath, onDue) {
49
49
  const RETENTION_MS = 7 * 86400 * 1000
50
50
 
51
51
  async function save() {
52
+ if (!filePath) return
52
53
  try {
53
54
  const now = Date.now()
54
55
  tasks = tasks.filter((t) => {
@@ -56,8 +57,7 @@ export function createScheduler(filePath, onDue) {
56
57
  const finishTime = t.firedAt || t.createdAt || 0
57
58
  return (now - finishTime) < RETENTION_MS
58
59
  })
59
- await mkdir(dirname(filePath), { recursive: true })
60
- await writeFile(filePath, JSON.stringify(tasks, null, 2), 'utf8')
60
+ await writeJsonAtomic(filePath, tasks)
61
61
  } catch {}
62
62
  }
63
63
 
@@ -140,4 +140,4 @@ export function createScheduler(filePath, onDue) {
140
140
  checkDue,
141
141
  getTasks: () => tasks,
142
142
  }
143
- }
143
+ }
@@ -0,0 +1,21 @@
1
+ import { writeFile, rename, mkdir, unlink } from 'node:fs/promises'
2
+ import { dirname } from 'node:path'
3
+ import { randomUUID } from 'node:crypto'
4
+
5
+ /**
6
+ * Write JSON atomically via temporary file and rename.
7
+ * Guarantees that readers never observe partially written files.
8
+ */
9
+ export async function writeJsonAtomic(filePath, data) {
10
+ const dir = dirname(filePath)
11
+ await mkdir(dir, { recursive: true })
12
+ const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
13
+ const serialized = JSON.stringify(data, null, 2)
14
+ try {
15
+ await writeFile(tmpPath, serialized, 'utf8')
16
+ await rename(tmpPath, filePath)
17
+ } catch (err) {
18
+ await unlink(tmpPath).catch(() => {})
19
+ throw err
20
+ }
21
+ }
package/lib/stream.js CHANGED
@@ -24,22 +24,48 @@ export function buildStreamPreview(streamText, toolName, maxLen = 3500) {
24
24
  return combined.slice(0, maxLen - 1) + '…'
25
25
  }
26
26
 
27
+ export function parseRetryAfter(err) {
28
+ if (!err) return 0
29
+ const msg = err.message || String(err)
30
+ const match = /retry after (\d+)/i.exec(msg)
31
+ if (match) return parseInt(match[1], 10) * 1000
32
+ return 0
33
+ }
34
+
27
35
  export function createEditScheduler(editFn, intervalMs = 1200) {
28
36
  let pending = null
29
37
  let timer = null
30
38
  let lastSent = ''
31
39
  let inFlight = Promise.resolve()
40
+ let retryDelayMs = 0
41
+
32
42
  const flush = () => {
33
43
  timer = null
34
44
  if (pending === null || pending === lastSent) return
35
45
  const text = pending
36
46
  inFlight = inFlight.then(async () => {
37
47
  try {
48
+ if (retryDelayMs > 0) {
49
+ const delay = retryDelayMs
50
+ retryDelayMs = 0
51
+ await new Promise((r) => setTimeout(r, delay))
52
+ }
38
53
  await editFn(text)
39
54
  lastSent = text
40
- } catch {}
55
+ } catch (err) {
56
+ const retry = parseRetryAfter(err)
57
+ if (retry > 0) {
58
+ retryDelayMs = retry
59
+ // Re-schedule flush with retry delay
60
+ if (!timer) {
61
+ timer = setTimeout(flush, retry + 100)
62
+ timer.unref?.()
63
+ }
64
+ }
65
+ }
41
66
  })
42
67
  }
68
+
43
69
  return {
44
70
  push(text) {
45
71
  pending = text
@@ -51,6 +77,17 @@ export function createEditScheduler(editFn, intervalMs = 1200) {
51
77
  if (timer) { clearTimeout(timer); timer = null }
52
78
  flush()
53
79
  await inFlight
80
+ // If there's still a pending difference (e.g. rate limit delay happened), wait and retry
81
+ if (pending !== null && pending !== lastSent) {
82
+ if (retryDelayMs > 0) {
83
+ await new Promise((r) => setTimeout(r, retryDelayMs + 50))
84
+ retryDelayMs = 0
85
+ }
86
+ try {
87
+ await editFn(pending)
88
+ lastSent = pending
89
+ } catch {}
90
+ }
54
91
  },
55
92
  }
56
93
  }
@@ -61,4 +98,4 @@ export function startTypingHeartbeat(typingFn, intervalMs = 4000) {
61
98
  const timer = setInterval(() => { typingFn().catch?.(() => {}) }, intervalMs)
62
99
  timer.unref?.()
63
100
  return () => clearInterval(timer)
64
- }
101
+ }
@@ -1,5 +1,19 @@
1
- import { readFileSync, writeFileSync, mkdirSync, existsSync } from "node:fs"
1
+ import { readFileSync, writeFileSync, mkdirSync, existsSync, renameSync, unlinkSync } from "node:fs"
2
2
  import { dirname } from "node:path"
3
+ import { randomUUID } from "node:crypto"
4
+
5
+ function writeJsonAtomicSync(filePath, data) {
6
+ const dir = dirname(filePath)
7
+ mkdirSync(dir, { recursive: true })
8
+ const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
9
+ try {
10
+ writeFileSync(tmpPath, JSON.stringify(data, null, 2), "utf8")
11
+ renameSync(tmpPath, filePath)
12
+ } catch (err) {
13
+ try { unlinkSync(tmpPath) } catch {}
14
+ throw err
15
+ }
16
+ }
3
17
 
4
18
  export function createVoicePrefs(filePath) {
5
19
  /** @type {Record<string, boolean>} */
@@ -12,8 +26,9 @@ export function createVoicePrefs(filePath) {
12
26
  }
13
27
  const persist = () => {
14
28
  if (!filePath) return
15
- mkdirSync(dirname(filePath), { recursive: true })
16
- writeFileSync(filePath, JSON.stringify(state, null, 2))
29
+ try {
30
+ writeJsonAtomicSync(filePath, state)
31
+ } catch {}
17
32
  }
18
33
  const key = (userId) => String(Number(userId) || userId || "")
19
34
  return {
@@ -43,4 +58,4 @@ export function shouldSpeakReply({ globalTts, voiceMode, inboundWasVoice, userPr
43
58
  if (voiceMode === "off") return false
44
59
  // mirror: speak if inbound was voice
45
60
  return Boolean(inboundWasVoice)
46
- }
61
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@goodandready/dsh-messenger-gateway",
3
- "version": "0.3.13",
3
+ "version": "0.3.14",
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",