diff --git a/src/Primitives/CrestApps.Core.AI.OpenAI.Azure/Handlers/AzureOpenAIConnectionHandler.cs b/src/Primitives/CrestApps.Core.AI.OpenAI.Azure/Handlers/AzureOpenAIConnectionHandler.cs index d1092982..0e9bb358 100644 --- a/src/Primitives/CrestApps.Core.AI.OpenAI.Azure/Handlers/AzureOpenAIConnectionHandler.cs +++ b/src/Primitives/CrestApps.Core.AI.OpenAI.Azure/Handlers/AzureOpenAIConnectionHandler.cs @@ -23,6 +23,11 @@ public override void Initializing(InitializingAIProviderConnectionContext contex return; } + if (!context.Connection.Has()) + { + return; + } + var metadata = context.Connection.GetOrCreate(); context.Values["Endpoint"] = metadata.Endpoint?.ToString(); diff --git a/src/Primitives/CrestApps.Core.AI.OpenAI/Handlers/OpenAIConnectionHandler.cs b/src/Primitives/CrestApps.Core.AI.OpenAI/Handlers/OpenAIConnectionHandler.cs index f2b79253..0cb109e9 100644 --- a/src/Primitives/CrestApps.Core.AI.OpenAI/Handlers/OpenAIConnectionHandler.cs +++ b/src/Primitives/CrestApps.Core.AI.OpenAI/Handlers/OpenAIConnectionHandler.cs @@ -23,7 +23,10 @@ public override void Initializing(InitializingAIProviderConnectionContext contex return; } - var metadata = context.Connection.GetOrCreate(); + if (!context.Connection.TryGet(out var metadata)) + { + return; + } if (!string.IsNullOrEmpty(metadata.ApiKey)) { diff --git a/src/Primitives/CrestApps.Core.AI/Services/AIProviderConnectionEntryFactory.cs b/src/Primitives/CrestApps.Core.AI/Services/AIProviderConnectionEntryFactory.cs index 953592fd..d22e060d 100644 --- a/src/Primitives/CrestApps.Core.AI/Services/AIProviderConnectionEntryFactory.cs +++ b/src/Primitives/CrestApps.Core.AI/Services/AIProviderConnectionEntryFactory.cs @@ -35,6 +35,11 @@ public static AIProviderConnectionEntry Create(AIProviderConnection connection, foreach (var value in context.Values) { + if (value.Value == null) + { + continue; + } + values[value.Key] = value.Value; } } diff --git a/tests/CrestApps.Core.Tests/Core/Services/AIProviderConnectionEntryFactoryTests.cs b/tests/CrestApps.Core.Tests/Core/Services/AIProviderConnectionEntryFactoryTests.cs index 8c20d092..8f1edfd3 100644 --- a/tests/CrestApps.Core.Tests/Core/Services/AIProviderConnectionEntryFactoryTests.cs +++ b/tests/CrestApps.Core.Tests/Core/Services/AIProviderConnectionEntryFactoryTests.cs @@ -38,6 +38,29 @@ public void Create_AppliesInitializingHandlersToStoredConnection() Assert.Equal("eastus", entry["Region"]); } + [Fact] + public void Create_DoesNotOverwriteExistingValuesWithNullHandlerValues() + { + var connection = new AIProviderConnection + { + Name = "azure-connection", + DisplayText = "Azure Connection", + ClientName = "azure-openai", + Properties = new Dictionary(StringComparer.OrdinalIgnoreCase) + { + ["Endpoint"] = "https://flat.openai.azure.com/", + ["AuthenticationType"] = "ApiKey", + ["ApiKey"] = "flat-key", + }, + }; + + var entry = AIProviderConnectionEntryFactory.Create(connection, [new TestAzureConnectionHandler()]); + + Assert.Equal("https://flat.openai.azure.com/", entry["Endpoint"]); + Assert.Equal("ApiKey", entry["AuthenticationType"]); + Assert.Equal("flat-key", entry["ApiKey"]); + } + private sealed class TestAzureConnectionHandler : IAIProviderConnectionHandler { public void Exporting(ExportingAIProviderConnectionContext context) @@ -46,6 +69,11 @@ public void Exporting(ExportingAIProviderConnectionContext context) public void Initializing(InitializingAIProviderConnectionContext context) { + if (!context.Connection.Has()) + { + return; + } + var metadata = context.Connection.GetOrCreate(); context.Values["Endpoint"] = metadata.Endpoint?.ToString();