The sendUserMessage() method sends text messages on behalf of the user programmatically. Use it to send contextual information or trigger specific persona responses without actual user input.
Messages sent via sendUserMessage() are not automatically added to the
transcript. To maintain an accurate conversation history, you must manually
add these messages to your transcript display.
The sendUserMessage() method differs from regular user messages in that:
It does not trigger message events that would normally update your UI
The message is sent directly to the persona without going through the normal message pipeline
You need to handle transcript updates separately in your application
Provide contextual information about user actions that the LLM can respond to:
Copy
Ask AI
// Inform the AI about user actionsanamClient.sendUserMessage( "Note to AI: I just clicked the checkout button");// The persona might respond with something like:// "Thanks for shopping with us today! Let me help you complete your purchase."
When providing context to the LLM, prefix your messages with “Note to AI:” or
similar to make the intent clear. This helps the LLM understand that it’s
receiving contextual information rather than a direct user question.
// Trigger a specific conversation flowanamClient.sendUserMessage("I want to learn about your pricing plans");// Or provide more detailed contextanamClient.sendUserMessage( "Note to AI: User navigated to pricing page and spent 30 seconds reading");
Use sendUserMessage() to implement your own client-side speech-to-text transcription. Capture and transcribe audio using your preferred service, then send the transcribed messages directly:
Copy
Ask AI
// Example using a custom transcription serviceasync function handleCustomTranscription(audioStream) { // Use your preferred transcription service (e.g., Web Speech API, Whisper, etc.) const transcribedText = await customTranscriptionService.transcribe(audioStream); // Send the transcribed message to the persona anamClient.sendUserMessage(transcribedText); // Update your UI transcript addMessageToTranscript(transcribedText, "user");}
With this approach you can use specialized transcription models, handle multiple
languages, or implement custom preprocessing of the audio before sending it to the persona.
Here’s a complete example showing how to use sendUserMessage() with proper transcript management:
Copy
Ask AI
import { createClient } from "@anam-ai/js-sdk";// Initialize the clientconst anamClient = createClient(sessionToken);await anamClient.streamToVideoElement("video-element-id");// Function to update your UI transcriptfunction addMessageToTranscript(message, sender) { const transcript = document.getElementById("transcript"); const messageElement = document.createElement("div"); messageElement.className = sender === "user" ? "user-message" : "ai-message"; messageElement.textContent = message; transcript.appendChild(messageElement);}// Send a message and update the transcriptfunction sendMessage(message) { // Add to transcript manually since sendUserMessage doesn't trigger events addMessageToTranscript(message, "user"); // Send the message to the persona anamClient.sendUserMessage(message);}// Example usagesendMessage("What products do you recommend?");// Provide context about user actionssendMessage( "Note to AI: User added item to cart with ID product_123");
The sendUserMessage() method throws errors in two scenarios:
Copy
Ask AI
// Error: Not currently streamingtry { anamClient.sendUserMessage("Hello");} catch (error) { // "Failed to send user message: not currently streaming"}// Error: No active sessiontry { anamClient.sendUserMessage("Hello");} catch (error) { // "Failed to send user message: no active session"}
Always ensure the client is streaming before calling this method:
Copy
Ask AI
if (anamClient.isStreaming()) { anamClient.sendUserMessage("Hello");}