Skip to content

Commit c3d95c2

Browse files
authored
fix(xai): preserve transcription audio on reopen (#130297)
1 parent a868258 commit c3d95c2

2 files changed

Lines changed: 56 additions & 19 deletions

File tree

‎extensions/xai/lazy-capability-providers.test.ts‎

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ describe("xAI lazy capability providers", () => {
228228
third,
229229
]);
230230
expect(runtimeMocks.transcriptionConnect).toHaveBeenCalledOnce();
231-
expect(runtimeMocks.transcriptionSendAudio.mock.invocationCallOrder.at(-1)).toBeLessThan(
232-
runtimeMocks.transcriptionConnect.mock.invocationCallOrder[0]!,
231+
expect(runtimeMocks.transcriptionConnect.mock.invocationCallOrder[0]).toBeLessThan(
232+
runtimeMocks.transcriptionSendAudio.mock.invocationCallOrder[0]!,
233233
);
234234
});
235235

@@ -250,30 +250,68 @@ describe("xAI lazy capability providers", () => {
250250
});
251251

252252
it("reopens transcription after close without replaying discarded audio", async () => {
253+
const reconnecting = createDeferred<void>();
254+
const forwarded: Buffer[] = [];
255+
const events: string[] = [];
256+
let connectCount = 0;
257+
let providerClosed = false;
258+
runtimeMocks.transcriptionConnect.mockImplementation(() => {
259+
providerClosed = false;
260+
connectCount += 1;
261+
if (connectCount === 1) {
262+
return Promise.resolve();
263+
}
264+
events.push("connect-start");
265+
return reconnecting.promise.then(() => {
266+
events.push("connect-settle");
267+
});
268+
});
269+
runtimeMocks.transcriptionClose.mockImplementation(() => {
270+
providerClosed = true;
271+
});
272+
runtimeMocks.transcriptionSendAudio.mockImplementation((audio: Buffer) => {
273+
events.push(`${providerClosed ? "drop" : "audio"}:${audio[0]}`);
274+
if (!providerClosed) {
275+
forwarded.push(audio);
276+
}
277+
});
253278
const lazy = await loadLazyProviders();
254279
const session = lazy.createLazyXaiRealtimeTranscriptionProvider().createSession({
255280
providerConfig: {},
256281
});
257282
const first = Buffer.from([0x01]);
258283
const discarded = Buffer.from([0x02]);
259-
const second = Buffer.from([0x03]);
284+
const droppedByLimit = Buffer.alloc(1024 * 1024, 0x03);
285+
const retainedFirst = Buffer.alloc(1024 * 1024, 0x04);
286+
const retainedSecond = Buffer.alloc(1024 * 1024, 0x05);
287+
const live = Buffer.from([0x06]);
260288

261289
session.sendAudio(first);
262290
await session.connect();
263291
session.close();
264292
session.close();
265293
session.sendAudio(discarded);
294+
events.length = 0;
266295

267296
const reconnectPromise = session.connect();
268-
session.sendAudio(second);
269-
await reconnectPromise;
297+
session.sendAudio(droppedByLimit);
298+
session.sendAudio(retainedFirst);
299+
session.sendAudio(retainedSecond);
300+
await vi.waitFor(() => expect(runtimeMocks.transcriptionConnect).toHaveBeenCalledTimes(2));
301+
session.sendAudio(live);
270302

271-
expect(runtimeMocks.transcriptionConnect).toHaveBeenCalledTimes(2);
272303
expect(runtimeMocks.transcriptionClose).toHaveBeenCalledOnce();
273-
expect(runtimeMocks.transcriptionSendAudio.mock.calls.map(([audio]) => audio)).toEqual([
274-
first,
275-
second,
304+
expect(forwarded.map((audio) => [audio[0], audio.byteLength])).toEqual([
305+
[1, 1],
306+
[4, 1024 * 1024],
307+
[5, 1024 * 1024],
308+
[6, 1],
276309
]);
310+
expect(events).toEqual(["connect-start", "audio:4", "audio:5", "audio:6"]);
311+
312+
reconnecting.resolve();
313+
await reconnectPromise;
314+
expect(events.at(-1)).toBe("connect-settle");
277315
});
278316

279317
it("preserves voice startup ordering and waits to trigger the greeting", async () => {

‎extensions/xai/lazy-capability-providers.ts‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,17 +135,13 @@ function createLazyXaiRealtimeTranscriptionSession(
135135
session = await sessionPromise;
136136
return session;
137137
};
138-
const beginConnectGeneration = () => {
139-
if (closed) {
140-
generation += 1;
141-
closed = false;
142-
}
143-
return generation;
144-
};
145-
146138
return {
147139
connect: async () => {
148-
const connectGeneration = beginConnectGeneration();
140+
if (closed) {
141+
generation += 1;
142+
closed = false;
143+
}
144+
const connectGeneration = generation;
149145
if (activeConnect?.generation === connectGeneration) {
150146
await activeConnect.promise;
151147
return;
@@ -158,11 +154,14 @@ function createLazyXaiRealtimeTranscriptionSession(
158154
}
159155
return;
160156
}
157+
// connect() synchronously reopens a closed provider session. Starting it
158+
// first keeps explicit-reconnect audio from being silently discarded.
159+
const providerConnect = loadedSession.connect();
161160
for (const audio of pendingAudio.drain()) {
162161
loadedSession.sendAudio(audio);
163162
}
164163
acceptsInput = true;
165-
await loadedSession.connect();
164+
await providerConnect;
166165
if (connectGeneration === generation && closed) {
167166
closeSession(connectGeneration, loadedSession);
168167
}

0 commit comments

Comments
 (0)