Skip to content

Agent events

Subscribe only to the events that your agent needs. The Agent SDK uses Node’s EventEmitter interface.

EventValue
messageEach received message after middleware
textPlain text
markdownMarkdown text
attachmentRemote attachment
inline-attachmentInline attachment
multi-attachmentMultiple remote attachments
reactionReaction
replyTagged reply; enrichment on the message
read-receiptRead receipt
actionsActions prompt
intentSelected action intent
transaction-referenceTransaction reference
wallet-send-callsWallet call request
group-updateGroup membership or metadata update
leave-requestLeave request
conversationConversation first discovered by this client
dmNew direct message conversation
groupNew group conversation
start, stopAgent lifecycle
unhandledErrorUnhandled error
unknownMessageUnknown or custom content without a built-in event
const agent: Agent<unknown>
agent
.
NodeJS.EventEmitter<EventHandlerMap<unknown>>.on<"text">(eventName: keyof EventEmitterEventMap | "actions" | "attachment" | "conversation" | "group-update" | "dm" | "group" | "inline-attachment" | "intent" | "leave-request" | "markdown" | "message" | "multi-attachment" | "reaction" | "read-receipt" | "reply" | "start" | "stop" | "text" | "transaction-reference" | "unhandledError" | "unknownMessage" | "wallet-send-calls", listener: (ctx: MessageContext<string, unknown>) => void): Agent<unknown>

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

server.on('connection', (stream) => {
console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a

@since ― v0.1.101

@param ― eventName The name of the event.

@param ― listener The callback function

on
("text", async (
ctx: MessageContext<string, unknown>
ctx
) => {
await
ctx: MessageContext<string, unknown>
ctx
.
MessageContext<string, unknown>.sendTextReply(text: string): Promise<void>

Send a text reply to this message with push disabled.

sendTextReply
(`Echo: ${
ctx: MessageContext<string, unknown>
ctx
.
MessageContext<string, unknown>.content: string

Replace the middleware value without changing the SDK message.

The decoded value selected by the content type.

content
}`);
});
const agent: Agent<unknown>
agent
.
NodeJS.EventEmitter<EventHandlerMap<unknown>>.on<"unhandledError">(eventName: keyof EventEmitterEventMap | "actions" | "attachment" | "conversation" | "group-update" | "dm" | "group" | "inline-attachment" | "intent" | "leave-request" | "markdown" | "message" | "multi-attachment" | "reaction" | "read-receipt" | "reply" | "start" | "stop" | "text" | "transaction-reference" | "unhandledError" | "unknownMessage" | "wallet-send-calls", listener: (error: Error) => void): Agent<unknown>

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

server.on('connection', (stream) => {
console.log('someone connected!');
});

Returns a reference to the EventEmitter, so that calls can be chained.

By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the event listener to the beginning of the listeners array.

import { EventEmitter } from 'node:events';
const myEE = new EventEmitter();
myEE.on('foo', () => console.log('a'));
myEE.prependListener('foo', () => console.log('b'));
myEE.emit('foo');
// Prints:
// b
// a

@since ― v0.1.101

@param ― eventName The name of the event.

@param ― listener The callback function

on
("unhandledError", (
error: Error
error
) => {
var console: Console
console
.
Console.error(...data: any[]): void

The console.error() static method outputs a message to the console at the "error" log level. The message is only displayed to the user if the console is configured to display error output. In most cases, the log level is configured within the console UI. The message may be formatted as an error, with red colors and call stack information.

MDN Reference

error
(
error: Error
error
);
});
await
const agent: Agent<unknown>
agent
.
Agent<unknown>.start(options?: AgentStreamingOptions): Promise<void>

Start conversation and message streams. Calling this while running is a no-op.

start
();

The Agent awaits async message and conversation listeners. Return or await all work that must finish before message delivery is accepted. A task that runs without await can continue after acceptance.

Conversation events exclude conversations already stored locally. They include a pending Welcome when the client first discovers its conversation.

start means that local readers are ready. It does not confirm a backend connection. Retryable network errors use the core recovery budget. A terminal stream error stops both readers. Handling the error does not restart them. Call agent.start() to start a new reader generation. Use onConnectionStateChange in the start() options to observe connection state.

Agent message events differ from the SDK client events. Use agent.client to listen for local storage, consent, and message-status changes.