Package 

Class AIChainClient


  • 
    public final class AIChainClient
    
                        

    Main 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}")
        }
    })
    • 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
      • setConfig

         final Unit setConfig(SessionConfig config, ConfigCallback callback)

        Send session configuration.

        Parameters:
        config - Session configuration
        callback - Callback for configuration result
      • beginSend

         final Unit beginSend()

        Generate a new CID for the next conversation.

      • 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 content
        extend - 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 content
        isEnd - Whether this is the last chunk
        extend - 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 content
        isEnd - 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 bytes
        extend - 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 bytes
        isEnd - Whether this is the last chunk
        extend - 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 bytes
        isEnd - 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 URL
        extend - Optional extended data
      • on

         final Unit on(String event, EventListener listener)

        Add an event listener.

        Parameters:
        event - Event name
        listener - Event listener
      • off

         final Unit off(String event, EventListener listener)

        Remove an event listener.

        Parameters:
        event - Event name
        listener - 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.