Skip to main content

Android toolkit API

The Android toolkit API for Conversational AI Engine provides the following classes and methods.

IConversationalAIAPI class

APIDescription
chatSend chat messages to a conversational agent.
addHandlerRegisters an event handler to receive agent session events.
removeHandlerRemoves a registered event handler.
subscribeMessageSubscribes to a channel to receive agent conversation events.
unsubscribeMessageUnsubscribes from the channel and stops receiving events.
interruptInterrupts the agent’s speech.
loadAudioSettingsSets audio parameters to optimize agent conversation performance.
destroyDestroys the API instance and releases resources.

chat

Send a message to the agent.

Support for sending different types of messages through the ChatMessage sealed class hierarchy:

  • ImageMessage: Used to send picture messages.

_1
fun chat(agentUserId: String, message: ChatMessage, completion: (error: ConversationalAIAPIError?) -> Unit)

ParameterTypeDescription
agentUserIdStringAgent user ID.
messageChatMessage Message object of type ImageMessage. See ChatMessage.
completion(error: ConversationalAIAPIError?) -> UnitCallback function. error is null if the call succeeds, and non-null if it fails.

addHandler148.5

Registers an event handler to receive agent session events.


_1
fun addHandler(handler: IConversationalAIAPIEventHandler)

ParameterTypeDescription
handlerIConversationalAIAPIEventHandlerEvent handler instance. See IConversationalAIAPIEventHandler.

removeHandler

Removes a registered event handler.


_1
fun removeHandler(handler: IConversationalAIAPIEventHandler)

ParameterTypeDescription
handlerIConversationalAIAPIEventHandlerEvent handler instance. See IConversationalAIAPIEventHandler.

subscribeMessage

Subscribes to a channel to receive agent conversation events.


_1
fun subscribeMessage(channelName: String, completion: (error: ConversationalAIAPIError?) -> Unit)

ParameterTypeDescription
channelNameStringChannel name.
completion(error: ConversationalAIAPIError?) -> UnitCallback. error is null on success, or non-null if the call fails. See ConversationalAIAPIError.

unsubscribeMessage

Unsubscribes from the channel and stops receiving events.


_1
fun unsubscribeMessage(channelName: String, completion: (error: ConversationalAIAPIError?) -> Unit)

ParameterTypeDescription
channelNameStringChannel name.
completion(error: ConversationalAIAPIError?) -> UnitCallback for unsubscription result. error is null if successful, or non-null if failed. See ConversationalAIAPIError.

interrupt

Interrupts the agent’s speech.


_1
fun interrupt(agentUserId: String, completion: (error: ConversationalAIAPIError?) -> Unit)

ParameterTypeDescription
agentUserIdStringAgent user ID.
completion(error: ConversationalAIAPIError?) -> UnitCallback. error is null on success or contains error info if the call fails. See ConversationalAIAPIError.

loadAudioSettings

Sets audio parameters to optimize the agent’s conversation performance.

info

You must call this method before each joinChannel call to ensure optimal audio quality.


_1
fun loadAudioSettings(scenario: Int = Constants.AUDIO_SCENARIO_AI_CLIENT)

ParameterTypeDescription
scenarioIntAudio scene. Default is AUDIO_SCENARIO_AI_CLIENT. Optimized for agent dialogs.

destroy

Destroys the API instance and releases resources.

Call this method when the instance is no longer needed. After calling, the instance becomes unusable.


_1
fun destroy()

IConversationalAIAPIEventHandler class

APIDescription
onMessageErrorMessage processing error callback.
onMessageReceiptUpdatedMessage receipt update callback.
onAgentStateChangedCalled when the agent’s status changes.
onAgentInterruptedCalled when an interrupt event occurs.
onAgentMetricsCalled when performance metrics are available.
onAgentErrorCalled when an agent error occurs.
onTranscriptionUpdatedCalled when transcription content is updated.
onDebugLogCalled for internal debug logging.

onMessageError

Message processing error callback.

This callback is triggered when an error occurs during message processing. For example, if a chat message fails to be sent, this callback is triggered and the error message is returned.


_1
fun onMessageError(agentUserId: String, error: MessageError)

ParameterTypeDescription
agentUserIdStringAgent user ID.
errorMessageErrorMessage error information, including error type and content. See MessageError for details.

onMessageReceiptUpdated

Message receipt update callback.


_1
fun onMessageReceiptUpdated(agentUserId: String, receipt: MessageReceipt)

ParameterTypeDescription
agentUserIdStringAgent user ID.
receiptMessageReceiptMessage receipt information. See MessageReceipt for details.

onAgentStateChanged

Triggered when the agent’s state changes.


_1
fun onAgentStateChanged(agentUserId: String, event: StateChangeEvent)

