Skip to main content

Overview

The interruptPersona() method allows you to programmatically stop the persona while it’s speaking.
Requires SDK version 3.4.0 or higher

Method Signature

interruptPersona(): void

Basic Usage

Stop the persona from speaking immediately:
anamClient.interruptPersona();
The persona will stop its current speech and be ready to receive new input.

Prerequisites

Before calling interruptPersona(), you must:
  1. Have an active streaming session (call stream() or streamToVideoElement() first)
  2. Have successfully connected to the Anam Engine

Error Handling

The method throws errors in the following cases:
try {
  anamClient.interruptPersona();
} catch (error) {
  // Handle the error
  console.error(error.message);
}
Error MessageCause
Failed to send interrupt command: not currently streamingCalled before starting a stream or after stopping
Failed to send interrupt command: no active sessionNo session ID available

Behavior

The interruptPersona() method:
  • Immediately stops any ongoing speech from the persona
  • The persona remains ready to receive new input after being interrupted
  • Has no effect if the persona is not currently speaking (no error thrown in this case)

Use Cases

User-Initiated Interruption

Allow users to interrupt the persona when they want to ask a different question or change topics:
document.getElementById("interrupt-btn").addEventListener("click", () => {
  try {
    anamClient.interruptPersona();
  } catch (error) {
    console.error("Could not interrupt:", error.message);
  }
});
Voice activity detection automatically interrupts the persona when the user starts speaking. Use this method for programmatic interruption or when you need immediate response to UI actions.

Topic Change Handling

Stop the persona when users navigate to a different section or topic:
function handleNavigationChange(newSection) {
  // Stop any ongoing explanation
  try {
    anamClient.interruptPersona();
  } catch (error) {
    // May fail if not streaming - that's fine for navigation
  }

  // Send context about the new topic
  anamClient.sendUserMessage(
    `Note to AI: User navigated to ${newSection}`
  );
}

Next Steps