jbc-dssi 1.1.146 → 1.1.148

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 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
+ onPacket: (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
  /*
@@ -4,4 +4,5 @@ export const PLUGIN_FEATURES = [
4
4
  'app_release_push',
5
5
  'file_bytes',
6
6
  'workspace_products',
7
+ 'speech_tts',
7
8
  ];
package/lib/protocol.js CHANGED
@@ -126,6 +126,10 @@ export const FRAME = {
126
126
 
127
127
  COMPACT_DONE: 0x7c,
128
128
 
129
+ TTS_CMD: 0x7d,
130
+
131
+ TTS_AUDIO: 0x7e,
132
+
129
133
  PING: 0x40,
130
134
  };
131
135
 
package/lib/tts.js ADDED
@@ -0,0 +1,127 @@
1
+ import WebSocket from 'ws';
2
+
3
+ export const TTS_VOICES = [
4
+ { id: 'stella', name: '海星', gender: '女声' },
5
+ { id: 'tide', name: '暗潮', gender: '男声' },
6
+ ];
7
+
8
+ const TTS_WS = 'wss://api.deepseek.com/v1/tools/tts';
9
+ const MAX_CHARS = 2000;
10
+ const MAX_INFLIGHT = 2;
11
+ const CONNECT_TIMEOUT_MS = 15_000;
12
+
13
+ function voiceOf(id) {
14
+ return TTS_VOICES.some((v) => v.id === id) ? id : TTS_VOICES[0].id;
15
+ }
16
+
17
+ function errorOf(code, message) {
18
+ const table = {
19
+ content_filter: '这段内容没通过语音审核',
20
+ invalid_input: '这段内容无法合成语音',
21
+ auth: 'DeepSeek Key 无效或没有语音权限',
22
+ quota: 'DeepSeek 余额不足',
23
+ rate_limit: '语音合成太频繁,请稍后再试',
24
+ service_error: '语音服务繁忙,请重试',
25
+ };
26
+ return { code, message: table[code] ?? message ?? '语音合成失败' };
27
+ }
28
+
29
+ export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket }) {
30
+ const inflight = new Map();
31
+
32
+ async function speak({ id, text, voice, onPacket, onDone, onError }) {
33
+ if (typeof id !== 'string' || id === '') return;
34
+ const body = String(text ?? '').slice(0, MAX_CHARS);
35
+ if (body.trim() === '') {
36
+ onError?.({ code: 'invalid_input', message: '没有可朗读的文本' });
37
+ return;
38
+ }
39
+ if (inflight.size >= MAX_INFLIGHT) {
40
+ onError?.({ code: 'busy', message: '语音合成排队中,请稍后再试' });
41
+ return;
42
+ }
43
+ const key = await resolveKey();
44
+ if (!key) {
45
+ onError?.({ code: 'no_key', message: '电脑上没有 DeepSeek Key' });
46
+ return;
47
+ }
48
+
49
+ const url = `${TTS_WS}?voice_id=${voiceOf(voice)}&format=opus`;
50
+ let settled = false;
51
+ let ws;
52
+ try {
53
+ ws = new WebSocketImpl(url, { headers: { Authorization: `Bearer ${key}` } });
54
+ } catch (err) {
55
+ onError?.({ code: 'network', message: `语音连接失败:${err?.message ?? err}` });
56
+ return;
57
+ }
58
+ inflight.set(id, ws);
59
+ let seq = 0;
60
+ const timer = setTimeout(() => finish({ code: 'timeout', message: '语音合成超时' }), CONNECT_TIMEOUT_MS);
61
+
62
+ function finish(error) {
63
+ if (settled) return;
64
+ settled = true;
65
+ clearTimeout(timer);
66
+ inflight.delete(id);
67
+ try {
68
+ ws.close();
69
+ } catch {
70
+ /* 已关闭 */
71
+ }
72
+ if (error) onError?.(errorOf(error.code, error.message));
73
+ else onDone?.({ id, packets: seq });
74
+ }
75
+
76
+ ws.on('open', () => {
77
+ try {
78
+ ws.send(JSON.stringify({ event: 'text', text: body }));
79
+ ws.send(JSON.stringify({ event: 'finish_text' }));
80
+ } catch (err) {
81
+ finish({ code: 'network', message: `语音发送失败:${err?.message ?? err}` });
82
+ }
83
+ });
84
+
85
+ ws.on('message', (data, isBinary) => {
86
+ if (isBinary) {
87
+ onPacket?.({ id, seq: seq++, data: Buffer.from(data).toString('base64'), codec: 'opus' });
88
+ return;
89
+ }
90
+ let message;
91
+ try {
92
+ message = JSON.parse(String(data));
93
+ } catch {
94
+ return;
95
+ }
96
+ if (message?.event !== 'finish') return;
97
+ if (message.code === 'success') finish(null);
98
+ else finish({ code: message.code ?? 'service_error', message: message.message });
99
+ });
100
+
101
+ ws.on('error', (err) => {
102
+ log?.(`[tts] 连接异常: ${err?.message ?? err}`);
103
+ finish({ code: 'network', message: '语音合成失败,检查电脑网络' });
104
+ });
105
+
106
+ ws.on('close', () => {
107
+ if (!settled) finish({ code: 'network', message: '语音连接中断' });
108
+ });
109
+ }
110
+
111
+ function stop(id) {
112
+ const ws = inflight.get(id);
113
+ if (!ws) return;
114
+ inflight.delete(id);
115
+ try {
116
+ ws.close();
117
+ } catch {
118
+ /* 已关闭 */
119
+ }
120
+ }
121
+
122
+ function stopAll() {
123
+ for (const id of [...inflight.keys()]) stop(id);
124
+ }
125
+
126
+ return { speak, stop, stopAll, inflight };
127
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jbc-dssi",
3
- "version": "1.1.146",
3
+ "version": "1.1.148",
4
4
  "type": "module",
5
5
  "description": "鲸聊 - 不只是手机遥控操作电脑,而是将电脑级的Agent能力装进手机",
6
6
  "keywords": [
@@ -50,5 +50,8 @@
50
50
  },
51
51
  "scripts": {
52
52
  "prepack": "node scripts/build-vendor.mjs"
53
+ },
54
+ "dependencies": {
55
+ "ws": "^8.18.0"
53
56
  }
54
57
  }