jbc-dssi 1.1.164 → 1.1.165

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
@@ -27,7 +27,7 @@ import { createScheduleEngine } from './lib/schedules.js';
27
27
  import { createProcEngine } from './lib/proc-commands.js';
28
28
  import { createTtsEngine } from './lib/tts.js';
29
29
  import { createEdgeTts, EDGE_VOICES } from './lib/edge-tts.js';
30
- import { createEdgeStream } from './lib/edge-stream.js';
30
+ import { createEdgeStreamSession } from './lib/edge-tts.js';
31
31
  import { createAppReleaseWatch } from './lib/app-release-watch.js';
32
32
  import { PLUGIN_FEATURES } from './lib/plugin-features.js';
33
33
  import { MAX_FILE_BYTES, mimeOfFile, mimeOfImage, resolveWorkspaceFilePath } from './lib/workspace-bytes.js';
@@ -2230,7 +2230,11 @@ class BridgeConnection {
2230
2230
  if (op === 'stop') {
2231
2231
  this.tts.stop(id);
2232
2232
  this.edge?.stop(id);
2233
- this.edgeStream?.stop(id);
2233
+ const live = this.edgeSessions?.get(id);
2234
+ if (live) {
2235
+ live.close();
2236
+ this.edgeSessions.delete(id);
2237
+ }
2234
2238
  return;
2235
2239
  }
2236
2240
  if (op === 'voices') {
@@ -2238,10 +2242,7 @@ class BridgeConnection {
2238
2242
  return;
2239
2243
  }
2240
2244
  this.edge ??= createEdgeTts({ log: (message) => this.opts.log(message) });
2241
- this.edgeStream ??= createEdgeStream({
2242
- edge: this.edge,
2243
- send: (frame) => this.send(FRAME.TTS_AUDIO, frame),
2244
- });
2245
+ this.edgeSessions ??= new Map();
2245
2246
  if (op === 'player') {
2246
2247
  const result = await this.edge.synthesize({
2247
2248
  id,
@@ -2256,7 +2257,16 @@ class BridgeConnection {
2256
2257
  if (op === 'open') {
2257
2258
  const voice = typeof payload?.voice === 'string' ? payload.voice : '';
2258
2259
  if (EDGE_VOICES.some((v) => v.id === voice)) {
2259
- this.edgeStream.open(id, voice);
2260
+ const session = createEdgeStreamSession({
2261
+ log: (message) => this.opts.log(message),
2262
+ voice,
2263
+ onSegment: (bytes) =>
2264
+ this.send(FRAME.TTS_AUDIO, { id, segment: bytes.toString('base64'), codec: 'mp3' }),
2265
+ onError: (message) =>
2266
+ this.send(FRAME.TTS_AUDIO, { id, error: { code: 'edge', message } }),
2267
+ onDone: () => this.send(FRAME.TTS_AUDIO, { id, done: true }),
2268
+ });
2269
+ this.edgeSessions.set(id, session);
2260
2270
  return;
2261
2271
  }
2262
2272
  await this.tts.open({
@@ -2269,13 +2279,19 @@ class BridgeConnection {
2269
2279
  return;
2270
2280
  }
2271
2281
  if (op === 'text') {
2272
- if (this.edgeStream?.has(id)) this.edgeStream.text(id, payload?.text);
2282
+ const live = this.edgeSessions?.get(id);
2283
+ if (live) live.send(payload?.text);
2273
2284
  else this.tts.text(id, payload?.text);
2274
2285
  return;
2275
2286
  }
2276
2287
  if (op === 'end') {
2277
- if (this.edgeStream?.has(id)) this.edgeStream.end(id);
2278
- else this.tts.end(id);
2288
+ const live = this.edgeSessions?.get(id);
2289
+ if (live) {
2290
+ this.edgeSessions.delete(id);
2291
+ live.finish();
2292
+ } else {
2293
+ this.tts.end(id);
2294
+ }
2279
2295
  }
2280
2296
  }
2281
2297
 
package/lib/edge-tts.js CHANGED
@@ -68,6 +68,146 @@ function ssmlMessage(text, voice, requestId) {
68
68
  return `X-RequestId:${requestId}\r\nContent-Type:application/ssml+xml\r\nX-Timestamp:${stamp()}\r\nPath:ssml\r\n\r\n${ssml(text, voice)}`;
69
69
  }
70
70
 
71
+
72
+ /**
73
+ * 流式会话:一条 WebSocket 连续合成多段文本。
74
+ * 每段合成完(turn.end)回调一次完整 mp3;段与段共用连接,省掉 1 秒左右的握手。
75
+ */
76
+ export function createEdgeStreamSession({ log, voice, onSegment, onError, onDone }) {
77
+ const segments = [];
78
+ let ws = null;
79
+ let connecting = null;
80
+ let busy = false;
81
+ let closed = false;
82
+ let finished = false;
83
+ let buffer = [];
84
+ let turnEnd = null;
85
+
86
+ async function ensureWs() {
87
+ if (ws) return ws;
88
+ if (connecting) return connecting;
89
+ connecting = (async () => {
90
+ const requestId = randomUUID();
91
+ const ticks = Math.floor(Date.now() / 1000) + 11644473600;
92
+ const rounded = ticks - (ticks % 300);
93
+ let secMsGec = '';
94
+ try {
95
+ secMsGec = await hashSecMsGecV2(`${rounded * 10000000}${TOKEN}`);
96
+ } catch (err) {
97
+ log?.(`[edge-tts] GEC 计算失败: ${err?.message ?? err}`);
98
+ }
99
+ const url = `${BASE}?TrustedClientToken=${TOKEN}&Sec-MS-GEC=${secMsGec}&Sec-MS-GEC-Version=2-143.0.3650.96&ConnectionId=${requestId}`;
100
+ const socket = new WebSocket(url, {
101
+ headers: {
102
+ Origin: 'chrome-extension://jdiccldimpdaibmpdkjnbmckianbfold',
103
+ Pragma: 'no-cache',
104
+ 'Cache-Control': 'no-cache',
105
+ 'User-Agent':
106
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0',
107
+ },
108
+ });
109
+ await new Promise((resolve, reject) => {
110
+ socket.once('open', resolve);
111
+ socket.once('error', reject);
112
+ });
113
+ socket.send(configMessage());
114
+ socket.on('message', (data, isBinary) => {
115
+ if (!isBinary) {
116
+ const textFrame = data.toString('utf8');
117
+ if (textFrame.includes('Path:turn.end') && turnEnd) {
118
+ const done = turnEnd;
119
+ turnEnd = null;
120
+ done();
121
+ }
122
+ return;
123
+ }
124
+ const raw = Buffer.from(data);
125
+ const headerLength = raw.readUInt16BE(0);
126
+ const header = raw.subarray(2, 2 + headerLength).toString('utf8');
127
+ if (!header.includes('Path:audio')) return;
128
+ const audio = raw.subarray(2 + headerLength);
129
+ if (audio.length > 0) buffer.push(audio);
130
+ });
131
+ socket.on('close', () => {
132
+ ws = null;
133
+ connecting = null;
134
+ if (turnEnd) {
135
+ const done = turnEnd;
136
+ turnEnd = null;
137
+ done();
138
+ }
139
+ if (!closed) onError?.('微软语音连接中断');
140
+ });
141
+ socket.on('error', (err) => {
142
+ log?.(`[edge-tts] 流式连接异常: ${err?.message ?? err}`);
143
+ });
144
+ ws = socket;
145
+ return socket;
146
+ })();
147
+ try {
148
+ return await connecting;
149
+ } finally {
150
+ connecting = null;
151
+ }
152
+ }
153
+
154
+ async function pump() {
155
+ if (busy || closed) return;
156
+ const text = segments.shift();
157
+ if (text == null) {
158
+ if (finished) onDone?.();
159
+ return;
160
+ }
161
+ busy = true;
162
+ try {
163
+ const socket = await ensureWs();
164
+ buffer = [];
165
+ socket.send(ssmlMessage(text, voiceOf(voice), randomUUID()));
166
+ await new Promise((resolve) => {
167
+ turnEnd = resolve;
168
+ setTimeout(() => {
169
+ if (turnEnd === resolve) {
170
+ turnEnd = null;
171
+ resolve();
172
+ }
173
+ }, 60_000);
174
+ });
175
+ const bytes = Buffer.concat(buffer);
176
+ buffer = [];
177
+ if (bytes.length > 0) onSegment?.(bytes);
178
+ else onError?.('语音服务没有返回音频');
179
+ } catch (err) {
180
+ onError?.(`语音合成失败:${err?.message ?? err}`);
181
+ } finally {
182
+ busy = false;
183
+ }
184
+ pump();
185
+ }
186
+
187
+ return {
188
+ send(text) {
189
+ const body = String(text ?? '');
190
+ if (body.trim() === '' || closed) return;
191
+ segments.push(body);
192
+ pump();
193
+ },
194
+ finish() {
195
+ finished = true;
196
+ pump();
197
+ },
198
+ close() {
199
+ closed = true;
200
+ segments.length = 0;
201
+ try {
202
+ ws?.close();
203
+ } catch {
204
+ /* 已关闭 */
205
+ }
206
+ ws = null;
207
+ },
208
+ };
209
+ }
210
+
71
211
  export function createEdgeTts({ log }) {
72
212
  const sessions = new Map();
73
213
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jbc-dssi",
3
- "version": "1.1.164",
3
+ "version": "1.1.165",
4
4
  "type": "module",
5
5
  "description": "鲸聊 - 不只是手机遥控操作电脑,而是将电脑级的Agent能力装进手机",
6
6
  "keywords": [
@@ -1,79 +0,0 @@
1
- export function createEdgeStream({ edge, send }) {
2
- const sessions = new Map();
3
-
4
- function open(id, voice) {
5
- sessions.set(id, { voice, queue: [], busy: false, done: false, cancelled: false });
6
- }
7
-
8
- function has(id) {
9
- return sessions.has(id);
10
- }
11
-
12
- function text(id, text) {
13
- const s = sessions.get(id);
14
- if (!s || s.cancelled) return;
15
- const body = typeof text === 'string' ? text : '';
16
- if (body.trim() === '') return;
17
- s.queue.push(body);
18
- pump(id);
19
- }
20
-
21
- function end(id) {
22
- const s = sessions.get(id);
23
- if (!s) return;
24
- s.done = true;
25
- if (!s.busy && s.queue.length === 0) finish(id);
26
- }
27
-
28
- function finish(id) {
29
- const s = sessions.get(id);
30
- if (!s) return;
31
- sessions.delete(id);
32
- if (!s.cancelled) send({ id, done: true, codec: 'mp3' });
33
- }
34
-
35
- async function pump(id) {
36
- const s = sessions.get(id);
37
- if (!s || s.busy || s.cancelled) return;
38
- s.busy = true;
39
- try {
40
- while (s.queue.length > 0 && !s.cancelled) {
41
- const body = s.queue.shift();
42
- const result = await edge.synthesize({
43
- id,
44
- text: body,
45
- voice: s.voice,
46
- onChunk: (frame) => {
47
- if (!s.cancelled) send(frame);
48
- },
49
- });
50
- if (!result?.ok) {
51
- const current = sessions.get(id);
52
- if (current) {
53
- sessions.delete(id);
54
- send({ id, error: { code: 'edge', message: result?.message ?? '语音合成失败' } });
55
- }
56
- return;
57
- }
58
- }
59
- } finally {
60
- s.busy = false;
61
- }
62
- if (s.done && s.queue.length === 0) finish(id);
63
- }
64
-
65
- function stop(id) {
66
- const s = sessions.get(id);
67
- if (!s) return;
68
- s.cancelled = true;
69
- s.queue.length = 0;
70
- sessions.delete(id);
71
- edge.stop(id);
72
- }
73
-
74
- function stopAll() {
75
- for (const id of [...sessions.keys()]) stop(id);
76
- }
77
-
78
- return { open, has, text, end, stop, stopAll };
79
- }