| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.AgentFramework.Budget; |
| | | 5 | | using NexusLabs.Needlr.AgentFramework.Context; |
| | | 6 | | using NexusLabs.Needlr.AgentFramework.Diagnostics; |
| | | 7 | | using NexusLabs.Needlr.AgentFramework.Iterative; |
| | | 8 | | using NexusLabs.Needlr.AgentFramework.Progress; |
| | | 9 | | using NexusLabs.Needlr.Injection; |
| | | 10 | | |
| | | 11 | | namespace NexusLabs.Needlr.AgentFramework; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Extension methods for <see cref="ConfiguredSyringe"/> that enable registering |
| | | 15 | | /// Microsoft Agent Framework infrastructure (namely <see cref="IAgentFactory"/>) |
| | | 16 | | /// as part of the Needlr build pipeline. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// <para> |
| | | 20 | | /// These helpers defer service registration using the Syringe |
| | | 21 | | /// post-plugin registration callback so that function discovery and |
| | | 22 | | /// registration are completed before the agent factory is added. |
| | | 23 | | /// </para> |
| | | 24 | | /// <para> |
| | | 25 | | /// When the Needlr source generator is active, the generated <c>[ModuleInitializer]</c> registers |
| | | 26 | | /// an <see cref="IAIFunctionProvider"/> that is used instead of reflection. This makes the |
| | | 27 | | /// integration NativeAOT-compatible without any code changes. |
| | | 28 | | /// </para> |
| | | 29 | | /// <para> |
| | | 30 | | /// When the source generator is not used, the integration falls back to reflection-based |
| | | 31 | | /// <see cref="Microsoft.Extensions.AI.AIFunction"/> schema generation, which requires dynamic code. |
| | | 32 | | /// </para> |
| | | 33 | | /// </remarks> |
| | | 34 | | public static class SyringeExtensionsForAgentFramework |
| | | 35 | | { |
| | | 36 | | /// <summary> |
| | | 37 | | /// Registers an <see cref="IAgentFactory"/> built via a |
| | | 38 | | /// <see cref="AgentFrameworkSyringe"/> instance. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="syringe"> |
| | | 41 | | /// The <see cref="ConfiguredSyringe"/> to augment with the registration. |
| | | 42 | | /// </param> |
| | | 43 | | /// <returns> |
| | | 44 | | /// A new <see cref="ConfiguredSyringe"/> instance containing the registration. |
| | | 45 | | /// </returns> |
| | | 46 | | public static ConfiguredSyringe UsingAgentFramework( |
| | | 47 | | this ConfiguredSyringe syringe) |
| | | 48 | | { |
| | 8 | 49 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 16 | 50 | | return syringe.UsingAgentFramework(s => s); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Registers an <see cref="IAgentFactory"/> built via a configurable |
| | | 55 | | /// <see cref="AgentFrameworkSyringe"/> instance. |
| | | 56 | | /// </summary> |
| | | 57 | | /// <param name="syringe"> |
| | | 58 | | /// The <see cref="ConfiguredSyringe"/> to augment with the registration. |
| | | 59 | | /// </param> |
| | | 60 | | /// <param name="configure"> |
| | | 61 | | /// A delegate that receives a pre-initialized <see cref="AgentFrameworkSyringe"/> |
| | | 62 | | /// (with its <see cref="AgentFrameworkSyringe.ServiceProvider"/> set) and |
| | | 63 | | /// returns the configured instance used to build the agent factory. |
| | | 64 | | /// </param> |
| | | 65 | | /// <returns> |
| | | 66 | | /// A new <see cref="ConfiguredSyringe"/> instance containing the registration. |
| | | 67 | | /// </returns> |
| | | 68 | | public static ConfiguredSyringe UsingAgentFramework( |
| | | 69 | | this ConfiguredSyringe syringe, |
| | | 70 | | Func<AgentFrameworkSyringe, AgentFrameworkSyringe> configure) |
| | | 71 | | { |
| | 222 | 72 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 222 | 73 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 74 | | |
| | 222 | 75 | | return syringe.UsingPostPluginRegistrationCallback(services => |
| | 222 | 76 | | { |
| | 222 | 77 | | RegisterAgentFrameworkCore(services, configure); |
| | 444 | 78 | | }); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Registers an <see cref="IAgentFactory"/> built via an |
| | | 83 | | /// <see cref="AgentFrameworkSyringe"/> created by the supplied delegate. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="syringe"> |
| | | 86 | | /// The <see cref="ConfiguredSyringe"/> to augment with the registration. |
| | | 87 | | /// </param> |
| | | 88 | | /// <param name="configure"> |
| | | 89 | | /// A factory that creates a fully-configured <see cref="AgentFrameworkSyringe"/> |
| | | 90 | | /// used to build the agent factory. Useful when configuration does |
| | | 91 | | /// not need the service provider. |
| | | 92 | | /// </param> |
| | | 93 | | /// <returns> |
| | | 94 | | /// A new <see cref="ConfiguredSyringe"/> instance containing the registration. |
| | | 95 | | /// </returns> |
| | | 96 | | public static ConfiguredSyringe UsingAgentFramework( |
| | | 97 | | this ConfiguredSyringe syringe, |
| | | 98 | | Func<AgentFrameworkSyringe> configure) |
| | | 99 | | { |
| | 1 | 100 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 1 | 101 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 102 | | |
| | | 103 | | // Route through the configurable overload so both paths share the same |
| | | 104 | | // BuiltAgentFrameworkSyringe holder, progress sink wiring, and |
| | | 105 | | // IProgressReporterFactory construction. |
| | 2 | 106 | | return syringe.UsingAgentFramework(_ => configure.Invoke()); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Shared implementation for agent framework registration. Called by both |
| | | 111 | | /// <see cref="UsingAgentFramework(ConfiguredSyringe, Func{AgentFrameworkSyringe, AgentFrameworkSyringe})"/> |
| | | 112 | | /// and <see cref="ServiceCollectionAgentFrameworkExtensions.AddNeedlrAgentFramework(IServiceCollection)"/> |
| | | 113 | | /// (and its configure overloads) to ensure a single code path with zero drift between the two entry points. |
| | | 114 | | /// </summary> |
| | | 115 | | internal static void RegisterAgentFrameworkCore( |
| | | 116 | | IServiceCollection services, |
| | | 117 | | Func<AgentFrameworkSyringe, AgentFrameworkSyringe>? configure = null) |
| | | 118 | | { |
| | 711 | 119 | | RegisterAgentFrameworkInfrastructure(services); |
| | | 120 | | |
| | 718 | 121 | | configure ??= s => s; |
| | | 122 | | |
| | 711 | 123 | | services.TryAddSingleton<BuiltAgentFrameworkSyringe>(sp => |
| | 711 | 124 | | { |
| | 225 | 125 | | AgentFrameworkSyringe afSyringe = new() |
| | 225 | 126 | | { |
| | 225 | 127 | | ServiceProvider = sp, |
| | 225 | 128 | | }; |
| | 225 | 129 | | afSyringe = configure.Invoke(afSyringe); |
| | 711 | 130 | | |
| | 225 | 131 | | if ((afSyringe.FunctionTypes is null || afSyringe.FunctionTypes.Count == 0) && |
| | 225 | 132 | | AgentFrameworkGeneratedBootstrap.TryGetFunctionTypes(out var functionProvider)) |
| | 711 | 133 | | { |
| | 179 | 134 | | afSyringe = afSyringe with { FunctionTypes = functionProvider().ToList() }; |
| | 711 | 135 | | } |
| | 711 | 136 | | |
| | 225 | 137 | | if ((afSyringe.FunctionGroupMap is null || afSyringe.FunctionGroupMap.Count == 0) && |
| | 225 | 138 | | AgentFrameworkGeneratedBootstrap.TryGetGroupTypes(out var groupProvider)) |
| | 711 | 139 | | { |
| | 208 | 140 | | afSyringe = afSyringe with { FunctionGroupMap = groupProvider() }; |
| | 711 | 141 | | } |
| | 711 | 142 | | |
| | 225 | 143 | | if ((afSyringe.AgentTypes is null || afSyringe.AgentTypes.Count == 0) && |
| | 225 | 144 | | AgentFrameworkGeneratedBootstrap.TryGetAgentTypes(out var agentProvider)) |
| | 711 | 145 | | { |
| | 126 | 146 | | afSyringe = afSyringe with { AgentTypes = agentProvider().ToList() }; |
| | 711 | 147 | | } |
| | 711 | 148 | | |
| | 225 | 149 | | return new BuiltAgentFrameworkSyringe(afSyringe); |
| | 711 | 150 | | }); |
| | | 151 | | |
| | 711 | 152 | | services.TryAddSingleton<IProgressReporterFactory>(sp => |
| | 711 | 153 | | { |
| | 18 | 154 | | var defaultSinks = sp.GetServices<IProgressSink>(); |
| | 18 | 155 | | return new ProgressReporterFactory( |
| | 18 | 156 | | defaultSinks, |
| | 18 | 157 | | sp.GetRequiredService<IProgressSequence>(), |
| | 18 | 158 | | sp.GetRequiredService<IProgressReporterErrorHandler>()); |
| | 711 | 159 | | }); |
| | | 160 | | |
| | 711 | 161 | | services.TryAddSingleton<IAgentFactory>(sp => |
| | 905 | 162 | | sp.GetRequiredService<BuiltAgentFrameworkSyringe>().Value.BuildAgentFactory()); |
| | | 163 | | |
| | 711 | 164 | | services.TryAddSingleton<IChatClientAccessor>(sp => |
| | 711 | 165 | | { |
| | 47 | 166 | | var afSyringe = sp.GetRequiredService<BuiltAgentFrameworkSyringe>().Value; |
| | 47 | 167 | | return new ChatClientAccessor(sp, afSyringe.ConfigureAgentFactory ?? []); |
| | 711 | 168 | | }); |
| | | 169 | | |
| | 711 | 170 | | services.TryAddSingleton<IIterativeAgentLoop>(sp => |
| | 724 | 171 | | new IterativeAgentLoop( |
| | 724 | 172 | | sp.GetRequiredService<IChatClientAccessor>(), |
| | 724 | 173 | | sp.GetService<IAgentDiagnosticsWriter>(), |
| | 724 | 174 | | sp.GetService<IAgentExecutionContextAccessor>(), |
| | 724 | 175 | | sp.GetService<IProgressReporterAccessor>(), |
| | 724 | 176 | | sp.GetService<ITokenBudgetTracker>(), |
| | 724 | 177 | | sp.GetService<IAgentMetrics>(), |
| | 724 | 178 | | genAiTokenMetrics: sp.GetService<IGenAiTokenMetrics>())); |
| | | 179 | | |
| | 711 | 180 | | services.TryAddSingleton<IWorkflowFactory>(provider => |
| | 801 | 181 | | new WorkflowFactory(provider.GetRequiredService<IAgentFactory>())); |
| | 711 | 182 | | } |
| | | 183 | | |
| | | 184 | | private static void RegisterAgentFrameworkInfrastructure(IServiceCollection services) |
| | | 185 | | { |
| | 711 | 186 | | services.TryAddSingleton<ITokenBudgetTracker, TokenBudgetTracker>(); |
| | 711 | 187 | | services.TryAddSingleton<IAgentExecutionContextAccessor, AgentExecutionContextAccessor>(); |
| | 711 | 188 | | services.TryAddSingleton<AgentDiagnosticsAccessor>(sp => |
| | 802 | 189 | | new AgentDiagnosticsAccessor( |
| | 802 | 190 | | sp.GetService<ChatCompletionCollectorHolder>(), |
| | 802 | 191 | | sp.GetService<ToolCallCollectorHolder>())); |
| | 786 | 192 | | services.TryAddSingleton<IAgentDiagnosticsAccessor>(sp => sp.GetRequiredService<AgentDiagnosticsAccessor>()); |
| | 754 | 193 | | services.TryAddSingleton<IAgentDiagnosticsWriter>(sp => sp.GetRequiredService<AgentDiagnosticsAccessor>()); |
| | 711 | 194 | | services.TryAddSingleton<IInFlightAgentDiagnosticsAccessor, InFlightAgentDiagnosticsAccessor>(); |
| | 711 | 195 | | services.TryAddSingleton<IToolMetricsAccessor, ToolMetricsAccessor>(); |
| | 711 | 196 | | services.TryAddSingleton<AgentFrameworkMetricsOptions>(sp => |
| | 711 | 197 | | { |
| | 39 | 198 | | var syringe = sp.GetService<BuiltAgentFrameworkSyringe>(); |
| | 39 | 199 | | return syringe?.Value.MetricsOptions ?? new AgentFrameworkMetricsOptions(); |
| | 711 | 200 | | }); |
| | 711 | 201 | | services.TryAddSingleton<IAgentMetrics>(sp => |
| | 711 | 202 | | { |
| | 55 | 203 | | var syringe = sp.GetService<BuiltAgentFrameworkSyringe>(); |
| | 55 | 204 | | var options = syringe?.Value.MetricsOptions ?? new AgentFrameworkMetricsOptions(); |
| | 55 | 205 | | return new AgentMetrics(options); |
| | 711 | 206 | | }); |
| | 711 | 207 | | services.TryAddSingleton<IGenAiTokenMetrics>(sp => |
| | 711 | 208 | | { |
| | 48 | 209 | | var syringe = sp.GetService<BuiltAgentFrameworkSyringe>(); |
| | 48 | 210 | | var options = syringe?.Value.MetricsOptions ?? new AgentFrameworkMetricsOptions(); |
| | 48 | 211 | | return new GenAiTokenMetrics(options); |
| | 711 | 212 | | }); |
| | 711 | 213 | | services.TryAddSingleton<IPipelineMetrics>(sp => |
| | 711 | 214 | | { |
| | 3 | 215 | | var syringe = sp.GetService<BuiltAgentFrameworkSyringe>(); |
| | 3 | 216 | | var options = syringe?.Value.PipelineMetricsOptions; |
| | 3 | 217 | | return options is null |
| | 3 | 218 | | ? new NoOpPipelineMetrics() |
| | 3 | 219 | | : new PipelineMetrics(options); |
| | 711 | 220 | | }); |
| | 711 | 221 | | services.TryAddSingleton<ChatCompletionCollectorHolder>(); |
| | 714 | 222 | | services.TryAddSingleton<IChatCompletionCollector>(sp => sp.GetRequiredService<ChatCompletionCollectorHolder>()) |
| | 711 | 223 | | services.TryAddSingleton<ToolCallCollectorHolder>(); |
| | 741 | 224 | | services.TryAddSingleton<IToolCallCollector>(sp => sp.GetRequiredService<ToolCallCollectorHolder>()); |
| | 711 | 225 | | services.TryAddSingleton<IProgressSequence, ProgressSequenceProvider>(); |
| | 711 | 226 | | services.TryAddSingleton<IProgressReporterAccessor, ProgressReporterAccessor>(); |
| | 711 | 227 | | services.TryAddSingleton<IProgressReporterErrorHandler, NullProgressReporterErrorHandler>(); |
| | 711 | 228 | | } |
| | | 229 | | } |