@goodandready/dsh-messenger-gateway 0.4.44 → 0.4.46
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 +4 -0
- package/README.ru.md +2 -0
- package/README.zh.md +2 -0
- package/lib/adapters/telegram.js +25 -13
- package/lib/client.js +205 -23
- package/lib/config.js +30 -6
- package/lib/forum-mirror.js +3 -0
- package/lib/gateway.js +20 -0
- package/lib/index.js +171 -147
- package/lib/outbox.js +4 -1
- package/lib/pairing.js +35 -20
- package/lib/personas.js +27 -11
- package/lib/scheduler.js +30 -11
- package/lib/secret-guard.js +2 -0
- package/lib/settings-bridge.js +32 -4
- package/lib/storage-atomic.js +174 -8
- package/lib/voice-prefs.js +31 -21
- package/lib/webapp.js +8 -5
- package/lib/workspace.js +23 -11
- package/package.json +1 -1
package/lib/settings-bridge.js
CHANGED
|
@@ -16,8 +16,17 @@ export function setupSettings(ctx, { namespace, Config, resolveConfig, baseConfi
|
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
+
// Bus listeners are registered INSIDE a ctx.effect (#223): when the plugin
|
|
20
|
+
// instance is disposed the unbinds run even if the settings service never
|
|
21
|
+
// mounted, and a repeated apply cannot accumulate stale listeners.
|
|
19
22
|
const unbinds = []
|
|
20
|
-
|
|
23
|
+
const unbindBusListeners = () => {
|
|
24
|
+
for (const unbind of unbinds.splice(0)) {
|
|
25
|
+
try { unbind() } catch (err) { logger?.debug?.(`${namespace}: unbind error:`, err?.message || err) }
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const registerBusListeners = () => {
|
|
29
|
+
if (typeof ctx?.on !== 'function') return
|
|
21
30
|
const u1 = ctx.on('loader/volatile-update', () => applyLiveConfig())
|
|
22
31
|
if (typeof u1 === 'function') unbinds.push(u1)
|
|
23
32
|
const u2 = ctx.on('internal/update', (cfg) => applyLiveConfig(cfg))
|
|
@@ -25,11 +34,25 @@ export function setupSettings(ctx, { namespace, Config, resolveConfig, baseConfi
|
|
|
25
34
|
const u3 = ctx.on('app-boot/config-reload', () => applyLiveConfig())
|
|
26
35
|
if (typeof u3 === 'function') unbinds.push(u3)
|
|
27
36
|
}
|
|
37
|
+
if (typeof ctx?.effect === 'function') {
|
|
38
|
+
ctx.effect(() => {
|
|
39
|
+
registerBusListeners()
|
|
40
|
+
return unbindBusListeners
|
|
41
|
+
}, `${namespace}: bus listeners`)
|
|
42
|
+
} else {
|
|
43
|
+
registerBusListeners()
|
|
44
|
+
}
|
|
28
45
|
|
|
46
|
+
let mounted = false
|
|
29
47
|
const mountSettings = (sctx) => {
|
|
48
|
+
// Idempotent mount (#223): a settings service that re-emits its mount
|
|
49
|
+
// event must not re-register watchers or duplicate subscriptions. A call
|
|
50
|
+
// without a service does not count as a mount.
|
|
51
|
+
if (mounted) return
|
|
30
52
|
const service = sctx?.settings || (typeof sctx?.get === 'function' ? sctx.get('settings') : null)
|
|
31
53
|
const effect = typeof sctx?.effect === 'function' ? sctx.effect.bind(sctx) : (typeof ctx.effect === 'function' ? ctx.effect.bind(ctx) : null)
|
|
32
54
|
if (!service) return
|
|
55
|
+
mounted = true
|
|
33
56
|
|
|
34
57
|
if (typeof service.register === 'function') {
|
|
35
58
|
settingsApi = service.register(namespace, Config, { base: baseConfig || {} })
|
|
@@ -120,17 +143,22 @@ export function setupSettings(ctx, { namespace, Config, resolveConfig, baseConfi
|
|
|
120
143
|
}
|
|
121
144
|
|
|
122
145
|
const onFn = typeof sctx?.on === 'function' ? sctx.on.bind(sctx) : (typeof ctx?.on === 'function' ? ctx.on.bind(ctx) : null)
|
|
146
|
+
let unbindDocumentUpdated = null
|
|
123
147
|
if (onFn) {
|
|
124
148
|
const d1 = onFn('settings/document-updated', (updatedNs) => {
|
|
125
149
|
if (updatedNs === namespace) applyLiveConfig()
|
|
126
150
|
})
|
|
127
|
-
if (typeof d1 === 'function')
|
|
151
|
+
if (typeof d1 === 'function') unbindDocumentUpdated = d1
|
|
128
152
|
}
|
|
129
153
|
|
|
130
154
|
if (effect) {
|
|
131
155
|
effect(() => () => {
|
|
132
|
-
|
|
133
|
-
|
|
156
|
+
// Bus listeners (loader/volatile-update etc.) belong to the top-level
|
|
157
|
+
// effect above; this dispose only cleans the settings-service side
|
|
158
|
+
// (#223).
|
|
159
|
+
if (unbindDocumentUpdated) {
|
|
160
|
+
try { unbindDocumentUpdated() } catch (err) { logger?.debug?.(`${namespace}: unbind error:`, err?.message || err) }
|
|
161
|
+
unbindDocumentUpdated = null
|
|
134
162
|
}
|
|
135
163
|
settingsApi = undefined
|
|
136
164
|
}, `${namespace}: settings`)
|
package/lib/storage-atomic.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { open, rename, mkdir, unlink } from 'node:fs/promises'
|
|
2
|
+
import { renameSync, mkdirSync, unlinkSync, openSync, writeSync, fsyncSync, closeSync, readFileSync, existsSync } from 'node:fs'
|
|
3
3
|
import { dirname } from 'node:path'
|
|
4
4
|
import { randomUUID } from 'node:crypto'
|
|
5
5
|
|
|
@@ -10,7 +10,55 @@ const FILE_MODE = 0o600
|
|
|
10
10
|
const DIR_MODE = 0o700
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
13
|
+
* A state file could not be parsed and no usable backup existed (#227).
|
|
14
|
+
* The corrupted bytes are never deleted: a preserved copy keeps the path in
|
|
15
|
+
* `corruptBackup` so nothing is lost silently.
|
|
16
|
+
*/
|
|
17
|
+
export class StateCorruptError extends Error {
|
|
18
|
+
constructor(filePath, cause) {
|
|
19
|
+
super(`state file ${filePath} is corrupted and no valid backup exists`)
|
|
20
|
+
this.name = 'StateCorruptError'
|
|
21
|
+
this.code = 'STATE_CORRUPT'
|
|
22
|
+
this.filePath = filePath
|
|
23
|
+
this.corruptBackup = null
|
|
24
|
+
this.cause = cause
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The state file was written by a NEWER schema version than this build supports. */
|
|
29
|
+
export class StateVersionError extends Error {
|
|
30
|
+
constructor(filePath, fileVersion, supportedVersion) {
|
|
31
|
+
super(`state file ${filePath} has schema version ${fileVersion}, newer than supported ${supportedVersion}`)
|
|
32
|
+
this.name = 'StateVersionError'
|
|
33
|
+
this.code = 'STATE_VERSION'
|
|
34
|
+
this.filePath = filePath
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function fsyncDir(dir) {
|
|
39
|
+
let fh
|
|
40
|
+
try {
|
|
41
|
+
fh = await open(dir, 'r')
|
|
42
|
+
await fh.sync()
|
|
43
|
+
} catch { /* directory fsync is best-effort (some filesystems refuse it) */ }
|
|
44
|
+
finally { try { await fh?.close() } catch { /* ignore */ } }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function fsyncDirSync(dir) {
|
|
48
|
+
let fd
|
|
49
|
+
try {
|
|
50
|
+
fd = openSync(dir, 'r')
|
|
51
|
+
fsyncSync(fd)
|
|
52
|
+
} catch { /* best-effort */ }
|
|
53
|
+
finally { try { if (fd !== undefined) closeSync(fd) } catch { /* ignore */ } }
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Write JSON atomically via temporary file and rename (#227):
|
|
58
|
+
* - the temp file is fsynced BEFORE the rename, so a power cut right after
|
|
59
|
+
* the rename cannot leave an empty or partial state file;
|
|
60
|
+
* - the directory is fsynced after the rename so the rename itself survives;
|
|
61
|
+
* - the previous good version is kept as `<path>.bak` for recovery.
|
|
14
62
|
* Guarantees that readers never observe partially written files.
|
|
15
63
|
*/
|
|
16
64
|
export async function writeJsonAtomic(filePath, data) {
|
|
@@ -18,27 +66,145 @@ export async function writeJsonAtomic(filePath, data) {
|
|
|
18
66
|
await mkdir(dir, { recursive: true, mode: DIR_MODE })
|
|
19
67
|
const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
|
|
20
68
|
const serialized = JSON.stringify(data, null, 2)
|
|
69
|
+
let fh
|
|
21
70
|
try {
|
|
22
|
-
await
|
|
23
|
-
await
|
|
71
|
+
fh = await open(tmpPath, 'w', FILE_MODE)
|
|
72
|
+
await fh.writeFile(serialized, { encoding: 'utf8' })
|
|
73
|
+
await fh.sync()
|
|
74
|
+
await fh.close()
|
|
75
|
+
fh = undefined
|
|
76
|
+
// Keep the last good version as a recovery sidecar; rotate, then move
|
|
77
|
+
// the new file into place, restoring the backup if the rename fails.
|
|
78
|
+
let hadPrevious = false
|
|
79
|
+
try {
|
|
80
|
+
await rename(filePath, `${filePath}.bak`)
|
|
81
|
+
hadPrevious = true
|
|
82
|
+
} catch (err) {
|
|
83
|
+
if (err?.code !== 'ENOENT') throw err
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
await rename(tmpPath, filePath)
|
|
87
|
+
} catch (err) {
|
|
88
|
+
if (hadPrevious) await rename(`${filePath}.bak`, filePath).catch(() => { void 0 })
|
|
89
|
+
throw err
|
|
90
|
+
}
|
|
91
|
+
await fsyncDir(dir)
|
|
24
92
|
} catch (err) {
|
|
93
|
+
try { await fh?.close() } catch { /* ignore */ }
|
|
25
94
|
await unlink(tmpPath).catch((e) => { void e })
|
|
26
95
|
throw err
|
|
27
96
|
}
|
|
28
97
|
}
|
|
29
98
|
|
|
30
99
|
/**
|
|
31
|
-
* Synchronous atomic JSON write
|
|
100
|
+
* Synchronous atomic JSON write: fsync temp file, rotate previous version
|
|
101
|
+
* to `<path>.bak`, rename, fsync directory (#227).
|
|
32
102
|
*/
|
|
33
103
|
export function writeJsonAtomicSync(filePath, data) {
|
|
34
104
|
const dir = dirname(filePath)
|
|
35
105
|
mkdirSync(dir, { recursive: true, mode: DIR_MODE })
|
|
36
106
|
const tmpPath = `${filePath}.${randomUUID().slice(0, 8)}.tmp`
|
|
107
|
+
let fd
|
|
37
108
|
try {
|
|
38
|
-
|
|
39
|
-
|
|
109
|
+
fd = openSync(tmpPath, 'w', FILE_MODE)
|
|
110
|
+
writeSync(fd, JSON.stringify(data, null, 2), 0, 'utf8')
|
|
111
|
+
fsyncSync(fd)
|
|
112
|
+
} finally {
|
|
113
|
+
try { if (fd !== undefined) closeSync(fd) } catch { /* ignore */ }
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
let hadPrevious = false
|
|
117
|
+
try {
|
|
118
|
+
renameSync(filePath, `${filePath}.bak`)
|
|
119
|
+
hadPrevious = true
|
|
120
|
+
} catch (err) {
|
|
121
|
+
if (err?.code !== 'ENOENT') throw err
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
renameSync(tmpPath, filePath)
|
|
125
|
+
} catch (err) {
|
|
126
|
+
if (hadPrevious) { try { renameSync(`${filePath}.bak`, filePath) } catch { /* ignore */ } }
|
|
127
|
+
throw err
|
|
128
|
+
}
|
|
129
|
+
fsyncDirSync(dir)
|
|
40
130
|
} catch (err) {
|
|
41
131
|
try { unlinkSync(tmpPath) } catch { /* ignore */ }
|
|
42
132
|
throw err
|
|
43
133
|
}
|
|
44
134
|
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Apply the schema version policy to a parsed state file (#227).
|
|
138
|
+
* Legacy files (no `schemaVersion`) go through `migrate(raw, 0)`.
|
|
139
|
+
* @param {any} parsed parsed JSON payload
|
|
140
|
+
* @param {number} version the schema version this build supports
|
|
141
|
+
* @param {((raw: any, fromVersion: number) => any) | undefined} migrate
|
|
142
|
+
* @param {string} filePath only used for error messages
|
|
143
|
+
*/
|
|
144
|
+
function applySchemaVersion(parsed, version, migrate, filePath) {
|
|
145
|
+
const fileVersion = Number(parsed?.schemaVersion)
|
|
146
|
+
if (!Number.isFinite(fileVersion)) {
|
|
147
|
+
// Legacy pre-versioning file: migrate from v0.
|
|
148
|
+
if (typeof migrate !== 'function') return { ...parsed }
|
|
149
|
+
return migrate(parsed, 0)
|
|
150
|
+
}
|
|
151
|
+
if (fileVersion > version) throw new StateVersionError(filePath, fileVersion, version)
|
|
152
|
+
if (fileVersion < version) {
|
|
153
|
+
if (typeof migrate !== 'function') throw new StateVersionError(filePath, fileVersion, version)
|
|
154
|
+
return migrate(parsed, fileVersion)
|
|
155
|
+
}
|
|
156
|
+
return parsed
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Recover from a corrupted state file (#227): the corrupted bytes are moved
|
|
161
|
+
* aside to a timestamped `<path>.corrupt-<ts>` copy (never deleted), then the
|
|
162
|
+
* `<path>.bak` sidecar written by the previous save is tried. If the backup
|
|
163
|
+
* parses, it self-heals the main file and is returned. Otherwise a
|
|
164
|
+
* StateCorruptError is thrown — callers must NOT fall back to empty state
|
|
165
|
+
* silently.
|
|
166
|
+
*/
|
|
167
|
+
function recoverCorrupt(filePath, version, migrate, cause) {
|
|
168
|
+
const stamp = new Date().toISOString().replace(/[:.]/g, '-')
|
|
169
|
+
const corruptPath = `${filePath}.corrupt-${stamp}`
|
|
170
|
+
try { renameSync(filePath, corruptPath) } catch { /* best-effort preservation */ }
|
|
171
|
+
const bakPath = `${filePath}.bak`
|
|
172
|
+
if (existsSync(bakPath)) {
|
|
173
|
+
try {
|
|
174
|
+
const bakParsed = JSON.parse(readFileSync(bakPath, 'utf8'))
|
|
175
|
+
if (bakParsed === null || typeof bakParsed !== 'object') throw new Error('backup is not an object')
|
|
176
|
+
const data = applySchemaVersion(bakParsed, version, migrate, filePath)
|
|
177
|
+
try { writeJsonAtomicSync(filePath, data) } catch { /* self-heal is best-effort */ }
|
|
178
|
+
return { data, recovered: true, corruptBackup: corruptPath }
|
|
179
|
+
} catch { /* backup unusable: fall through to the explicit error */ }
|
|
180
|
+
}
|
|
181
|
+
const err = new StateCorruptError(filePath, cause)
|
|
182
|
+
err.corruptBackup = existsSync(corruptPath) ? corruptPath : null
|
|
183
|
+
throw err
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Read a versioned JSON state file synchronously (#227).
|
|
188
|
+
* - missing file: `{ data: null, recovered: false }`
|
|
189
|
+
* - legacy/older schema: migrated through `migrate`
|
|
190
|
+
* - corrupted file: recovered from `.bak` when possible, otherwise
|
|
191
|
+
* StateCorruptError (corrupted bytes preserved next to the file)
|
|
192
|
+
* - newer schema than this build: StateVersionError
|
|
193
|
+
*
|
|
194
|
+
* @param {string} filePath state file path
|
|
195
|
+
* @param {{ version?: number, migrate?: (raw: any, fromVersion: number) => any }} [opts]
|
|
196
|
+
* @returns {{ data: any, recovered: boolean, corruptBackup: string | null }}
|
|
197
|
+
*/
|
|
198
|
+
export function readJsonStateSync(filePath, { version = 1, migrate } = {}) {
|
|
199
|
+
if (!filePath || !existsSync(filePath)) return { data: null, recovered: false, corruptBackup: null }
|
|
200
|
+
let parsed
|
|
201
|
+
try {
|
|
202
|
+
parsed = JSON.parse(readFileSync(filePath, 'utf8'))
|
|
203
|
+
} catch (err) {
|
|
204
|
+
return recoverCorrupt(filePath, version, migrate, err)
|
|
205
|
+
}
|
|
206
|
+
if (parsed === null || typeof parsed !== 'object') {
|
|
207
|
+
return recoverCorrupt(filePath, version, migrate, new Error(`unexpected payload type ${typeof parsed}`))
|
|
208
|
+
}
|
|
209
|
+
return { data: applySchemaVersion(parsed, version, migrate, filePath), recovered: false, corruptBackup: null }
|
|
210
|
+
}
|
package/lib/voice-prefs.js
CHANGED
|
@@ -1,38 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dirname } from "node:path"
|
|
3
|
-
import { randomUUID } from "node:crypto"
|
|
1
|
+
import { writeJsonAtomicSync, readJsonStateSync, StateCorruptError } from "./storage-atomic.js"
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
const VOICE_PREFS_SCHEMA_VERSION = 1
|
|
4
|
+
|
|
5
|
+
// Legacy files store the prefs map at the top level; v1 wraps it so the
|
|
6
|
+
// schema version field never collides with a user id key (#227).
|
|
7
|
+
const migrateVoicePrefs = (raw) => ({
|
|
8
|
+
schemaVersion: VOICE_PREFS_SCHEMA_VERSION,
|
|
9
|
+
prefs: raw && typeof raw === "object" ? raw : {},
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
const logLoadFailure = (logger, kind, err) => {
|
|
13
|
+
const detail = err instanceof StateCorruptError
|
|
14
|
+
? `state file corrupted and unrecoverable; starting empty; corrupted copy preserved at ${err.corruptBackup || "an unknown path"}`
|
|
15
|
+
: `read error: ${err?.message || err}`
|
|
16
|
+
const message = `[dsh-messenger-gateway] ${kind} ${detail}`
|
|
17
|
+
if (typeof logger?.error === "function") logger.error(message)
|
|
18
|
+
else logger?.warn?.(message)
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
/** @param {string} filePath @param {any} [opts] */
|
|
19
|
-
export function createVoicePrefs(filePath, { logger } = {}) {
|
|
22
|
+
export function createVoicePrefs(filePath, { logger, kind = "voice prefs" } = {}) {
|
|
20
23
|
/** @type {Record<string, boolean>} */
|
|
21
24
|
let state = {}
|
|
22
|
-
if (filePath
|
|
25
|
+
if (filePath) {
|
|
23
26
|
try {
|
|
24
|
-
const
|
|
25
|
-
|
|
27
|
+
const { data, recovered, corruptBackup } = readJsonStateSync(filePath, {
|
|
28
|
+
version: VOICE_PREFS_SCHEMA_VERSION,
|
|
29
|
+
migrate: migrateVoicePrefs,
|
|
30
|
+
})
|
|
31
|
+
if (data) state = data.prefs && typeof data.prefs === "object" ? data.prefs : {}
|
|
32
|
+
if (recovered) {
|
|
33
|
+
logger?.warn?.(`[dsh-messenger-gateway] ${kind} recovered from .bak after corruption; preserved copy: ${corruptBackup}`)
|
|
34
|
+
}
|
|
26
35
|
} catch (err) {
|
|
27
|
-
|
|
36
|
+
if (err instanceof StateCorruptError || err?.name === "StateVersionError") logLoadFailure(logger, kind, err)
|
|
37
|
+
else logger?.warn?.(`[dsh-messenger-gateway] ${kind} read error:`, err?.message || err)
|
|
28
38
|
}
|
|
29
39
|
}
|
|
30
40
|
const persist = () => {
|
|
31
41
|
if (!filePath) return
|
|
32
42
|
try {
|
|
33
|
-
writeJsonAtomicSync(filePath, state)
|
|
43
|
+
writeJsonAtomicSync(filePath, { schemaVersion: VOICE_PREFS_SCHEMA_VERSION, prefs: state })
|
|
34
44
|
} catch (err) {
|
|
35
|
-
logger?.warn?.(
|
|
45
|
+
logger?.warn?.(`[dsh-messenger-gateway] ${kind} persist error:`, err?.message || err)
|
|
36
46
|
}
|
|
37
47
|
}
|
|
38
48
|
const key = (userId) => String(Number(userId) || userId || "")
|
package/lib/webapp.js
CHANGED
|
@@ -585,7 +585,8 @@ export function createWebAppHtml(options = {}) {
|
|
|
585
585
|
document.getElementById('stat-uptime').textContent = hrs + 'h ' + mins + 'm'
|
|
586
586
|
document.getElementById('stat-sent').textContent = s.stats && s.stats.sent || 0
|
|
587
587
|
document.getElementById('stat-chats').textContent = s.activeChats || 0
|
|
588
|
-
document.getElementById('stat-outbox').textContent = s.outbox && s.outbox.pending || 0
|
|
588
|
+
document.getElementById('stat-outbox').textContent = (s.outbox && s.outbox.pending || 0)
|
|
589
|
+
+ ((s.outbox && s.outbox.dlq) ? ' (+' + s.outbox.dlq + ' DLQ)' : '')
|
|
589
590
|
document.getElementById('tg-details').textContent = 'Bot: @' + (s.botUsername || 'none') + ' | Transport: ' + (s.config && s.config.telegram && s.config.telegram.transport || 'poll')
|
|
590
591
|
document.getElementById('adapters-list').textContent = (s.adapters || []).map(a => '• ' + a + ': active').join('\n') || 'None'
|
|
591
592
|
document.getElementById('model-details').textContent = 'Model: ' + (s.config && s.config.agent && s.config.agent.model || 'default') + ' | CWD: ' + (s.config && s.config.agent && s.config.agent.effectiveCwd || 'standard')
|
|
@@ -703,10 +704,12 @@ export function createWebAppHandler(options = {}) {
|
|
|
703
704
|
activeChats: gw?.messenger?.activeChats?.() || 0,
|
|
704
705
|
adapters: gw?.messenger?.adapters?.() || [],
|
|
705
706
|
stats: gw?.stats || {},
|
|
706
|
-
outbox: {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
707
|
+
outbox: (() => {
|
|
708
|
+
// Real queue counters (#231): the outbox exposes status(), not
|
|
709
|
+
// pendingCount/dlqCount properties — the old reads were always 0.
|
|
710
|
+
const ob = gw?.outbox?.status?.()
|
|
711
|
+
return { pending: ob?.pendingCount || 0, dlq: ob?.dlqCount || 0 }
|
|
712
|
+
})(),
|
|
710
713
|
config: {
|
|
711
714
|
telegram: {
|
|
712
715
|
transport: tgCfg.transport || 'poll',
|
package/lib/workspace.js
CHANGED
|
@@ -5,7 +5,7 @@ import { readFileSync } from 'node:fs'
|
|
|
5
5
|
import { randomUUID } from 'node:crypto'
|
|
6
6
|
import os from 'node:os'
|
|
7
7
|
import path from 'node:path'
|
|
8
|
-
import { writeJsonAtomic } from './storage-atomic.js'
|
|
8
|
+
import { writeJsonAtomic, writeJsonAtomicSync, readJsonStateSync, StateCorruptError } from './storage-atomic.js'
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* Validates configured cwd path and returns an error description if invalid.
|
|
@@ -148,7 +148,6 @@ export async function registerSessionInWorkspace(ctx, sessionId, cwd, logger) {
|
|
|
148
148
|
|
|
149
149
|
return false
|
|
150
150
|
}
|
|
151
|
-
import { writeJsonAtomicSync } from "./storage-atomic.js"
|
|
152
151
|
|
|
153
152
|
export function inspectGitWorkspace(dirPath) {
|
|
154
153
|
try {
|
|
@@ -170,27 +169,40 @@ export function inspectGitWorkspace(dirPath) {
|
|
|
170
169
|
return { isGit: false, isWorktree: false }
|
|
171
170
|
}
|
|
172
171
|
|
|
172
|
+
const TOPIC_WORKSPACES_SCHEMA_VERSION = 1
|
|
173
|
+
|
|
174
|
+
// Legacy files carried the workspace map bare or under {workspaces}; v1 adds
|
|
175
|
+
// the schema version envelope (#227).
|
|
176
|
+
const migrateTopicWorkspaces = (raw) => ({
|
|
177
|
+
schemaVersion: TOPIC_WORKSPACES_SCHEMA_VERSION,
|
|
178
|
+
workspaces: raw?.workspaces && typeof raw.workspaces === "object" ? raw.workspaces : (raw && typeof raw === "object" ? raw : {}),
|
|
179
|
+
})
|
|
180
|
+
|
|
173
181
|
/** @param {string} filePath @param {any} [opts] */
|
|
174
182
|
export function createTopicWorkspaceStore(filePath, { logger } = {}) {
|
|
175
183
|
let cache = { workspaces: {} }
|
|
176
184
|
try {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
}
|
|
185
|
+
const { data, recovered, corruptBackup } = readJsonStateSync(filePath, {
|
|
186
|
+
version: TOPIC_WORKSPACES_SCHEMA_VERSION,
|
|
187
|
+
migrate: migrateTopicWorkspaces,
|
|
188
|
+
})
|
|
189
|
+
if (data) cache = { workspaces: data.workspaces && typeof data.workspaces === "object" ? data.workspaces : {} }
|
|
190
|
+
if (recovered) {
|
|
191
|
+
logger?.warn?.(`[dsh-messenger-gateway] topic-workspaces recovered from .bak after corruption; preserved copy: ${corruptBackup}`)
|
|
184
192
|
}
|
|
185
193
|
} catch (err) {
|
|
186
|
-
if (err?.
|
|
194
|
+
if (err instanceof StateCorruptError || err?.name === "StateVersionError") {
|
|
195
|
+
const message = `[dsh-messenger-gateway] topic-workspaces corrupted; starting empty; corrupted copy preserved at ${err.corruptBackup || filePath}`
|
|
196
|
+
if (typeof logger?.error === "function") logger.error(message)
|
|
197
|
+
else logger?.warn?.(message)
|
|
198
|
+
} else if (err?.code !== "ENOENT") {
|
|
187
199
|
logger?.warn?.(`[dsh-messenger-gateway] topic-workspaces load error: ${err?.message || err}`)
|
|
188
200
|
}
|
|
189
201
|
}
|
|
190
202
|
|
|
191
203
|
function save() {
|
|
192
204
|
try {
|
|
193
|
-
writeJsonAtomicSync(filePath, cache)
|
|
205
|
+
writeJsonAtomicSync(filePath, { schemaVersion: TOPIC_WORKSPACES_SCHEMA_VERSION, ...cache })
|
|
194
206
|
} catch (err) {
|
|
195
207
|
logger?.warn?.(`[dsh-messenger-gateway] topic-workspaces save error: ${err?.message || err}`)
|
|
196
208
|
}
|
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.46",
|
|
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",
|