Hierarchical Planner Agent with Custom UI (useChat)
This scenario demonstrates a more advanced AgentB setup:
Backend: An AgentB server configured in
hierarchicalPlannermode. The primary agent is aPlanningAgentthat uses theDelegateToSpecialistToolto delegate sub-tasks to "specialist" agents (toolsets).Frontend: A React application with a custom chat UI built using the
useChathook from@ulifeai/agentb-ui. This allows for fine-grained rendering of the complex event flow from a planning agent.
Goal: Illustrate how to handle the events from a planning agent in a custom UI, showing delegation steps and specialist results.
Part 1: Backend Setup (Conceptual Overview)
For this scenario, your AgentB backend server (e.g., server.ts) would be initialized to support a planning agent.
Key Backend Configuration (server.ts - simplified):
// server.ts (Simplified for this scenario)
import { AgentB, ToolProviderSourceConfig, ApiInteractionManagerOptions } from '@ulifeai/agentb';
// ... other necessary imports (Express, CORS, etc.)
async function startAdvancedServer() {
// 1. Define ToolProviderSourceConfigs for "Specialists"
const weatherSpecialistConfig: ToolProviderSourceConfig = {
id: 'weatherServiceProvider',
type: 'openapi',
openapiConnectorOptions: {
sourceId: 'weatherServiceProvider', // Matches id
// Assuming a spec that provides a 'getWeather(location: string)' tool
specUrl: 'YOUR_WEATHER_API_SPEC_URL_OR_LOCAL_PATH',
// authentication: { ... if needed ... }
},
toolsetCreationStrategy: 'allInOne', // One toolset for all weather tools
allInOneToolsetName: 'WeatherSpecialistTools',
allInOneToolsetDescription: 'Provides tools to get weather forecasts.'
};
const calendarSpecialistConfig: ToolProviderSourceConfig = {
id: 'calendarServiceProvider',
type: 'openapi',
openapiConnectorOptions: {
sourceId: 'calendarServiceProvider',
// Assuming a spec that provides 'createCalendarEvent(title: string, dateTime: string, location?: string)'
specUrl: 'YOUR_CALENDAR_API_SPEC_URL_OR_LOCAL_PATH',
},
toolsetCreationStrategy: 'allInOne',
allInOneToolsetName: 'CalendarSpecialistTools',
allInOneToolsetDescription: 'Provides tools to manage calendar events.'
};
// 2. Initialize AgentB
// The AgentB facade, when given multiple tool providers, will typically
// default to a mode conducive to planning (like 'hierarchicalPlanner').
AgentB.initialize({
llmProvider: { provider: 'openai', model: 'gpt-4o-mini' }, // Or a more capable model for planning
toolProviders: [weatherSpecialistConfig, calendarSpecialistConfig],
defaultAgentRunConfig: {
// The PlanningAgent will use a prompt like DEFAULT_PLANNER_SYSTEM_PROMPT
// which lists 'WeatherSpecialistTools' and 'CalendarSpecialistTools' as available specialists.
model: 'gpt-4o' // Planners often benefit from more capable models
}
});
// 3. Setup Express App and SSE Endpoint (as in previous tutorials)
const app = express();
// ... app.use(express.json()), app.use(cors()) ...
app.post('/advanced/stream', AgentB.getExpressStreamingHttpHandler({ /* ... options ... */ }));
// ... app.listen() ...
console.log("🚀 Advanced AgentB Server running with Planner configuration.");
}
startAdvancedServer();Explanation of Backend Setup:
We register two tool providers (Weather and Calendar), each intended to become a "specialist" toolset.
AgentB.initializewith these providers will lead the internalApiInteractionManagerto likely operate inhierarchicalPlannermode.The primary agent will be a
PlanningAgent. Its main tool will bedelegateToSpecialistAgent.The system prompt for the
PlanningAgentwill list "WeatherSpecialistTools" and "CalendarSpecialistTools" (IDs derived fromToolProviderSourceConfig.idorallInOneToolsetName) as available specialists it can delegate to.
Part 2: Frontend Custom UI with useChat
useChatNow, let's build a React component that uses useChat and renders the specific events from our planning agent.
src/PlannerChatInterface.tsx:
To use this in your React app (e.g. App.tsx):
Running the Advanced Scenario
Start your Backend: Ensure your
server.ts(configured for hierarchical planning with Weather and Calendar specialists as outlined in Part 1) is running.Start your Frontend: Run your React development server (
npm startoryarn start).Interact: Open your browser to the React app. Try a prompt like:
"What's the weather like in London today, and can you create a calendar event for me tomorrow at 10 AM titled 'Follow up on weather report'?"
Expected UI Behavior and Event Handling
When you send the complex prompt, your custom UI (thanks to the renderChatMessage function and useChat's event processing) should display something like this sequence:
You: "What's the weather..."
Planner is thinking...
Agent (tool_thought): Planner intends to use
delegateToSpecialistAgent.Tool:
delegateToSpecialistAgentArgs:
{"specialistId": "WeatherSpecialistTools", "subTaskDescription": "Get current weather for London"}
Agent (tool_executing): Executing
delegateToSpecialistAgent(Weather).(Internally, the
DelegateToSpecialistToolis now running a worker agent. TheuseChathook in the UI doesn't see the worker's internal events by default, but it gets the final result of thedelegateToSpecialistAgenttool call.)
Agent (tool_result):
DelegateToSpecialistAgent(Weather) completed.Success: true
Data:
"The weather in London is 15°C and cloudy."(This is the output from the Weather specialist worker)
Planner is responding... (The planner LLM is now processing the weather result)
Agent (tool_thought): Planner intends to use
delegateToSpecialistAgent.Tool:
delegateToSpecialistAgentArgs:
{"specialistId": "CalendarSpecialistTools", "subTaskDescription": "Create event: 'Follow up on weather report' tomorrow 10 AM"}
Agent (tool_executing): Executing
delegateToSpecialistAgent(Calendar).Agent (tool_result):
DelegateToSpecialistAgent(Calendar) completed.Success: true
Data:
"Event 'Follow up on weather report' created successfully for tomorrow at 10 AM."
Planner is responding...
Agent (ai): "The weather in London is currently 15°C and cloudy. I've also added an event 'Follow up on weather report' to your calendar for tomorrow at 10 AM."
Key aspects demonstrated in PlannerChatInterface.tsx:
useChat: Manages the overall flow.renderChatMessage: Custom function to inspectmsg.senderandmsg.metadata.originalEventto provide richer display for different event types.Displaying Tool Calls: When the planner decides to use
delegateToSpecialistAgent, the UI shows the intended specialist and sub-task.Displaying Tool Results: The UI shows the outcome from the
delegateToSpecialistAgenttool, which includes the information retrieved or action performed by the specialist worker agent. Theagent.sub_agent.invocation.completedevent is key here anduseChatmaps this to atool_resultsender type.
Customization and Further Steps
Detailed Sub-Agent View: For even more insight, you could modify
DelegateToSpecialistToolto stream the internal events of its worker agent back to the planner agent (perhaps as part of its own streaming output or via a side channel). The planner could then emit custom events that the UI could pick up to show a nested view of the specialist's activity. This is significantly more complex.Error Handling: Enhance
renderChatMessageto display errors from tool executions or sub-agent invocations more prominently.Styling: Apply more sophisticated CSS or a UI library for a polished look.
Loading/Streaming Indicators: The example has basic indicators; you can make these more elaborate.
This advanced scenario showcases the power of combining AgentB's hierarchical planning backend with a custom UI built using useChat, allowing you to visualize and interact with complex, multi-step agent behaviors.
Last updated