AgentforceUIDelegate

Overview

Protocol that enables customization of user interactions and conversation behavior within Agentforce UI components. Implement this protocol to intercept and modify utterances before sending, receive notifications about conversation events, and handle agent switching scenarios.

Declaration

protocol AgentforceUIDelegate

Methods

Method Signature Description
modifyUtteranceBeforeSending func modifyUtteranceBeforeSending(_ utterance: AgentforceUtterance) async -> AgentforceUtterance Allows modification of user utterances before they are sent to the agent. Returns the original utterance unchanged by default.
didSendUtterance func didSendUtterance(_ utterance: AgentforceUtterance) Called after an utterance has been successfully sent to the agent. No-op by default.
userDidSwitchAgents func userDidSwitchAgents(newConversation: AgentConversation) Called when the user switches to a different agent during a conversation. No-op by default.
userInitiatedVoice func userInitiatedVoice(for conversation: AgentConversation) Called when the user initiates a voice interaction for a conversation. No-op by default.

Implementation Patterns

class MyUIDelegate: AgentforceUIDelegate {
    // Modify utterances before sending
    func modifyUtteranceBeforeSending(_ utterance: AgentforceUtterance) async -> AgentforceUtterance {
        // Add user context to the message
        var modifiedUtterance = utterance
        modifiedUtterance.context = [
            "userId": UserSession.current.userId,
            "timestamp": Date().iso8601
        ]
        return modifiedUtterance
    }
}
class AnalyticsUIDelegate: AgentforceUIDelegate {
    // Track when messages are sent
    func didSendUtterance(_ utterance: AgentforceUtterance) {
        Analytics.track("message_sent", properties: [
            "message_length": utterance.text.count,
            "has_attachments": !utterance.attachments.isEmpty
        ])
    }
}
class AgentSwitchDelegate: AgentforceUIDelegate {
    func userDidSwitchAgents(newConversation: AgentConversation) {
        NotificationCenter.default.post(
            name: .agentSwitched,
            object: nil,
            userInfo: ["conversation": newConversation]
        )
    }
}

Switch From Chat View to Voice View

Use userInitiatedVoice to present your own instance of AgentforceVoiceView. userInitiatedVoice is called when a user taps the button that opens the voice view from the chat view.

This sample code demonstrates rerendering the view when a user selects the voice option.

enum PresentationState {
    case none
    case chat
    case voice
}
struct MyCustomView: View {
    @State var presentationState: PresentationState = .none
    var body: some View {
        // Show chatView and voiceView based on presentationState
    }
}
extension MyCustomView: AgentforceUIDelegate {
    func userInitiatedVoice(for conversation: any AgentConversation) {
        presentationState = .voice
    }
}