jbc-dssi 1.1.146 → 1.1.147
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/index.js +57 -0
- package/lib/plugin-features.js +1 -0
- package/lib/protocol.js +4 -0
- package/lib/tts.js +118 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -25,6 +25,7 @@ import { createRestartAutocheck } from './lib/restart-autocheck.js';
|
|
|
25
25
|
import { createGroupsEventBuffer, shouldBufferGroupEvent } from './lib/groups-event-buffer.js';
|
|
26
26
|
import { createScheduleEngine } from './lib/schedules.js';
|
|
27
27
|
import { createProcEngine } from './lib/proc-commands.js';
|
|
28
|
+
import { createTtsEngine } from './lib/tts.js';
|
|
28
29
|
import { createAppReleaseWatch } from './lib/app-release-watch.js';
|
|
29
30
|
import { PLUGIN_FEATURES } from './lib/plugin-features.js';
|
|
30
31
|
import { MAX_FILE_BYTES, mimeOfFile, resolveWorkspaceFilePath } from './lib/workspace-bytes.js';
|
|
@@ -1953,6 +1954,8 @@ class BridgeConnection {
|
|
|
1953
1954
|
this.schedules = opts.schedules ?? null;
|
|
1954
1955
|
/** 程序指令执行器(2026-11-09):手机 proc_* 命令的电脑侧实现。 */
|
|
1955
1956
|
this.proc = opts.proc ?? null;
|
|
1957
|
+
/** DeepSeek 语音合成(2026-11-25):首次用到才创建,连接关闭时停掉。 */
|
|
1958
|
+
this.tts = null;
|
|
1956
1959
|
/** 群聊能力(GROUPS_* / ROOM_SNAPSHOT 帧);随群模块/配置开关启用。 */
|
|
1957
1960
|
this.groups = opts.groups ?? null;
|
|
1958
1961
|
this.botsSeq = 0;
|
|
@@ -2007,6 +2010,8 @@ class BridgeConnection {
|
|
|
2007
2010
|
...(this.ctx.get?.('commands') && this.ctx.get?.('sessionController') ? ['compact'] : []),
|
|
2008
2011
|
// 向上翻更早的历史:需要会话控制器(官方 page() 分页)
|
|
2009
2012
|
...(this.ctx.get?.('sessionController') ? ['earlier'] : []),
|
|
2013
|
+
// DeepSeek 语音合成:电脑侧代持 Key,手机只发文本
|
|
2014
|
+
...(this.ctx.get?.('credentials') ? ['tts'] : []),
|
|
2010
2015
|
'rename',
|
|
2011
2016
|
],
|
|
2012
2017
|
});
|
|
@@ -2155,6 +2160,48 @@ class BridgeConnection {
|
|
|
2155
2160
|
this.#fire('bots-refresh', this.#refreshBots({ force: true }));
|
|
2156
2161
|
}
|
|
2157
2162
|
|
|
2163
|
+
/**
|
|
2164
|
+
* DeepSeek 语音合成(2026-11-25 老谢定):手机只发文本和音色,
|
|
2165
|
+
* 电脑侧用 DeepSeek Key 调 `/v1/tools/tts`,PCM 边下边回、手机边收边播。
|
|
2166
|
+
*
|
|
2167
|
+
* 为什么放在电脑侧:Key 一旦打进 App 就等于公开;而智能体本来就在电脑上跑,
|
|
2168
|
+
* 语音跟它同一条链路,电脑离线时语音本来也没意义。
|
|
2169
|
+
*/
|
|
2170
|
+
async #ttsCommand(payload) {
|
|
2171
|
+
const op = payload?.op;
|
|
2172
|
+
const id = typeof payload?.id === 'string' ? payload.id : '';
|
|
2173
|
+
if (id === '') return;
|
|
2174
|
+
if (op === 'stop') {
|
|
2175
|
+
this.tts?.stop(id);
|
|
2176
|
+
return;
|
|
2177
|
+
}
|
|
2178
|
+
if (op !== 'start' || this.closed) return;
|
|
2179
|
+
this.tts ??= createTtsEngine({
|
|
2180
|
+
log: (message) => this.opts.log(message),
|
|
2181
|
+
resolveKey: () => this.#deepseekKey(),
|
|
2182
|
+
});
|
|
2183
|
+
await this.tts.speak({
|
|
2184
|
+
id,
|
|
2185
|
+
text: payload?.text,
|
|
2186
|
+
voice: payload?.voice,
|
|
2187
|
+
onChunk: (frame) => this.send(FRAME.TTS_AUDIO, frame),
|
|
2188
|
+
onDone: (frame) => this.send(FRAME.TTS_AUDIO, { id, done: true, bytes: frame?.bytes ?? 0 }),
|
|
2189
|
+
onError: (error) => this.send(FRAME.TTS_AUDIO, { id, error }),
|
|
2190
|
+
});
|
|
2191
|
+
}
|
|
2192
|
+
|
|
2193
|
+
async #deepseekKey() {
|
|
2194
|
+
try {
|
|
2195
|
+
const credentials = this.ctx.get('credentials');
|
|
2196
|
+
const hit = await credentials?.resolve?.('DEEPSEEK_API_KEY');
|
|
2197
|
+
if (hit?.value) return hit.value;
|
|
2198
|
+
} catch (err) {
|
|
2199
|
+
this.opts.log(`[tts] 读取 DeepSeek Key 失败: ${err?.message ?? err}`);
|
|
2200
|
+
}
|
|
2201
|
+
const env = process.env.DEEPSEEK_API_KEY;
|
|
2202
|
+
return typeof env === 'string' && env !== '' ? env : null;
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2158
2205
|
#onClose() {
|
|
2159
2206
|
this.closed = true;
|
|
2160
2207
|
/*
|
|
@@ -2175,6 +2222,12 @@ class BridgeConnection {
|
|
|
2175
2222
|
}
|
|
2176
2223
|
this.followAbort?.abort();
|
|
2177
2224
|
this.followAbort = null;
|
|
2225
|
+
try {
|
|
2226
|
+
this.tts?.stopAll();
|
|
2227
|
+
} catch {
|
|
2228
|
+
/* 语音已停 */
|
|
2229
|
+
}
|
|
2230
|
+
this.tts = null;
|
|
2178
2231
|
// 连接断了,挂着没人答的审批一律交回浏览器
|
|
2179
2232
|
for (const resolve of this.pending.values()) resolve(null);
|
|
2180
2233
|
this.pending.clear();
|
|
@@ -2392,6 +2445,10 @@ class BridgeConnection {
|
|
|
2392
2445
|
this.#fire('read-file-bytes', this.#readFileBytes(payload));
|
|
2393
2446
|
break;
|
|
2394
2447
|
|
|
2448
|
+
case FRAME.TTS_CMD:
|
|
2449
|
+
this.#fire('tts', this.#ttsCommand(payload));
|
|
2450
|
+
break;
|
|
2451
|
+
|
|
2395
2452
|
case FRAME.FOCUS: {
|
|
2396
2453
|
this.focused = payload?.active === true;
|
|
2397
2454
|
/*
|
package/lib/plugin-features.js
CHANGED
package/lib/protocol.js
CHANGED
package/lib/tts.js
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const TTS_URL = 'https://api.deepseek.com/v1/tools/tts';
|
|
2
|
+
|
|
3
|
+
export const TTS_VOICES = [
|
|
4
|
+
{ id: 'stella', name: '海星', gender: '女声' },
|
|
5
|
+
{ id: 'tide', name: '暗潮', gender: '男声' },
|
|
6
|
+
];
|
|
7
|
+
|
|
8
|
+
const MAX_CHARS = 2000;
|
|
9
|
+
const MAX_INFLIGHT = 2;
|
|
10
|
+
|
|
11
|
+
function voiceOf(id) {
|
|
12
|
+
return TTS_VOICES.some((v) => v.id === id) ? id : TTS_VOICES[0].id;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function errorOf(status, detail) {
|
|
16
|
+
const text = String(detail ?? '').slice(0, 300);
|
|
17
|
+
if (status === 400 && /Content Exists Risk|content_filter/i.test(text)) {
|
|
18
|
+
return { code: 'content_filter', message: '这段内容没通过语音审核' };
|
|
19
|
+
}
|
|
20
|
+
if (status === 400) {
|
|
21
|
+
return { code: 'invalid_input', message: text || '这段内容无法合成语音' };
|
|
22
|
+
}
|
|
23
|
+
if (status === 401 || status === 403) {
|
|
24
|
+
return { code: 'auth', message: 'DeepSeek Key 无效或没有语音权限' };
|
|
25
|
+
}
|
|
26
|
+
if (status === 402) {
|
|
27
|
+
return { code: 'quota', message: 'DeepSeek 余额不足' };
|
|
28
|
+
}
|
|
29
|
+
if (status === 429) {
|
|
30
|
+
return { code: 'rate_limit', message: '语音合成太频繁,请稍后再试' };
|
|
31
|
+
}
|
|
32
|
+
if (status === 503 || status === 504) {
|
|
33
|
+
return { code: 'server', message: '语音服务繁忙,请重试' };
|
|
34
|
+
}
|
|
35
|
+
return { code: 'http', message: text || `语音合成失败(${status})` };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function createTtsEngine({ log, resolveKey, fetchImpl = globalThis.fetch }) {
|
|
39
|
+
const inflight = new Map();
|
|
40
|
+
|
|
41
|
+
async function speak({ id, text, voice, onChunk, onDone, onError }) {
|
|
42
|
+
if (typeof id !== 'string' || id === '') return;
|
|
43
|
+
const body = String(text ?? '').slice(0, MAX_CHARS);
|
|
44
|
+
if (body.trim() === '') {
|
|
45
|
+
onError?.({ code: 'invalid_input', message: '没有可朗读的文本' });
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
if (inflight.size >= MAX_INFLIGHT) {
|
|
49
|
+
onError?.({ code: 'busy', message: '语音合成排队中,请稍后再试' });
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const key = await resolveKey();
|
|
53
|
+
if (!key) {
|
|
54
|
+
onError?.({ code: 'no_key', message: '电脑上没有 DeepSeek API Key' });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const abort = new AbortController();
|
|
59
|
+
inflight.set(id, abort);
|
|
60
|
+
let seq = 0;
|
|
61
|
+
try {
|
|
62
|
+
const res = await fetchImpl(TTS_URL, {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
headers: {
|
|
65
|
+
Authorization: `Bearer ${key}`,
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
},
|
|
68
|
+
body: JSON.stringify({ text: body, voice_id: voiceOf(voice), format: 'pcm' }),
|
|
69
|
+
signal: abort.signal,
|
|
70
|
+
});
|
|
71
|
+
if (!res.ok) {
|
|
72
|
+
const detail = await res.text().catch(() => '');
|
|
73
|
+
log?.(`[tts] HTTP ${res.status}: ${String(detail).slice(0, 160)}`);
|
|
74
|
+
onError?.(errorOf(res.status, detail));
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
let bytes = 0;
|
|
78
|
+
for await (const chunk of res.body) {
|
|
79
|
+
if (abort.signal.aborted) return;
|
|
80
|
+
const data = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
81
|
+
if (data.length === 0) continue;
|
|
82
|
+
bytes += data.length;
|
|
83
|
+
onChunk?.({ id, seq: seq++, data: data.toString('base64') });
|
|
84
|
+
}
|
|
85
|
+
if (abort.signal.aborted) return;
|
|
86
|
+
if (bytes === 0) {
|
|
87
|
+
onError?.({ code: 'empty_audio', message: '语音合成没有返回音频' });
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
onDone?.({ id, bytes });
|
|
91
|
+
} catch (err) {
|
|
92
|
+
if (!abort.signal.aborted) {
|
|
93
|
+
log?.(`[tts] 请求失败: ${err?.message ?? err}`);
|
|
94
|
+
onError?.({ code: 'network', message: '语音合成失败,检查电脑网络' });
|
|
95
|
+
}
|
|
96
|
+
} finally {
|
|
97
|
+
inflight.delete(id);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function stop(id) {
|
|
102
|
+
const abort = inflight.get(id);
|
|
103
|
+
if (abort) {
|
|
104
|
+
inflight.delete(id);
|
|
105
|
+
try {
|
|
106
|
+
abort.abort();
|
|
107
|
+
} catch {
|
|
108
|
+
/* 已中止 */
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function stopAll() {
|
|
114
|
+
for (const id of [...inflight.keys()]) stop(id);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return { speak, stop, stopAll, inflight };
|
|
118
|
+
}
|