Overview
You can send text messages to the AI agent programmatically, either by sending events directly or using the convenient sendText method.
Using sendText Method
The React SDK provides a convenient sendText method that handles both events for you:
sendText will generate a response only if the model is currently not generating anything.
Options
The sendText method accepts an optional second parameter with these options:
| Option | Type | Default | Description |
|---|
cancelOngoing | boolean | false | Cancel any ongoing response generation before sending the new text |
Cancel Ongoing Responses
When the AI is generating a response, you can cancel it and send a new message:
Using Events
To send a text message, you need to send two events:
Example Usage
Important Notes
Concurrent sendText Calls
By default, sendText will generate a response only if the model is currently not generating anything.
If you call sendText while the AI is responding, you have two options:
Option 1: Handle the error (default behavior)
Option 2: Cancel ongoing response
Error Response
If you don’t use cancelOngoing: true, simultaneous calls will fail with this error: