Backend Factories¶
Backend factories create the backend objects that UI components and hooks use to communicate with your server. This abstraction decouples UI components from specific HTTP endpoints, making them reusable across different server configurations.
Available Factories¶
| Factory | Returns | Description |
|---|---|---|
| createHttpAgentBackend | AgentBackend | HTTP request/response agent backend (no streaming) |
| createSseAgentBackend | AgentBackend | SSE streaming agent backend |
| createHttpTraceBackend | TraceBackend | HTTP backend for trace list and detail operations |
| createHttpConversationBackend | ConversationBackend | HTTP backend for conversation CRUD and messaging |
Pattern¶
All factories follow the same pattern:
- Call the factory with an options object containing
baseUrl,getHeaders, and optional path configuration. - Receive a backend object that implements the required interface.
- Pass the backend to a component or hook via the
backendprop.
import {
createSseAgentBackend,
AgentChat,
} from "@modernpath/agent-ui-react";
// 1. Create the backend
const backend = createSseAgentBackend({
baseUrl: "https://api.example.com",
getHeaders: () => ({
Authorization: `Bearer ${getToken()}`,
}),
});
// 2. Pass to component
<AgentChat backend={backend} auditingId={1} agentType="qa-chat" stream />
Authentication¶
All factories accept a getHeaders function that returns a headers object. This function is called on every request, allowing you to include fresh authentication tokens:
const backend = createHttpAgentBackend({
baseUrl: "/api",
getHeaders: () => ({
Authorization: `Bearer ${localStorage.getItem("token")}`,
"X-Tenant-Id": "tenant-123",
}),
});
Token Refresh
Since getHeaders is called on every request, you can implement token refresh logic outside the backend and the latest token will always be used.
Custom Paths¶
Agent backends support custom path builders for cases where your server uses non-default URL patterns:
Backend Interfaces¶
Each backend factory returns an object conforming to a specific interface. See the TypeScript Types page for the complete interface definitions:
AgentBackend--execute()and optionallyexecuteStream()TraceBackend--listTraces(),getTrace(), and related methodsConversationBackend--listConversations(),createConversation(),sendMessage(), etc.
Related Pages¶
- AgentChat -- uses
AgentBackend - TraceExplorer -- uses
TraceBackend - ConversationPanel -- uses
ConversationBackend - TypeScript Types -- backend interface definitions