Skip to main content

Web toolkit API

The Web toolkit API provides the following classes and methods.

API overview

APIDescription
chatSend chat messages to a conversational agent.
getInstanceGets a singleton instance of ConversationalAIAPI.
initInitializes the ConversationalAIAPI singleton instance.
subscribeMessageSubscribes to the messaging channel to get real-time updates.
unsubscribeUnsubscribes from the message channel and cleans up resources.
interruptSends an interrupt message to the specified agent user.
destroyDestroys the ConversationalAIAPI instance and cleans up resources.

ConversationalAIAPI class

Class for managing conversational AI engine interactions through Agora's RTC and RTM services.

chat

Send chat messages to a conversational agent.

info

Since v1.7


_1
public async chat(agentUserId: string, message: IChatMessageText | IChatMessageImage)

Sample code


_6
// Send an image message
_6
const imageMessage: IChatMessageImage = {
_6
messageType: EChatMessageType.IMAGE,
_6
imageData: urlImageData
_6
};
_6
await api.chat("user123", imageMessage);

ParameterTypeDescription
agentUserIdstringUnique identifier of the agent.
messageIChatMessageText | IChatMessageImageChat message to be sent. Should be of type IChatMessageImage, see details in IChatMessageImage.

Return Value
If the method call succeeds, a Promise is returned, indicating the result of the message sending.

getInstance

Gets a singleton instance of ConversationalAIAPI. This method ensures that only one instance exists throughout the entire application lifecycle.


_1
public static getInstance()

info
  • You must call init before using this method.
  • If not initialized, an error is thrown.

Return Value: If the method call succeeds, a ConversationalAIAPI instance is returned. If not initialized, a NotFoundError exception is thrown.

init

Initializes the ConversationalAIAPI singleton instance.


_1
public static init(cfg: IConversationalAIAPIConfig)

This method sets the RTC and RTM engines, rendering mode, and logging options. You must call this method before calling other methods of ConversationalAIAPI.

info
  • Only one instance can be initialized at a time.
  • If already initialized, an error is thrown.
ParameterTypeDescription
cfgIConversationalAIAPIConfigConfiguration object used to initialize the API. See IConversationalAIAPIConfig.

Return Value: If the method call succeeds, a ConversationalAIAPI instance is returned.

subscribeMessage

Subscribes to the messaging channel to get real-time updates.


_1
public subscribeMessage(channel: string)

This method binds the necessary RTC and RTM events and starts CovSubRenderController to process received messages.

info
  • You must call init before using this method.
  • If not initialized, an error is thrown.
ParameterTypeDescription
channelstringThe channel to subscribe to for messages.

unsubscribe

Unsubscribes from the message channel and cleans up resources.


_1
public unsubscribe()

info

This method unbinds RTC and RTM events, clears channels, and cleans up CovSubRenderController. You must call subscribeMessage before calling this method, otherwise an error is thrown.

interrupt

Sends an interrupt message to the specified agent user.


_1
public async interrupt(agentUserId: string)

info
  • You must call init before using this method.
  • If not initialized or sending fails, an error is thrown.
ParameterTypeDescription
agentUserIdstringThe user ID of the agent user to be interrupted.

destroy

Destroys the ConversationalAIAPI instance and cleans up resources.


_1
public destroy(): void

info
  • You must call unsubscribe before calling this method.
  • If not initialized, an error is thrown.

IConversationalAIAPIEventHandlers interface

Event handler interface for the Conversational AI API module.

A set of event handlers that respond to various events emitted by the conversational AI system, including agent state changes, interruptions, performance metrics, errors, and transcription updates.

ParameterTypeDescription
agentUserIdstringUnique identifier for the AI agent.
eventTStateChangeEvent | { turnID: number; timestamp: number }Event data. The type depends on the event type. See TStateChangeEvent.
metricsTAgentMetricPerformance indicator data of the agent. See TAgentMetric.
errorTModuleErrorError message when an error occurs in the agent. See TModuleError.
transcriptionISubtitleHelperItem<Partial<IUserTranscription | IAgentTranscription>>[]An array of transcripts of the conversation between the user and the agent. See ISubtitleHelperItem.
messagestringDebug log message string.

Types and Interfaces

TMessageReceipt

Message receipt type definition.

info

Since v1.7


_6
export type TMessageReceipt = {
_6
moduleType: EModuleType
_6
messageType: EChatMessageType
_6
message: string
_6
turnId: number
_6
}

ParameterTypeDescription
moduleTypeEModuleTypeThe type of module sending the message. See EModuleType.
messageTypeEChatMessageTypeThe type of message. See EChatMessageType.
messagestringThe content of the message.
turnIdnumberUnique identifier for the conversation turn.

IChatMessageBase

The IChatMessageBase interface that contains the properties of the underlying message type.

info

Since v1.7


_3
export interface IChatMessageBase {
_3
messageType: EChatMessageType
_3
}

ParameterTypeDescription
messageTypeEChatMessageTypeType of message. See EChatMessageType for details.

IChatMessageImage

Represents an image-based chat message, which can contain URL image data.

info

Since v1.7


_5
export interface IChatMessageImage extends IChatMessageBase {
_5
messageType: EChatMessageType.IMAGE
_5
uuid: string
_5
url?: string
_5
}

