jbc-dssi 1.1.149 → 1.1.151

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/lib/ogg-opus.js CHANGED
@@ -67,7 +67,36 @@ const OPUS_TAGS = (() => {
67
67
  return b;
68
68
  })();
69
69
 
70
- export function createOggOpusStream({ samplesPerPacket = 960 } = {}) {
70
+ const FRAME_MS = [
71
+ 10, 20, 40, 60,
72
+ 10, 20, 40, 60,
73
+ 10, 20, 40, 60,
74
+ 10, 20,
75
+ 10, 20,
76
+ 2.5, 5, 10, 20,
77
+ 2.5, 5, 10, 20,
78
+ 2.5, 5, 10, 20,
79
+ 2.5, 5, 10, 20,
80
+ ];
81
+
82
+ function packetSamples(packet) {
83
+ if (!packet || packet.length < 1) return 960;
84
+ const toc = packet[0];
85
+ const config = toc >> 3;
86
+ const code = toc & 0x03;
87
+ const ms = FRAME_MS[config] ?? 20;
88
+ let frames = 1;
89
+ if (code === 1) frames = 2;
90
+ else if (code === 2) frames = 2;
91
+ else if (code === 3) {
92
+ if (packet.length < 2) return 960;
93
+ frames = packet[1] & 0x3f;
94
+ if (frames <= 0) frames = 1;
95
+ }
96
+ return Math.round((ms * frames * 48));
97
+ }
98
+
99
+ export function createOggOpusStream() {
71
100
  const serial = Math.floor(Math.random() * 0x7fffffff);
72
101
  let sequence = 0;
73
102
  let granule = 0;
@@ -80,7 +109,7 @@ export function createOggOpusStream({ samplesPerPacket = 960 } = {}) {
80
109
  }
81
110
 
82
111
  function packet(data) {
83
- granule += samplesPerPacket;
112
+ granule += packetSamples(data);
84
113
  pages.push(page({ headerType: 0x00, granule, serial, sequence: sequence++, packets: [data] }));
85
114
  return drain();
86
115
  }
package/lib/tts.js CHANGED
@@ -41,8 +41,18 @@ export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket })
41
41
  onError?.({ id, error: normalizeError('busy') });
42
42
  return;
43
43
  }
44
+ const early = [];
45
+ let earlyEnd = false;
46
+ sessions.set(id, {
47
+ early,
48
+ sendText: (text) => early.push(text),
49
+ end: () => {
50
+ earlyEnd = true;
51
+ },
52
+ });
44
53
  const key = await resolveKey();
45
54
  if (!key) {
55
+ sessions.delete(id);
46
56
  onError?.({ id, error: normalizeError('no_key') });
47
57
  return;
48
58
  }
@@ -51,12 +61,14 @@ export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket })
51
61
  try {
52
62
  ws = new WebSocketImpl(url, { headers: { Authorization: `Bearer ${key}` } });
53
63
  } catch (err) {
64
+ sessions.delete(id);
54
65
  onError?.({ id, error: normalizeError('network', `语音连接失败:${err?.message ?? err}`) });
55
66
  return;
56
67
  }
57
68
 
58
69
  const mux = createOggOpusStream();
59
70
  const session = {
71
+ pendingFinish: earlyEnd,
60
72
  ws,
61
73
  mux,
62
74
  settled: false,
@@ -173,7 +185,8 @@ export function createTtsEngine({ log, resolveKey, WebSocketImpl = WebSocket })
173
185
  function end(id) {
174
186
  const session = sessions.get(id);
175
187
  if (!session) return;
176
- session.finish();
188
+ if (typeof session.finish === 'function') session.finish();
189
+ else if (typeof session.end === 'function') session.end();
177
190
  }
178
191
 
179
192
  function stop(id) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jbc-dssi",
3
- "version": "1.1.149",
3
+ "version": "1.1.151",
4
4
  "type": "module",
5
5
  "description": "鲸聊 - 不只是手机遥控操作电脑,而是将电脑级的Agent能力装进手机",
6
6
  "keywords": [