ParameterTypeDescription
agentUserIdStringAgent user ID.
eventStateChangeEventState change event. See StateChangeEvent.

onAgentInterrupted

Triggered when an interrupt event occurs.


_1
fun onAgentInterrupted(agentUserId: String, event: InterruptEvent)

ParameterTypeDescription
agentUserIdStringAgent user ID.
eventInterruptEventInterrupt event. See InterruptEvent.

onAgentMetrics

Triggered when performance metrics are available.


_1
fun onAgentMetrics(agentUserId: String, metric: Metric)

ParameterTypeDescription
agentUserIdStringAgent user ID.
metricMetricPerformance metric. See Metric.

onAgentError

Triggered when an agent error occurs.


_1
fun onAgentError(agentUserId: String, error: ModuleError)

ParameterTypeDescription
agentUserIdStringAgent user ID.
errorModuleErrorError details. See ModuleError.

onTranscriptionUpdated

Triggered when the transcription content is updated.


_1
fun onTranscriptionUpdated(agentUserId: String, transcription: Transcription)

info

This callback may trigger frequently. If deduplication is needed, handle it in your business logic.

ParameterTypeDescription
agentUserIdStringAgent user ID.
transcriptionTranscriptionTranscription data. See Transcription.

onDebugLog

Triggered for internal debug logs.


_1
fun onDebugLog(log: String)

ParameterTypeDescription
logStringDebug log information.

Structures

StateChangeEvent

Represents an agent state change event.
Tracks session flow and updates status indications in the UI, including the event timestamp.


_5
data class StateChangeEvent(
_5
val state: AgentState,
_5
val turnId: Long,
_5
val timestamp: Long,
_5
)

ParameterTypeDescription
stateAgentStateCurrent agent status: silent, listening, thinking, speaking. See AgentState.
turnIdLongConversation turn ID.
timestampLongTimestamp in milliseconds since Unix epoch (January 1, 1970 UTC).

InterruptEvent

Indicates an interrupt event.
Triggered when the user or system interrupts the agent. Used for logging and handling.


_4
data class InterruptEvent(
_4
val turnId: Long,
_4
val timestamp: Long
_4
)

ParameterTypeDescription
turnIdLongID of the interrupted conversation turn.
timestampLongTimestamp in milliseconds since Unix epoch (January 1, 1970 UTC).

Metric

Used to define system performance indicator data.


_6
data class Metric(
_6
val type: ModuleType,
_6
val name: String,
_6
val value: Double,
_6
val timestamp: Long
_6
)

ParameterTypeDescription
typeModuleTypeType of indicator. See ModuleType.
nameStringDescriptive name of the metric.
valueDoubleMetric value (e.g., latency in ms).
timestampLongTimestamp when metric was recorded in milliseconds since Unix epoch (January 1, 1970 UTC).

ModuleError

Processes and reports agent-related error information.


_6
data class ModuleError(
_6
val type: ModuleType,
_6
val code: Int,
_6
val message: String,
_6
val timestamp: Long
_6
)

ParameterTypeDescription
typeModuleTypeError type (e.g., LLM failure, TTS exception). See ModuleType.
codeIntSpecific error code.
messageStringDescription of the error.
timestampLongTimestamp of the error in milliseconds since Unix epoch (January 1, 1970 UTC).

ChatMessage

Sealed base class for all message types sent to agents.

This sealed class hierarchy provides a type-safe way to handle messages of different content types.


_1
sealed class ChatMessage

ImageMessage

Used to send image content to the agent.

Supports specifying image files via HTTP/HTTPS links.


_4
data class ImageMessage(
_4
val uuid: String,
_4
val imageUrl: String,
_4
) : ChatMessage()

ParameterTypeDescription
uuidStringUnique identifier for the image message.
imageUrlString?HTTP/HTTPS image file link, mutually exclusive with imageBase64.

MessageReceipt

MessageReceipt represents message receipt information and supports processing multiple media types through MediaInfo.


_6
data class MessageReceipt(
_6
val type: ModuleType,
_6
val chatMessageType: ChatMessageType,
_6
val turnId: Long,
_6
val message: String
_6
)

ParameterTypeDescription
typeModuleTypeModule type. See ModuleType, for example: llm, mllm, tts, context.
chatMessageTypeChatMessageTypeMessage error type. See ChatMessageType.
turnIdLongTurn ID of the message.
messageStringMessage content. Must be parsed according to the type field:
Context type: usually a JSON string containing resource info.

ChatMessageType

Used to distinguish different types of messages in the session system.


_11
enum class ChatMessageType(val value: String) {
_11
Text("text"),
_11
Image("picture"),
_11
UNKNOWN("unknown");
_11
_11
companion object {
_11
fun fromValue(value: String): ChatMessageType {
_11
return ChatMessageType.entries.find { it.value == value } ?: UNKNOWN
_11
}
_11
}
_11
}