ParameterTypeDescription
messageTypeEChatMessageType.IMAGEMessage type, must be IMAGE. See EChatMessageType.
uuidstringUnique identifier for the image message.
urlstringOptional. URL pointing to the image resource.

IConversationalAIAPIConfig

Parameters used to configure the interface.


_6
export interface IConversationalAIAPIConfig {
_6
rtcEngine: IAgoraRTCClient;
_6
rtmEngine: RTMClient;
_6
renderMode?: ESubtitleHelperMode;
_6
enableLog?: boolean;
_6
}

ParameterTypeDescription
rtcEngineIAgoraRTCClientAgora RTC engine instance. See IAgoraRTCClient.
rtmEngineRTMClientAgora RTM engine instance. See RTMClient.
renderModeESubtitleHelperModeRendering mode. See ESubtitleHelperMode.
enableLogbooleanWhether to enable logging: true enables logging, false disables logging.

TStateChangeEvent

Event type used to describe a change in the state of a voice agent.


_6
export type TStateChangeEvent = {
_6
state: EAgentState
_6
turnID: number
_6
timestamp: number
_6
reason: string
_6
}

info

The state change event is triggered when the state of the voice agent changes. The timestamp uses the UNIX timestamp in milliseconds.

ParameterTypeDescription
stateEAgentStateThe current state of the voice agent. See EAgentState.
turnIDnumberUnique identifier for the current session turn.
timestampnumberTimestamp in milliseconds when the state change occurred.
reasonstringDescription of the reason for the status change.

TAgentMetric

Used to store metric data during agent runtime.


_6
export type TAgentMetric = {
_6
type: EModuleType
_6
name: string
_6
value: number
_6
timestamp: number
_6
}

ParameterTypeDescription
typeEModuleTypeIndicator module type. See EModuleType.
namestringThe name of the indicator.
valuenumberThe indicator value.
timestampnumberTimestamp in milliseconds since Unix epoch (January 1, 1970 UTC).

TModuleError

Used to represent error messages from different AI modules, including error type, error code, error message, and timestamp.


_6
export type TModuleError = {
_6
type: EModuleType
_6
code: number
_6
message: string
_6
timestamp: number
_6
}

ParameterTypeDescription
typeEModuleTypeThe module type where the error occurred. See EModuleType.
codenumberModule-specific error code.
messagestringReadable error description that makes it easier to understand and handle.
timestampnumberTimestamp in milliseconds since Unix epoch (January 1, 1970 UTC).

ISubtitleHelperItem

Transcript auxiliary item interface.

The data structure for a single transcript item in the transcription system. This includes information such as user ID, stream ID, turn ID, timestamp, text content, status, and metadata.


_9
export interface ISubtitleHelperItem<T> {
_9
uid: string;
_9
stream_id: number;
_9
turn_id: number;
_9
_time: number;
_9
text: string;
_9
status: ETurnStatus;
_9
metadata: T | null;
_9
}

ParameterTypeDescription
uidstringUnique user identifier.
stream_idnumberStream identifier.
turn_idnumberThe turn identifier within the session.
_timenumberTimestamp in milliseconds since Unix epoch (January 1, 1970 UTC).
textstringTranscript text content.
statusETurnStatusThe status of the transcript item. See ETurnStatus.
metadataT | nullAdditional metadata information.

Enum classes

EChatMessagePriority

info

Since v1.7

Used to set the chat message processing priority.

Valuedescription
INTERRUPTED('interrupted'): Interrupts current processing and immediately handles this message.
APPEND('append'): Adds the message to the processing queue to be handled in order.
IGNORE('ignore'): Discards the message without processing.

EChatMessageType

Chat message types supported in conversational AI.

info

Since v1.7

ValueDescription
TEXT('text'): Text message.
IMAGE('image'): Image message.
UNKNOWN('unknown'): Unknown message type.

EAgentState

Agent state enumeration.


_7
export enum EAgentState {
_7
IDLE = "idle",
_7
LISTENING = "listening",
_7
THINKING = "thinking",
_7
SPEAKING = "speaking",
_7
SILENT = "silent"
_7
}

ValueDescription
IDLE("idle"): The agent is idle and ready for new interactions.
LISTENING("listening"): The agent is receiving user input.
THINKING("thinking"): The agent is processing the input it receives.
SPEAKING("speaking"): The agent is outputting a response.
SILENT("silent"): The agent intentionally does not respond.

EModuleType

Enumeration of module types for AI capabilities.

ValueDescription
LLM("llm"): Large Language Model.
MLLM("mllm"): Multimodal Large Language Model.
TTS("tts"): Text-to-speech module.
CONTEXT("context"): Context management module.
UNKNOWN("unknown"): Unknown module type.

ESubtitleHelperMode

The mode type for transcript processing.

ValueDescription
TEXTProcesses transcript in text mode.
WORDProcesses transcript in word mode.
UNKNOWNUnknown transcript processing mode.

ETurnStatus

TURN connection status enumeration.

ValueDescription
IN_PROGRESS(0): TURN connection in progress.
END(1): TURN connection has ended.
INTERRUPTED(2): TURN connection has been disconnected.