Messaging
Send messages and commands to the agent from your code
Voice is the primary way users talk to the agent — but your code can message it too. This page covers the SDK's sending surface: injecting messages into the conversation and driving the session with typed commands. For what you receive, see Events.
Send a message
Inject text as the user, or as silent system context
Speak a line
Send talk in puppeteer mode
Command reference
Every command sendCommand accepts
From your backend
Injecting context server-side
Send a message
Use sendCommand with a SEND_MESSAGE command. Commands are typed as a union keyed by type, using the exported DataChannelMessageType enum.
Send commands once the session is live: wait for onAvatarReady(true), which fires after the data channel opens. While the channel is closed, or while the inactivity warning is showing, the SDK drops commands without an error.
import { DataChannelMessageType } from "@touchcastllc/napster-companion-api";
// Send a text message as the user and trigger a response
instance.sendCommand({
type: DataChannelMessageType.SEND_MESSAGE,
data: {
text: "Hello",
role: "user",
trigger_response: true,
},
});The role decides how the message enters the conversation:
"user": treated as if the user typed it. Pair withtrigger_response: trueso the agent answers. If the agent is in the middle of a reply, this interrupts it; setdelay: trueto wait for the reply to finish instead."system": context for the agent rather than words from the user. Withtrigger_response: false, the agent takes in the information silently and uses it when relevant. Use it to feed page state or app events into the conversation ("The user just opened the billing page"). Withtrigger_response: true, the agent responds to it.
// Silent context: inform the agent without triggering a spoken response
instance.sendCommand({
type: DataChannelMessageType.SEND_MESSAGE,
data: {
text: "The user just opened the billing page.",
role: "system",
trigger_response: false,
},
});Message fields
The package doesn't export a type for these fields, so write the data object inline.
| Field | Type | Required | Description |
|---|---|---|---|
text | string | Yes | The message text. |
role | "user" | "system" | Yes | Who the message is from. |
trigger_response | boolean | Yes | Whether the agent should respond. |
previous_item_id | string | No | The conversation item to insert this message after. Leave it out to add the message at the end. Realtime architecture only. |
delay | boolean | No | With trigger_response: true, wait for the agent's current reply to finish instead of interrupting it. Ignored when trigger_response is false. Defaults to false. |
Speak a line in puppeteer mode
In puppeteer mode, the agent doesn't listen or reply. It speaks only the lines you send with talk:
import type { NapsterCompanionApiInstance } from "@touchcastllc/napster-companion-api";
type Command = Parameters<NapsterCompanionApiInstance["sendCommand"]>[0];
instance.sendCommand({
type: "talk",
data: {
talkId: "intro-1",
text: "Welcome to Acme. Let me show you around the new dashboard.",
},
} as unknown as Command);talk isn't in the typed command union yet, so TypeScript needs the cast. The package doesn't export the union type itself, so the example derives it from sendCommand. In plain JavaScript, leave the cast out. Track the line with talk_state_changed events in onData, and stop it with stopAvatarTalking(). See the Puppeteer mode guide for the full setup.
Command reference
The typed command union covers these commands. Others the server accepts, such as talk and send_mcp_approval, need a cast like the one above.
DataChannelMessageType | Value | Payload | Purpose |
|---|---|---|---|
SEND_MESSAGE | "send_message" | Message fields | Send a text message into the conversation. |
CANCEL | "cancel" | — | Interrupt the agent's current response. |
SET_SETTINGS | "set_settings" | { modality?, inline_functions?, … } | Update session settings. The typed payload covers modality and inline_functions, which replaces any tools registered this way earlier. For instructions, temperature, or turn_detection, cast the command as in the talk example. See set_settings. |
START_VIDEO | "start_video" | null | Tell the agent your screen-share frames are starting. Sent for you by startScreenShare(). |
STOP_VIDEO | "stop_video" | null | Tell the agent screen sharing stopped. Sent for you by stopScreenShare(). |
FUNCTION_CALL_OUTPUT | "send_function_output" | { call_id, output, delay? } | Return the result of a client-side tool call, using the call_id from the call. output can be any JSON value. Without delay: true, the output interrupts the agent's current reply. Outputs that arrive after the tool-call timeout are ignored. |
Most integrations don't send raw commands — the SDK sends start_video / stop_video for screen sharing, and stopAvatarTalking() wraps cancel. Reach for sendCommand when you need to inject a message or manage settings directly. For the protocol as a whole — the same commands over both WebRTC and WebSocket — see Client Commands.
From your backend
Your client isn't the only side that can put words into the conversation. If you use WebSocket-based explicit tools, your functions server can add context server-side with context_update, the backend counterpart of send_message. It's typically used to deliver deferred tool results. See Updating the conversation context.