AI Chat Agent

Updated at:

This topic describes how to integrate the AI Chat Agent on iOS and Mac.

Introduction

You can use the real-time messaging feature of DingRTC to receive service status, transcription messages, and control commands from the AI Chat Agent. This feature is dependent on meetings, so you must join a meeting before you can use it.

Concepts

Session: A real-time messaging session identified by a unique sessionId. A session supports join, leave, and close operations. You must join the session before you can send or receive real-time messages from the agent.

Agent ID: The user ID of the agent.

Transcription message: The transcribed information from the conversation with the agent.

Natural conversation/Walkie-talkie mode: The conversation mode of the agent. This mode is created using OpenAPI.

Integration method

  1. Before joining a meeting, integrate the basic real-time messaging feature as described in the iOS & Mac integration method.

  2. Then, obtain the DingRtmAgentClient from the DingRtmClient:

    DingRtmAgentClient *agentClient = [rtmClient getRtmAgentClient];
  3. Set a callback listener for the DingRtmAgentClient instance:

    @implementation DingRtmAgentClientDelegateImpl
    
    ...
    - (void)registerRtcDelegate {
     [self.agentClient setDelegate:self];
    }
    ...
    //MARK: - DingRtmAgentClientDelegate
    
    - (void)onAgentUserReady:(NSString *)sessionId agentId:(NSString *)agentId {
    }
    
    - (void)onAgentStateChanged:(NSString *)sessionId agentId:(NSString *)agentId state:(DingAgentState)state {
    }
    
    - (void)onTranscriptionMessage:(NSString *)sessionId agentId:(NSString *)agentId data:(DingRtmAgentTranscriptionData *)data {
    }
    @end
    
    
  4. After you successfully join the meeting, call the StartAgent OpenAPI operation to have the agent join the meeting and the session.

  5. Other members in the meeting will receive an onSessionCreate event notification. After receiving the notification, they must join the same session:

    [rtmClient joinSession:sessionId]; // If the session exists, call joinSession to join directly.
  6. You receive an onAgentUserReady event notification when you join a session with an agent or when an agent joins your session. The application layer must maintain the list of agents in the session:

    - (void)onAgentUserReady:(NSString *)sessionId agentId:(NSString *)agentId {
        // The application layer must retain the agentId to call agent-related methods.
    }
  7. Use the onAgentStateChanged event to monitor status changes for the current agent:

    - (void)onAgentStateChanged:(NSString *)sessionId agentId:(NSString *)agentId state:(DingAgentState)state {
        // Use state to get the current agent's status: Listening, Thinking, or Responding.
    }
  8. Send specific signals through the SDK to control the agent's behavior. For example, you can have the agent reply with an opening statement:

    [agentClient sendGreetingReady:sessionId agentId:agentId];

    Manually interrupt the agent:

    [agentClient sendInterrupt:sessionId agentId:agentId];

    In walkie-talkie mode, send control signals. Note: Before sending DingAgentPushToTalkCmdSend, you must first send DingAgentPushToTalkCmdStart:

    // Make the agent start accepting user speech input.
    [agentClient sendPushToTalk:sessionId agentId:agentId cmd:DingAgentPushToTalkCmdStart];
    ...
    // Make the agent start processing the user's current speech input data.
    [agentClient sendPushToTalk:sessionId agentId:agentId cmd:DingAgentPushToTalkCmdSend];
    ...
    // Cancel the user's current speech input.
    [agentClient sendPushToTalk:sessionId agentId:agentId cmd:DingAgentPushToTalkCmdCancel];
  9. If you no longer need the agent's real-time messaging feature, leave the session:

    [rtmClient leaveSession:sessionId];

Notes

  1. The server-side StartAgent OpenAPI operation is used to create an agent and have it join an RTM session.

  2. Do not manually close an RTM session that contains an agent. The session is managed by the server-side agent service OpenAPI.

  3. If a remote user leaves the channel without calling leaveSession, you might not receive the onSessionRemoteUserLeave event notification for that user. After receiving the onRemoteUserOffLineNotify event for that user, the application layer must manually remove the user from the member lists of all sessions.

  4. Set the callback listener for the DingRtmAgentClient instance before joining the meeting to avoid missing callback messages.