@goodandready/dsh-messenger-gateway 0.4.23 → 0.4.25
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-inbound.js +1 -1
- package/lib/adapters/telegram.js +154 -2
- package/lib/artifacts.js +353 -23
- package/lib/client.js +38 -0
- package/lib/config.js +5 -0
- package/lib/documents.js +167 -3
- package/lib/gateway-turn.js +133 -45
- package/lib/gateway.js +8 -6
- package/lib/index.js +5 -0
- package/lib/locales/en.js +6 -4
- package/lib/locales/zh.js +5 -3
- package/lib/stream.js +128 -4
- package/lib/telegram-format.js +9 -4
- package/package.json +1 -1
package/lib/stream.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { escapeHtml } from './telegram-format.js'
|
|
2
|
+
|
|
1
3
|
/** Helpers for Telegram streaming edits + progress. */
|
|
2
4
|
|
|
3
5
|
export function extractTextDelta(chunk) {
|
|
@@ -23,14 +25,136 @@ export function formatProgressLine(toolName, locale = 'en') {
|
|
|
23
25
|
return `🔧 ${toolName}…`
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
export function
|
|
28
|
+
export function extractReasoningDelta(chunk) {
|
|
29
|
+
if (!chunk || typeof chunk !== 'object') return ''
|
|
30
|
+
if ((chunk.type === 'reasoning-delta' || chunk.type === 'thought-delta') && typeof chunk.text === 'string') return chunk.text
|
|
31
|
+
if (chunk.type === 'reasoning' && typeof chunk.text === 'string') return chunk.text
|
|
32
|
+
if (typeof chunk.reasoning_content === 'string') return chunk.reasoning_content
|
|
33
|
+
if (typeof chunk.reasoning === 'string') return chunk.reasoning
|
|
34
|
+
if (typeof chunk.thought === 'string') return chunk.thought
|
|
35
|
+
return ''
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function parseReasoningStream(streamText = '', deltaReasoning = '') {
|
|
39
|
+
const raw = String(streamText || '')
|
|
40
|
+
let thought = String(deltaReasoning || '')
|
|
41
|
+
let content = raw
|
|
42
|
+
let isThinking = false
|
|
43
|
+
let isComplete = false
|
|
44
|
+
|
|
45
|
+
const completeTagRegex = /<(?:think|thought)>([\s\S]*?)<\/(?:think|thought)>|\[THOUGHT\]([\s\S]*?)\[\/THOUGHT\]|\|begin_of_thought\|([\s\S]*?)\|end_of_thought\|/gi
|
|
46
|
+
|
|
47
|
+
let match
|
|
48
|
+
const thoughtsFromTags = []
|
|
49
|
+
while ((match = completeTagRegex.exec(raw)) !== null) {
|
|
50
|
+
const t = match[1] ?? match[2] ?? match[3] ?? ''
|
|
51
|
+
thoughtsFromTags.push(t)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (thoughtsFromTags.length > 0) {
|
|
55
|
+
content = raw.replace(completeTagRegex, '')
|
|
56
|
+
thought = (thought ? thought + '\n' : '') + thoughtsFromTags.join('\n')
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const openTagRegex = /<(?:think|thought)>([\s\S]*)$|\[THOUGHT\]([\s\S]*)$|\|begin_of_thought\|([\s\S]*)$/i
|
|
60
|
+
const openMatch = content.match(openTagRegex)
|
|
61
|
+
if (openMatch) {
|
|
62
|
+
const unclosedThought = openMatch[1] ?? openMatch[2] ?? openMatch[3] ?? ''
|
|
63
|
+
thought = (thought ? thought + '\n' : '') + unclosedThought
|
|
64
|
+
content = content.replace(openTagRegex, '')
|
|
65
|
+
isThinking = true
|
|
66
|
+
isComplete = false
|
|
67
|
+
} else if (deltaReasoning && !content.trim()) {
|
|
68
|
+
isThinking = true
|
|
69
|
+
isComplete = false
|
|
70
|
+
} else {
|
|
71
|
+
isComplete = Boolean(thought.trim()) && Boolean(content.trim())
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return {
|
|
75
|
+
thought: thought.trim(),
|
|
76
|
+
content: content.trim(),
|
|
77
|
+
isThinking,
|
|
78
|
+
isComplete: !isThinking && isComplete,
|
|
79
|
+
hasThought: Boolean(thought.trim()),
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function buildStreamPreview(streamText, toolName, maxLen = 3500, options = {}) {
|
|
84
|
+
const parsed = parseReasoningStream(streamText, options?.reasoningText)
|
|
85
|
+
const showReasoning = options?.showReasoning !== false
|
|
86
|
+
const thoughtCollapse = options?.thoughtCollapse !== false
|
|
87
|
+
const locale = options?.locale || 'en'
|
|
88
|
+
const elapsedSec = options?.startTime ? Math.max(1, Math.round((Date.now() - options.startTime) / 1000)) : 0
|
|
89
|
+
const timeStr = elapsedSec > 0 ? ` (${elapsedSec}s)` : ''
|
|
90
|
+
|
|
91
|
+
const toolHead = toolName ? `${formatProgressLine(toolName, locale)}\n\n` : ''
|
|
92
|
+
|
|
93
|
+
if (parsed.isThinking) {
|
|
94
|
+
if (!showReasoning || !parsed.thought) {
|
|
95
|
+
const thinkingHeader = toolName
|
|
96
|
+
? formatProgressLine(toolName, locale)
|
|
97
|
+
: (String(locale).toLowerCase().startsWith('zh') ? `⏳ 思考中…${timeStr}` : `⏳ Thinking…${timeStr}`)
|
|
98
|
+
const combined = `${thinkingHeader}\n\n${parsed.content || '…'}`.trim()
|
|
99
|
+
return combined.length <= maxLen ? combined : combined.slice(0, maxLen - 1) + '…'
|
|
100
|
+
}
|
|
101
|
+
const header = toolName
|
|
102
|
+
? `🔧 <b>${escapeHtml(toolName)}…</b>`
|
|
103
|
+
: (String(locale).toLowerCase().startsWith('zh') ? `🧠 <b>思考中…</b><i>${timeStr}</i>` : `🧠 <b>Thinking…</b><i>${timeStr}</i>`)
|
|
104
|
+
const available = Math.max(200, maxLen - header.length - 100)
|
|
105
|
+
let thoughtText = parsed.thought
|
|
106
|
+
if (thoughtText.length > available) {
|
|
107
|
+
thoughtText = '…' + thoughtText.slice(-(available - 1))
|
|
108
|
+
}
|
|
109
|
+
const quote = `<blockquote>${escapeHtml(thoughtText)}</blockquote>`
|
|
110
|
+
const body = parsed.content ? `\n\n${escapeHtml(parsed.content)}` : ''
|
|
111
|
+
const combined = `${header}\n${quote}${body}`
|
|
112
|
+
return combined.length <= maxLen ? combined : combined.slice(0, maxLen - 1) + '…'
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (parsed.hasThought) {
|
|
116
|
+
if (showReasoning) {
|
|
117
|
+
const quoteTag = thoughtCollapse ? '<blockquote expandable>' : '<blockquote>'
|
|
118
|
+
const thoughtHeader = String(locale).toLowerCase().startsWith('zh') ? `🧠 <i>思考过程${timeStr}:</i>` : `🧠 <i>Thought process${timeStr}:</i>`
|
|
119
|
+
const contentText = parsed.content || '…'
|
|
120
|
+
const contentBudget = Math.min(2500, maxLen - 300)
|
|
121
|
+
const thoughtBudget = Math.max(200, maxLen - contentBudget - 150)
|
|
122
|
+
let thoughtText = parsed.thought
|
|
123
|
+
if (thoughtText.length > thoughtBudget) {
|
|
124
|
+
thoughtText = thoughtText.slice(0, thoughtBudget - 1) + '…'
|
|
125
|
+
}
|
|
126
|
+
const quote = `${quoteTag}${thoughtHeader}\n${escapeHtml(thoughtText)}</blockquote>`
|
|
127
|
+
const combined = `${toolHead}${quote}\n\n${escapeHtml(contentText)}`.trim()
|
|
128
|
+
return combined.length <= maxLen ? combined : combined.slice(0, maxLen - 1) + '…'
|
|
129
|
+
} else {
|
|
130
|
+
const contentText = parsed.content || '…'
|
|
131
|
+
const combined = `${toolHead}${escapeHtml(contentText)}`.trim()
|
|
132
|
+
return combined.length <= maxLen ? combined : combined.slice(0, maxLen - 1) + '…'
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
27
136
|
const body = String(streamText || '').trimEnd()
|
|
28
|
-
const
|
|
29
|
-
const combined = head + (body || '…')
|
|
137
|
+
const combined = toolHead + (body || '…')
|
|
30
138
|
if (combined.length <= maxLen) return combined
|
|
31
139
|
return combined.slice(0, maxLen - 1) + '…'
|
|
32
140
|
}
|
|
33
141
|
|
|
142
|
+
export function formatReasoningFinal(rawText, deltaReasoning, options = {}) {
|
|
143
|
+
const showReasoning = options?.showReasoning !== false
|
|
144
|
+
const thoughtCollapse = options?.thoughtCollapse !== false
|
|
145
|
+
const locale = options?.locale || 'en'
|
|
146
|
+
const parsed = parseReasoningStream(rawText, deltaReasoning)
|
|
147
|
+
|
|
148
|
+
if (!parsed.hasThought || !showReasoning) {
|
|
149
|
+
return parsed.content || String(rawText || '')
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const quoteTag = thoughtCollapse ? '<blockquote expandable>' : '<blockquote>'
|
|
153
|
+
const thoughtHeader = String(locale).toLowerCase().startsWith('zh') ? '🧠 <i>思考过程:</i>' : '🧠 <i>Thought process:</i>'
|
|
154
|
+
const thoughtBlock = `${quoteTag}${thoughtHeader}\n${escapeHtml(parsed.thought)}</blockquote>\n\n`
|
|
155
|
+
return `${thoughtBlock}${parsed.content}`
|
|
156
|
+
}
|
|
157
|
+
|
|
34
158
|
export function parseRetryAfter(err) {
|
|
35
159
|
if (!err) return 0
|
|
36
160
|
const msg = err.message || String(err)
|
|
@@ -121,4 +245,4 @@ export function startTypingHeartbeat(typingFn, intervalMs = 4000, logger = null)
|
|
|
121
245
|
active = false
|
|
122
246
|
clearInterval(timer)
|
|
123
247
|
}
|
|
124
|
-
}
|
|
248
|
+
}
|
package/lib/telegram-format.js
CHANGED
|
@@ -35,7 +35,7 @@ export function decodeHtmlEntities(text) {
|
|
|
35
35
|
return String(text ?? '').replace(/&(lt|gt|quot|#39|apos|nbsp);|&/g, (match) => ENTITIES[match] ?? '&')
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const SAFE_TAG_ANY_RE = new RegExp(`<a\\s+href="([^"]{1,512})"\\s*>|<(/?)(${SAFE_TAG_NAMES}|a)>`, 'gi')
|
|
38
|
+
const SAFE_TAG_ANY_RE = new RegExp(`<a\\s+href="([^"]{1,512})"\\s*>|<blockquote(\\s+expandable)?>|<(/?)(${SAFE_TAG_NAMES}|a)>`, 'gi')
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* Вынимает в слоты только сбалансированные теги из белого списка. Незакрытый
|
|
@@ -44,13 +44,17 @@ const SAFE_TAG_ANY_RE = new RegExp(`<a\\s+href="([^"]{1,512})"\\s*>|<(/?)(${SAFE
|
|
|
44
44
|
*/
|
|
45
45
|
function protectSafeTags(text, stash) {
|
|
46
46
|
const open = []
|
|
47
|
-
return text.replace(SAFE_TAG_ANY_RE, (match, href, closing, rawName) => {
|
|
48
|
-
const tag = String(rawName || 'a').toLowerCase()
|
|
47
|
+
return text.replace(SAFE_TAG_ANY_RE, (match, href, expandable, closing, rawName) => {
|
|
49
48
|
if (href !== undefined) {
|
|
50
49
|
if (!isSafeUrl(href)) return match
|
|
51
|
-
open.push(
|
|
50
|
+
open.push('a')
|
|
51
|
+
return stash(match)
|
|
52
|
+
}
|
|
53
|
+
if (expandable !== undefined) {
|
|
54
|
+
open.push('blockquote')
|
|
52
55
|
return stash(match)
|
|
53
56
|
}
|
|
57
|
+
const tag = String(rawName || 'a').toLowerCase()
|
|
54
58
|
if (closing) {
|
|
55
59
|
const index = open.lastIndexOf(tag)
|
|
56
60
|
if (index === -1) return match
|
|
@@ -67,6 +71,7 @@ export function stripSafeTags(text) {
|
|
|
67
71
|
return decodeHtmlEntities(text)
|
|
68
72
|
.replace(SAFE_ANCHOR_RE, '')
|
|
69
73
|
.replace(/<\/a>/gi, '')
|
|
74
|
+
.replace(/<blockquote\b[^>]*>/gi, '')
|
|
70
75
|
.replace(SAFE_TAG_RE, '')
|
|
71
76
|
}
|
|
72
77
|
|
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.25",
|
|
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",
|