jbc-dssi 1.1.147 → 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.
Files changed (3) hide show
  1. package/index.js +1 -1
  2. package/lib/tts.js +77 -68
  3. package/package.json +4 -1
package/index.js CHANGED
@@ -2184,7 +2184,7 @@ class BridgeConnection {
2184
2184
  id,
2185
2185
  text: payload?.text,
2186
2186
  voice: payload?.voice,
2187
- onChunk: (frame) => this.send(FRAME.TTS_AUDIO, frame),
2187
+ onPacket: (frame) => this.send(FRAME.TTS_AUDIO, frame),
2188
2188
  onDone: (frame) => this.send(FRAME.TTS_AUDIO, { id, done: true, bytes: frame?.bytes ?? 0 }),
2189
2189
  onError: (error) => this.send(FRAME.TTS_AUDIO, { id, error }),
2190
2190
  });
package/lib/tts.js CHANGED
@@ -1,44 +1,35 @@
1
- const TTS_URL = 'https://api.deepseek.com/v1/tools/tts';
1
+ import WebSocket from 'ws';
2
2
 
3
3
  export const TTS_VOICES = [
4
4
  { id: 'stella', name: '海星', gender: '女声' },
5
5
  { id: 'tide', name: '暗潮', gender: '男声' },
6
6
  ];
7
7
 
8
+ const TTS_WS = 'wss://api.deepseek.com/v1/tools/tts';
8
9
  const MAX_CHARS = 2000;
9
10
  const MAX_INFLIGHT = 2;
11
+ const CONNECT_TIMEOUT_MS = 15_000;
10
12
 
11
13
  function voiceOf(id) {
12
14
  return TTS_VOICES.some((v) => v.id === id) ? id : TTS_VOICES[0].id;
13
15
  }
14
16
 
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})` };
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 ?? '语音合成失败' };
36
27
  }
37
28
 
38
- export function createTtsEngine({ log, resolveKey, fetchImpl = globalThis.fetch }) {
29
+ export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket }) {
39
30
  const inflight = new Map();
40
31
 
41
- async function speak({ id, text, voice, onChunk, onDone, onError }) {
32
+ async function speak({ id, text, voice, onPacket, onDone, onError }) {
42
33
  if (typeof id !== 'string' || id === '') return;
43
34
  const body = String(text ?? '').slice(0, MAX_CHARS);
44
35
  if (body.trim() === '') {
@@ -51,62 +42,80 @@ export function createTtsEngine({ log, resolveKey, fetchImpl = globalThis.fetch
51
42
  }
52
43
  const key = await resolveKey();
53
44
  if (!key) {
54
- onError?.({ code: 'no_key', message: '电脑上没有 DeepSeek API Key' });
45
+ onError?.({ code: 'no_key', message: '电脑上没有 DeepSeek Key' });
55
46
  return;
56
47
  }
57
48
 
58
- const abort = new AbortController();
59
- inflight.set(id, abort);
60
- let seq = 0;
49
+ const url = `${TTS_WS}?voice_id=${voiceOf(voice)}&format=opus`;
50
+ let settled = false;
51
+ let ws;
61
52
  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;
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
+ /* 已关闭 */
76
71
  }
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') });
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}` });
84
82
  }
85
- if (abort.signal.aborted) return;
86
- if (bytes === 0) {
87
- onError?.({ code: 'empty_audio', message: '语音合成没有返回音频' });
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
88
  return;
89
89
  }
90
- onDone?.({ id, bytes });
91
- } catch (err) {
92
- if (!abort.signal.aborted) {
93
- log?.(`[tts] 请求失败: ${err?.message ?? err}`);
94
- onError?.({ code: 'network', message: '语音合成失败,检查电脑网络' });
90
+ let message;
91
+ try {
92
+ message = JSON.parse(String(data));
93
+ } catch {
94
+ return;
95
95
  }
96
- } finally {
97
- inflight.delete(id);
98
- }
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
+ });
99
109
  }
100
110
 
101
111
  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
- }
112
+ const ws = inflight.get(id);
113
+ if (!ws) return;
114
+ inflight.delete(id);
115
+ try {
116
+ ws.close();
117
+ } catch {
118
+ /* 已关闭 */
110
119
  }
111
120
  }
112
121
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jbc-dssi",
3
- "version": "1.1.147",
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
  }