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.
- package/index.js +1 -1
- package/lib/tts.js +77 -68
- 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
|
-
|
|
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
|
-
|
|
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(
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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,
|
|
29
|
+
export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket }) {
|
|
39
30
|
const inflight = new Map();
|
|
40
31
|
|
|
41
|
-
async function speak({ id, text, voice,
|
|
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
|
|
45
|
+
onError?.({ code: 'no_key', message: '电脑上没有 DeepSeek Key' });
|
|
55
46
|
return;
|
|
56
47
|
}
|
|
57
48
|
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
let
|
|
49
|
+
const url = `${TTS_WS}?voice_id=${voiceOf(voice)}&format=opus`;
|
|
50
|
+
let settled = false;
|
|
51
|
+
let ws;
|
|
61
52
|
try {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
let message;
|
|
91
|
+
try {
|
|
92
|
+
message = JSON.parse(String(data));
|
|
93
|
+
} catch {
|
|
94
|
+
return;
|
|
95
95
|
}
|
|
96
|
-
|
|
97
|
-
|
|
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
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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.
|
|
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
|
}
|