The ApiInteractionManager
The ApiInteractionManager (AIM) is a central class in the AgentB framework, responsible for orchestrating agent interactions. While the AgentB facade provides a simplified entry point, using AIM directly offers more granular control over agent configuration, operational modes, and tool setups.
It's the workhorse that the AgentB facade uses under the hood. Understanding AIM is beneficial if you need to:
Explicitly define the agent's operational mode.
Use multiple, differently configured agent setups within a single application.
Integrate custom
IToolProviderinstances directly.Have fine-grained control over the system prompt and default agent configurations.
Core Responsibilities
Mode Management: Explicitly configures the agent system to operate in a specific mode (
genericOpenApi,hierarchicalPlanner, etc.).Agent Instantiation: Determines which agent class to use (e.g.,
BaseAgent,PlanningAgent, or a custom implementation) based on the mode and configuration.Tool Environment Setup:
In
genericOpenApimode, it configures and uses a singleOpenAPIConnectoras the primary tool provider.In
hierarchicalPlanner(and the evolvingtoolsetsRouter) mode, it sets up aToolsetOrchestratorto manage multiple toolsets (specialists) and equips the primary planning agent with theDelegateToSpecialistTool.
System Prompt Generation: Generates the appropriate system prompt for the LLM based on the selected mode and available tools.
Agent Execution: Provides the
runAgentInteractionmethod to execute an agent for a given thread and input, yielding a stream ofAgentEvents.Contextualization: Assembles the
IAgentContextrequired by the agent for each run.
ApiInteractionManagerOptions
ApiInteractionManagerOptionsWhen creating an ApiInteractionManager instance, you provide an options object:
mode: ApiInteractionMode(Mandatory):You explicitly choose the operational mode. This is the primary difference from the
AgentBfacade, which tries to infer the mode.See Operational Modes for details.
llmClient: ILLMClient(Mandatory): Your configured LLM client instance (e.g.,OpenAIAdapter).Storage Options:
messageStorage,agentRunStorage,threadStorageallow you to plug in persistent storage. They default toMemoryStorage.Mode-Specific Configs:
toolsetOrchestratorConfig: IfmodeishierarchicalPlanner(or the oldertoolsetsRouter), you provide an array ofToolProviderSourceConfigto define how different toolsets (specialists) are created.genericOpenApiProviderConfig: IfmodeisgenericOpenApi, you provide theOpenAPIConnectorOptionsfor the single API the agent will interact with.
defaultAgentRunConfig?: Partial<AgentRunConfig>: Default settings (model, temperature, system prompt overrides, etc.) for agent runs initiated by this AIM instance.agentImplementation?: new () => IAgent:Allows you to specify a custom agent class.
If
modeishierarchicalPlannerand this is not set or isBaseAgent, AIM will typically default to usingPlanningAgentas the primary agent. If you providePlanningAgenthere explicitly, it will also use it. If you provide a different custom agent class inhierarchicalPlannermode, that custom agent will be used as the top-level planner, and it's expected to effectively use theDelegateToSpecialistToolif it wants to delegate.
businessContextText?: string: General context added to system prompts.
Initialization (ensureInitialized())
ensureInitialized())Before an AIM instance can be used, its internal components (like OpenAPIConnector or ToolsetOrchestrator) need to be initialized (e.g., fetching and parsing OpenAPI specs).
This method is idempotent; calling it multiple times is safe.
Key Methods
getPrimaryLLMFormattedTools(): Promise<any[]>
getPrimaryLLMFormattedTools(): Promise<any[]>Returns the tool definitions that the primary agent (controlled by this AIM instance) will be aware of, formatted for the llmClient.
genericOpenApimode: Tools from the configuredOpenAPIConnector.hierarchicalPlannermode: Primarily theDelegateToSpecialistTooldefinition.
getPrimaryLLMSystemPrompt(customBusinessContext?: string): Promise<string>
getPrimaryLLMSystemPrompt(customBusinessContext?: string): Promise<string>Generates the system prompt for the primary agent.
genericOpenApimode: A prompt describing how to use the tools from the API spec (often including thegenericHttpRequesttool).hierarchicalPlannermode: The detailed system prompt for thePlanningAgent(e.g.,DEFAULT_PLANNER_SYSTEM_PROMPT), guiding it on how to break down tasks and use theDelegateToSpecialistTool.
runAgentInteraction(threadId, initialTurnMessages, agentRunConfigOverride?, existingRunId?): AsyncGenerator<AgentEvent>
runAgentInteraction(threadId, initialTurnMessages, agentRunConfigOverride?, existingRunId?): AsyncGenerator<AgentEvent>This is the core method to execute an agent interaction.
threadId: string: The ID of the conversation.initialTurnMessages: LLMMessage[]: The messages initiating this turn (e.g., new user input, or tool results if continuing a run).agentRunConfigOverride?: Partial<AgentRunConfig>: Overrides for the default run configuration for this specific execution. Can includerequestAuthOverrides.existingRunId?: string: If continuing a run that was paused (e.g.,requires_action).
It assembles the IAgentContext with the appropriate tool provider (based on the mode) and runs the configured agent implementation. It returns an async generator yielding AgentEvents.
Example Usage:
continueAgentRunWithToolOutputs(runId, threadId, toolOutputs, agentRunConfigOverride?): AsyncGenerator<AgentEvent>
continueAgentRunWithToolOutputs(runId, threadId, toolOutputs, agentRunConfigOverride?): AsyncGenerator<AgentEvent>Used to resume an agent run that paused with a status: 'requires_action' (typically when BaseAgent itself doesn't auto-execute tools, or hits maxToolCallContinuations). You provide the outputs of the tools the agent requested.
updateAuthentication(...)
updateAuthentication(...)Allows dynamic updates to the authentication configuration of underlying OpenAPIConnectors. Its behavior depends on the AIM's mode.
genericOpenApimode: Takes aConnectorAuthenticationobject directly.hierarchicalPlannermode (viaToolsetOrchestrator): Takes a callback function(sourceId: string, currentOptions: OpenAPIConnectorOptions) => ConnectorAuthentication | undefinedto update auth for multiple providers.
getToolset(toolsetId): Promise<IToolSet | undefined> and getAllToolsets(): Promise<IToolSet[]>
getToolset(toolsetId): Promise<IToolSet | undefined> and getAllToolsets(): Promise<IToolSet[]>If in hierarchicalPlanner mode (or the older toolsetsRouter), these methods allow you to inspect the IToolSets (specialists) managed by the internal ToolsetOrchestrator.
Operational Modes in Detail
For a deeper dive into how each mode configures the agent and its tools, see the Operational Modes Guide.
Why Use ApiInteractionManager Directly?
ApiInteractionManager Directly?Explicit Mode Control: You directly specify
genericOpenApiorhierarchicalPlanner, removing any ambiguity that might arise from theAgentBfacade's inference.Multiple AIM Instances: Your application might need different agent configurations for different purposes or API endpoints. You can create multiple AIM instances, each with its own mode, tool setup, and default configurations.
Direct Custom Provider Integration: While
ToolsetOrchestratoris evolving to better support customIToolProviderinstances viaToolProviderSourceConfig, AIM can offer more direct pathways if you need to construct and inject a customIToolProviderinstance (e.g., by customizing how theIAgentContext.toolProvideris assembled before callingagent.run(), though this level of customization often means you are building your own agent loop rather than just usingaim.runAgentInteraction).Testing: When unit testing or integration testing parts of your agent system, using AIM directly can make it easier to mock dependencies and control the environment.
The ApiInteractionManager is a powerful class that gives you significant control over how your agents are configured and executed. It builds upon the core AgentB components to provide a cohesive orchestration layer.
Last updated