Skip to content

Commit 643c819

Browse files
CopilotEvangelink
andauthored
Respect unavailable AI chat client providers (#8453)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
1 parent 6b1b2e5 commit 643c819

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

src/Platform/Microsoft.Testing.Platform.AI/ChatClientProviderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public static void AddChatClientProvider(this ITestApplicationBuilder testApplic
3333
/// </summary>
3434
/// <param name="serviceProvider">The service provider.</param>
3535
/// <param name="cancellationToken">The cancellation token.</param>
36-
/// <returns>A task representing the asynchronous operation that returns an instance of <see cref="IChatClient"/>.</returns>
36+
/// <returns>A task representing the asynchronous operation that returns an instance of <see cref="IChatClient"/>, or <see langword="null"/> when no available provider is registered.</returns>
3737
public static async Task<IChatClient?> GetChatClientAsync(this IServiceProvider serviceProvider, CancellationToken cancellationToken)
3838
{
3939
var provider = (IChatClientProvider?)serviceProvider.GetService(typeof(IChatClientProvider));
40-
return provider is null
40+
return provider is null || !provider.IsAvailable
4141
? null
4242
: await provider.CreateChatClientAsync(cancellationToken).ConfigureAwait(false);
4343
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Microsoft.Extensions.AI;
5+
using Microsoft.Testing.Platform.AI;
6+
using Microsoft.Testing.Platform.Services;
7+
8+
namespace Microsoft.Testing.Platform.UnitTests.AI;
9+
10+
#pragma warning disable TPEXP // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
11+
12+
[TestClass]
13+
public sealed class ChatClientProviderExtensionsTests
14+
{
15+
[TestMethod]
16+
public async Task GetChatClientAsync_WhenProviderIsUnavailable_ReturnsNull()
17+
{
18+
ServiceProvider serviceProvider = new();
19+
UnavailableChatClientProvider provider = new();
20+
serviceProvider.AddService(provider);
21+
22+
IChatClient? chatClient = await serviceProvider.GetChatClientAsync(CancellationToken.None).ConfigureAwait(false);
23+
24+
Assert.IsNull(chatClient);
25+
Assert.AreEqual(1, provider.IsAvailableCallCount);
26+
Assert.AreEqual(0, provider.CreateChatClientAsyncCallCount);
27+
}
28+
29+
private sealed class UnavailableChatClientProvider : IChatClientProvider
30+
{
31+
public int IsAvailableCallCount { get; private set; }
32+
33+
public int CreateChatClientAsyncCallCount { get; private set; }
34+
35+
public bool IsAvailable
36+
{
37+
get
38+
{
39+
IsAvailableCallCount++;
40+
return false;
41+
}
42+
}
43+
44+
public bool HasToolsCapability => false;
45+
46+
public string ModelName => "Unavailable";
47+
48+
public Task<IChatClient> CreateChatClientAsync(CancellationToken cancellationToken)
49+
{
50+
CreateChatClientAsyncCallCount++;
51+
throw new InvalidOperationException("CreateChatClientAsync should not be called for unavailable providers.");
52+
}
53+
}
54+
}
55+
56+
#pragma warning restore TPEXP

test/UnitTests/Microsoft.Testing.Platform.UnitTests/Microsoft.Testing.Platform.UnitTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939
<PackageReference Include="Moq" />
4040
</ItemGroup>
4141

42+
<ItemGroup>
43+
<ProjectReference Include="$(RepoRoot)src\Platform\Microsoft.Testing.Platform.AI\Microsoft.Testing.Platform.AI.csproj">
44+
<SetTargetFramework Condition="$([MSBuild]::GetTargetFrameworkIdentifier('$(TargetFramework)')) != '.NETCoreApp'">TargetFramework=netstandard2.0</SetTargetFramework>
45+
</ProjectReference>
46+
</ItemGroup>
47+
4248
<ItemGroup>
4349
<Compile Include="$(RepoRoot)src/Polyfills/**/*.cs" Link="Polyfills\%(RecursiveDir)%(Filename)%(Extension)" />
4450
</ItemGroup>

0 commit comments

Comments
 (0)