// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Net.Http;
using System.Threading.Channels;
using Azure.Core;
using Microsoft.Agents.AI.Purview.Models.Jobs;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
namespace Microsoft.Agents.AI.Purview;
///
/// Extension methods to add Purview capabilities to an .
///
public static class PurviewExtensions
{
private static PurviewWrapper CreateWrapper(TokenCredential tokenCredential, PurviewSettings purviewSettings, ILogger? logger = null, IDistributedCache? cache = null)
{
MemoryDistributedCacheOptions options = new()
{
SizeLimit = purviewSettings.InMemoryCacheSizeLimit,
};
IDistributedCache distributedCache = cache ?? new MemoryDistributedCache(Options.Create(options));
ServiceCollection services = new();
services.AddSingleton(tokenCredential);
services.AddSingleton(purviewSettings);
services.AddSingleton();
services.AddSingleton();
services.AddSingleton(distributedCache);
services.AddSingleton();
services.AddSingleton();
services.AddSingleton(logger ?? NullLogger.Instance);
services.AddSingleton();
services.AddSingleton(Channel.CreateBounded(purviewSettings.PendingBackgroundJobLimit));
services.AddSingleton();
services.AddSingleton();
ServiceProvider serviceProvider = services.BuildServiceProvider();
return serviceProvider.GetRequiredService();
}
///
/// Adds Purview capabilities to an .
///
/// The AI Agent builder for the .
/// The token credential used to authenticate with Purview.
/// The settings for communication with Purview.
/// The logger to use for logging.
/// The distributed cache to use for caching Purview responses. An in memory cache will be used if this is null.
/// The updated
public static AIAgentBuilder WithPurview(this AIAgentBuilder builder, TokenCredential tokenCredential, PurviewSettings purviewSettings, ILogger? logger = null, IDistributedCache? cache = null)
{
PurviewWrapper purviewWrapper = CreateWrapper(tokenCredential, purviewSettings, logger, cache);
return builder.Use((innerAgent) => new PurviewAgent(innerAgent, purviewWrapper));
}
///
/// Adds Purview capabilities to a .
///
/// The chat client builder for the .
/// The token credential used to authenticate with Purview.
/// The settings for communication with Purview.
/// The logger to use for logging.
/// The distributed cache to use for caching Purview responses. An in memory cache will be used if this is null.
/// The updated
public static ChatClientBuilder WithPurview(this ChatClientBuilder builder, TokenCredential tokenCredential, PurviewSettings purviewSettings, ILogger? logger = null, IDistributedCache? cache = null)
{
PurviewWrapper purviewWrapper = CreateWrapper(tokenCredential, purviewSettings, logger, cache);
return builder.Use((innerChatClient) => new PurviewChatClient(innerChatClient, purviewWrapper));
}
///
/// Creates a Purview middleware function for use with a .
///
/// The token credential used to authenticate with Purview.
/// The settings for communication with Purview.
/// The logger to use for logging.
/// The distributed cache to use for caching Purview responses. An in memory cache will be used if this is null.
/// A chat middleware delegate.
public static Func PurviewChatMiddleware(TokenCredential tokenCredential, PurviewSettings purviewSettings, ILogger? logger = null, IDistributedCache? cache = null)
{
PurviewWrapper purviewWrapper = CreateWrapper(tokenCredential, purviewSettings, logger, cache);
return (innerChatClient) => new PurviewChatClient(innerChatClient, purviewWrapper);
}
///
/// Creates a Purview middleware function for use with an .
///
/// The token credential used to authenticate with Purview.
/// The settings for communication with Purview.
/// The logger to use for logging.
/// The distributed cache to use for caching Purview responses. An in memory cache will be used if this is null.
/// An agent middleware delegate.
public static Func PurviewAgentMiddleware(TokenCredential tokenCredential, PurviewSettings purviewSettings, ILogger? logger = null, IDistributedCache? cache = null)
{
PurviewWrapper purviewWrapper = CreateWrapper(tokenCredential, purviewSettings, logger, cache);
return (innerAgent) => new PurviewAgent(innerAgent, purviewWrapper);
}
///
/// Sets the user id for a message.
///
/// The message.
/// The id of the owner of the message.
public static void SetUserId(this ChatMessage message, Guid userId)
{
message.AdditionalProperties ??= [];
message.AdditionalProperties[Constants.UserId] = userId.ToString();
}
}