@goodandready/dsh-messenger-gateway 0.4.26 → 0.4.28
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/adapters/index.js +1 -0
- package/lib/adapters/telegram.js +16 -1
- package/lib/artifacts.js +16 -1
- package/lib/commands.js +13 -5
- package/lib/config.js +4 -0
- package/lib/cost-tracker.js +228 -0
- package/lib/gateway-commands.js +62 -0
- package/lib/gateway-turn.js +39 -0
- package/lib/gateway.js +37 -0
- package/lib/index.js +15 -0
- package/lib/outbox.js +262 -0
- package/lib/webapp.js +723 -0
- package/package.json +1 -1
package/lib/adapters/index.js
CHANGED
|
@@ -27,6 +27,7 @@ export default function createAdapters(deps) {
|
|
|
27
27
|
apiRoot: tg.apiRoot,
|
|
28
28
|
quickActions: tg.quickActions !== false,
|
|
29
29
|
artifactPreviews: tg.artifactPreviews !== false,
|
|
30
|
+
webAppUrl: tg.webAppUrl || (tg.webhookUrl && String(tg.webhookUrl).startsWith('http') ? `${new URL(tg.webhookUrl).origin}/dsh-messenger-gateway/webapp` : ''),
|
|
30
31
|
mediaGroupDebounceMs: tg.mediaGroupDebounceMs,
|
|
31
32
|
media: config.media,
|
|
32
33
|
onMessage,
|
package/lib/adapters/telegram.js
CHANGED
|
@@ -43,6 +43,7 @@ export class TelegramAdapter {
|
|
|
43
43
|
this.timeoutSeconds = Number(opts.timeoutSeconds) || 50
|
|
44
44
|
this.pollIntervalMs = Number(opts.pollIntervalMs) || 500
|
|
45
45
|
this.media = opts.media || {}
|
|
46
|
+
this.webAppUrl = String(opts.webAppUrl || '').trim()
|
|
46
47
|
this.onMessage = opts.onMessage
|
|
47
48
|
this.onCallback = opts.onCallback
|
|
48
49
|
this.onUnauthorized = opts.onUnauthorized
|
|
@@ -203,6 +204,20 @@ export class TelegramAdapter {
|
|
|
203
204
|
try {
|
|
204
205
|
await this.registerCommands()
|
|
205
206
|
this.logger?.info?.(`dsh-messenger-gateway: telegram commands registered (${this.commands.length})`)
|
|
207
|
+
if (this.webAppUrl) {
|
|
208
|
+
try {
|
|
209
|
+
await this.call('setChatMenuButton', {
|
|
210
|
+
menu_button: {
|
|
211
|
+
type: 'web_app',
|
|
212
|
+
text: '📊 Dashboard',
|
|
213
|
+
web_app: { url: this.webAppUrl },
|
|
214
|
+
},
|
|
215
|
+
})
|
|
216
|
+
this.logger?.info?.(`dsh-messenger-gateway: telegram menu button configured (${this.webAppUrl})`)
|
|
217
|
+
} catch (err) {
|
|
218
|
+
this.logger?.warn?.(`dsh-messenger-gateway: telegram setChatMenuButton: ${err.message}`)
|
|
219
|
+
}
|
|
220
|
+
}
|
|
206
221
|
} catch (err) {
|
|
207
222
|
this.logger?.warn?.(`dsh-messenger-gateway: telegram setMyCommands: ${err.message}`)
|
|
208
223
|
}
|
|
@@ -715,7 +730,7 @@ export class TelegramAdapter {
|
|
|
715
730
|
const effectiveMarkup = replyMarkup !== undefined
|
|
716
731
|
? replyMarkup
|
|
717
732
|
: (this.quickActions && !threadId
|
|
718
|
-
? buildQuickActionsKeyboard()
|
|
733
|
+
? buildQuickActionsKeyboard(this.webAppUrl)
|
|
719
734
|
: (!threadId ? REMOVE_REPLY_KEYBOARD : undefined))
|
|
720
735
|
for (let i = 0; i < chunks.length; i++) {
|
|
721
736
|
const params = {
|
package/lib/artifacts.js
CHANGED
|
@@ -404,7 +404,8 @@ export function processDiagramsAndTables(rawText, options = {}) {
|
|
|
404
404
|
}
|
|
405
405
|
|
|
406
406
|
export async function processDiagramsAndTablesAsync(rawText, options = {}) {
|
|
407
|
-
const { artifactPreviews = true } = options
|
|
407
|
+
const { artifactPreviews = true, artifactStore = null, chatId = null, webAppUrl = '' } = options
|
|
408
|
+
const createdArtifacts = []
|
|
408
409
|
let text = String(rawText || "")
|
|
409
410
|
const files = []
|
|
410
411
|
|
|
@@ -417,6 +418,20 @@ export async function processDiagramsAndTablesAsync(rawText, options = {}) {
|
|
|
417
418
|
const typeLabel = type === "pie" ? "Pie Chart" : type === "xychart" ? "Chart" : "Diagram"
|
|
418
419
|
const title = `${typeLabel} ${diagramIndex}`
|
|
419
420
|
const svgContent = generateDiagramSvg(diag.code, title)
|
|
421
|
+
let artifactId = null
|
|
422
|
+
if (artifactStore) {
|
|
423
|
+
artifactId = `art-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`
|
|
424
|
+
const stored = artifactStore.add({
|
|
425
|
+
id: artifactId,
|
|
426
|
+
title,
|
|
427
|
+
type,
|
|
428
|
+
code: diag.code,
|
|
429
|
+
svg: svgContent,
|
|
430
|
+
createdAt: Date.now(),
|
|
431
|
+
chatId,
|
|
432
|
+
})
|
|
433
|
+
if (stored) createdArtifacts.push(stored)
|
|
434
|
+
}
|
|
420
435
|
const pngBytes = await convertSvgToPng(svgContent)
|
|
421
436
|
|
|
422
437
|
if (pngBytes && pngBytes.length > 0) {
|
package/lib/commands.js
CHANGED
|
@@ -27,6 +27,8 @@ export const DEFAULT_TELEGRAM_COMMANDS = [
|
|
|
27
27
|
{ command: 'project', description: 'Bind or show topic workspace: /project' },
|
|
28
28
|
{ command: 'memory', description: 'User memory facts: /memory [add <fact>]' },
|
|
29
29
|
{ command: 'forget', description: 'Forget user memory: /forget <id|all>' },
|
|
30
|
+
{ command: 'outbox', description: 'Outbox queue & DLQ: /outbox [retry|clear]' },
|
|
31
|
+
{ command: 'cost', description: 'Session token usage and estimated cost' },
|
|
30
32
|
{ command: 'cwd', description: 'Show working directory: /cwd' },
|
|
31
33
|
{ command: 'files', description: 'Workspace file explorer: /files [dir]' },
|
|
32
34
|
{ command: 'get', description: 'Download file to messenger: /get <path>' },
|
|
@@ -77,12 +79,16 @@ export function mergeDynamicCommands(baseCommands = DEFAULT_TELEGRAM_COMMANDS, d
|
|
|
77
79
|
return out.slice(0, maxTotal)
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
export function buildQuickActionsKeyboard() {
|
|
82
|
+
export function buildQuickActionsKeyboard(webAppUrl = '') {
|
|
83
|
+
const rows = [
|
|
84
|
+
[{ text: '🔄 /new' }, { text: '🛑 /stop' }],
|
|
85
|
+
[{ text: '🎙️ /voice' }, { text: '📊 /status' }],
|
|
86
|
+
]
|
|
87
|
+
if (webAppUrl && String(webAppUrl).startsWith('http')) {
|
|
88
|
+
rows.push([{ text: '📊 Open Dashboard', web_app: { url: String(webAppUrl) } }])
|
|
89
|
+
}
|
|
81
90
|
return {
|
|
82
|
-
keyboard:
|
|
83
|
-
[{ text: '🔄 /new' }, { text: '🛑 /stop' }],
|
|
84
|
-
[{ text: '🎙️ /voice' }, { text: '📊 /status' }],
|
|
85
|
-
],
|
|
91
|
+
keyboard: rows,
|
|
86
92
|
resize_keyboard: true,
|
|
87
93
|
}
|
|
88
94
|
}
|
|
@@ -110,6 +116,8 @@ export const HELP_TEXT = [
|
|
|
110
116
|
'• /project — topic workspace binding (/project bind <path>)',
|
|
111
117
|
'• /memory — show remembered facts about you (/memory add <fact>)',
|
|
112
118
|
'• /forget <id|all> — delete stored user facts',
|
|
119
|
+
'• /outbox — transactional queue & DLQ (/outbox [retry|clear])',
|
|
120
|
+
'• /cost — session token usage, breakdown & estimated cost',
|
|
113
121
|
'• /cwd — show current working directory',
|
|
114
122
|
'• /files [dir] — workspace file explorer',
|
|
115
123
|
'• /get <path> — download file from workspace',
|
package/lib/config.js
CHANGED
|
@@ -58,6 +58,10 @@ export const PluginConfig = z.object({
|
|
|
58
58
|
.description('mirror: TTS when inbound was voice; always/off override (per-user /voice wins)'),
|
|
59
59
|
quickActions: z.boolean().default(false).description('Show quick actions keyboard in Telegram (/new, /stop, /voice, /status)'),
|
|
60
60
|
artifactPreviews: z.boolean().default(true).description('Render diagrams and formatted tables as previews'),
|
|
61
|
+
webAppEnabled: z.boolean().default(true).description('Enable Telegram Mini App dashboard for artifacts and status'),
|
|
62
|
+
webAppUrl: z.string().default('').description('Public HTTPS URL for Telegram Mini App (empty = auto-detect from webhookUrl origin)'),
|
|
63
|
+
costFooter: z.boolean().default(false).description('Append compact execution & cost footer to bot replies (e.g. ⏱️ 2.4s · 1.8k tokens · deepseek-chat)'),
|
|
64
|
+
budgetAlertThresholdUsd: z.number().default(0).description('Session budget warning threshold in USD (0 = disabled)'),
|
|
61
65
|
notifyBridge: z.object({
|
|
62
66
|
enabled: z.boolean().default(false),
|
|
63
67
|
events: z.array(z.string()).default(['task_done', 'error']),
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Token usage and cost tracker in messenger sessions (dsh-cost-meter bridge, #156).
|
|
3
|
+
*
|
|
4
|
+
* Provides:
|
|
5
|
+
* - Accurate token extraction from turn events and DSH session projections.
|
|
6
|
+
* - Model cost estimation using standard rate cards and dsh-cost-meter tariffs.
|
|
7
|
+
* - Compact response footer (e.g. ⏱️ 2.4s · 1.8k tokens (~$0.004) · deepseek-chat).
|
|
8
|
+
* - Detailed `/cost` command with session breakdown and daily budget tracking.
|
|
9
|
+
* - Budget threshold alerts when nearing session limits.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { escapeHtml } from './telegram-format.js'
|
|
13
|
+
|
|
14
|
+
export const DEFAULT_MODEL_RATES = {
|
|
15
|
+
'deepseek-chat': { input: 0.44, output: 1.32, cacheRead: 0.014, cacheWrite: 0.44 },
|
|
16
|
+
'deepseek-v3': { input: 0.44, output: 1.32, cacheRead: 0.014, cacheWrite: 0.44 },
|
|
17
|
+
'deepseek-reasoner': { input: 1.32, output: 3.96, cacheRead: 0.044, cacheWrite: 1.32 },
|
|
18
|
+
'deepseek-r1': { input: 1.32, output: 3.96, cacheRead: 0.044, cacheWrite: 1.32 },
|
|
19
|
+
'gpt-4o': { input: 2.50, output: 10.00, cacheRead: 1.25, cacheWrite: 2.50 },
|
|
20
|
+
'gpt-4o-mini': { input: 0.15, output: 0.60, cacheRead: 0.075, cacheWrite: 0.15 },
|
|
21
|
+
'claude-3-5-sonnet': { input: 3.00, output: 15.00, cacheRead: 0.30, cacheWrite: 3.75 },
|
|
22
|
+
'fallback': { input: 0.50, output: 1.50, cacheRead: 0.05, cacheWrite: 0.50 },
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function resolveModelRates(modelId) {
|
|
26
|
+
if (!modelId || typeof modelId !== 'string') return DEFAULT_MODEL_RATES.fallback
|
|
27
|
+
const clean = modelId.toLowerCase().split('/').pop()
|
|
28
|
+
for (const [key, rates] of Object.entries(DEFAULT_MODEL_RATES)) {
|
|
29
|
+
if (clean.includes(key)) return rates
|
|
30
|
+
}
|
|
31
|
+
return DEFAULT_MODEL_RATES.fallback
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function normalizeUsage(rawUsage) {
|
|
35
|
+
if (!rawUsage || typeof rawUsage !== 'object') {
|
|
36
|
+
return {
|
|
37
|
+
inputTokens: 0,
|
|
38
|
+
outputTokens: 0,
|
|
39
|
+
cacheReadTokens: 0,
|
|
40
|
+
cacheWriteTokens: 0,
|
|
41
|
+
totalTokens: 0,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const inputTokens = Number(rawUsage.inputTokens || rawUsage.prompt_tokens || rawUsage.promptTokens || rawUsage.input || 0)
|
|
46
|
+
const outputTokens = Number(rawUsage.outputTokens || rawUsage.completion_tokens || rawUsage.completionTokens || rawUsage.output || 0)
|
|
47
|
+
const cacheReadTokens = Number(rawUsage.cacheReadTokens || rawUsage.cache_read_tokens || rawUsage.cached_tokens || 0)
|
|
48
|
+
const cacheWriteTokens = Number(rawUsage.cacheWriteTokens || rawUsage.cache_creation_input_tokens || 0)
|
|
49
|
+
const totalTokens = Number(rawUsage.totalTokens || rawUsage.total_tokens || (inputTokens + outputTokens + cacheReadTokens + cacheWriteTokens))
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
inputTokens,
|
|
53
|
+
outputTokens,
|
|
54
|
+
cacheReadTokens,
|
|
55
|
+
cacheWriteTokens,
|
|
56
|
+
totalTokens,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function calculateEstimatedCostUsd(usage, modelId) {
|
|
61
|
+
const norm = normalizeUsage(usage)
|
|
62
|
+
const rates = resolveModelRates(modelId)
|
|
63
|
+
|
|
64
|
+
const inputCost = (norm.inputTokens * rates.input) / 1_000_000
|
|
65
|
+
const outputCost = (norm.outputTokens * rates.output) / 1_000_000
|
|
66
|
+
const cacheReadCost = (norm.cacheReadTokens * rates.cacheRead) / 1_000_000
|
|
67
|
+
const cacheWriteCost = (norm.cacheWriteTokens * rates.cacheWrite) / 1_000_000
|
|
68
|
+
|
|
69
|
+
return inputCost + outputCost + cacheReadCost + cacheWriteCost
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function formatTokenCount(n) {
|
|
73
|
+
const count = Number(n) || 0
|
|
74
|
+
if (count < 1000) return String(count)
|
|
75
|
+
const k = count / 1000
|
|
76
|
+
return (k >= 10 ? k.toFixed(0) : k.toFixed(1)) + 'k'
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function formatDurationSeconds(ms) {
|
|
80
|
+
const sec = Math.max(0.1, (Number(ms) || 0) / 1000)
|
|
81
|
+
return sec >= 10 ? `${sec.toFixed(0)}s` : `${sec.toFixed(1)}s`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function formatCostUsd(usd) {
|
|
85
|
+
const val = Number(usd) || 0
|
|
86
|
+
if (val <= 0) return '$0.00'
|
|
87
|
+
if (val < 0.001) return '<$0.001'
|
|
88
|
+
return `~$${val.toFixed(4)}`
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function formatCostFooter({ durationMs, usage, model }) {
|
|
92
|
+
const norm = normalizeUsage(usage)
|
|
93
|
+
if (!norm.totalTokens && !durationMs) return ''
|
|
94
|
+
|
|
95
|
+
const durStr = formatDurationSeconds(durationMs)
|
|
96
|
+
const tokenStr = formatTokenCount(norm.totalTokens)
|
|
97
|
+
const cost = calculateEstimatedCostUsd(norm, model)
|
|
98
|
+
const costStr = formatCostUsd(cost)
|
|
99
|
+
const modelName = (model || 'model').split('/').pop()
|
|
100
|
+
|
|
101
|
+
return `\n\n<i>⏱️ ${durStr} · ${tokenStr} tokens (${costStr}) · ${escapeHtml(modelName)}</i>`
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function extractSessionStats(chat) {
|
|
105
|
+
let promptTokens = 0
|
|
106
|
+
let completionTokens = 0
|
|
107
|
+
let cachedTokens = 0
|
|
108
|
+
let totalTokens = 0
|
|
109
|
+
let turnsCount = 0
|
|
110
|
+
|
|
111
|
+
// 1. Try DSH session projections if available
|
|
112
|
+
const session = chat?.agent?.session
|
|
113
|
+
let projection = null
|
|
114
|
+
try {
|
|
115
|
+
if (typeof session?.projection === 'function') {
|
|
116
|
+
projection = session.projection('costByModel')
|
|
117
|
+
}
|
|
118
|
+
} catch {
|
|
119
|
+
/* projection not registered or session closed */
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (projection?.models && Array.isArray(projection.models)) {
|
|
123
|
+
for (const m of projection.models) {
|
|
124
|
+
if (m.slots && typeof m.slots === 'object') {
|
|
125
|
+
for (const slot of Object.values(m.slots)) {
|
|
126
|
+
promptTokens += Number(slot.uncachedInputTokens || 0)
|
|
127
|
+
completionTokens += Number(slot.outputTokens || 0)
|
|
128
|
+
cachedTokens += Number(slot.cacheReadTokens || 0)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (Array.isArray(projection.turns)) {
|
|
133
|
+
turnsCount = projection.turns.length
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 2. Accumulate chat turn metrics if projection empty
|
|
138
|
+
if (!promptTokens && !completionTokens && chat?.turnStats) {
|
|
139
|
+
promptTokens = chat.turnStats.inputTokens || 0
|
|
140
|
+
completionTokens = chat.turnStats.outputTokens || 0
|
|
141
|
+
cachedTokens = chat.turnStats.cacheReadTokens || 0
|
|
142
|
+
turnsCount = chat.turnStats.turnsCount || 0
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// 3. Fallback to last turn usage if available
|
|
146
|
+
if (!promptTokens && !completionTokens && chat?.lastTurnUsage) {
|
|
147
|
+
const lastNorm = normalizeUsage(chat.lastTurnUsage)
|
|
148
|
+
promptTokens = lastNorm.inputTokens
|
|
149
|
+
completionTokens = lastNorm.outputTokens
|
|
150
|
+
cachedTokens = lastNorm.cacheReadTokens
|
|
151
|
+
turnsCount = 1
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
totalTokens = promptTokens + completionTokens + cachedTokens
|
|
155
|
+
|
|
156
|
+
return {
|
|
157
|
+
promptTokens,
|
|
158
|
+
completionTokens,
|
|
159
|
+
cachedTokens,
|
|
160
|
+
totalTokens,
|
|
161
|
+
turnsCount,
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function formatSessionCostReport({ chat, config, model }) {
|
|
166
|
+
const stats = extractSessionStats(chat)
|
|
167
|
+
const activeModel = model || config?.agent?.model || 'deepseek-chat'
|
|
168
|
+
const costUsd = calculateEstimatedCostUsd({
|
|
169
|
+
inputTokens: stats.promptTokens,
|
|
170
|
+
outputTokens: stats.completionTokens,
|
|
171
|
+
cacheReadTokens: stats.cachedTokens,
|
|
172
|
+
}, activeModel)
|
|
173
|
+
|
|
174
|
+
const budgetThreshold = Number(config?.telegram?.budgetAlertThresholdUsd || 0)
|
|
175
|
+
|
|
176
|
+
const lines = [
|
|
177
|
+
'📊 <b>Session Token & Cost Meter:</b>',
|
|
178
|
+
`• Active Model: <code>${escapeHtml(activeModel)}</code>`,
|
|
179
|
+
`• Total Tokens: <b>${formatTokenCount(stats.totalTokens)}</b> (${stats.totalTokens.toLocaleString()})`,
|
|
180
|
+
` ├─ Prompt (input): ${formatTokenCount(stats.promptTokens)}`,
|
|
181
|
+
` ├─ Completion (output): ${formatTokenCount(stats.completionTokens)}`,
|
|
182
|
+
` └─ Cached read: ${formatTokenCount(stats.cachedTokens)}`,
|
|
183
|
+
`• Completed Turns: <b>${stats.turnsCount}</b>`,
|
|
184
|
+
`• Estimated Cost: <b>${formatCostUsd(costUsd)}</b> USD`,
|
|
185
|
+
]
|
|
186
|
+
|
|
187
|
+
if (budgetThreshold > 0) {
|
|
188
|
+
const remaining = Math.max(0, budgetThreshold - costUsd)
|
|
189
|
+
const pct = Math.min(100, (costUsd / budgetThreshold) * 100).toFixed(1)
|
|
190
|
+
lines.push(
|
|
191
|
+
'',
|
|
192
|
+
'🎯 <b>Budget Tracking:</b>',
|
|
193
|
+
`• Budget Threshold: <b>$${budgetThreshold.toFixed(2)}</b>`,
|
|
194
|
+
`• Consumed: <b>${pct}%</b>`,
|
|
195
|
+
`• Remaining: <b>$${remaining.toFixed(4)}</b>`,
|
|
196
|
+
)
|
|
197
|
+
if (costUsd >= budgetThreshold) {
|
|
198
|
+
lines.push('⚠️ <b>Budget threshold reached!</b> Consider using `/new` to reset context.')
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
lines.push('', '<i>Run <code>/cost</code> anytime to check current session spend.</i>')
|
|
203
|
+
return lines.join('\n')
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export function checkBudgetThreshold({ currentSpendUsd, thresholdUsd }) {
|
|
207
|
+
const threshold = Number(thresholdUsd) || 0
|
|
208
|
+
if (threshold <= 0) return null
|
|
209
|
+
const spend = Number(currentSpendUsd) || 0
|
|
210
|
+
|
|
211
|
+
if (spend >= threshold) {
|
|
212
|
+
return {
|
|
213
|
+
alert: true,
|
|
214
|
+
level: 'limit',
|
|
215
|
+
message: `Session cost ($${spend.toFixed(4)}) has exceeded the configured budget ($${threshold.toFixed(2)}).`,
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (spend >= threshold * 0.8) {
|
|
220
|
+
return {
|
|
221
|
+
alert: true,
|
|
222
|
+
level: 'warning',
|
|
223
|
+
message: `Session cost ($${spend.toFixed(4)}) has reached 80% of budget limit ($${threshold.toFixed(2)}).`,
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return null
|
|
228
|
+
}
|
package/lib/gateway-commands.js
CHANGED
|
@@ -16,6 +16,7 @@ import { HELP_TEXT } from './commands.js'
|
|
|
16
16
|
import { resolveEffectiveCwd, getCwdWarning, inspectGitWorkspace, registerSessionInWorkspace } from './workspace.js'
|
|
17
17
|
import { escapeHtml } from './telegram-format.js'
|
|
18
18
|
import { rememberUserFact, listUserMemories, forgetUserMemory } from './memory-brain.js'
|
|
19
|
+
import { formatSessionCostReport } from './cost-tracker.js'
|
|
19
20
|
|
|
20
21
|
|
|
21
22
|
export function releaseChatTurn(chat) {
|
|
@@ -400,6 +401,67 @@ export async function handleGatewayCommand(gw, key, text, input) {
|
|
|
400
401
|
return reply(`🗑️ Forgotten memory <code>${escapeHtml(targetId)}</code>.`)
|
|
401
402
|
}
|
|
402
403
|
|
|
404
|
+
if (cmd === '/outbox') {
|
|
405
|
+
const sub = parts[1]?.toLowerCase()
|
|
406
|
+
if (!gw.outbox) {
|
|
407
|
+
return reply('ℹ️ Outbox queue is not initialized.')
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
if (sub === 'retry') {
|
|
411
|
+
const count = await gw.outbox.retryDlq()
|
|
412
|
+
return reply(`🔄 Moved <b>${count}</b> DLQ items back to outbox queue for retry.`)
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
if (sub === 'clear') {
|
|
416
|
+
const count = await gw.outbox.clearDlq()
|
|
417
|
+
return reply(`🗑️ Cleared <b>${count}</b> items from Dead Letter Queue.`)
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const st = gw.outbox.status()
|
|
421
|
+
const lines = [
|
|
422
|
+
'📬 <b>Outbox Transactional Queue:</b>',
|
|
423
|
+
`• Pending messages: <b>${st.pendingCount}</b>`,
|
|
424
|
+
`• Dead Letter Queue (DLQ): <b>${st.dlqCount}</b>`,
|
|
425
|
+
]
|
|
426
|
+
if (st.dlqCount > 0) {
|
|
427
|
+
lines.push('', '<b>Recent DLQ Failures:</b>')
|
|
428
|
+
const sample = st.dlq.slice(0, 5)
|
|
429
|
+
for (const item of sample) {
|
|
430
|
+
lines.push(`- [<code>${escapeHtml(item.id.slice(0, 8))}</code>] to ${escapeHtml(String(item.target?.chatId))}: ${escapeHtml(item.error || 'unknown error')}`)
|
|
431
|
+
}
|
|
432
|
+
lines.push('', 'Commands:', '• Retry all: <code>/outbox retry</code>', '• Clear DLQ: <code>/outbox clear</code>')
|
|
433
|
+
} else {
|
|
434
|
+
lines.push('', '<i>All outbound messages successfully delivered.</i>')
|
|
435
|
+
}
|
|
436
|
+
return reply(lines.join('\n'))
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (cmd === '/app' || cmd === '/dashboard') {
|
|
440
|
+
const webAppUrl = gw.resolveWebAppUrl()
|
|
441
|
+
if (!webAppUrl) {
|
|
442
|
+
await reply('⚠️ Telegram Mini App URL is not configured. Set <code>telegram.webAppUrl</code> or <code>telegram.webhookUrl</code> in settings.')
|
|
443
|
+
return { handled: true }
|
|
444
|
+
}
|
|
445
|
+
await reply('📊 <b>DSH Gateway Dashboard</b>\nOpen the Mini App to view artifacts, diagrams, host health, and session controls.', {
|
|
446
|
+
replyMarkup: {
|
|
447
|
+
inline_keyboard: [
|
|
448
|
+
[{ text: '📊 Open Dashboard', web_app: { url: webAppUrl } }],
|
|
449
|
+
],
|
|
450
|
+
},
|
|
451
|
+
})
|
|
452
|
+
return { handled: true }
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (cmd === '/cost') {
|
|
456
|
+
const chat = gw.chats.get(key)
|
|
457
|
+
const report = formatSessionCostReport({
|
|
458
|
+
chat,
|
|
459
|
+
config: gw.config,
|
|
460
|
+
model: gw.config.agent?.model || 'deepseek-chat',
|
|
461
|
+
})
|
|
462
|
+
return reply(report)
|
|
463
|
+
}
|
|
464
|
+
|
|
403
465
|
if (cmd === '/cwd') {
|
|
404
466
|
const chat = gw.chats.get(key)
|
|
405
467
|
const sessionCwd = chat?.agent?.meta?.cwd || chat?.agent?.session?.meta?.cwd || (gw.resolveEffectiveCwdFor ? gw.resolveEffectiveCwdFor(chatId, threadId) : resolveEffectiveCwd(gw.config.agent?.cwd, gw.ctx?.logger))
|
package/lib/gateway-turn.js
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
buildStreamPreview, formatProgressLine, createEditScheduler, startTypingHeartbeat, parseReasoningStream
|
|
16
16
|
} from './stream.js'
|
|
17
17
|
import { escapeHtml } from './telegram-format.js'
|
|
18
|
+
import { formatCostFooter, normalizeUsage, extractSessionStats, calculateEstimatedCostUsd, checkBudgetThreshold } from './cost-tracker.js'
|
|
18
19
|
import { recallUserMemories, recordTurnMemory } from './memory-brain.js'
|
|
19
20
|
|
|
20
21
|
const PLUGIN = 'dsh-messenger-gateway'
|
|
@@ -215,6 +216,7 @@ export async function buildUserContent(gw, input, signal) {
|
|
|
215
216
|
|
|
216
217
|
export async function runGatewayTurn(gw, chat, input, signal) {
|
|
217
218
|
const { reply, typing, startStream, startProgress, react, inboundWasVoice, userId } = input
|
|
219
|
+
const turnStartTime = Date.now()
|
|
218
220
|
chat.turnActive = true
|
|
219
221
|
const sessionId = chat.agent.session.id
|
|
220
222
|
const tg = gw.tg()
|
|
@@ -306,6 +308,9 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
306
308
|
}
|
|
307
309
|
const processed = await processDiagramsAndTablesAsync(rawAnswer, {
|
|
308
310
|
artifactPreviews: gw.tg().artifactPreviews !== false,
|
|
311
|
+
artifactStore: gw.artifactStore,
|
|
312
|
+
chatId: input.chatId,
|
|
313
|
+
webAppUrl: gw.resolveWebAppUrl(),
|
|
309
314
|
})
|
|
310
315
|
const answer = processed.text
|
|
311
316
|
if (collector.reason?.kind === 'error') {
|
|
@@ -330,6 +335,23 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
330
335
|
else await reply(noResp)
|
|
331
336
|
return
|
|
332
337
|
}
|
|
338
|
+
if (collector.usage) {
|
|
339
|
+
chat.lastTurnUsage = collector.usage
|
|
340
|
+
if (!chat.turnStats) chat.turnStats = { inputTokens: 0, outputTokens: 0, cacheReadTokens: 0, turnsCount: 0 }
|
|
341
|
+
chat.turnStats.turnsCount++
|
|
342
|
+
const norm = normalizeUsage(collector.usage)
|
|
343
|
+
chat.turnStats.inputTokens += norm.inputTokens
|
|
344
|
+
chat.turnStats.outputTokens += norm.outputTokens
|
|
345
|
+
chat.turnStats.cacheReadTokens += norm.cacheReadTokens
|
|
346
|
+
}
|
|
347
|
+
if (tg.costFooter && answer) {
|
|
348
|
+
const footer = formatCostFooter({
|
|
349
|
+
durationMs: Date.now() - turnStartTime,
|
|
350
|
+
usage: collector.usage,
|
|
351
|
+
model: gw.config?.agent?.model,
|
|
352
|
+
})
|
|
353
|
+
if (footer) answer += footer
|
|
354
|
+
}
|
|
333
355
|
const maxLen = Number(gw.config.agent?.maxMessageLength) || 4000
|
|
334
356
|
const chunks = answer ? splitText(answer, maxLen) : ['']
|
|
335
357
|
if (stream) {
|
|
@@ -363,6 +385,23 @@ export async function runGatewayTurn(gw, chat, input, signal) {
|
|
|
363
385
|
}
|
|
364
386
|
}
|
|
365
387
|
}
|
|
388
|
+
const budgetThreshold = Number(tg.budgetAlertThresholdUsd || 0)
|
|
389
|
+
if (budgetThreshold > 0) {
|
|
390
|
+
const stats = extractSessionStats(chat)
|
|
391
|
+
const cost = calculateEstimatedCostUsd({
|
|
392
|
+
inputTokens: stats.promptTokens,
|
|
393
|
+
outputTokens: stats.completionTokens,
|
|
394
|
+
cacheReadTokens: stats.cachedTokens,
|
|
395
|
+
}, gw.config?.agent?.model)
|
|
396
|
+
const warning = checkBudgetThreshold({ currentSpendUsd: cost, thresholdUsd: budgetThreshold })
|
|
397
|
+
if (warning && !chat.budgetWarned) {
|
|
398
|
+
chat.budgetWarned = true
|
|
399
|
+
gw.sendAlert('status', {
|
|
400
|
+
title: 'Budget Alert',
|
|
401
|
+
details: `${warning.message} (Chat: ${input.chatId})`,
|
|
402
|
+
}).catch(() => {})
|
|
403
|
+
}
|
|
404
|
+
}
|
|
366
405
|
if (gw.config?.memory?.enabled !== false && userId && answer && input.text) {
|
|
367
406
|
recordTurnMemory({
|
|
368
407
|
config: gw.config.memory,
|
package/lib/gateway.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createArtifactStore } from './webapp.js'
|
|
1
2
|
import { randomUUID } from 'node:crypto'
|
|
2
3
|
import { join } from 'node:path'
|
|
3
4
|
import { homedir } from 'node:os'
|
|
@@ -30,6 +31,7 @@ import {
|
|
|
30
31
|
} from './forum-mirror.js'
|
|
31
32
|
import { resolveInternalBaseUrl } from './config.js'
|
|
32
33
|
import { resolveEffectiveCwd, registerSessionInWorkspace, createTopicWorkspaceStore, getCwdWarning } from './workspace.js'
|
|
34
|
+
import { createOutboxQueue, resolveOutboxStoragePath } from './outbox.js'
|
|
33
35
|
import { realpathSync } from 'node:fs'
|
|
34
36
|
import { resolve } from 'node:path'
|
|
35
37
|
|
|
@@ -63,6 +65,21 @@ export class Gateway {
|
|
|
63
65
|
this.personas = createPersonaStore(join(home, 'messenger-gateway', 'personas.json'), { logger: this.logger })
|
|
64
66
|
this.topicWorkspaces = createTopicWorkspaceStore(join(home, 'messenger-gateway', 'topic-workspaces.json'), { logger: this.logger })
|
|
65
67
|
this.chatLocales = createVoicePrefs(join(home, 'messenger-gateway', 'chat-locales.json'), { logger: this.logger })
|
|
68
|
+
this.outbox = createOutboxQueue({
|
|
69
|
+
storagePath: resolveOutboxStoragePath(join(home, 'messenger-gateway')),
|
|
70
|
+
senderFn: async (job) => {
|
|
71
|
+
await this.messengerSend(job.target, job.payload)
|
|
72
|
+
},
|
|
73
|
+
onDlq: (job, err) => {
|
|
74
|
+
this.sendAlert('error', {
|
|
75
|
+
code: 'OUTBOX_DLQ',
|
|
76
|
+
message: `Outbox message failed permanently to ${job.target?.chatId}: ${err.message}`,
|
|
77
|
+
chatId: job.target?.chatId,
|
|
78
|
+
threadId: job.target?.threadId,
|
|
79
|
+
}).catch((e) => this.recordApiFailure('outbox.dlq_alert', e))
|
|
80
|
+
},
|
|
81
|
+
logger: this.logger,
|
|
82
|
+
})
|
|
66
83
|
this.scheduler = createScheduler(join(home, 'messenger-gateway', 'scheduled.json'), async (task) => {
|
|
67
84
|
const target = {
|
|
68
85
|
platform: task.platform || 'telegram',
|
|
@@ -88,6 +105,7 @@ export class Gateway {
|
|
|
88
105
|
}, { logger: this.ctx?.logger || console })
|
|
89
106
|
this.stats = { sent: 0, errors: 0, startedAt: Date.now() }
|
|
90
107
|
this.apiHealth = new ApiHealthTracker({ logger: this.logger })
|
|
108
|
+
this.artifactStore = createArtifactStore(100)
|
|
91
109
|
this.skillNameMap = new Map()
|
|
92
110
|
this.rawSkillNames = new Set()
|
|
93
111
|
}
|
|
@@ -210,7 +228,13 @@ export class Gateway {
|
|
|
210
228
|
if (text) collector.lastText = text
|
|
211
229
|
const extra = collectAssistantParts(msg)
|
|
212
230
|
for (const img of extra.images) collector.images.push(img)
|
|
231
|
+
const usage = event.data?.usage || msg?.usage
|
|
232
|
+
if (usage) collector.usage = usage
|
|
213
233
|
} else if (event.type === 'assistant/chunk') {
|
|
234
|
+
const chunk = event.data?.chunk || event.data
|
|
235
|
+
if (chunk?.type === 'usage' && chunk?.usage) {
|
|
236
|
+
collector.usage = chunk.usage
|
|
237
|
+
}
|
|
214
238
|
const delta = extractTextDelta(event.data?.chunk || event.data)
|
|
215
239
|
const reasoningDelta = extractReasoningDelta(event.data?.chunk || event.data)
|
|
216
240
|
if (delta || reasoningDelta) {
|
|
@@ -293,11 +317,13 @@ export class Gateway {
|
|
|
293
317
|
const idleMs = Number.isFinite(rawIdle) && rawIdle > 0 ? rawIdle : 86_400_000
|
|
294
318
|
this.idleTimer = setInterval(() => this.reapIdle(), Math.min(idleMs, 60_000))
|
|
295
319
|
this.idleTimer.unref?.()
|
|
320
|
+
this.outbox?.start()
|
|
296
321
|
this.scheduler.start()
|
|
297
322
|
}
|
|
298
323
|
|
|
299
324
|
stop() {
|
|
300
325
|
this.stopped = true
|
|
326
|
+
if (this.outbox) this.outbox.stop()
|
|
301
327
|
if (this.scheduler) this.scheduler.stop()
|
|
302
328
|
if (this.disposeListener) this.disposeListener()
|
|
303
329
|
if (this.idleTimer) clearInterval(this.idleTimer)
|
|
@@ -321,6 +347,17 @@ export class Gateway {
|
|
|
321
347
|
|
|
322
348
|
getAdapter(platform) { return this.adapters.get(platform) }
|
|
323
349
|
|
|
350
|
+
async sendToMessenger(target, payload, opts = {}) {
|
|
351
|
+
if (opts.reliable !== false && this.outbox) {
|
|
352
|
+
return this.outbox.enqueue({
|
|
353
|
+
target,
|
|
354
|
+
payload: typeof payload === 'string' ? { text: payload } : payload,
|
|
355
|
+
type: opts.type || 'message',
|
|
356
|
+
})
|
|
357
|
+
}
|
|
358
|
+
return this.messengerSend(target, payload)
|
|
359
|
+
}
|
|
360
|
+
|
|
324
361
|
async messengerSend(target, payload) {
|
|
325
362
|
let resolved = target
|
|
326
363
|
if (!resolved?.chatId && resolved?.platform) {
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createWebAppHandler } from './webapp.js'
|
|
1
2
|
import { homedir } from 'node:os'
|
|
2
3
|
import { join, dirname, resolve } from 'node:path'
|
|
3
4
|
import { fileURLToPath } from 'node:url'
|
|
@@ -678,6 +679,20 @@ export function apply(ctx, config) {
|
|
|
678
679
|
registerMessengerRoute(ctx, getGw, '/dsh-messenger-gateway/messenger/ask', 'ask', source)
|
|
679
680
|
|
|
680
681
|
const manifestPath = resolve(dirname(fileURLToPath(import.meta.url)), '../package.json')
|
|
682
|
+
ctx.effect(() => {
|
|
683
|
+
const ws = getWebServer()
|
|
684
|
+
if (!ws?.register) return
|
|
685
|
+
return ws.register({
|
|
686
|
+
kind: 'prefix',
|
|
687
|
+
path: '/dsh-messenger-gateway/webapp',
|
|
688
|
+
handler: createWebAppHandler({
|
|
689
|
+
getGateway: getGw,
|
|
690
|
+
getConfig: source,
|
|
691
|
+
ctx,
|
|
692
|
+
}),
|
|
693
|
+
})
|
|
694
|
+
}, 'dsh-messenger-gateway: webapp dashboard')
|
|
695
|
+
|
|
681
696
|
ctx.effect(() => registerPluginUpdater(ctx, {
|
|
682
697
|
manifestPath,
|
|
683
698
|
packageName: '@goodandready/dsh-messenger-gateway',
|