| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.AgentFramework.Context; |
| | | 5 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Extension methods that register the small set of Needlr Agent Framework accessors |
| | | 11 | | /// that tools and test harnesses depend on, without registering the full |
| | | 12 | | /// <see cref="IAgentFactory"/> / <see cref="WorkflowFactory"/> / iterative-loop infrastructure. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// <para> |
| | | 16 | | /// Use this when you need the Needlr accessors (<see cref="IAgentExecutionContextAccessor"/>, |
| | | 17 | | /// <see cref="IAgentDiagnosticsAccessor"/>, <see cref="IInFlightAgentDiagnosticsAccessor"/>, |
| | | 18 | | /// <see cref="IAgentDiagnosticsWriter"/>) but do not want the rest of the Agent Framework |
| | | 19 | | /// wiring — typically because you are constructing a minimal service provider for a |
| | | 20 | | /// tool-level test. |
| | | 21 | | /// </para> |
| | | 22 | | /// <para> |
| | | 23 | | /// The <c>UsingAgentFramework()</c> extension on <see cref="NexusLabs.Needlr.Injection.ConfiguredSyringe"/> |
| | | 24 | | /// already registers these accessors as part of its broader infrastructure setup. Calling |
| | | 25 | | /// <see cref="AddAgentFrameworkAccessors"/> after <c>UsingAgentFramework()</c> is a no-op because |
| | | 26 | | /// every registration uses <see cref="ServiceCollectionDescriptorExtensions.TryAdd(IServiceCollection, ServiceDescripto |
| | | 27 | | /// </para> |
| | | 28 | | /// </remarks> |
| | | 29 | | public static class AgentFrameworkAccessorServiceCollectionExtensions |
| | | 30 | | { |
| | | 31 | | /// <summary> |
| | | 32 | | /// Registers the Needlr Agent Framework accessor singletons: |
| | | 33 | | /// <see cref="IAgentExecutionContextAccessor"/>, <see cref="IAgentDiagnosticsAccessor"/>, |
| | | 34 | | /// <see cref="IInFlightAgentDiagnosticsAccessor"/>, and <see cref="IAgentDiagnosticsWriter"/>. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="services">The service collection to add registrations to.</param> |
| | | 37 | | /// <returns>The same <paramref name="services"/> instance for chaining.</returns> |
| | | 38 | | /// <remarks> |
| | | 39 | | /// All registrations use <c>TryAddSingleton</c>, so calling this method is safe even if the |
| | | 40 | | /// accessors have already been registered by <c>UsingAgentFramework()</c> or another path. |
| | | 41 | | /// </remarks> |
| | | 42 | | public static IServiceCollection AddAgentFrameworkAccessors(this IServiceCollection services) |
| | | 43 | | { |
| | 78 | 44 | | ArgumentNullException.ThrowIfNull(services); |
| | | 45 | | |
| | 78 | 46 | | services.TryAddSingleton<IAgentExecutionContextAccessor, AgentExecutionContextAccessor>(); |
| | 78 | 47 | | services.TryAddSingleton<AgentDiagnosticsAccessor>(sp => |
| | 78 | 48 | | new AgentDiagnosticsAccessor( |
| | 78 | 49 | | sp.GetService<ChatCompletionCollectorHolder>(), |
| | 78 | 50 | | sp.GetService<ToolCallCollectorHolder>())); |
| | 78 | 51 | | services.TryAddSingleton<IAgentDiagnosticsAccessor>(sp => |
| | 78 | 52 | | sp.GetRequiredService<AgentDiagnosticsAccessor>()); |
| | 78 | 53 | | services.TryAddSingleton<IAgentDiagnosticsWriter>(sp => |
| | 78 | 54 | | sp.GetRequiredService<AgentDiagnosticsAccessor>()); |
| | 78 | 55 | | services.TryAddSingleton<IInFlightAgentDiagnosticsAccessor, InFlightAgentDiagnosticsAccessor>(); |
| | | 56 | | |
| | 78 | 57 | | return services; |
| | | 58 | | } |
| | | 59 | | } |