Skip to main content

Android toolkit API

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

IConversationalAIAPI class

APIDescription
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.

addHandler

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 intelligent 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
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.

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).

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.