ParameterTypeDescription
valueStringThe string value to match against.

MessageError

Used to process and report message error information.


_6
data class MessageError(
_6
val chatMessageType: ChatMessageType,
_6
val code: Int,
_6
val message: String,
_6
val timestamp: Long
_6
)

ParameterTypeDescription
chatMessageTypeChatMessageTypeMessage error type. See ChatMessageType for details.
codeIntError code used to identify specific error scenarios.
messageStringError description providing detailed explanation, usually a JSON string containing resource information.
timestampLongTimestamp of when the event occurred (milliseconds since January 1, 1970 UTC).

Transcription

Represents a full transcribed message used to render the UI.


_7
data class Transcription(
_7
val turnId: Long,
_7
val userId: String = "",
_7
val text: String,
_7
var status: TranscriptionStatus,
_7
var type: TranscriptionType
_7
)

ParameterTypeDescription
turnIdLongSession turn identifier.
userIdStringUser ID linked to the transcript.
textStringTranscribed message content.
statusTranscriptionStatusCurrent status of transcription. See TranscriptionStatus.
typeTranscriptionTypeTranscript type (e.g., AGENT, USER). See TranscriptionType.

ConversationalAIAPIConfig

Holds configuration parameters for initializing the Conversational AI API.


_6
data class ConversationalAIAPIConfig(
_6
val rtcEngine: RtcEngine,
_6
val rtmClient: RtmClient,
_6
val renderMode: TranscriptionRenderMode = TranscriptionRenderMode.Word,
_6
val enableLog: Boolean = false
_6
)

ParameterTypeDescription
rtcEngineRtcEngineAudio/video engine instance. See RtcEngine.
rtmClientRtmClientReal-time messaging client. See RtmClient.
renderModeTranscriptionRenderModeTranscription rendering style. Default: word-by-word. See TranscriptionRenderMode.
enableLogBooleanEnables logging if true. Default is false.

ConversationalAIAPIError

Represents an error type in the Conversational AI API.


_19
sealed class ConversationalAIAPIError : Exception() {
_19
data class RtmError(val code: Int, val msg: String) : ConversationalAIAPIError()
_19
data class RtcError(val code: Int, val msg: String) : ConversationalAIAPIError()
_19
data class UnknownError(val msg: String) : ConversationalAIAPIError()
_19
_19
val errorCode: Int
_19
get() = when (this) {
_19
is RtmError -> this.code
_19
is RtcError -> this.code
_19
is UnknownError -> -100
_19
}
_19
_19
val errorMessage: String
_19
get() = when (this) {
_19
is RtmError -> this.msg
_19
is RtcError -> this.msg
_19
is UnknownError -> this.msg
_19
}
_19
}

PropertyTypeDescription
errorCodeIntRtmError/RtcError: specific code; UnknownError: returns -100.
errorMessageStringHuman-readable description of the error.

Enum classes

Priority

Controls the priority with which the agent handles incoming messages during an interaction.

ValueDescription
INTERRUPTHigh priority: Immediately interrupt the current interaction and process the message. Suitable for urgent or time-sensitive content.
APPENDMedium priority: The message is queued for processing after the current interaction is completed and is suitable for subsequent questions.
IGNORELow priority: This message is only processed when the agent is idle, and will be discarded during ongoing interactions. Suitable for optional content.

AgentState

Represents the current state of the agent.

ValueDescription
SILENTAgent is silent.
LISTENINGAgent is listening.
THINKINGAgent is processing.
SPEAKINGAgent is speaking.
UNKNOWNUnknown state.

ModuleType

Performance module type enumeration.

ValueDescription
LLMLLM inference latency measurement.
MLLMMLLM inference latency measurement.
TTSText-to-speech synthesis latency measurement.
UNKNOWNUnknown type.

MessageType

Used to distinguish different types of messages in the system.

ValueDescription
ASSISTANTAI assistant message.
USERUser message.
ERRORError message.
METRICSPerformance metrics message.
INTERRUPTInterrupt message.
UNKNOWNUnknown message type.

TranscriptionRenderMode

Transcript rendering mode.

ValueDescription
WordWord-by-word transcription and rendering.
TextFull text transcription and rendering.

TranscriptionType

Transcription source type.

ValueDescription
AGENTAgent transcription.
USERUser transcription.

TranscriptionStatus

Indicates the current status of the transcription.

ValueDescription
IN_PROGRESSThe transcription is still being generated or the speech is still in progress.
ENDThe transcription completed normally.
INTERRUPTEDTranscription was interrupted before completion.
UNKNOWNUnknown status.