| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Extension methods for <see cref="IServiceCollection"/> that register the Needlr Agent Framework |
| | | 7 | | /// infrastructure directly, without requiring the <see cref="Injection.ConfiguredSyringe"/> fluent builder. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// Use this when registering the agent framework from an <see cref="IServiceCollectionPlugin"/> |
| | | 12 | | /// so that feature projects can self-register without modifying the composition root: |
| | | 13 | | /// </para> |
| | | 14 | | /// <code> |
| | | 15 | | /// public sealed class AgentFrameworkPlugin : IServiceCollectionPlugin |
| | | 16 | | /// { |
| | | 17 | | /// public void Configure(ServiceCollectionPluginOptions options) |
| | | 18 | | /// { |
| | | 19 | | /// options.Services.AddNeedlrAgentFramework(); |
| | | 20 | | /// } |
| | | 21 | | /// } |
| | | 22 | | /// </code> |
| | | 23 | | /// <para> |
| | | 24 | | /// The configure overloads let plugins own the full agent-framework configuration surface |
| | | 25 | | /// (custom <see cref="Microsoft.Extensions.AI.IChatClient"/>, metrics meter / activity source |
| | | 26 | | /// names, diagnostics, token budget tracking, etc.) without exfiltrating that configuration |
| | | 27 | | /// to the composition root: |
| | | 28 | | /// </para> |
| | | 29 | | /// <code> |
| | | 30 | | /// public sealed class AgentFrameworkPlugin : IServiceCollectionPlugin |
| | | 31 | | /// { |
| | | 32 | | /// public void Configure(ServiceCollectionPluginOptions options) |
| | | 33 | | /// { |
| | | 34 | | /// options.Services.AddNeedlrAgentFramework(af => af |
| | | 35 | | /// .ConfigureMetrics(o => |
| | | 36 | | /// { |
| | | 37 | | /// o.MeterName = "MyApp.Agents"; |
| | | 38 | | /// o.ActivitySourceName = "MyApp.Agents"; |
| | | 39 | | /// })); |
| | | 40 | | /// } |
| | | 41 | | /// } |
| | | 42 | | /// </code> |
| | | 43 | | /// <para> |
| | | 44 | | /// All overloads call the same code path as |
| | | 45 | | /// <see cref="SyringeExtensionsForAgentFramework.UsingAgentFramework(Injection.ConfiguredSyringe)"/> |
| | | 46 | | /// — zero duplication, zero drift between the two entry points. |
| | | 47 | | /// </para> |
| | | 48 | | /// <para> |
| | | 49 | | /// When both this entry point and <c>UsingAgentFramework</c> are used in the same application, |
| | | 50 | | /// the first registration wins (<c>TryAddSingleton</c> semantics). Configuration delegates |
| | | 51 | | /// supplied by later registrations are silently discarded. |
| | | 52 | | /// </para> |
| | | 53 | | /// </remarks> |
| | | 54 | | public static class ServiceCollectionAgentFrameworkExtensions |
| | | 55 | | { |
| | | 56 | | /// <summary> |
| | | 57 | | /// Registers the full Needlr Agent Framework infrastructure on the service collection. |
| | | 58 | | /// </summary> |
| | | 59 | | /// <param name="services">The service collection to register into.</param> |
| | | 60 | | /// <returns>The same <paramref name="services"/> instance for chaining.</returns> |
| | | 61 | | public static IServiceCollection AddNeedlrAgentFramework( |
| | | 62 | | this IServiceCollection services) |
| | | 63 | | { |
| | 8 | 64 | | ArgumentNullException.ThrowIfNull(services); |
| | | 65 | | |
| | 8 | 66 | | SyringeExtensionsForAgentFramework.RegisterAgentFrameworkCore(services); |
| | | 67 | | |
| | 8 | 68 | | return services; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Registers the full Needlr Agent Framework infrastructure on the service collection |
| | | 73 | | /// with a configurable <see cref="AgentFrameworkSyringe"/>. |
| | | 74 | | /// </summary> |
| | | 75 | | /// <param name="services">The service collection to register into.</param> |
| | | 76 | | /// <param name="configure"> |
| | | 77 | | /// A delegate that receives a pre-initialized <see cref="AgentFrameworkSyringe"/> |
| | | 78 | | /// (with its <see cref="AgentFrameworkSyringe.ServiceProvider"/> set) and |
| | | 79 | | /// returns the configured instance used to build the agent factory. Invoked |
| | | 80 | | /// lazily the first time <see cref="BuiltAgentFrameworkSyringe"/> is resolved. |
| | | 81 | | /// </param> |
| | | 82 | | /// <returns>The same <paramref name="services"/> instance for chaining.</returns> |
| | | 83 | | /// <exception cref="ArgumentNullException"> |
| | | 84 | | /// Thrown when <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>. |
| | | 85 | | /// </exception> |
| | | 86 | | public static IServiceCollection AddNeedlrAgentFramework( |
| | | 87 | | this IServiceCollection services, |
| | | 88 | | Func<AgentFrameworkSyringe, AgentFrameworkSyringe> configure) |
| | | 89 | | { |
| | 483 | 90 | | ArgumentNullException.ThrowIfNull(services); |
| | 482 | 91 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 92 | | |
| | 481 | 93 | | SyringeExtensionsForAgentFramework.RegisterAgentFrameworkCore(services, configure); |
| | | 94 | | |
| | 481 | 95 | | return services; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Registers the full Needlr Agent Framework infrastructure on the service collection |
| | | 100 | | /// using an <see cref="AgentFrameworkSyringe"/> created by the supplied factory. |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="services">The service collection to register into.</param> |
| | | 103 | | /// <param name="configure"> |
| | | 104 | | /// A factory that creates a fully-configured <see cref="AgentFrameworkSyringe"/> |
| | | 105 | | /// used to build the agent factory. Useful when configuration does not need the |
| | | 106 | | /// service provider. Routes through the |
| | | 107 | | /// <see cref="AddNeedlrAgentFramework(IServiceCollection, Func{AgentFrameworkSyringe, AgentFrameworkSyringe})"/> |
| | | 108 | | /// overload so both paths share the same <see cref="BuiltAgentFrameworkSyringe"/> |
| | | 109 | | /// construction and progress sink wiring. |
| | | 110 | | /// </param> |
| | | 111 | | /// <returns>The same <paramref name="services"/> instance for chaining.</returns> |
| | | 112 | | /// <exception cref="ArgumentNullException"> |
| | | 113 | | /// Thrown when <paramref name="services"/> or <paramref name="configure"/> is <see langword="null"/>. |
| | | 114 | | /// </exception> |
| | | 115 | | public static IServiceCollection AddNeedlrAgentFramework( |
| | | 116 | | this IServiceCollection services, |
| | | 117 | | Func<AgentFrameworkSyringe> configure) |
| | | 118 | | { |
| | 3 | 119 | | ArgumentNullException.ThrowIfNull(services); |
| | 3 | 120 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 121 | | |
| | 4 | 122 | | return services.AddNeedlrAgentFramework(_ => configure.Invoke()); |
| | | 123 | | } |
| | | 124 | | } |