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
IToolProvider
instances 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
genericOpenApi
mode, it configures and uses a singleOpenAPIConnector
as the primary tool provider.In
hierarchicalPlanner
(and the evolvingtoolsetsRouter
) mode, it sets up aToolsetOrchestrator
to 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
runAgentInteraction
method to execute an agent for a given thread and input, yielding a stream ofAgentEvent
s.Contextualization: Assembles the
IAgentContext
required by the agent for each run.
ApiInteractionManagerOptions
ApiInteractionManagerOptions
When creating an ApiInteractionManager
instance, you provide an options object:
interface ApiInteractionManagerOptions {
mode: ApiInteractionMode; // 'genericOpenApi' | 'hierarchicalPlanner'
llmClient: ILLMClient; // Mandatory
messageStorage?: IMessageStorage; // Defaults to MemoryStorage
agentRunStorage?: IAgentRunStorage; // Defaults to MemoryStorage
threadStorage?: IThreadStorage; // Defaults to MemoryStorage
// Mode-specific configurations:
toolsetOrchestratorConfig?: ToolProviderSourceConfig[]; // For 'hierarchicalPlanner'
genericOpenApiProviderConfig?: OpenAPIConnectorOptions; // For 'genericOpenApi'
defaultAgentRunConfig?: Partial<AgentRunConfig>;
agentImplementation?: new () => IAgent; // Custom agent class (e.g., BaseAgent, PlanningAgent)
businessContextText?: string; // Appended to system prompts
}
mode: ApiInteractionMode
(Mandatory):You explicitly choose the operational mode. This is the primary difference from the
AgentB
facade, 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
,threadStorage
allow you to plug in persistent storage. They default toMemoryStorage
.Mode-Specific Configs:
toolsetOrchestratorConfig
: Ifmode
ishierarchicalPlanner
(or the oldertoolsetsRouter
), you provide an array ofToolProviderSourceConfig
to define how different toolsets (specialists) are created.genericOpenApiProviderConfig
: Ifmode
isgenericOpenApi
, you provide theOpenAPIConnectorOptions
for 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
mode
ishierarchicalPlanner
and this is not set or isBaseAgent
, AIM will typically default to usingPlanningAgent
as the primary agent. If you providePlanningAgent
here explicitly, it will also use it. If you provide a different custom agent class inhierarchicalPlanner
mode, that custom agent will be used as the top-level planner, and it's expected to effectively use theDelegateToSpecialistTool
if 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).
const aim = new ApiInteractionManager(options);
await aim.ensureInitialized(); // This is crucial!
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
.
genericOpenApi
mode: Tools from the configuredOpenAPIConnector
.hierarchicalPlanner
mode: Primarily theDelegateToSpecialistTool
definition.
getPrimaryLLMSystemPrompt(customBusinessContext?: string): Promise<string>
getPrimaryLLMSystemPrompt(customBusinessContext?: string): Promise<string>
Generates the system prompt for the primary agent.
genericOpenApi
mode: A prompt describing how to use the tools from the API spec (often including thegenericHttpRequest
tool).hierarchicalPlanner
mode: 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 AgentEvent
s.
Example Usage:
// Assuming 'aim' is an initialized ApiInteractionManager instance
const threadId = "my-conversation-123";
const userMessage: LLMMessage = { role: 'user', content: "Plan a trip to Paris." };
async function processEvents() {
try {
const eventStream = aim.runAgentInteraction(threadId, [userMessage], {
// Example override:
// requestAuthOverrides: { 'myTravelAPI': { type: 'bearer', token: 'user_specific_token'} }
});
for await (const event of eventStream) {
console.log(`Event: ${event.type}`, event.data);
// Process events for UI or logging
}
} catch (error) {
console.error("Agent interaction failed:", error);
}
}
processEvents();
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 OpenAPIConnector
s. Its behavior depends on the AIM's mode.
genericOpenApi
mode: Takes aConnectorAuthentication
object directly.hierarchicalPlanner
mode (viaToolsetOrchestrator
): Takes a callback function(sourceId: string, currentOptions: OpenAPIConnectorOptions) => ConnectorAuthentication | undefined
to 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 IToolSet
s (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
genericOpenApi
orhierarchicalPlanner
, removing any ambiguity that might arise from theAgentB
facade'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.
const generalChatAim = new ApiInteractionManager({ mode: 'genericOpenApi', ...commonConfig, genericOpenApiProviderConfig: faqApiConfig }); const complexTaskAim = new ApiInteractionManager({ mode: 'hierarchicalPlanner', ...commonConfig, toolsetOrchestratorConfig: allMyApisConfig });
Direct Custom Provider Integration: While
ToolsetOrchestrator
is evolving to better support customIToolProvider
instances viaToolProviderSourceConfig
, AIM can offer more direct pathways if you need to construct and inject a customIToolProvider
instance (e.g., by customizing how theIAgentContext.toolProvider
is 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