Overview
The Agent Web SDK is a TypeScript SDK that facilitates real-time, bi-directional audio conversations with your PlayAI 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 PlayAI Agent into a
Flutter app, check out our Flutter SDK. We plan to support other platforms in
the future.
Installation
Create agent
To start a conversation with your agent, first create an agent in PlayAI 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 theconnectAgent
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 optionalConnectAgentConfig
object as the second parameter to connectAgent
.
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 activeAgentConnectionController
from connectAgent
, you can mute or unmute the user’s microphone:
Hangup
UseagentController.hangup()
to end the conversation from the user side.
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
connectAgent
Promise. You can handle these errors in the catch
block.
Errors that occur during the conversation are caught by the onError
listener.