@goodandready/dsh-messenger-gateway 0.4.25 → 0.4.27

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
@@ -29,7 +29,10 @@ import {
29
29
  restoreSkillGesture,
30
30
  } from './forum-mirror.js'
31
31
  import { resolveInternalBaseUrl } from './config.js'
32
- import { resolveEffectiveCwd, registerSessionInWorkspace } from './workspace.js'
32
+ import { resolveEffectiveCwd, registerSessionInWorkspace, createTopicWorkspaceStore, getCwdWarning } from './workspace.js'
33
+ import { createOutboxQueue, resolveOutboxStoragePath } from './outbox.js'
34
+ import { realpathSync } from 'node:fs'
35
+ import { resolve } from 'node:path'
33
36
 
34
37
 
35
38
  const PLUGIN = 'dsh-messenger-gateway'
@@ -59,7 +62,23 @@ export class Gateway {
59
62
  this.chatTts = createVoicePrefs(join(home, 'messenger-gateway', 'chat-tts.json'), { logger: this.logger })
60
63
  this.muted = createVoicePrefs(join(home, 'messenger-gateway', 'muted.json'), { logger: this.logger })
61
64
  this.personas = createPersonaStore(join(home, 'messenger-gateway', 'personas.json'), { logger: this.logger })
65
+ this.topicWorkspaces = createTopicWorkspaceStore(join(home, 'messenger-gateway', 'topic-workspaces.json'), { logger: this.logger })
62
66
  this.chatLocales = createVoicePrefs(join(home, 'messenger-gateway', 'chat-locales.json'), { logger: this.logger })
67
+ this.outbox = createOutboxQueue({
68
+ storagePath: resolveOutboxStoragePath(join(home, 'messenger-gateway')),
69
+ senderFn: async (job) => {
70
+ await this.messengerSend(job.target, job.payload)
71
+ },
72
+ onDlq: (job, err) => {
73
+ this.sendAlert('error', {
74
+ code: 'OUTBOX_DLQ',
75
+ message: `Outbox message failed permanently to ${job.target?.chatId}: ${err.message}`,
76
+ chatId: job.target?.chatId,
77
+ threadId: job.target?.threadId,
78
+ }).catch((e) => this.recordApiFailure('outbox.dlq_alert', e))
79
+ },
80
+ logger: this.logger,
81
+ })
63
82
  this.scheduler = createScheduler(join(home, 'messenger-gateway', 'scheduled.json'), async (task) => {
64
83
  const target = {
65
84
  platform: task.platform || 'telegram',
@@ -207,7 +226,13 @@ export class Gateway {
207
226
  if (text) collector.lastText = text
208
227
  const extra = collectAssistantParts(msg)
209
228
  for (const img of extra.images) collector.images.push(img)
229
+ const usage = event.data?.usage || msg?.usage
230
+ if (usage) collector.usage = usage
210
231
  } else if (event.type === 'assistant/chunk') {
232
+ const chunk = event.data?.chunk || event.data
233
+ if (chunk?.type === 'usage' && chunk?.usage) {
234
+ collector.usage = chunk.usage
235
+ }
211
236
  const delta = extractTextDelta(event.data?.chunk || event.data)
212
237
  const reasoningDelta = extractReasoningDelta(event.data?.chunk || event.data)
213
238
  if (delta || reasoningDelta) {
@@ -290,11 +315,13 @@ export class Gateway {
290
315
  const idleMs = Number.isFinite(rawIdle) && rawIdle > 0 ? rawIdle : 86_400_000
291
316
  this.idleTimer = setInterval(() => this.reapIdle(), Math.min(idleMs, 60_000))
292
317
  this.idleTimer.unref?.()
318
+ this.outbox?.start()
293
319
  this.scheduler.start()
294
320
  }
295
321
 
296
322
  stop() {
297
323
  this.stopped = true
324
+ if (this.outbox) this.outbox.stop()
298
325
  if (this.scheduler) this.scheduler.stop()
299
326
  if (this.disposeListener) this.disposeListener()
300
327
  if (this.idleTimer) clearInterval(this.idleTimer)
@@ -318,6 +345,17 @@ export class Gateway {
318
345
 
319
346
  getAdapter(platform) { return this.adapters.get(platform) }
320
347
 
348
+ async sendToMessenger(target, payload, opts = {}) {
349
+ if (opts.reliable !== false && this.outbox) {
350
+ return this.outbox.enqueue({
351
+ target,
352
+ payload: typeof payload === 'string' ? { text: payload } : payload,
353
+ type: opts.type || 'message',
354
+ })
355
+ }
356
+ return this.messengerSend(target, payload)
357
+ }
358
+
321
359
  async messengerSend(target, payload) {
322
360
  let resolved = target
323
361
  if (!resolved?.chatId && resolved?.platform) {
@@ -571,10 +609,27 @@ export class Gateway {
571
609
  return { provider: provider || selection.provider, model: model || selection.model }
572
610
  }
573
611
 
612
+ resolveEffectiveCwdFor(chatId, threadId = 0) {
613
+ if (this.topicWorkspaces) {
614
+ const bound = this.topicWorkspaces.get(chatId, threadId)
615
+ if (bound?.path) {
616
+ const warning = getCwdWarning(bound.path)
617
+ if (!warning) {
618
+ try {
619
+ return realpathSync(resolve(bound.path))
620
+ } catch {
621
+ return resolve(bound.path)
622
+ }
623
+ }
624
+ }
625
+ }
626
+ return resolveEffectiveCwd(this.config.agent?.cwd, this.ctx?.logger)
627
+ }
628
+
574
629
  async createChat(key, input, existingSessionId) {
575
630
  const { provider, model } = this.resolveAgentModel()
576
631
  const agentCfg = this.config.agent || {}
577
- const cwd = resolveEffectiveCwd(agentCfg.cwd, this.ctx.logger)
632
+ const cwd = this.resolveEffectiveCwdFor(input.chatId, input.threadId)
578
633
  const self = this
579
634
  const agents = this.ctx.get?.('agents') || this.ctx.agents
580
635
  const targetSessionId = existingSessionId ? SessionId(existingSessionId) : SessionId(`msgw-${randomUUID()}`)
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Companion client for dsh-memory-brain service (#152).
3
+ *
4
+ * Implements persistent user memory and profile context over memory-brain's
5
+ * JSON-RPC /mcp and REST interface:
6
+ * - Scoped strictly to `user:<userId>` to prevent cross-user context leakage.
7
+ * - Non-blocking, fail-open: gateway turns never fail if memory-brain is offline.
8
+ * - Native Node 20 fetch without external dependencies.
9
+ */
10
+
11
+ export function resolveMemoryConfig(config) {
12
+ const cfg = config || {}
13
+ const tokenEnv = cfg.tokenEnv || 'MEMORY_BRAIN_TOKEN'
14
+ const token = cfg.token || (typeof process !== 'undefined' && process.env ? process.env[tokenEnv] : '') || ''
15
+ const baseUrl = (cfg.baseUrl || (typeof process !== 'undefined' && process.env ? process.env.MEMORY_BRAIN_URL : '') || 'http://127.0.0.1:28080').replace(/\/+$/, '')
16
+ return {
17
+ enabled: cfg.enabled !== false,
18
+ baseUrl,
19
+ token,
20
+ tokenEnv,
21
+ autoInject: cfg.autoInject !== false,
22
+ budget: Number(cfg.budget) || 2000,
23
+ timeoutMs: Number(cfg.timeoutMs) || 3500,
24
+ }
25
+ }
26
+
27
+ export function parseMemoryListText(text) {
28
+ if (!text || typeof text !== 'string') return []
29
+ const items = []
30
+ for (const line of text.split('\n')) {
31
+ const trimmed = line.trim()
32
+ const idMatch = trimmed.match(/\[id:([0-9a-fA-F-]+)\]/)
33
+ if (!idMatch) continue
34
+ const id = idMatch[1]
35
+ let content = trimmed
36
+ const dashIdx = trimmed.indexOf('—')
37
+ if (dashIdx !== -1) {
38
+ content = trimmed.slice(dashIdx + 1).trim()
39
+ } else {
40
+ content = trimmed.replace(/^-?\s*\[id:[^\]]+\]\s*/, '').trim()
41
+ }
42
+ items.push({ id, content, raw: trimmed })
43
+ }
44
+ return items
45
+ }
46
+
47
+ export function cleanRecalledMemories(text) {
48
+ if (!text || typeof text !== 'string') return ''
49
+ const lines = text.split('\n')
50
+ const out = []
51
+ for (const line of lines) {
52
+ const trimmed = line.trim()
53
+ if (!trimmed) continue
54
+ if (trimmed.startsWith('[protocol') || trimmed.startsWith('Index not fully ready')) continue
55
+ const idMatch = trimmed.match(/\[id:([0-9a-fA-F-]+)\]\s*(.*?)(?:\s+—\s+written by.*)?$/)
56
+ if (idMatch) {
57
+ const fact = idMatch[2].replace(/\s+—\s+written by.*$/, '').trim()
58
+ if (fact) out.push(`- ${fact}`)
59
+ } else if (trimmed.startsWith('- ')) {
60
+ out.push(trimmed)
61
+ }
62
+ }
63
+ return out.join('\n')
64
+ }
65
+
66
+ export function formatMemoryContext(recalledText, userId) {
67
+ const clean = cleanRecalledMemories(recalledText)
68
+ if (!clean) return ''
69
+ return `[User Memory Profile (userId: ${userId})]:\n${clean}`
70
+ }
71
+
72
+ export function extractConversationalPreferences(text) {
73
+ if (!text || typeof text !== 'string') return null
74
+ const s = text.trim()
75
+ if (s.startsWith('/')) return null
76
+ const patterns = [
77
+ /^(?:запомни|сохрани в память)(?:\s*[:,]\s*|\s+)(.+)$/i,
78
+ /^(?:remember|note that)(?:\s*[:,]\s*|\s+)(.+)$/i,
79
+ /(?:я предпочитаю|мое предпочтение|мой выбор)\s+(.+)/i,
80
+ /(?:i prefer|my preference is)\s+(.+)/i,
81
+ /(?:всегда отвечай|всегда пиши|всегда используй)\s+(.+)/i,
82
+ /(?:always answer|always reply|always use)\s+(.+)/i,
83
+ /(?:меня зовут|мое имя)\s+([A-Za-zА-Яа-яЁё0-9_-]+)/i,
84
+ /(?:my name is|call me)\s+([A-Za-z0-9_-]+)/i,
85
+ ]
86
+ for (const pat of patterns) {
87
+ const m = s.match(pat)
88
+ if (m && m[1]) return m[1].trim()
89
+ }
90
+ return null
91
+ }
92
+
93
+ export async function callMemoryMcp(rawConfig, toolName, args, signal) {
94
+ const cfg = resolveMemoryConfig(rawConfig)
95
+ if (!cfg.enabled) {
96
+ return { ok: false, error: 'memory_disabled' }
97
+ }
98
+
99
+ const controller = new AbortController()
100
+ const timeoutId = setTimeout(() => controller.abort(), cfg.timeoutMs)
101
+ let onAbort = null
102
+ if (signal) {
103
+ onAbort = () => controller.abort()
104
+ signal.addEventListener('abort', onAbort, { once: true })
105
+ }
106
+
107
+ try {
108
+ const headers = {
109
+ 'Content-Type': 'application/json',
110
+ Accept: 'application/json, text/event-stream',
111
+ }
112
+ if (cfg.token) {
113
+ headers.Authorization = `Bearer ${cfg.token}`
114
+ }
115
+
116
+ const res = await fetch(`${cfg.baseUrl}/mcp`, {
117
+ method: 'POST',
118
+ headers,
119
+ body: JSON.stringify({
120
+ jsonrpc: '2.0',
121
+ id: `msgw-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`,
122
+ method: 'tools/call',
123
+ params: {
124
+ name: toolName,
125
+ arguments: args || {},
126
+ },
127
+ }),
128
+ signal: controller.signal,
129
+ })
130
+
131
+ if (!res.ok) {
132
+ return { ok: false, error: `HTTP ${res.status}` }
133
+ }
134
+
135
+ const data = await res.json()
136
+ if (data?.error) {
137
+ return { ok: false, error: data.error.message || `error code ${data.error.code}` }
138
+ }
139
+
140
+ const text = data?.result?.content?.[0]?.text
141
+ return { ok: true, text: typeof text === 'string' ? text : JSON.stringify(data?.result ?? {}) }
142
+ } catch (err) {
143
+ return { ok: false, error: err?.message || String(err) }
144
+ } finally {
145
+ clearTimeout(timeoutId)
146
+ if (signal && onAbort) {
147
+ signal.removeEventListener('abort', onAbort)
148
+ }
149
+ }
150
+ }
151
+
152
+ export async function listUserMemories({ config, userId, limit = 20, signal }) {
153
+ if (!userId) return { ok: false, error: 'missing_user_id', items: [] }
154
+ const scope = `user:${userId}`
155
+ const res = await callMemoryMcp(config, 'brain_list', { scope, limit }, signal)
156
+ if (!res.ok) {
157
+ return { ok: false, error: res.error, items: [] }
158
+ }
159
+ const items = parseMemoryListText(res.text)
160
+ return { ok: true, items, raw: res.text }
161
+ }
162
+
163
+ export async function recallUserMemories({ config, userId, query = '', budget, signal }) {
164
+ if (!userId) return ''
165
+ const scope = `user:${userId}`
166
+ const cleanQ = String(query || '').trim()
167
+
168
+ // If query is trivial or empty, recall user profile by listing memories
169
+ if (!cleanQ || cleanQ.length < 3) {
170
+ const listRes = await listUserMemories({ config, userId, limit: 10, signal })
171
+ if (!listRes.ok || !listRes.items.length) return ''
172
+ const facts = listRes.items.map((it) => `- ${it.content}`).join('\n')
173
+ return formatMemoryContext(facts, userId)
174
+ }
175
+
176
+ const res = await callMemoryMcp(config, 'brain_recall', {
177
+ query: cleanQ,
178
+ scope,
179
+ limit: 10,
180
+ budget: budget || 2000,
181
+ }, signal)
182
+
183
+ if (!res.ok || !res.text) return ''
184
+ return formatMemoryContext(res.text, userId)
185
+ }
186
+
187
+ export async function rememberUserFact({ config, userId, content, category = 'preference', signal }) {
188
+ if (!userId) return { ok: false, error: 'missing_user_id' }
189
+ const text = String(content || '').trim()
190
+ if (!text) return { ok: false, error: 'empty_content' }
191
+ const scope = `user:${userId}`
192
+
193
+ const res = await callMemoryMcp(config, 'brain_remember', {
194
+ content: text,
195
+ scope,
196
+ category,
197
+ permanent: true,
198
+ }, signal)
199
+
200
+ if (!res.ok) return res
201
+
202
+ // Try extracting memoryId from json response if present
203
+ let memoryId = null
204
+ try {
205
+ const parsed = JSON.parse(res.text)
206
+ memoryId = parsed.memoryId || parsed.id || parsed.memoryIds?.[0] || null
207
+ } catch {
208
+ /* raw string response is fine */
209
+ }
210
+
211
+ return { ok: true, memoryId, text: res.text }
212
+ }
213
+
214
+ export async function forgetUserMemory({ config, userId, memoryId, signal }) {
215
+ if (!userId) return { ok: false, error: 'missing_user_id' }
216
+ const target = String(memoryId || '').trim()
217
+ if (!target) return { ok: false, error: 'missing_memory_id' }
218
+
219
+ if (target.toLowerCase() === 'all') {
220
+ const listRes = await listUserMemories({ config, userId, limit: 100, signal })
221
+ if (!listRes.ok) return listRes
222
+ let forgotten = 0
223
+ for (const item of listRes.items) {
224
+ const delRes = await callMemoryMcp(config, 'brain_forget', {
225
+ memory_id: item.id,
226
+ confirm: true,
227
+ }, signal)
228
+ if (delRes.ok) forgotten++
229
+ }
230
+ return { ok: true, count: forgotten }
231
+ }
232
+
233
+ // Support short prefix matches (e.g. 8 chars)
234
+ let resolvedId = target
235
+ if (target.length < 36) {
236
+ const listRes = await listUserMemories({ config, userId, limit: 50, signal })
237
+ if (listRes.ok && listRes.items) {
238
+ const match = listRes.items.find((it) => it.id.toLowerCase().startsWith(target.toLowerCase()))
239
+ if (match) resolvedId = match.id
240
+ }
241
+ }
242
+
243
+ const res = await callMemoryMcp(config, 'brain_forget', {
244
+ memory_id: resolvedId,
245
+ confirm: true,
246
+ }, signal)
247
+
248
+ return res
249
+ }
250
+
251
+ export async function recordTurnMemory({ config, userId, userText, assistantText, signal, logger }) {
252
+ const extracted = extractConversationalPreferences(userText)
253
+ if (!extracted) return { ok: true, skipped: true }
254
+
255
+ try {
256
+ const res = await rememberUserFact({
257
+ config,
258
+ userId,
259
+ content: extracted,
260
+ category: 'preference',
261
+ signal,
262
+ })
263
+ if (res.ok && logger) {
264
+ logger.debug?.(`[memory-brain] auto-recorded user preference: "${extracted}" for user ${userId}`)
265
+ }
266
+ return res
267
+ } catch (err) {
268
+ if (logger) {
269
+ logger.debug?.(`[memory-brain] recordTurnMemory failed: ${err?.message || err}`)
270
+ }
271
+ return { ok: false, error: err?.message || String(err) }
272
+ }
273
+ }
package/lib/outbox.js ADDED
@@ -0,0 +1,262 @@
1
+ /**
2
+ * Outbound Transactional Queue (Outbox DLQ) with Offline Recovery & Backoff (#155).
3
+ *
4
+ * Guarantees zero message loss during temporary network hiccups, Telegram 429
5
+ * throttling, 502/504 gateway errors, and process restarts:
6
+ * - Atomic persistent disk queue (~/.dsh/storages/outbox-queue.json).
7
+ * - Exponential backoff with jitter and Telegram 429 `retry_after` awareness.
8
+ * - Dead Letter Queue (DLQ) for items failing after max retries or terminal errors.
9
+ * - Recovery of pending uncommitted outbound jobs on gateway startup.
10
+ */
11
+
12
+ import { existsSync, readFileSync } from 'node:fs'
13
+ import { join } from 'node:path'
14
+ import { homedir } from 'node:os'
15
+ import { randomUUID } from 'node:crypto'
16
+ import { writeJsonAtomic } from './storage-atomic.js'
17
+ import { isTopicGoneError } from './telegram-errors.js'
18
+
19
+ export function resolveOutboxStoragePath(baseDir) {
20
+ if (baseDir) return join(baseDir, 'outbox-queue.json')
21
+ const dshHome = (typeof process !== 'undefined' && process.env?.DSH_HOME) || ''
22
+ if (dshHome) return join(dshHome, 'storages', 'outbox-queue.json')
23
+ return join(homedir(), '.dsh', 'storages', 'outbox-queue.json')
24
+ }
25
+
26
+ export function parseRetryAfter(err) {
27
+ if (!err) return null
28
+ if (typeof err.parameters?.retry_after === 'number') {
29
+ return err.parameters.retry_after
30
+ }
31
+ if (typeof err.response?.parameters?.retry_after === 'number') {
32
+ return err.response.parameters.retry_after
33
+ }
34
+ const msg = String(err.message || '')
35
+ const match = msg.match(/retry after (\d+)/i)
36
+ if (match) {
37
+ const val = parseInt(match[1], 10)
38
+ if (!Number.isNaN(val) && val > 0) return val
39
+ }
40
+ return null
41
+ }
42
+
43
+ export function computeOutboxBackoffMs(attempts, retryAfterSeconds, maxDelayMs = 60_000) {
44
+ const jitter = Math.floor(Math.random() * 400)
45
+ if (typeof retryAfterSeconds === 'number' && retryAfterSeconds > 0) {
46
+ return Math.min(maxDelayMs, (retryAfterSeconds * 1000) + jitter)
47
+ }
48
+ const count = Math.max(1, Math.min(10, Number(attempts) || 1))
49
+ const baseDelay = 1000 * Math.pow(2, count - 1)
50
+ return Math.min(maxDelayMs, baseDelay + jitter)
51
+ }
52
+
53
+ export function isTerminalSendError(err) {
54
+ if (!err) return false
55
+ if (isTopicGoneError(err)) return true
56
+ const msg = String(err.message || '').toLowerCase()
57
+ return (
58
+ msg.includes('bot was blocked by the user') ||
59
+ msg.includes('user is deactivated') ||
60
+ msg.includes('chat not found') ||
61
+ msg.includes('message is too long') ||
62
+ msg.includes('message text is empty') ||
63
+ msg.includes("can't parse entities") ||
64
+ msg.includes('wrong file identifier') ||
65
+ msg.includes('have no rights to send a message')
66
+ )
67
+ }
68
+
69
+ export function createOutboxQueue(options = {}) {
70
+ const {
71
+ storagePath = resolveOutboxStoragePath(),
72
+ senderFn,
73
+ onDlq,
74
+ logger,
75
+ flushIntervalMs = 1000,
76
+ defaultMaxAttempts = 5,
77
+ } = options
78
+
79
+ let pending = []
80
+ let dlq = []
81
+ let timer = null
82
+ let isFlushing = false
83
+ let isStopped = false
84
+
85
+ function loadSync() {
86
+ try {
87
+ if (existsSync(storagePath)) {
88
+ const raw = readFileSync(storagePath, 'utf8')
89
+ const data = JSON.parse(raw)
90
+ if (Array.isArray(data.pending)) pending = data.pending
91
+ if (Array.isArray(data.dlq)) dlq = data.dlq
92
+ if (pending.length > 0 && logger) {
93
+ logger.info?.(`[outbox] recovered ${pending.length} pending outbound jobs from disk`)
94
+ }
95
+ }
96
+ } catch (err) {
97
+ if (logger) {
98
+ logger.warn?.(`[outbox] failed to load storage: ${err.message}`)
99
+ }
100
+ }
101
+ }
102
+
103
+ loadSync()
104
+
105
+ async function persist() {
106
+ try {
107
+ await writeJsonAtomic(storagePath, { pending, dlq })
108
+ } catch (err) {
109
+ if (logger) {
110
+ logger.warn?.(`[outbox] failed to persist queue to disk: ${err.message}`)
111
+ }
112
+ }
113
+ }
114
+
115
+ async function flush() {
116
+ if (isFlushing || isStopped) return
117
+ if (!pending.length) return
118
+ isFlushing = true
119
+ const now = Date.now()
120
+ let dirty = false
121
+
122
+ try {
123
+ const readyJobs = pending.filter((j) => (j.nextRetryAt || 0) <= now)
124
+ for (const job of readyJobs) {
125
+ if (isStopped) break
126
+ try {
127
+ if (typeof senderFn !== 'function') {
128
+ throw new Error('outbox senderFn not configured')
129
+ }
130
+ await senderFn(job)
131
+ pending = pending.filter((j) => j.id !== job.id)
132
+ dirty = true
133
+ } catch (err) {
134
+ job.attempts = (job.attempts || 0) + 1
135
+ job.lastError = err?.message || String(err)
136
+ const terminal = isTerminalSendError(err)
137
+ const max = job.maxAttempts || defaultMaxAttempts
138
+
139
+ if (terminal || job.attempts >= max) {
140
+ pending = pending.filter((j) => j.id !== job.id)
141
+ dlq.unshift({
142
+ id: job.id,
143
+ type: job.type,
144
+ target: job.target,
145
+ payload: job.payload,
146
+ attempts: job.attempts,
147
+ createdAt: job.createdAt,
148
+ failedAt: Date.now(),
149
+ terminal,
150
+ error: job.lastError,
151
+ })
152
+ if (dlq.length > 100) dlq.length = 100
153
+ dirty = true
154
+ if (typeof onDlq === 'function') {
155
+ try { onDlq(job, err) } catch (dlqErr) {
156
+ logger?.warn?.(`[outbox] onDlq error: ${dlqErr.message}`)
157
+ }
158
+ }
159
+ } else {
160
+ const retryAfter = parseRetryAfter(err)
161
+ const delayMs = computeOutboxBackoffMs(job.attempts, retryAfter)
162
+ job.nextRetryAt = Date.now() + delayMs
163
+ dirty = true
164
+ if (logger) {
165
+ logger.warn?.(`[outbox] job ${job.id} failed (attempt ${job.attempts}/${max}), retrying in ${Math.round(delayMs / 1000)}s: ${job.lastError}`)
166
+ }
167
+ }
168
+ }
169
+ }
170
+ } finally {
171
+ isFlushing = false
172
+ if (dirty) {
173
+ await persist()
174
+ }
175
+ }
176
+ }
177
+
178
+ async function enqueue(job) {
179
+ const item = {
180
+ id: job.id || randomUUID(),
181
+ type: job.type || 'message',
182
+ target: job.target,
183
+ payload: job.payload,
184
+ attempts: 0,
185
+ maxAttempts: job.maxAttempts || defaultMaxAttempts,
186
+ createdAt: Date.now(),
187
+ nextRetryAt: Date.now(),
188
+ lastError: null,
189
+ }
190
+ pending.push(item)
191
+ await persist()
192
+ void flush()
193
+ return item
194
+ }
195
+
196
+ function start() {
197
+ isStopped = false
198
+ void flush()
199
+ if (!timer && flushIntervalMs > 0) {
200
+ timer = setInterval(() => { void flush() }, flushIntervalMs)
201
+ timer.unref?.()
202
+ }
203
+ }
204
+
205
+ function stop() {
206
+ isStopped = true
207
+ if (timer) {
208
+ clearInterval(timer)
209
+ timer = null
210
+ }
211
+ }
212
+
213
+ function status() {
214
+ return {
215
+ pendingCount: pending.length,
216
+ dlqCount: dlq.length,
217
+ pending: [...pending],
218
+ dlq: [...dlq],
219
+ }
220
+ }
221
+
222
+ async function retryDlq() {
223
+ if (!dlq.length) return 0
224
+ const count = dlq.length
225
+ for (const item of dlq) {
226
+ pending.push({
227
+ id: item.id || randomUUID(),
228
+ type: item.type,
229
+ target: item.target,
230
+ payload: item.payload,
231
+ attempts: 0,
232
+ maxAttempts: defaultMaxAttempts,
233
+ createdAt: Date.now(),
234
+ nextRetryAt: Date.now(),
235
+ lastError: null,
236
+ })
237
+ }
238
+ dlq = []
239
+ await persist()
240
+ await flush()
241
+ return count
242
+ }
243
+
244
+ async function clearDlq() {
245
+ const count = dlq.length
246
+ dlq = []
247
+ await persist()
248
+ return count
249
+ }
250
+
251
+ return {
252
+ enqueue,
253
+ flush,
254
+ start,
255
+ stop,
256
+ status,
257
+ retryDlq,
258
+ clearDlq,
259
+ get pending() { return pending },
260
+ get dlq() { return dlq },
261
+ }
262
+ }