Skip to content

[MEAI] IChatClient dependency extensions should support lifetimes beyond singleton #5875

@eiriktsarpalis

Description

@eiriktsarpalis

The IServiceCollection extensions provided with the MEAI package are hardcoding singleton lifetimes for registered services:

public static ChatClientBuilder AddChatClient(
this IServiceCollection serviceCollection,
Func<IServiceProvider, IChatClient> innerClientFactory)
{
_ = Throw.IfNull(serviceCollection);
_ = Throw.IfNull(innerClientFactory);
var builder = new ChatClientBuilder(innerClientFactory);
_ = serviceCollection.AddSingleton(builder.Build);
return builder;
}

This doesn't work well in the context of Github Copilot agents that forward to the Github completions API since the clients need to be scoped to the specific Github token used in the request. We should consider extending the helpers so that all lifetimes are supported -- most likely this is going to be a breaking change since we'd need to rename the methods to adhere to naming conventions used DI elsewhere: AddSingletonChatClient, AddScopedChatClient, AddTransientChatClient, etc.

cc @stephentoub @SteveSandersonMS

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions