Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using CrestApps.Core.AI.Handlers;
using CrestApps.Core.AI.Models;
using CrestApps.Core.Azure.Models;
using Microsoft.AspNetCore.DataProtection;

namespace CrestApps.Core.AI.OpenAI.Azure.Handlers;

internal sealed class AzureOpenAIConnectionHandler : AIProviderConnectionHandlerBase
{
private const string ConnectionProtectorName = "AIProviderConnection";

private readonly IDataProtectionProvider _dataProtectionProvider;

public AzureOpenAIConnectionHandler(IDataProtectionProvider dataProtectionProvider)
{
_dataProtectionProvider = dataProtectionProvider;
}

public override void Initializing(InitializingAIProviderConnectionContext context)
{
if (!string.Equals(context.Connection.ClientName, AzureOpenAIConstants.ClientName, StringComparison.Ordinal))
{
return;
}

var metadata = context.Connection.GetOrCreate<AzureConnectionMetadata>();

context.Values["Endpoint"] = metadata.Endpoint?.ToString();
context.Values["AuthenticationType"] = metadata.AuthenticationType.ToString();
context.Values["IdentityId"] = metadata.IdentityId;

if (!string.IsNullOrEmpty(metadata.ApiKey))
{
var protector = _dataProtectionProvider.CreateProtector(ConnectionProtectorName);

context.Values["ApiKey"] = protector.Unprotect(metadata.ApiKey);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CrestApps.Core.AI.Clients;
using CrestApps.Core.AI.Models;
using CrestApps.Core.AI.OpenAI.Azure.Handlers;
using CrestApps.Core.AI.OpenAI.Azure.Models;
using CrestApps.Core.AI.OpenAI.Azure.Services;
using CrestApps.Core.Builders;
Expand All @@ -22,6 +24,7 @@ public static IServiceCollection AddCoreAIAzureOpenAI(this IServiceCollection se
services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<AzureClientOptions>, AzureClientOptionsConfiguration>());
services.TryAddEnumerable(ServiceDescriptor.Scoped<IAIClientProvider, AzureOpenAIClientProvider>());
services.TryAddEnumerable(ServiceDescriptor.Scoped<IAIClientProvider, AzureSpeechClientProvider>());
services.TryAddEnumerable(ServiceDescriptor.Scoped<IAIProviderConnectionHandler, AzureOpenAIConnectionHandler>());

services.AddCoreAIProfile<AzureOpenAICompletionClient>(AzureOpenAIConstants.ClientName, o =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using CrestApps.Core.AI.Handlers;
using CrestApps.Core.AI.Models;
using CrestApps.Core.AI.OpenAI.Models;
using Microsoft.AspNetCore.DataProtection;

namespace CrestApps.Core.AI.OpenAI.Handlers;

internal sealed class OpenAIConnectionHandler : AIProviderConnectionHandlerBase
{
private const string ConnectionProtectorName = "AIProviderConnection";

private readonly IDataProtectionProvider _dataProtectionProvider;

public OpenAIConnectionHandler(IDataProtectionProvider dataProtectionProvider)
{
_dataProtectionProvider = dataProtectionProvider;
}

public override void Initializing(InitializingAIProviderConnectionContext context)
{
if (!string.Equals(context.Connection.ClientName, OpenAIConstants.ClientName, StringComparison.Ordinal))
{
return;
}

var metadata = context.Connection.GetOrCreate<OpenAIConnectionMetadata>();

if (!string.IsNullOrEmpty(metadata.ApiKey))
{
var protector = _dataProtectionProvider.CreateProtector(ConnectionProtectorName);

context.Values["ApiKey"] = protector.Unprotect(metadata.ApiKey);
}

context.Values["Endpoint"] = metadata.Endpoint?.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using CrestApps.Core.AI.Clients;
using CrestApps.Core.AI.Models;
using CrestApps.Core.AI.OpenAI.Handlers;
using CrestApps.Core.AI.OpenAI.Services;
using CrestApps.Core.Builders;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -17,6 +19,7 @@ public static IServiceCollection AddCoreAIOpenAI(this IServiceCollection service
ArgumentNullException.ThrowIfNull(services);

services.TryAddEnumerable(ServiceDescriptor.Scoped<IAIClientProvider, OpenAIClientProvider>());
services.TryAddEnumerable(ServiceDescriptor.Scoped<IAIProviderConnectionHandler, OpenAIConnectionHandler>());

services.AddCoreAIProfile<OpenAICompletionClient>(OpenAIConstants.ClientName, o =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CrestApps.Core.AI;
using CrestApps.Core.AI.Deployments;
using CrestApps.Core.AI.Models;
using CrestApps.Core.AI.OpenAI;
using CrestApps.Core.AI.OpenAI.Azure;
using CrestApps.Core.AI.OpenAI.Azure.Models;
using CrestApps.Core.AI.Services;
Expand Down Expand Up @@ -71,6 +72,36 @@ public void AddCoreAIAzureOpenAI_WhenAzureClientSettingsConfigured_ShouldBindAzu
Assert.False(options.EnableMessageContentLogging);
}

[Fact]
public void AddCoreAIAzureOpenAI_ShouldRegisterAzureConnectionHandler()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddDataProtection();
services.AddCoreAIServices();
services.AddCoreAIAzureOpenAI();
using var serviceProvider = services.BuildServiceProvider();

var handlers = serviceProvider.GetServices<IAIProviderConnectionHandler>().ToArray();

Assert.Contains(handlers, handler => handler.GetType().FullName == "CrestApps.Core.AI.OpenAI.Azure.Handlers.AzureOpenAIConnectionHandler");
}

[Fact]
public void AddCoreAIOpenAI_ShouldRegisterOpenAIConnectionHandler()
{
var services = new ServiceCollection();
services.AddLogging();
services.AddDataProtection();
services.AddCoreAIServices();
services.AddCoreAIOpenAI();
using var serviceProvider = services.BuildServiceProvider();

var handlers = serviceProvider.GetServices<IAIProviderConnectionHandler>().ToArray();

Assert.Contains(handlers, handler => handler.GetType().FullName == "CrestApps.Core.AI.OpenAI.Handlers.OpenAIConnectionHandler");
}

[Fact]
public void AddCrestAppsAI_WhenClientNameIsMissing_ShouldIgnoreConnectionEntry()
{
Expand Down
Loading