-
public final class AIChainClientMain client for IIP Dispatch Protocol.
This class provides WebSocket-based real-time communication with the IIP Dispatch service. It supports STT (Speech-to-Text), NLU (Natural Language Understanding), and TTS (Text-to-Speech).
Example usage:
val client = AIChainClient.Builder() .appId("your-app-id") .appKey("your-app-key") .host("your-host") .sn("your-sn") .build() client.on("session.created") { args -> val sid = args[0] as String println("Session created: $sid") } client.connect(object : ConnectionCallback { override fun onSuccess() { println("Connected") } override fun onError(error: AIChainError) { println("Error: ${error.message}") } })
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classAIChainClient.BuilderBuilder for creating AIChainClient instances.
public classAIChainClient.Companion
-
Method Summary
Modifier and Type Method Description final Unitconnect(ConnectionCallback callback)Establish WebSocket connection and wait for session creation. final Unitdisconnect()Close the WebSocket connection. final Unitreconnect(ConnectionCallback callback)Manually reconnect to the server. final UnitonAppBackground()通知 SDK App 进入后台。App 应在 Activity/Fragment 的 onStop() 或 ProcessLifecycleOwner 回调中调用此方法。 SDK 会记录前后台状态,在断开诊断日志中标注 "App状态: 后台",方便排查后台断连问题。注意:进入后台后 Android 系统可能施加 Doze/AppStandby 限制,导致心跳超时断连。 如不希望后台断连,建议在进入后台时主动调用 disconnect,回到前台时调用 connect。 final UnitonAppForeground()通知 SDK App 回到前台。App 应在 Activity/Fragment 的 onStart() 或 ProcessLifecycleOwner 回调中调用此方法。 final BooleanisAppInBackground()查询 App 当前是否在后台。 final BooleanisConnected()Check if currently connected. final ConnectionStategetConnectionState()Get current connection state. final ConnectionInfogetConnectionInfo()Get connection information. final StringgetSid()Get current session ID. final StringgetCid()Get current conversation ID. final UnitsetConfig(SessionConfig config, ConfigCallback callback)Send session configuration. final SessionConfiggetConfig()Get current session configuration. final UnitbeginSend()Generate a new CID for the next conversation. final Unitcancel()Cancel the current conversation. final UnitcancelCid(String cid)Cancel a specific conversation by cid. final StringsendText(String text, Map<String, Object> extend)Send a text message. final StringsendText(String text)Send a text message. final StringsendTextStream(String text, Boolean isEnd, Map<String, Object> extend)Send text in streaming mode. final StringsendTextStream(String text, Boolean isEnd)Send text in streaming mode. final StringsendAudio(ByteArray audioData, Map<String, Object> extend)Send audio data (single frame). final StringsendAudio(ByteArray audioData)Send audio data (single frame). final StringsendAudioStream(ByteArray audioData, Boolean isEnd, Map<String, Object> extend)Send audio in streaming mode. final StringsendAudioStream(ByteArray audioData, Boolean isEnd)Send audio in streaming mode. final StringsendImage(String imageData, Map<String, Object> extend)Send an image. final StringsendMsgs(List<MessageItem> items)Send multiple message items (multi-modal). final Uniton(String event, EventListener listener)Add an event listener. final Unitoff(String event, EventListener listener)Remove an event listener. final UnitremoveAllListeners(String event)Remove all event listeners. -
-
Method Detail
-
connect
final Unit connect(ConnectionCallback callback)
Establish WebSocket connection and wait for session creation.
- Parameters:
callback- Callback for connection result
-
disconnect
final Unit disconnect()
Close the WebSocket connection.
-
reconnect
final Unit reconnect(ConnectionCallback callback)
Manually reconnect to the server. Waits 200ms after disconnect to allow server cleanup.
- Parameters:
callback- Callback for reconnection result
-
onAppBackground
final Unit onAppBackground()
通知 SDK App 进入后台。
App 应在 Activity/Fragment 的 onStop() 或 ProcessLifecycleOwner 回调中调用此方法。 SDK 会记录前后台状态,在断开诊断日志中标注 "App状态: 后台",方便排查后台断连问题。
注意:进入后台后 Android 系统可能施加 Doze/AppStandby 限制,导致心跳超时断连。 如不希望后台断连,建议在进入后台时主动调用 disconnect,回到前台时调用 connect。
-
onAppForeground
final Unit onAppForeground()
通知 SDK App 回到前台。
App 应在 Activity/Fragment 的 onStart() 或 ProcessLifecycleOwner 回调中调用此方法。
-
isAppInBackground
final Boolean isAppInBackground()
查询 App 当前是否在后台。
-
isConnected
final Boolean isConnected()
Check if currently connected.
-
getConnectionState
final ConnectionState getConnectionState()
Get current connection state.
-
getConnectionInfo
final ConnectionInfo getConnectionInfo()
Get connection information.
-
setConfig
final Unit setConfig(SessionConfig config, ConfigCallback callback)
Send session configuration.
- Parameters:
config- Session configurationcallback- Callback for configuration result
-
getConfig
final SessionConfig getConfig()
Get current session configuration.
-
cancel
final Unit cancel()
Cancel the current conversation. Sends response.cancel for all active response CIDs and clears their buffers.
-
cancelCid
final Unit cancelCid(String cid)
Cancel a specific conversation by cid. Sends response.cancel for the specified cid regardless of whether it's in activeResponseCids. Also removes the cid from activeResponseCids and rotates currentInputCid if it matches.
- Parameters:
cid- The conversation ID to cancel
-
sendText
@JvmOverloads() final String sendText(String text, Map<String, Object> extend)
Send a text message.
- Parameters:
text- Text contentextend- Optional extended data passed through to server processors
-
sendText
@JvmOverloads() final String sendText(String text)
Send a text message.
- Parameters:
text- Text content
-
sendTextStream
@JvmOverloads() final String sendTextStream(String text, Boolean isEnd, Map<String, Object> extend)
Send text in streaming mode.
- Parameters:
text- Text contentisEnd- Whether this is the last chunkextend- Optional extended data passed through to server processors
-
sendTextStream
@JvmOverloads() final String sendTextStream(String text, Boolean isEnd)
Send text in streaming mode.
- Parameters:
text- Text contentisEnd- Whether this is the last chunk
-
sendAudio
@JvmOverloads() final String sendAudio(ByteArray audioData, Map<String, Object> extend)
Send audio data (single frame).
- Parameters:
audioData- Audio data bytesextend- Optional extended data passed through to server processors
-
sendAudio
@JvmOverloads() final String sendAudio(ByteArray audioData)
Send audio data (single frame).
- Parameters:
audioData- Audio data bytes
-
sendAudioStream
@JvmOverloads() final String sendAudioStream(ByteArray audioData, Boolean isEnd, Map<String, Object> extend)
Send audio in streaming mode.
- Parameters:
audioData- Audio data bytesisEnd- Whether this is the last chunkextend- Optional extended data passed through to server processors
-
sendAudioStream
@JvmOverloads() final String sendAudioStream(ByteArray audioData, Boolean isEnd)
Send audio in streaming mode.
- Parameters:
audioData- Audio data bytesisEnd- Whether this is the last chunk
-
sendImage
final String sendImage(String imageData, Map<String, Object> extend)
Send an image.
- Parameters:
imageData- Base64 encoded image data or URLextend- Optional extended data
-
sendMsgs
final String sendMsgs(List<MessageItem> items)
Send multiple message items (multi-modal).
- Parameters:
items- List of message items
-
on
final Unit on(String event, EventListener listener)
Add an event listener.
- Parameters:
event- Event namelistener- Event listener
-
off
final Unit off(String event, EventListener listener)
Remove an event listener.
- Parameters:
event- Event namelistener- Event listener to remove, or null to remove all listeners for this event
-
removeAllListeners
final Unit removeAllListeners(String event)
Remove all event listeners.
- Parameters:
event- Optional event name.
-
-
-
-