77
88namespace Devlooped . Agents . AI ;
99
10+ /// <summary>
11+ /// A configuration-driven <see cref="AIAgent"/> which monitors configuration changes and
12+ /// re-applies them to the inner agent automatically.
13+ /// </summary>
1014public sealed partial class ConfigurableAIAgent : AIAgent , IDisposable
1115{
1216 readonly IServiceProvider services ;
@@ -36,24 +40,34 @@ public ConfigurableAIAgent(IServiceProvider services, string section, string nam
3640 reloadToken = configuration . GetReloadToken ( ) . RegisterChangeCallback ( OnReload , state : null ) ;
3741 }
3842
43+ /// <summary>Disposes the client and stops monitoring configuration changes.</summary>
3944 public void Dispose ( ) => reloadToken ? . Dispose ( ) ;
4045
46+ /// <inheritdoc/>
4147 public override object ? GetService ( Type serviceType , object ? serviceKey = null ) => serviceType switch
4248 {
4349 Type t when t == typeof ( ChatClientAgentOptions ) => options ,
4450 Type t when t == typeof ( IChatClient ) => chat ,
4551 _ => agent . GetService ( serviceType , serviceKey )
4652 } ;
4753
54+ /// <inheritdoc/>
4855 public override string Id => agent . Id ;
56+ /// <inheritdoc/>
4957 public override string ? Description => agent . Description ;
58+ /// <inheritdoc/>
5059 public override string DisplayName => agent . DisplayName ;
51- public override string ? Name => agent . Name ;
60+ /// <inheritdoc/>
61+ public override string ? Name => this . name ;
62+ /// <inheritdoc/>
5263 public override AgentThread DeserializeThread ( JsonElement serializedThread , JsonSerializerOptions ? jsonSerializerOptions = null )
5364 => agent . DeserializeThread ( serializedThread , jsonSerializerOptions ) ;
65+ /// <inheritdoc/>
5466 public override AgentThread GetNewThread ( ) => agent . GetNewThread ( ) ;
67+ /// <inheritdoc/>
5568 public override Task < AgentRunResponse > RunAsync ( IEnumerable < ChatMessage > messages , AgentThread ? thread = null , AgentRunOptions ? options = null , CancellationToken cancellationToken = default )
5669 => agent . RunAsync ( messages , thread , options , cancellationToken ) ;
70+ /// <inheritdoc/>
5771 public override IAsyncEnumerable < AgentRunResponseUpdate > RunStreamingAsync ( IEnumerable < ChatMessage > messages , AgentThread ? thread = null , AgentRunOptions ? options = null , CancellationToken cancellationToken = default )
5872 => agent . RunStreamingAsync ( messages , thread , options , cancellationToken ) ;
5973
@@ -75,6 +89,15 @@ public override IAsyncEnumerable<AgentRunResponseUpdate> RunStreamingAsync(IEnum
7589
7690 configure ? . Invoke ( name , options ) ;
7791
92+ if ( options . AIContextProviderFactory is null )
93+ {
94+ var contextFactory = services . GetKeyedService < AIContextProviderFactory > ( name ) ??
95+ services . GetService < AIContextProviderFactory > ( ) ;
96+
97+ if ( contextFactory is not null )
98+ options . AIContextProviderFactory = contextFactory . CreateProvider ;
99+ }
100+
78101 LogConfigured ( name ) ;
79102
80103 return ( new ChatClientAgent ( client , options , services . GetRequiredService < ILoggerFactory > ( ) , services ) , options , client ) ;
0 commit comments