dsh-tg-bridge 1.52.0 → 1.53.0
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/chat/speech.mjs +72 -30
- package/chat/telegram.mjs +1 -1
- package/docs/CHANGELOG.md +19 -0
- package/docs/VOICE.md +6 -4
- package/package.json +1 -1
package/chat/speech.mjs
CHANGED
|
@@ -30,7 +30,7 @@ export const DEFAULT_VOICES = {
|
|
|
30
30
|
zh: 'zh-CN-YunjianNeural',
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
-
/**
|
|
33
|
+
/** One voice note is a message; a longer answer is several, not a shorter one. */
|
|
34
34
|
export const MAX_SPEECH_CHARS = 1200;
|
|
35
35
|
|
|
36
36
|
/** The config block, with the defaults this module promises. */
|
|
@@ -40,6 +40,9 @@ export function speechConfig(config) {
|
|
|
40
40
|
provider: raw.provider || 'edge-tts',
|
|
41
41
|
python: raw.python || 'python',
|
|
42
42
|
ffmpeg: raw.ffmpeg || 'ffmpeg',
|
|
43
|
+
// An engine that already speaks OGG/Opus skips the ffmpeg conversion; the
|
|
44
|
+
// flag was documented before it was read, which made it dead config.
|
|
45
|
+
ogg: raw.ogg === true,
|
|
43
46
|
maxChars: Number(raw.maxChars) > 0 ? Number(raw.maxChars) : MAX_SPEECH_CHARS,
|
|
44
47
|
timeoutMs: Number(raw.timeoutMs) > 0 ? Number(raw.timeoutMs) : 120000,
|
|
45
48
|
voices: { ...DEFAULT_VOICES, ...(raw.voices || {}) },
|
|
@@ -71,8 +74,13 @@ export function setSpeech(state, key, patch, config, lang = 'ru') {
|
|
|
71
74
|
* Code blocks, markup and tables are not speech: reading `**` aloud or spelling
|
|
72
75
|
* out a pipe-separated row makes the message worse than silence. Emoji are
|
|
73
76
|
* dropped for the same reason; a voice model pronounces "🙂" as a pause.
|
|
77
|
+
*
|
|
78
|
+
* Nothing is cut here: what this returns is the whole answer, and `speechParts`
|
|
79
|
+
* decides how it is divided into notes. A cut in this function was the defect
|
|
80
|
+
* this module grew out of — a 3000-char answer arrived as its first 1155
|
|
81
|
+
* characters, and the person heard a reply that ended mid-thought.
|
|
74
82
|
*/
|
|
75
|
-
export function forSpeech(text
|
|
83
|
+
export function forSpeech(text) {
|
|
76
84
|
let out = String(text || '');
|
|
77
85
|
// A fenced block is a document the person will read, not listen to.
|
|
78
86
|
out = out.replace(/```[\s\S]*?```/g, ' ');
|
|
@@ -83,12 +91,33 @@ export function forSpeech(text, maxChars = MAX_SPEECH_CHARS) {
|
|
|
83
91
|
out = out.replace(/^\s*\|.*\|\s*$/gm, ' ');
|
|
84
92
|
out = out.replace(/[*_~#>]+/g, ' ');
|
|
85
93
|
out = out.replace(/[\u{1F300}-\u{1FAFF}\u{2600}-\u{27BF}\u{FE0F}\u{200D}]/gu, ' ');
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
94
|
+
return out.replace(/\s+/g, ' ').trim();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The answer as a list of notes, each a thing a person can listen to whole.
|
|
99
|
+
*
|
|
100
|
+
* Every note ends on a sentence when the window has one, else on a word, and
|
|
101
|
+
* never in the middle of a word; the last note is whatever remains, even short.
|
|
102
|
+
* The concatenation of the notes is always the full cleaned answer — a tail
|
|
103
|
+
* that is silently dropped is half an answer pretending to be one.
|
|
104
|
+
*/
|
|
105
|
+
export function speechParts(text, maxChars = MAX_SPEECH_CHARS) {
|
|
106
|
+
const full = forSpeech(text);
|
|
107
|
+
if (!full) return [];
|
|
108
|
+
if (full.length <= maxChars) return [full];
|
|
109
|
+
const parts = [];
|
|
110
|
+
let rest = full;
|
|
111
|
+
while (rest.length > maxChars) {
|
|
112
|
+
const window = rest.slice(0, maxChars);
|
|
113
|
+
let stop = Math.max(window.lastIndexOf('. '), window.lastIndexOf('! '), window.lastIndexOf('? '));
|
|
114
|
+
if (stop < maxChars * 0.5) stop = window.lastIndexOf(' ');
|
|
115
|
+
if (stop <= 0) stop = maxChars;
|
|
116
|
+
parts.push(rest.slice(0, stop + 1).trim());
|
|
117
|
+
rest = rest.slice(stop + 1).trim();
|
|
118
|
+
}
|
|
119
|
+
if (rest) parts.push(rest);
|
|
120
|
+
return parts.filter(Boolean);
|
|
92
121
|
}
|
|
93
122
|
|
|
94
123
|
/** The arguments for one provider run. Kept separate so a test can read them. */
|
|
@@ -109,36 +138,49 @@ export function providerArgs(cfg, { text, mp3 }) {
|
|
|
109
138
|
/**
|
|
110
139
|
* Say the answer out loud, if this topic asked for it.
|
|
111
140
|
*
|
|
112
|
-
*
|
|
141
|
+
* A long answer is several voice notes sent in order, never one note with the
|
|
142
|
+
* rest of the answer dropped. Each note is a full engine run, so one hanging
|
|
143
|
+
* engine costs that note, not the notes after it, and never the answer, which
|
|
144
|
+
* the caller already sent as text.
|
|
145
|
+
*
|
|
146
|
+
* `runner` and `sender` are injectable so the tests can prove the sequence
|
|
147
|
+
* without spawning a real engine or the network.
|
|
148
|
+
*
|
|
149
|
+
* @returns {Promise<boolean>} whether at least one voice note was sent
|
|
113
150
|
*/
|
|
114
|
-
export async function speak({ chatId, threadId, state, key, text, config, lang = 'ru', log = () => {} }) {
|
|
151
|
+
export async function speak({ chatId, threadId, state, key, text, config, lang = 'ru', log = () => {}, runner = run, sender = sendVoice }) {
|
|
115
152
|
const setting = speechOf(state, key, config, lang);
|
|
116
153
|
if (!setting.on) return false;
|
|
117
154
|
const cfg = speechConfig(config);
|
|
118
|
-
const
|
|
119
|
-
if (!
|
|
155
|
+
const parts = speechParts(text, cfg.maxChars);
|
|
156
|
+
if (!parts.length) return false;
|
|
120
157
|
|
|
121
158
|
const dir = mkdtempSync(join(tmpdir(), 'dsh-speech-'));
|
|
122
|
-
|
|
123
|
-
const ogg = join(dir, 'say.ogg');
|
|
159
|
+
let sent = 0;
|
|
124
160
|
try {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
161
|
+
for (const [index, part] of parts.entries()) {
|
|
162
|
+
const mp3 = join(dir, `say-${index + 1}.mp3`);
|
|
163
|
+
const ogg = join(dir, `say-${index + 1}.ogg`);
|
|
164
|
+
try {
|
|
165
|
+
// The provider writes `mp3`; a provider that already speaks OGG/Opus
|
|
166
|
+
// (`speech.ogg: true`) skips the conversion and its ffmpeg dependency.
|
|
167
|
+
const target = cfg.ogg ? ogg : mp3;
|
|
168
|
+
const { command, args } = providerArgs({ ...cfg, voice: setting.voice }, { text: part, mp3: target });
|
|
169
|
+
await runner(command, args, { timeout: cfg.timeoutMs, windowsHide: true, maxBuffer: 8 << 20 });
|
|
170
|
+
if (!cfg.ogg) {
|
|
171
|
+
await runner(cfg.ffmpeg, ['-y', '-hide_banner', '-loglevel', 'error', '-i', mp3,
|
|
172
|
+
'-c:a', 'libopus', '-b:a', '48k', '-ar', '48000', '-ac', '1', ogg],
|
|
173
|
+
{ timeout: cfg.timeoutMs, windowsHide: true });
|
|
174
|
+
}
|
|
175
|
+
await sender(chatId, threadId, ogg);
|
|
176
|
+
sent += 1;
|
|
177
|
+
} catch (error) {
|
|
178
|
+
// Loud in the log, silent in the chat: the text already arrived.
|
|
179
|
+
log(`speech failed for note ${index + 1}/${parts.length} chat=${chatId}: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
180
|
+
}
|
|
134
181
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
return true;
|
|
138
|
-
} catch (error) {
|
|
139
|
-
// Loud in the log, silent in the chat: the text already arrived.
|
|
140
|
-
log(`speech failed for chat=${chatId}: ${String(error?.message ?? error).slice(0, 200)}`);
|
|
141
|
-
return false;
|
|
182
|
+
if (sent) log(`spoke ${sent}/${parts.length} note(s) ${parts.join('').length} chars in ${setting.voice} chat=${chatId} topic=${threadId ?? 0}`);
|
|
183
|
+
return sent > 0;
|
|
142
184
|
} finally {
|
|
143
185
|
try { rmSync(dir, { recursive: true, force: true }); } catch { /* temp dir, best effort */ }
|
|
144
186
|
}
|
package/chat/telegram.mjs
CHANGED
|
@@ -92,7 +92,7 @@ export async function sendVoice(chatId, threadId, filePath, caption = '') {
|
|
|
92
92
|
const res = await fetch(`https://api.telegram.org/bot${token}/sendVoice`, { method: 'POST', body: form });
|
|
93
93
|
const json = await res.json().catch(() => ({}));
|
|
94
94
|
if (!json.ok) throw new Error(`sendVoice: ${json.description || res.status}`);
|
|
95
|
-
log(`sent voice ${basename(filePath)} (${buf.length} B) chat=${chatId}`);
|
|
95
|
+
log(`sent voice ${basename(filePath)} (${buf.length} B) chat=${chatId} topic=${threadId ?? 0}`);
|
|
96
96
|
return json.result;
|
|
97
97
|
}
|
|
98
98
|
|
package/docs/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# CHANGELOG — DSH Telegram bridge
|
|
2
2
|
|
|
3
|
+
## 1.53.0
|
|
4
|
+
|
|
5
|
+
- **`/voice` now speaks the whole answer.** 1.52.0 cut the spoken text at
|
|
6
|
+
`maxChars` — a 3300-character answer arrived as its first 1155 characters and
|
|
7
|
+
the person heard a reply that ended mid-thought, which read as a stitching
|
|
8
|
+
defect ("parts of different messages in one voice"). The answer is now
|
|
9
|
+
**several voice notes sent in order**, each cut at a sentence, each its own
|
|
10
|
+
engine run: `maxChars` is the per-note limit, not the limit of what is spoken.
|
|
11
|
+
A note that fails is logged as `speech failed for note 2/4` and costs that
|
|
12
|
+
note, never the notes after it and never the answer. 4 tests for the split
|
|
13
|
+
(sentence end, word boundary, no dropped tail) and the sequence (order,
|
|
14
|
+
per-note failure, one log line per failure).
|
|
15
|
+
- **`speech.ogg: true` is read now.** It was documented in `docs/VOICE.md`
|
|
16
|
+
before `speechConfig` read it, so an engine already speaking OGG/Opus still
|
|
17
|
+
paid an ffmpeg run it did not need.
|
|
18
|
+
- **`sent voice` log lines carry the topic.** A voice note in the log had
|
|
19
|
+
`chat=` but no `topic=`, which made a voice sent to its own topic look
|
|
20
|
+
indistinguishable from one sent to the wrong one when reading `bridge.log`.
|
|
21
|
+
|
|
3
22
|
## 1.52.0
|
|
4
23
|
|
|
5
24
|
- **`/voice` — the answer can be listened to.** `/voice on` makes the topic send
|
package/docs/VOICE.md
CHANGED
|
@@ -125,10 +125,12 @@ the `ru` one rather than going silent. `/voice <name>` sets a voice for that top
|
|
|
125
125
|
without touching the config, which is how a person tries one out.
|
|
126
126
|
|
|
127
127
|
The answer is spoken, not the thinking and not the progress lines: code blocks,
|
|
128
|
-
tables, links and emoji are stripped first
|
|
129
|
-
cut at a sentence
|
|
130
|
-
|
|
131
|
-
|
|
128
|
+
tables, links and emoji are stripped first. A long answer is **several voice
|
|
129
|
+
notes in order**, each cut at a sentence, so the whole answer is heard —
|
|
130
|
+
`maxChars` is the per-note limit, not the limit of what gets spoken. The engine
|
|
131
|
+
writes MP3 and `ffmpeg` converts it to OGG/Opus, because Telegram draws a voice
|
|
132
|
+
bubble only for that container. A provider that already speaks OGG sets
|
|
133
|
+
`speech.ogg: true` and needs no ffmpeg.
|
|
132
134
|
|
|
133
135
|
A local Piper install is the same shape:
|
|
134
136
|
|