AgentB Facade
The AgentB object is a static class providing a high-level facade for initializing and interacting with the AgentB framework. It simplifies common setup tasks and provides convenient HTTP handlers.
Import:
import { AgentB } from '@ulifeai/agentb';Static Methods
AgentB.initialize(options: AgentBInitializationOptions): void
AgentB.initialize(options: AgentBInitializationOptions): voidInitializes the AgentB framework with global configurations. This method should be called once at the start of your application.
options: AgentBInitializationOptions:llmProvider?: AgentBLLMProviderConfig: Configures the LLM provider.provider: 'openai' | string: Currently 'openai'.apiKey?: string: OpenAI API key (overridesprocess.env.OPENAI_API_KEY).model?: string: Default LLM model (e.g., 'gpt-4o-mini').options?: Record<string, any>: Additional options for the LLM adapter (e.g.,baseURLfor OpenAI).
messageStorage?: IMessageStorage: Custom message storage implementation. Defaults toMemoryStorage.agentRunStorage?: IAgentRunStorage: Custom agent run storage. Defaults toMemoryStorage.threadStorage?: IThreadStorage: Custom thread storage. Defaults toMemoryStorage.defaultAgentRunConfig?: Partial<AgentRunConfig>: Global default settings for agent runs (e.g., temperature, default system prompt). Merged with library defaults.toolProviders?: ToolProviderSourceConfig[]: An initial list of configurations for tool providers to be registered.
Example:
AgentB.registerToolProvider(sourceConfig: ToolProviderSourceConfig): void
AgentB.registerToolProvider(sourceConfig: ToolProviderSourceConfig): voidRegisters a tool provider source with the framework. This configuration will be used by the internal ApiInteractionManager and ToolsetOrchestrator to create and make tools available to agents. Calling this after initial interactions might cause the internal ApiInteractionManager to re-initialize.
sourceConfig: ToolProviderSourceConfig:id: string: Unique identifier for this tool source (e.g., 'myApiV1').type?: 'openapi': Type of provider (currently 'openapi').openapiConnectorOptions: Omit<OpenAPIConnectorOptions, 'sourceId'>: Configuration for theOpenAPIConnectorif type is 'openapi'. ThesourceIdfor the connector will be taken from the top-levelid.toolsetCreationStrategy?: 'byTag' | 'allInOne': How to group tools from this source intoIToolSets.allInOneToolsetName?: string: Custom name ifallInOnestrategy is used.allInOneToolsetDescription?: string: Custom description ifallInOnestrategy is used.maxToolsPerLogicalGroup?: number: Threshold for attempting LLM-based splitting of large toolsets.llmSplittingConfig?: { model: string; ... }: Configuration for the LLM used in toolset splitting.
Example:
AgentB.runHttpInteractionStream(threadId: string, userMessage: LLMMessage, agentRunConfigOverride?: Partial<AgentRunConfig>, existingRunId?: string): AsyncGenerator<AgentEvent, void, undefined>
AgentB.runHttpInteractionStream(threadId: string, userMessage: LLMMessage, agentRunConfigOverride?: Partial<AgentRunConfig>, existingRunId?: string): AsyncGenerator<AgentEvent, void, undefined>The core method for executing an agent interaction and receiving a stream of AgentEvents. This method is framework-agnostic and can be used directly or as the basis for custom HTTP handlers.
threadId: string: The ID of the conversation thread. A new thread is not automatically created by this method; ensure it exists or handle creation externally if needed (thoughgetExpressStreamingHttpHandler's defaultgetThreadIdcallback does handle creation).userMessage: LLMMessage: The user's message object (must haverole: 'user'and validcontent).agentRunConfigOverride?: Partial<AgentRunConfig>: Optional configuration overrides for this specific agent run. These merge with defaults. Can includerequestAuthOverridesfor dynamic tool authentication.existingRunId?: string: Optional. If provided, attempts to continue an existing agent run (e.g., one that paused withstatus: 'requires_action').
Returns: An AsyncGenerator that yields AgentEvent objects as the agent processes the request.
Throws: ConfigurationError or InvalidStateError if prerequisites are not met.
Example:
AgentB.getExpressStreamingHttpHandler(handlerOptions?: AgentBExpressHttpHandlerOptions): (req: any, res: any) => Promise<void>
AgentB.getExpressStreamingHttpHandler(handlerOptions?: AgentBExpressHttpHandlerOptions): (req: any, res: any) => Promise<void>Returns an Express.js compatible request handler function for creating a Server-Sent Events (SSE) streaming endpoint.
handlerOptions?: AgentBExpressHttpHandlerOptions:getThreadId?: (req: any, threadStorage: IThreadStorage) => Promise<string>: Callback to extract or create athreadIdfrom the Express request (req).threadStorageis the initialized storage instance.Default: Uses
req.body.threadIdorreq.query.threadId; creates a new thread if not found/provided.
getUserMessage?: (req: any) => Promise<string | LLMMessage>: Callback to extract the user's input fromreq.Default: Expects
req.body.prompt(string).
authorizeRequest?: (req: any, threadId: string) => Promise<boolean | PerProviderAuthOverrides>: Callback for request authorization and dynamic tool authentication overrides.Return
true: Authorized.Return
false: Forbidden (handler sends 403).Return
PerProviderAuthOverrides: Authorized with dynamic auth details.
initialAgentRunConfig?: Partial<AgentRunConfig>: DefaultAgentRunConfigsettings specifically for requests handled by this route.
Returns: An async function (req: express.Request, res: express.Response) => Promise<void>.
Example (in an Express app):
AgentB.getApiInteractionManager(): Promise<ApiInteractionManager> (Advanced)
AgentB.getApiInteractionManager(): Promise<ApiInteractionManager> (Advanced)Provides direct access to the underlying ApiInteractionManager instance that the AgentB facade is using. This is for advanced use cases where you need more granular control than the facade methods offer.
Returns: A Promise resolving to the initialized ApiInteractionManager instance.
Note: Modifying the ApiInteractionManager returned by this method can affect all subsequent interactions handled by the AgentB facade. Use with caution if you are also using facade methods like runHttpInteractionStream or getExpressStreamingHttpHandler.
Internal Properties (for understanding, not direct manipulation)
While not part of the public API for modification, understanding these can be helpful:
AgentB.llmClient: ILLMClient: The initialized LLM client.AgentB.messageStorage: IMessageStorage: The initialized message storage.AgentB.agentRunStorage: IAgentRunStorage: The initialized agent run storage.AgentB.threadStorage: IThreadStorage: The initialized thread storage.AgentB.globalDefaultAgentRunConfig: AgentRunConfig: The resolved global default agent run configuration.
These are managed internally by AgentB.initialize(). For custom instances, you generally pass them during initialization or use ApiInteractionManager directly.
Last updated