Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions __tests__/speaking-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import Vapi from "../vapi";

jest.mock("@daily-co/daily-js", () => ({
__esModule: true,
default: { createCallObject: () => null },
}));

jest.mock("../client", () => ({
client: { baseUrl: "", setSecurityData: () => {}, call: {} },
}));

const speaking = { participantsAudioLevel: { assistant: 0.1 } } as any;

describe("speech-end timer", () => {
let vapi: Vapi;
let events: string[];

beforeEach(() => {
jest.useFakeTimers();
vapi = new Vapi("dummy_token");
vapi["call"] = { destroy: jest.fn().mockResolvedValue(undefined) } as any;
events = [];
vapi.on("speech-start", () => events.push("speech-start"));
vapi.on("speech-end", () => events.push("speech-end"));
});

afterEach(() => {
jest.useRealTimers();
});

it("emits speech-end once the assistant stops speaking", () => {
vapi["handleRemoteParticipantsAudioLevel"](speaking);
jest.advanceTimersByTime(1000);
expect(events).toEqual(["speech-start", "speech-end"]);
});

it("does not fire a stale speech-end into the next call after stop()", async () => {
vapi["handleRemoteParticipantsAudioLevel"](speaking);
await vapi.stop();
events.length = 0;

// Next call: the assistant starts speaking 500 ms later.
jest.advanceTimersByTime(500);
vapi["handleRemoteParticipantsAudioLevel"](speaking);
jest.advanceTimersByTime(600);

// The old timer must not end the new call's speech early.
expect(events).toEqual(["speech-start"]);
jest.advanceTimersByTime(500);
expect(events).toEqual(["speech-start", "speech-end"]);
});

it("emits speech-end when the call is torn down while the assistant speaks", async () => {
vapi["handleRemoteParticipantsAudioLevel"](speaking);
events.length = 0;
await vapi.stop();
expect(events).toEqual(["speech-end"]);
jest.advanceTimersByTime(5000);
expect(events).toEqual(["speech-end"]);
});
});
17 changes: 15 additions & 2 deletions vapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ export default class Vapi extends VapiEventEmitter {
}
this.audioPlayer = null;
this.pendingAudioBuilds.clear();
this.speakingTimeout = null;
this.endSpeaking();
}

private isMobileDevice() {
Expand Down Expand Up @@ -1081,6 +1081,19 @@ export default class Vapi extends VapiEventEmitter {
}, 1000);
}

/**
* Cancels the pending speech-end timer. If the assistant was mid-speech,
* emits speech-end now, so it is not fired later into the next call.
*/
private endSpeaking() {
if (!this.speakingTimeout) {
return;
}
clearTimeout(this.speakingTimeout);
this.speakingTimeout = null;
this.emit('speech-end');
}

private handleLocalAudioLevel(e: DailyEventObjectLocalAudioLevel) {
this.emit('local-volume-level', e.audioLevel);
}
Expand All @@ -1099,7 +1112,7 @@ export default class Vapi extends VapiEventEmitter {
}
this.audioPlayer = null;
this.pendingAudioBuilds.clear();
this.speakingTimeout = null;
this.endSpeaking();
}

/**
Expand Down