Build a Headless Experience

This approach is for developers who want to build a completely custom UI or run agent interactions in the background. The AgentforceService handles the communication and state, emitting events via Kotlin Flows that your app uses to update its own UI or trigger logic.

Instantiate AgentforceService 

Use the AgentforceServiceProvider to create an AgentforceService instance for a specific agent.

1import com.salesforce.android.agentforceservice.AgentforceServiceProvider
2import com.salesforce.android.agentforceservice.AgentforceService
3
4// Create the service provider with required dependencies
5val serviceProvider = AgentforceServiceProvider(
6    configurationLocale = Locale.getDefault(),
7    domain = "https://your-domain.my.salesforce.com",
8    network = MyAppNetwork(okHttpClient),
9    credentialProvider = MyAppCredentialProvider(userSession),
10    instrumentationHandler = myInstrumentationHandler, // Optional
11    logger = myLogger // Optional
12)
13
14// Get the service for your specific agent
15val agentId = "YOUR_AGENT_ID"
16val agentforceService: AgentforceService = serviceProvider.getAgentforceService(agentId)

Send Messages 

To send a message to the agent programmatically, you can use a method like sendUtterance on your AgentforceConversation object:

1// Example: Sending a message with an optional attachment
2conversation.sendUtterance(
3    utterance = "Hello, world!",
4    attachment = null // or provide an AgentforceAttachment object
5)

Receive Messages 

To receive messages from the agent, you can use the conversationManager property on the AgentforceConversation object:

1// Example: Using conversationManager to send and receive messages
2val responseChannel = conversation.conversationManager.sendMessage(
3    inputRepresentation = ConversationInputRepresentation("Hello, world!"),
4    agentforceAttachment = null, // Optional attachment
5    file = null // Optional file
6)

Next Steps