Model View Agentic Controller (MVAC) Pattern
Similar to existing MVC, but the controller delegates actions and responses to agents.
In Salesforce, this pattern can be implemented using Apex classes for the Model, Visualforce or Lightning components for the View, and Apex classes that instantiate AIAgent.runAgent() and returns the Agent response message to the View.
private static String postRequest(String recordId, String userPrompt) {
ID modelId = [
SELECT Id, Name
FROM rooms__AIModel__c
WHERE Name = :DEFAULT_AGENT_NAME
LIMIT 1
].Id;
System.debug('Controller.postRequest using AIModel ' + modelId);
rooms.AIAgent helper = new rooms.AIAgent()
.withModelId(modelId)
.withRecordId(recordId)
.runChat(userPrompt);
return helper.message; // JSON payload from the agent. Deserialized to Apex class structures and returned to the View.
}
The AIAgent response message is typically a JSON payload that can be deserialized into Apex class structures for further processing or direct rendering in the View.
// Make a callout via postRequest with the controller prompt.
String responseBody = null;
try {
String userPrompt = buildControllerPrompt();
responseBody = postRequest(String.valueOf(recordId), userPrompt);
} catch (Exception e) {
System.debug('Controller.getRecommendations: Error during postRequest callout: ' + e.getMessage());
}
if (!String.isBlank(responseBody)) {
try {
results = (List<Recommendation>) JSON.deserialize(
responseBody,
List<Recommendation>.class
);
} catch (Exception e) {
System.debug('Controller.getRecommendations: Error deserializing agent JSON: ' + e.getMessage());
// Production should surface an empty set instead of mock data.
results = new List<Recommendation>();
}
} else {
System.debug('Controller.getRecommendations: No agent response; returning empty list.');
}
Prompt Structure
Keep agentic controllers focused on a JSON payload response. This is effectively using an Agent as a REST API. Provide detailed examples of both and good bad JSON responses. The fields and properties in the JSON must map 1:1 to Apex class structures for deserialization.
For any inline reasoning logic or rules, consider managing those in an AI Knowledge store, with each knowledge item representing a discrete rule or logic statement.
Then configure the controller Agent with an inline lookup reference to the knowledge base.