Web SDK
Integrate Play.ai Agent into your web application
Overview
The Agent Web SDK is a TypeScript SDK that facilitates real-time, bi-directional audio conversations with your Play.ai Agent via WebSocket API. It takes care of the following:
- WebSocket connection management
- Microphone capture and voice activity detection (VAD)
- Sending user audio to the Agent
- Receiving Agent audio and playing it back in the browser
- Managing event listeners such as user or agent transcripts
- Muting/unmuting the user’s microphone
- Hanging up (ending) the agent conversation
- Error handling
This SDK is designed for modern web browsers that support the Web Audio API and WebSockets. If you want to integrate Play.ai Agent into a mobile app or a different platform, please refer to the WebSocket API documentation.
Installation
Create agent
To start a conversation with your agent, first create an agent in Play.ai app. Once you have an agent, you can find the agent ID in the agent “Deploy · Web” section, which is required to connect to the agent.
Basic usage
Below is a simple example illustrating how to initiate a conversation with your agent using the connectAgent
function:
The function connectAgent
returns a Promise
- If any error occurs during the connection process, the Promise is rejected.
- When the conversation is successfully established, the Promise resolves to
AgentConnectionController
object.
Config
You can customize the agent’s configuration by passing an optional ConnectAgentConfig
object as the second parameter to connectAgent
.
Config Options:
debug
: Enables debug logging for troubleshooting.customGreeting
: Overrides the default greeting used by the agent.prompt
: Appends additional instructions to the agent’s core prompt.continueConversation
: An optional conversation ID to continue a previous conversation.listeners
: Attach various listener callbacks (see Event listeners section).
Event listeners
Event listeners enable you to handle specific moments during the conversation:
Mute/unmute
Once you have an active AgentConnectionController
from connectAgent
, you can mute or unmute the user’s microphone:
Hangup
Use agentController.hangup()
to end the conversation from the user side.
When the conversation ends (either by user or agent), the onHangup
callback (if provided) is triggered.
Error handling
Errors can occur at different stages of the conversation:
- Starting the conversation. For example:
- Microphone permissions denied
- WebSocket fails to connect or closes unexpectedly
- Invalid agent ID
- During the conversation. For example:
- Agent fails to generate a response
- Internal Agent errors
- Network issues
Errors that occur before the conversation starts are caught by the connectAgent
Promise. You can handle these errors in the catch
block.
Errors that occur during the conversation are caught by the onError
listener.
Error object: