Adding Event Listeners
After initializing the Anam client, you can register event listeners using theaddListener method:
Available Events
| Event Name | Description |
|---|---|
CONNECTION_ESTABLISHED | Called when the direct connection between the browser and the Anam Engine has been established |
SESSION_READY | Called after the session initialises and all the backend components are ready. Useful for removing loading indicators |
CONNECTION_CLOSED | Called when the direct connection between the browser and the Anam Engine has been closed |
VIDEO_PLAY_STARTED | Fired when the first frames start playing during video streaming. |
MESSAGE_HISTORY_UPDATED | Called with the message history transcription each time the user or persona finishes speaking |
MESSAGE_STREAM_EVENT_RECEIVED | For persona speech: Updated with each transcribed chunk as the persona speaks For user speech: Updated with complete transcription once they finish speaking |
INPUT_AUDIO_STREAM_STARTED | Called with the user’s input audio stream when microphone input has been initialized |
TALK_STREAM_INTERRUPTED | Called when the user interrupts a TalkMessageStream by speaking. Includes the interrupted stream’s correlationId |
TOOL_CALL | Called when the AI persona invokes a client tool. Includes the tool name and arguments for handling UI actions like navigation or modal displays |
Example Usage
Loading States
Use connection events to manage loading states:Message History
Track conversation history:Real-time Transcription
Monitor speech in real-time:Client Tool Events
Handle UI actions triggered by the AI persona:Tool Call Events (Detailed Guide)
Client tools enable your AI persona to trigger events in your client application - like navigating to pages, opening modals, updating UI state, or controlling media playback. When the LLM invokes a client tool during a conversation, the Anam SDK emits aTOOL_CALL event that you can handle in your application code.
Complete Example
Use theaddListener method to register a handler for tool call events. The event name is provided by the AnamEvent enum, and the payload is typed with ClientToolEvent.
Event Payload
TheTOOL_CALL event provides a payload object with the following structure:
The
name of the client tool that was invoked.An object containing the parameters and their values as extracted by the LLM from the conversation. The structure of this object matches the
parameters JSON schema you defined for the tool.event payload would look like this:
Removing Event Listeners
It’s important to clean up listeners to prevent memory leaks, especially in single-page applications. Use theremoveListener method. This is often done in a cleanup function, such as React’s useEffect hook.
Best Practices
Use a switch statement
Use a switch statement
A
switch statement is a clean way to handle different tool calls. Always include a default case to log unhandled tools.Validate arguments
Validate arguments
Never trust the arguments from a tool call without validation. Even though the LLM tries to provide correct data, it can sometimes make mistakes. Sanitize and validate all inputs before performing actions.
Provide user feedback
Provide user feedback
After handling a tool call, provide some form of visual feedback in your UI to confirm the action was performed (e.g., a toast notification, a loading spinner, or the navigation itself).
Handle potential errors
Handle potential errors
Wrap your tool handling logic in
try...catch blocks to gracefully handle any errors that occur during execution.
