jbc-dssi 1.1.147 → 1.1.149
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 +22 -13
- package/lib/ogg-opus.js +101 -0
- package/lib/tts.js +156 -78
- package/package.json +4 -1
package/index.js
CHANGED
|
@@ -2171,23 +2171,32 @@ class BridgeConnection {
|
|
|
2171
2171
|
const op = payload?.op;
|
|
2172
2172
|
const id = typeof payload?.id === 'string' ? payload.id : '';
|
|
2173
2173
|
if (id === '') return;
|
|
2174
|
-
if (
|
|
2175
|
-
this.tts?.stop(id);
|
|
2176
|
-
return;
|
|
2177
|
-
}
|
|
2178
|
-
if (op !== 'start' || this.closed) return;
|
|
2174
|
+
if (this.closed) return;
|
|
2179
2175
|
this.tts ??= createTtsEngine({
|
|
2180
2176
|
log: (message) => this.opts.log(message),
|
|
2181
2177
|
resolveKey: () => this.#deepseekKey(),
|
|
2182
2178
|
});
|
|
2183
|
-
|
|
2184
|
-
id
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2179
|
+
if (op === 'stop') {
|
|
2180
|
+
this.tts.stop(id);
|
|
2181
|
+
return;
|
|
2182
|
+
}
|
|
2183
|
+
if (op === 'open') {
|
|
2184
|
+
await this.tts.open({
|
|
2185
|
+
id,
|
|
2186
|
+
voice: payload?.voice,
|
|
2187
|
+
onPage: (frame) => this.send(FRAME.TTS_AUDIO, frame),
|
|
2188
|
+
onDone: (frame) => this.send(FRAME.TTS_AUDIO, { id, done: true, pages: frame?.pages ?? 0 }),
|
|
2189
|
+
onError: (frame) => this.send(FRAME.TTS_AUDIO, { id, error: frame?.error }),
|
|
2190
|
+
});
|
|
2191
|
+
return;
|
|
2192
|
+
}
|
|
2193
|
+
if (op === 'text') {
|
|
2194
|
+
this.tts.text(id, payload?.text);
|
|
2195
|
+
return;
|
|
2196
|
+
}
|
|
2197
|
+
if (op === 'end') {
|
|
2198
|
+
this.tts.end(id);
|
|
2199
|
+
}
|
|
2191
2200
|
}
|
|
2192
2201
|
|
|
2193
2202
|
async #deepseekKey() {
|
package/lib/ogg-opus.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
const SEGMENT_MAX = 255;
|
|
2
|
+
const CRC_TABLE = (() => {
|
|
3
|
+
const table = new Int32Array(256);
|
|
4
|
+
for (let i = 0; i < 256; i += 1) {
|
|
5
|
+
let r = i << 24;
|
|
6
|
+
for (let j = 0; j < 8; j += 1) {
|
|
7
|
+
r = (r & 0x80000000) !== 0 ? ((r << 1) ^ 0x04c11db7) : (r << 1);
|
|
8
|
+
}
|
|
9
|
+
table[i] = r;
|
|
10
|
+
}
|
|
11
|
+
return table;
|
|
12
|
+
})();
|
|
13
|
+
|
|
14
|
+
function crc32(buffer, start, end) {
|
|
15
|
+
let crc = 0;
|
|
16
|
+
for (let i = start; i < end; i += 1) {
|
|
17
|
+
crc = ((crc << 8) ^ CRC_TABLE[((crc >>> 24) ^ buffer[i]) & 0xff]) >>> 0;
|
|
18
|
+
}
|
|
19
|
+
return crc >>> 0;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function page({ headerType, granule, serial, sequence, packets }) {
|
|
23
|
+
const segments = [];
|
|
24
|
+
for (const packet of packets) {
|
|
25
|
+
let rest = packet.length;
|
|
26
|
+
while (rest >= SEGMENT_MAX) {
|
|
27
|
+
segments.push(SEGMENT_MAX);
|
|
28
|
+
rest -= SEGMENT_MAX;
|
|
29
|
+
}
|
|
30
|
+
segments.push(rest);
|
|
31
|
+
}
|
|
32
|
+
const body = Buffer.concat(packets);
|
|
33
|
+
const header = Buffer.alloc(27 + segments.length);
|
|
34
|
+
header.write('OggS', 0, 'ascii');
|
|
35
|
+
header.writeUInt8(0, 4);
|
|
36
|
+
header.writeUInt8(headerType, 5);
|
|
37
|
+
header.writeBigInt64LE(BigInt(granule), 6);
|
|
38
|
+
header.writeUInt32LE(serial >>> 0, 14);
|
|
39
|
+
header.writeUInt32LE(sequence >>> 0, 18);
|
|
40
|
+
header.writeUInt32LE(0, 22);
|
|
41
|
+
header.writeUInt8(segments.length, 26);
|
|
42
|
+
for (let i = 0; i < segments.length; i += 1) header.writeUInt8(segments[i], 27 + i);
|
|
43
|
+
const out = Buffer.concat([header, body]);
|
|
44
|
+
out.writeUInt32LE(crc32(out, 0, out.length), 22);
|
|
45
|
+
return out;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const OPUS_HEAD = (() => {
|
|
49
|
+
const b = Buffer.alloc(19);
|
|
50
|
+
b.write('OpusHead', 0, 'ascii');
|
|
51
|
+
b.writeUInt8(1, 8);
|
|
52
|
+
b.writeUInt8(1, 9);
|
|
53
|
+
b.writeUInt16LE(3840, 10);
|
|
54
|
+
b.writeUInt32LE(48000, 12);
|
|
55
|
+
b.writeInt16LE(0, 16);
|
|
56
|
+
b.writeUInt8(0, 18);
|
|
57
|
+
return b;
|
|
58
|
+
})();
|
|
59
|
+
|
|
60
|
+
const OPUS_TAGS = (() => {
|
|
61
|
+
const vendor = Buffer.from('jbc-dssi', 'ascii');
|
|
62
|
+
const b = Buffer.alloc(8 + 4 + vendor.length + 4);
|
|
63
|
+
b.write('OpusTags', 0, 'ascii');
|
|
64
|
+
b.writeUInt32LE(vendor.length, 8);
|
|
65
|
+
vendor.copy(b, 12);
|
|
66
|
+
b.writeUInt32LE(0, 12 + vendor.length);
|
|
67
|
+
return b;
|
|
68
|
+
})();
|
|
69
|
+
|
|
70
|
+
export function createOggOpusStream({ samplesPerPacket = 960 } = {}) {
|
|
71
|
+
const serial = Math.floor(Math.random() * 0x7fffffff);
|
|
72
|
+
let sequence = 0;
|
|
73
|
+
let granule = 0;
|
|
74
|
+
const pages = [];
|
|
75
|
+
|
|
76
|
+
function header() {
|
|
77
|
+
pages.push(page({ headerType: 0x02, granule: 0, serial, sequence: sequence++, packets: [OPUS_HEAD] }));
|
|
78
|
+
pages.push(page({ headerType: 0x00, granule: 0, serial, sequence: sequence++, packets: [OPUS_TAGS] }));
|
|
79
|
+
return drain();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function packet(data) {
|
|
83
|
+
granule += samplesPerPacket;
|
|
84
|
+
pages.push(page({ headerType: 0x00, granule, serial, sequence: sequence++, packets: [data] }));
|
|
85
|
+
return drain();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function end() {
|
|
89
|
+
const out = [];
|
|
90
|
+
out.push(page({ headerType: 0x04, granule, serial, sequence: sequence++, packets: [Buffer.alloc(0)] }));
|
|
91
|
+
pages.push(...out);
|
|
92
|
+
return drain();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function drain() {
|
|
96
|
+
const out = pages.splice(0, pages.length);
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return { header, packet, end };
|
|
101
|
+
}
|
package/lib/tts.js
CHANGED
|
@@ -1,118 +1,196 @@
|
|
|
1
|
-
|
|
1
|
+
import WebSocket from 'ws';
|
|
2
|
+
import { createOggOpusStream } from './ogg-opus.js';
|
|
2
3
|
|
|
3
4
|
export const TTS_VOICES = [
|
|
4
5
|
{ id: 'stella', name: '海星', gender: '女声' },
|
|
5
6
|
{ id: 'tide', name: '暗潮', gender: '男声' },
|
|
6
7
|
];
|
|
7
8
|
|
|
8
|
-
const
|
|
9
|
+
const TTS_WS = 'wss://api.deepseek.com/v1/tools/tts';
|
|
10
|
+
const MAX_CHARS = 8000;
|
|
9
11
|
const MAX_INFLIGHT = 2;
|
|
12
|
+
const CONNECT_TIMEOUT_MS = 15_000;
|
|
10
13
|
|
|
11
14
|
function voiceOf(id) {
|
|
12
15
|
return TTS_VOICES.some((v) => v.id === id) ? id : TTS_VOICES[0].id;
|
|
13
16
|
}
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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})` };
|
|
18
|
+
const ERROR_TEXT = {
|
|
19
|
+
content_filter: '这段内容没通过语音审核',
|
|
20
|
+
invalid_input: '这段内容无法合成语音',
|
|
21
|
+
auth: 'DeepSeek Key 无效或没有语音权限',
|
|
22
|
+
quota: 'DeepSeek 余额不足',
|
|
23
|
+
rate_limit: '语音合成太频繁,请稍后再试',
|
|
24
|
+
service_error: '语音服务繁忙,请重试',
|
|
25
|
+
no_key: '电脑上没有 DeepSeek Key',
|
|
26
|
+
busy: '语音合成排队中,请稍后再试',
|
|
27
|
+
network: '语音合成失败,检查电脑网络',
|
|
28
|
+
timeout: '语音合成超时',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
function normalizeError(code, message) {
|
|
32
|
+
return { code: code ?? 'service_error', message: ERROR_TEXT[code] ?? message ?? '语音合成失败' };
|
|
36
33
|
}
|
|
37
34
|
|
|
38
|
-
export function createTtsEngine({ log, resolveKey,
|
|
39
|
-
const
|
|
35
|
+
export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket }) {
|
|
36
|
+
const sessions = new Map();
|
|
40
37
|
|
|
41
|
-
async function
|
|
38
|
+
async function open({ id, voice, onPage, onDone, onError }) {
|
|
42
39
|
if (typeof id !== 'string' || id === '') return;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
onError?.({ code: 'invalid_input', message: '没有可朗读的文本' });
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (inflight.size >= MAX_INFLIGHT) {
|
|
49
|
-
onError?.({ code: 'busy', message: '语音合成排队中,请稍后再试' });
|
|
40
|
+
if (sessions.size >= MAX_INFLIGHT) {
|
|
41
|
+
onError?.({ id, error: normalizeError('busy') });
|
|
50
42
|
return;
|
|
51
43
|
}
|
|
52
44
|
const key = await resolveKey();
|
|
53
45
|
if (!key) {
|
|
54
|
-
onError?.({
|
|
46
|
+
onError?.({ id, error: normalizeError('no_key') });
|
|
55
47
|
return;
|
|
56
48
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
inflight.set(id, abort);
|
|
60
|
-
let seq = 0;
|
|
49
|
+
const url = `${TTS_WS}?voice_id=${voiceOf(voice)}&format=opus`;
|
|
50
|
+
let ws;
|
|
61
51
|
try {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
52
|
+
ws = new WebSocketImpl(url, { headers: { Authorization: `Bearer ${key}` } });
|
|
53
|
+
} catch (err) {
|
|
54
|
+
onError?.({ id, error: normalizeError('network', `语音连接失败:${err?.message ?? err}`) });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const mux = createOggOpusStream();
|
|
59
|
+
const session = {
|
|
60
|
+
ws,
|
|
61
|
+
mux,
|
|
62
|
+
settled: false,
|
|
63
|
+
seq: 0,
|
|
64
|
+
chars: 0,
|
|
65
|
+
started: false,
|
|
66
|
+
pending: [],
|
|
67
|
+
onPage,
|
|
68
|
+
onDone,
|
|
69
|
+
onError,
|
|
70
|
+
};
|
|
71
|
+
sessions.set(id, session);
|
|
72
|
+
|
|
73
|
+
const timer = setTimeout(() => finish(session, id, { code: 'timeout' }), CONNECT_TIMEOUT_MS);
|
|
74
|
+
|
|
75
|
+
function flushPages(bytes) {
|
|
76
|
+
for (const chunk of bytes) {
|
|
77
|
+
if (session.settled) return;
|
|
78
|
+
session.onPage?.({ id, seq: session.seq++, data: chunk.toString('base64'), codec: 'ogg-opus' });
|
|
76
79
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function finish(target, sessionId, error) {
|
|
83
|
+
if (target.settled) return;
|
|
84
|
+
target.settled = true;
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
sessions.delete(sessionId);
|
|
87
|
+
try {
|
|
88
|
+
target.ws.close();
|
|
89
|
+
} catch {
|
|
90
|
+
/* 已关闭 */
|
|
84
91
|
}
|
|
85
|
-
if (
|
|
86
|
-
|
|
87
|
-
|
|
92
|
+
if (error) target.onError?.({ id: sessionId, error: normalizeError(error.code, error.message) });
|
|
93
|
+
else target.onDone?.({ id: sessionId, pages: target.seq });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
ws.on('open', () => {
|
|
97
|
+
session.started = true;
|
|
98
|
+
flushPages(mux.header());
|
|
99
|
+
for (const text of session.pending) sendText(text);
|
|
100
|
+
session.pending = [];
|
|
101
|
+
if (session.pendingFinish) sendFinish();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
function sendText(text) {
|
|
105
|
+
if (session.settled) return;
|
|
106
|
+
if (!session.started) {
|
|
107
|
+
session.pending.push(text);
|
|
88
108
|
return;
|
|
89
109
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
110
|
+
if (session.chars >= MAX_CHARS) return;
|
|
111
|
+
session.chars += text.length;
|
|
112
|
+
try {
|
|
113
|
+
ws.send(JSON.stringify({ event: 'text', text }));
|
|
114
|
+
} catch (err) {
|
|
115
|
+
finish(session, id, { code: 'network', message: `语音发送失败:${err?.message ?? err}` });
|
|
95
116
|
}
|
|
96
|
-
} finally {
|
|
97
|
-
inflight.delete(id);
|
|
98
117
|
}
|
|
99
|
-
}
|
|
100
118
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
119
|
+
ws.on('message', (data, isBinary) => {
|
|
120
|
+
if (isBinary) {
|
|
121
|
+
flushPages(mux.packet(Buffer.from(data)));
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
let parsed;
|
|
105
125
|
try {
|
|
106
|
-
|
|
126
|
+
parsed = JSON.parse(String(data));
|
|
107
127
|
} catch {
|
|
108
|
-
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (parsed?.event !== 'finish') return;
|
|
131
|
+
if (parsed.code === 'success') {
|
|
132
|
+
flushPages(mux.end());
|
|
133
|
+
finish(session, id, null);
|
|
134
|
+
} else {
|
|
135
|
+
finish(session, id, { code: parsed.code, message: parsed.message });
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
ws.on('error', (err) => {
|
|
140
|
+
log?.(`[tts] 连接异常: ${err?.message ?? err}`);
|
|
141
|
+
finish(session, id, { code: 'network' });
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
ws.on('close', () => {
|
|
145
|
+
if (!session.settled) finish(session, id, { code: 'network', message: '语音连接中断' });
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
function sendFinish() {
|
|
149
|
+
if (session.settled) return;
|
|
150
|
+
if (!session.started) {
|
|
151
|
+
session.pendingFinish = true;
|
|
152
|
+
return;
|
|
109
153
|
}
|
|
154
|
+
try {
|
|
155
|
+
ws.send(JSON.stringify({ event: 'finish_text' }));
|
|
156
|
+
} catch (err) {
|
|
157
|
+
log?.(`[tts] finish 发送失败: ${err?.message ?? err}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
session.sendText = sendText;
|
|
162
|
+
session.finish = sendFinish;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function text(id, body) {
|
|
166
|
+
const session = sessions.get(id);
|
|
167
|
+
if (!session) return;
|
|
168
|
+
const value = String(body ?? '');
|
|
169
|
+
if (value.trim() === '') return;
|
|
170
|
+
session.sendText(value);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function end(id) {
|
|
174
|
+
const session = sessions.get(id);
|
|
175
|
+
if (!session) return;
|
|
176
|
+
session.finish();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function stop(id) {
|
|
180
|
+
const session = sessions.get(id);
|
|
181
|
+
if (!session) return;
|
|
182
|
+
session.settled = true;
|
|
183
|
+
sessions.delete(id);
|
|
184
|
+
try {
|
|
185
|
+
session.ws.close();
|
|
186
|
+
} catch {
|
|
187
|
+
/* 已关闭 */
|
|
110
188
|
}
|
|
111
189
|
}
|
|
112
190
|
|
|
113
191
|
function stopAll() {
|
|
114
|
-
for (const id of [...
|
|
192
|
+
for (const id of [...sessions.keys()]) stop(id);
|
|
115
193
|
}
|
|
116
194
|
|
|
117
|
-
return {
|
|
195
|
+
return { open, text, end, stop, stopAll, sessions };
|
|
118
196
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "jbc-dssi",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.149",
|
|
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
|
}
|