@goodandready/dsh-messenger-gateway 0.4.21 → 0.4.23
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/forum-mirror.js +47 -0
- package/lib/gateway.js +58 -2
- package/lib/locales/en.js +1 -0
- package/lib/locales/zh.js +1 -0
- package/package.json +1 -1
package/lib/forum-mirror.js
CHANGED
|
@@ -24,11 +24,58 @@ export async function collectDynamicSkills(ctx) {
|
|
|
24
24
|
}
|
|
25
25
|
return skills
|
|
26
26
|
}
|
|
27
|
+
/**
|
|
28
|
+
* Restore original hyphenated skill name from Telegram sanitized underscore command.
|
|
29
|
+
* E.g. /code_review -> /code-review
|
|
30
|
+
* Does not rewrite if the skill literally exists with underscores.
|
|
31
|
+
* Also strips optional @BotName mention.
|
|
32
|
+
*/
|
|
33
|
+
export function restoreSkillGesture(gw, body) {
|
|
34
|
+
if (!body || !body.startsWith('/')) return body
|
|
35
|
+
const parts = body.split(/\s+/)
|
|
36
|
+
const firstToken = parts[0]
|
|
37
|
+
const atIdx = firstToken.indexOf('@')
|
|
38
|
+
const rawCmd = (atIdx !== -1 ? firstToken.slice(1, atIdx) : firstToken.slice(1)).toLowerCase()
|
|
39
|
+
|
|
40
|
+
// If a skill literally named `rawCmd` exists, do NOT rewrite
|
|
41
|
+
if (gw?.rawSkillNames?.has(rawCmd)) {
|
|
42
|
+
return atIdx !== -1 ? `/${rawCmd}${body.slice(firstToken.length)}` : body
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// If mapped from sanitized form (e.g. code_review -> code-review)
|
|
46
|
+
const original = gw?.skillNameMap?.get(rawCmd)
|
|
47
|
+
if (original) {
|
|
48
|
+
return `/${original}${body.slice(firstToken.length)}`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Just strip @bot if present
|
|
52
|
+
if (atIdx !== -1) {
|
|
53
|
+
return `/${rawCmd}${body.slice(firstToken.length)}`
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return body
|
|
57
|
+
}
|
|
58
|
+
|
|
27
59
|
export async function syncTelegramCommands(gw) {
|
|
28
60
|
const tgAdapter = gw.adapters.get('telegram')
|
|
29
61
|
if (!tgAdapter || typeof tgAdapter.registerCommands !== 'function') return
|
|
30
62
|
const baseCommands = gw.config.telegram?.commands || []
|
|
31
63
|
const dynamicSkills = await collectDynamicSkills(gw.ctx)
|
|
64
|
+
|
|
65
|
+
if (!gw.skillNameMap) gw.skillNameMap = new Map()
|
|
66
|
+
if (!gw.rawSkillNames) gw.rawSkillNames = new Set()
|
|
67
|
+
gw.skillNameMap.clear()
|
|
68
|
+
gw.rawSkillNames.clear()
|
|
69
|
+
|
|
70
|
+
for (const s of dynamicSkills) {
|
|
71
|
+
const rawName = String(s.name || '').trim()
|
|
72
|
+
if (!rawName) continue
|
|
73
|
+
gw.rawSkillNames.add(rawName.toLowerCase())
|
|
74
|
+
const sanitized = rawName.replace(/^\//, '').toLowerCase().replace(/[^a-z0-9_]/g, '_').slice(0, 32)
|
|
75
|
+
if (sanitized && sanitized !== rawName) {
|
|
76
|
+
gw.skillNameMap.set(sanitized, rawName)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
32
79
|
const merged = mergeDynamicCommands(baseCommands, dynamicSkills, 100)
|
|
33
80
|
try {
|
|
34
81
|
await tgAdapter.registerCommands(merged)
|
package/lib/gateway.js
CHANGED
|
@@ -26,6 +26,7 @@ import { runGatewayTurn, buildUserContent, answerApproval } from './gateway-turn
|
|
|
26
26
|
import {
|
|
27
27
|
collectDynamicSkills, syncTelegramCommands,
|
|
28
28
|
mirrorSessionToForumTopic, relayTurnToForumMirror,
|
|
29
|
+
restoreSkillGesture,
|
|
29
30
|
} from './forum-mirror.js'
|
|
30
31
|
import { resolveInternalBaseUrl } from './config.js'
|
|
31
32
|
import { resolveEffectiveCwd, registerSessionInWorkspace } from './workspace.js'
|
|
@@ -84,6 +85,8 @@ export class Gateway {
|
|
|
84
85
|
}, { logger: this.ctx?.logger || console })
|
|
85
86
|
this.stats = { sent: 0, errors: 0, startedAt: Date.now() }
|
|
86
87
|
this.apiHealth = new ApiHealthTracker({ logger: this.logger })
|
|
88
|
+
this.skillNameMap = new Map()
|
|
89
|
+
this.rawSkillNames = new Set()
|
|
87
90
|
}
|
|
88
91
|
|
|
89
92
|
get consecutiveApiFailures() {
|
|
@@ -421,7 +424,46 @@ export class Gateway {
|
|
|
421
424
|
break
|
|
422
425
|
}
|
|
423
426
|
}
|
|
424
|
-
const cleanBody =
|
|
427
|
+
const cleanBody = restoreSkillGesture(this, body)
|
|
428
|
+
|
|
429
|
+
// Try DSH runtime command execution (e.g. /compact, /goal, plugin commands)
|
|
430
|
+
if (body.startsWith('/')) {
|
|
431
|
+
const commandsService = this.ctx.get ? this.ctx.get('commands') : this.ctx?.commands
|
|
432
|
+
if (commandsService && typeof commandsService.execute === 'function') {
|
|
433
|
+
const cmdAbort = chat.abort || new AbortController()
|
|
434
|
+
const createdAbort = !chat.abort
|
|
435
|
+
if (createdAbort) {
|
|
436
|
+
chat.abort = cmdAbort
|
|
437
|
+
}
|
|
438
|
+
try {
|
|
439
|
+
const outcome = await commandsService.execute(chat.agent, cleanBody, attachments || [], cmdAbort.signal)
|
|
440
|
+
if (outcome !== undefined) {
|
|
441
|
+
const replyText = outcome?.result?.kind === 'text'
|
|
442
|
+
? outcome.result.text
|
|
443
|
+
: (typeof outcome?.result?.text === 'string'
|
|
444
|
+
? outcome.result.text
|
|
445
|
+
: (outcome?.result?.kind === 'error' ? (outcome.result.text || 'Command execution error') : undefined))
|
|
446
|
+
const cmdName = cleanBody.split(/\s+/)[0]
|
|
447
|
+
const msg = replyText ?? t('msg.command_done', { cmd: cmdName }, this.resolveLocale(input))
|
|
448
|
+
if (msg) {
|
|
449
|
+
await reply(msg)
|
|
450
|
+
}
|
|
451
|
+
return
|
|
452
|
+
}
|
|
453
|
+
} catch (cmdErr) {
|
|
454
|
+
if (cmdAbort.signal.aborted) return
|
|
455
|
+
const errMsg = cmdErr instanceof Error ? cmdErr.message : String(cmdErr)
|
|
456
|
+
this.ctx.logger?.warn?.(`dsh-messenger-gateway: runtime command: ${errMsg}`)
|
|
457
|
+
try { await reply(t('msg.exception', { message: errMsg }, this.resolveLocale(input))) } catch (repErr) { this.recordApiFailure('reply.exception', repErr) }
|
|
458
|
+
return
|
|
459
|
+
} finally {
|
|
460
|
+
if (createdAbort && chat.abort === cmdAbort) {
|
|
461
|
+
chat.abort = undefined
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
425
467
|
const turnInput = { ...input, text: cleanBody, attachments, inboundWasVoice, personaOverride }
|
|
426
468
|
|
|
427
469
|
// Hermes-like steer: while a turn is running, inject followup instead of abort+restart
|
|
@@ -473,7 +515,21 @@ export class Gateway {
|
|
|
473
515
|
}
|
|
474
516
|
|
|
475
517
|
async collectDynamicSkills() {
|
|
476
|
-
|
|
518
|
+
const skills = await collectDynamicSkills(this.ctx)
|
|
519
|
+
if (!this.skillNameMap) this.skillNameMap = new Map()
|
|
520
|
+
if (!this.rawSkillNames) this.rawSkillNames = new Set()
|
|
521
|
+
this.skillNameMap.clear()
|
|
522
|
+
this.rawSkillNames.clear()
|
|
523
|
+
for (const s of skills) {
|
|
524
|
+
const rawName = String(s.name || '').trim()
|
|
525
|
+
if (!rawName) continue
|
|
526
|
+
this.rawSkillNames.add(rawName.toLowerCase())
|
|
527
|
+
const sanitized = rawName.replace(/^\//, '').toLowerCase().replace(/[^a-z0-9_]/g, '_').slice(0, 32)
|
|
528
|
+
if (sanitized && sanitized !== rawName) {
|
|
529
|
+
this.skillNameMap.set(sanitized, rawName)
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
return skills
|
|
477
533
|
}
|
|
478
534
|
|
|
479
535
|
async syncTelegramCommands() {
|
package/lib/locales/en.js
CHANGED
|
@@ -36,6 +36,7 @@ export const en = {
|
|
|
36
36
|
'msg.start': 'Gateway connected. Send messages to the agent. /help for commands.',
|
|
37
37
|
'msg.steer_added': '↪️ Added to current turn',
|
|
38
38
|
'msg.turn_stopped': 'Turn interrupted.',
|
|
39
|
+
'msg.command_done': 'Command {cmd} completed.',
|
|
39
40
|
'msg.no_active_session': 'No active session found.',
|
|
40
41
|
'msg.unknown_command': 'Unknown command {cmd}. /help',
|
|
41
42
|
'msg.not_allowed': 'Access denied. Ask the owner to whitelist your ID.',
|
package/lib/locales/zh.js
CHANGED
|
@@ -36,6 +36,7 @@ export const zh = {
|
|
|
36
36
|
'msg.start': '网关已连接。请向智能体发送消息。发送 /help 查看命令。',
|
|
37
37
|
'msg.steer_added': '↪️ 已追加到当前回答',
|
|
38
38
|
'msg.turn_stopped': '回答已中断。',
|
|
39
|
+
'msg.command_done': '命令 {cmd} 执行完成。',
|
|
39
40
|
'msg.no_active_session': '未找到活跃会话。',
|
|
40
41
|
'msg.unknown_command': '未知命令 {cmd}。发送 /help',
|
|
41
42
|
'msg.not_allowed': '访问受限。请联系管理员将您的ID加入白名单。',
|
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.23",
|
|
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",
|