// Copyright (c) Microsoft. All rights reserved.
using System;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Shared.Diagnostics;
namespace Microsoft.Agents.AI.Hosting;
///
/// Provides extension methods for configuring .
///
public static class HostedAgentBuilderExtensions
{
///
/// Configures the host agent builder to use an in-memory session store for agent session management.
///
/// The host agent builder to configure with the in-memory session store.
/// The same instance, configured to use an in-memory session store.
public static IHostedAgentBuilder WithInMemorySessionStore(this IHostedAgentBuilder builder)
{
builder.ServiceCollection.AddKeyedSingleton(builder.Name, new InMemoryAgentSessionStore());
return builder;
}
///
/// Registers the specified agent session store with the host agent builder, enabling session-specific storage for
/// agent operations.
///
/// The host agent builder to configure with the session store. Cannot be null.
/// The agent session store instance to register. Cannot be null.
/// The same host agent builder instance, allowing for method chaining.
public static IHostedAgentBuilder WithSessionStore(this IHostedAgentBuilder builder, AgentSessionStore store)
{
builder.ServiceCollection.AddKeyedSingleton(builder.Name, store);
return builder;
}
///
/// Configures the host agent builder to use a custom session store implementation for agent sessions.
///
/// The host agent builder to configure.
/// A factory function that creates an agent session store instance using the provided service provider and agent
/// name.
/// The same host agent builder instance, enabling further configuration.
public static IHostedAgentBuilder WithSessionStore(this IHostedAgentBuilder builder, Func createAgentSessionStore)
{
builder.ServiceCollection.AddKeyedSingleton(builder.Name, (sp, key) =>
{
Throw.IfNull(key);
var keyString = key as string;
Throw.IfNullOrEmpty(keyString);
return createAgentSessionStore(sp, keyString) ??
throw new InvalidOperationException($"The agent session store factory did not return a valid {nameof(AgentSessionStore)} instance for key '{keyString}'.");
});
return builder;
}
///
/// Adds an AI tool to an agent being configured with the service collection.
///
/// The hosted agent builder.
/// The AI tool to add to the agent.
/// The same instance so that additional calls can be chained.
/// Thrown when or is .
public static IHostedAgentBuilder WithAITool(this IHostedAgentBuilder builder, AITool tool)
{
Throw.IfNull(builder);
Throw.IfNull(tool);
builder.ServiceCollection.AddKeyedSingleton(builder.Name, tool);
return builder;
}
///
/// Adds multiple AI tools to an agent being configured with the service collection.
///
/// The hosted agent builder.
/// The collection of AI tools to add to the agent.
/// The same instance so that additional calls can be chained.
/// Thrown when or is .
public static IHostedAgentBuilder WithAITools(this IHostedAgentBuilder builder, params AITool[] tools)
{
Throw.IfNull(builder);
Throw.IfNull(tools);
foreach (var tool in tools)
{
builder.WithAITool(tool);
}
return builder;
}
///
/// Adds AI tool to an agent being configured with the service collection.
///
/// The hosted agent builder.
/// A factory function that creates a AI tool using the provided service provider.
public static IHostedAgentBuilder WithAITool(this IHostedAgentBuilder builder, Func factory)
{
Throw.IfNull(builder);
Throw.IfNull(factory);
builder.ServiceCollection.AddKeyedSingleton(builder.Name, (sp, name) => factory(sp));
return builder;
}
}