Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Live calls (ElevenLabs): caption corrections; end even the first answer at agent_response_complete

Bugfixer 28b9beceafcac
diff --git a/src/lib/live-call-elevenlabs.test.ts b/src/lib/live-call-elevenlabs.test.ts
index 830d6239f4..2406f91462 100644
--- a/src/lib/live-call-elevenlabs.test.ts
+++ b/src/lib/live-call-elevenlabs.test.ts
@@ -77,8 +77,6 @@ function harness({ denied = false, held = false } = {}) {
const AUDIO = btoa(String.fromCharCode(1, 0, 2, 0));
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
-/** Longer than the pause that ends the agent's turn. */
-const pause = () => new Promise((resolve) => setTimeout(resolve, 350));
describe("an ElevenLabs Live session", () => {
it("opens the signed URL, sends the call's instructions first, and joins once ElevenLabs and the microphone are ready", async () => {
@@ -150,7 +148,7 @@ describe("an ElevenLabs Live session", () => {
expect(t.audio().played).toEqual([4]);
expect(t.callbacks.onModeChange).toHaveBeenLastCalledWith("speaking");
t.audio().finish();
- await pause();
+ t.socket().receive({ type: "agent_response_complete" });
expect(t.callbacks.onModeChange).toHaveBeenLastCalledWith("listening");
t.socket().receive({ type: "audio", audio_event: { audio_base_64: AUDIO, event_id: 2 } });
t.socket().receive({ type: "interruption", interruption_event: { event_id: 3 } });
@@ -163,20 +161,40 @@ describe("an ElevenLabs Live session", () => {
expect(t.audio().played).toEqual([4, 4, 4]);
});
- it("keeps speaking through a short gap between the agent's audio chunks", async () => {
+ it("keeps speaking through a gap in the agent's first answer, before any agent_response_complete", async () => {
const t = harness();
const session = await t.connect();
- t.socket().receive({ type: "audio", audio_event: { audio_base_64: AUDIO, event_id: 1 } });
- session.sendUserMessage("[OpenMausBot] You have two new emails.");
- t.audio().finish();
- t.socket().receive({ type: "audio", audio_event: { audio_base_64: AUDIO, event_id: 1 } });
- await pause();
- expect(t.callbacks.onModeChange).not.toHaveBeenCalledWith("listening");
- expect(t.socket().sent.map((m) => m.type)).not.toContain("user_message");
- t.audio().finish();
- await pause();
- expect(t.callbacks.onModeChange).toHaveBeenLastCalledWith("listening");
- expect(t.socket().sent.at(-1)).toEqual({ type: "user_message", text: "[OpenMausBot] You have two new emails." });
+ vi.useFakeTimers();
+ try {
+ t.socket().receive({ type: "audio", audio_event: { audio_base_64: AUDIO, event_id: 1 } });
+ session.sendUserMessage("[OpenMausBot] You have two new emails.");
+ t.audio().finish();
+ vi.advanceTimersByTime(1_000);
+ t.socket().receive({ type: "audio", audio_event: { audio_base_64: AUDIO, event_id: 1 } });
+ t.audio().finish();
+ vi.advanceTimersByTime(2_000);
+ expect(t.callbacks.onModeChange).not.toHaveBeenCalledWith("listening");
+ expect(t.socket().sent.map((m) => m.type)).not.toContain("user_message");
+ t.socket().receive({ type: "agent_response_complete" });
+ expect(t.callbacks.onModeChange).toHaveBeenLastCalledWith("listening");
+ expect(t.socket().sent.at(-1)).toEqual({ type: "user_message", text: "[OpenMausBot] You have two new emails." });
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it("corrects the agent's caption to what the person heard when they talk over it", async () => {
+ const t = harness();
+ await t.connect();
+ const full = "You have two emails, one from the bank and one from the school.";
+ t.socket().receive({ type: "agent_response", agent_response_event: { agent_response: full } });
+ t.socket().receive({ type: "interruption", interruption_event: { event_id: 2 } });
+ t.socket().receive({ type: "user_transcript", user_transcription_event: { user_transcript: "stop" } });
+ t.socket().receive({
+ type: "agent_response_correction",
+ agent_response_correction_event: { original_agent_response: full, corrected_agent_response: "You have two emails," },
+ });
+ expect(t.callbacks.onAgentResponseDelta).toHaveBeenLastCalledWith({ segmentId: "a0", text: "You have two emails," });
});
it("ends the agent's turn at agent_response_complete once ElevenLabs sends it, not at a long gap in its audio", async () => {
@@ -237,7 +255,7 @@ describe("an ElevenLabs Live session", () => {
session.sendUserMessage("[OpenMausBot] still looking", { audio: false });
expect(t.socket().sent.at(-1)).not.toHaveProperty("type", "contextual_update");
t.audio().finish();
- await pause();
+ t.socket().receive({ type: "agent_response_complete" });
expect(t.socket().sent.slice(-2)).toEqual([
{ type: "user_message", text: "[OpenMausBot] You have two new emails." },
{ type: "contextual_update", text: "[OpenMausBot] still looking" },
diff --git a/src/lib/live-call-elevenlabs.ts b/src/lib/live-call-elevenlabs.ts
index 46df9b1ead..16f94ae538 100644
--- a/src/lib/live-call-elevenlabs.ts
+++ b/src/lib/live-call-elevenlabs.ts
@@ -8,9 +8,8 @@ import { decodeBase64, encodeBase64, type GeminiSocket } from "./live-call-gemin
/** The agent is set up to answer with 16 kHz PCM; the first message names the format. */
const DEFAULT_RATE = 16_000;
-/** An agent set up before `agent_response_complete` was sent ends its turn with a pause in its audio this long. */
-const IDLE_GRACE_MS = 300;
-/** Ends the agent's turn when its audio has stopped this long but no `agent_response_complete` came after it. */
+/** Ends the agent's turn when its audio has stopped this long but no `agent_response_complete` came after it, so a lost
+ * event cannot hold the bot's words back for the rest of the call. */
const TURN_END_WAIT_MS = 3_000;
export interface ElevenLabsSessionDeps {
@@ -32,6 +31,7 @@ type ServerMessage = {
interruption_event?: { event_id?: number };
user_transcription_event?: { user_transcript?: string };
agent_response_event?: { agent_response?: string };
+ agent_response_correction_event?: { corrected_agent_response?: string };
ping_event?: { event_id?: number };
client_tool_call?: { tool_name?: string; tool_call_id?: string; parameters?: Record<string, unknown> };
};
@@ -63,10 +63,10 @@ class ElevenLabsLiveSession implements FishAgentSession {
private segment = 0;
private speaking = false;
private idleTimer: ReturnType<typeof setTimeout> | null = null;
- /** Audio came after the last `agent_response_complete`. */
+ /** Audio came after the last `agent_response_complete`: the agent's turn goes on through a pause in its audio. */
private answering = false;
- /** ElevenLabs sends `agent_response_complete`, so a pause in the audio no longer ends the agent's turn. */
- private turnEnds = false;
+ /** The caption of the agent's latest answer. */
+ private answerSegment = "";
/** Audio of an interrupted answer still on its way is not played. */
private interruptedAt = -1;
@@ -203,11 +203,19 @@ class ElevenLabsLiveSession implements FishAgentSession {
}
case "agent_response": {
const text = message.agent_response_event?.agent_response?.trim();
- if (text) this.options.callbacks.onAgentResponseDelta({ segmentId: `a${this.segment}`, text });
+ if (!text) return;
+ this.answerSegment = `a${this.segment}`;
+ this.options.callbacks.onAgentResponseDelta({ segmentId: this.answerSegment, text });
+ return;
+ }
+ case "agent_response_correction": {
+ // After an interruption ElevenLabs cuts the answer down to what the person heard of it; the person's words may
+ // already be captioned after it, so the correction goes to the answer's own caption.
+ const text = message.agent_response_correction_event?.corrected_agent_response?.trim();
+ if (text && this.answerSegment) this.options.callbacks.onAgentResponseDelta({ segmentId: this.answerSegment, text });
return;
}
case "agent_response_complete":
- this.turnEnds = true;
this.answering = false;
if (this.audio?.idle() !== false) this.playbackPaused();
return;
@@ -224,11 +232,11 @@ class ElevenLabsLiveSession implements FishAgentSession {
private playbackPaused(): void {
this.clearIdleTimer();
- if (this.turnEnds && !this.answering) return this.playbackIdle();
+ if (!this.answering) return this.playbackIdle();
this.idleTimer = setTimeout(() => {
this.idleTimer = null;
this.playbackIdle();
- }, this.turnEnds ? TURN_END_WAIT_MS : IDLE_GRACE_MS);
+ }, TURN_END_WAIT_MS);
}
private clearIdleTimer(): void {