| | | 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 | | { |
| | 4 | 49 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 8 | 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 | | { |
| | 208 | 72 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 208 | 73 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 74 | | |
| | 208 | 75 | | return syringe.UsingPostPluginRegistrationCallback(services => |
| | 208 | 76 | | { |
| | 208 | 77 | | RegisterAgentFrameworkCore(services, configure); |
| | 416 | 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 | | /// 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 | | { |
| | 681 | 119 | | RegisterAgentFrameworkInfrastructure(services); |
| | | 120 | | |
| | 688 | 121 | | configure ??= s => s; |
| | | 122 | | |
| | 681 | 123 | | services.TryAddSingleton<BuiltAgentFrameworkSyringe>(sp => |
| | 681 | 124 | | { |
| | 200 | 125 | | AgentFrameworkSyringe afSyringe = new() |
| | 200 | 126 | | { |
| | 200 | 127 | | ServiceProvider = sp, |
| | 200 | 128 | | }; |
| | 200 | 129 | | afSyringe = configure.Invoke(afSyringe); |
| | 681 | 130 | | |
| | 200 | 131 | | if ((afSyringe.FunctionTypes is null || afSyringe.FunctionTypes.Count == 0) && |
| | 200 | 132 | | AgentFrameworkGeneratedBootstrap.TryGetFunctionTypes(out var functionProvider)) |
| | 681 | 133 | | { |
| | 154 | 134 | | afSyringe = afSyringe with { FunctionTypes = functionProvider().ToList() }; |
| | 681 | 135 | | } |
| | 681 | 136 | | |
| | 200 | 137 | | if ((afSyringe.FunctionGroupMap is null || afSyringe.FunctionGroupMap.Count == 0) && |
| | 200 | 138 | | AgentFrameworkGeneratedBootstrap.TryGetGroupTypes(out var groupProvider)) |
| | 681 | 139 | | { |
| | 183 | 140 | | afSyringe = afSyringe with { FunctionGroupMap = groupProvider() }; |
| | 681 | 141 | | } |
| | 681 | 142 | | |
| | 200 | 143 | | if ((afSyringe.AgentTypes is null || afSyringe.AgentTypes.Count == 0) && |
| | 200 | 144 | | AgentFrameworkGeneratedBootstrap.TryGetAgentTypes(out var agentProvider)) |
| | 681 | 145 | | { |
| | 101 | 146 | | afSyringe = afSyringe with { AgentTypes = agentProvider().ToList() }; |
| | 681 | 147 | | } |
| | 681 | 148 | | |
| | 200 | 149 | | return new BuiltAgentFrameworkSyringe(afSyringe); |
| | 681 | 150 | | }); |
| | | 151 | | |
| | 681 | 152 | | services.TryAddSingleton<IProgressReporterFactory>(sp => |
| | 681 | 153 | | { |
| | 17 | 154 | | var defaultSinks = sp.GetServices<IProgressSink>(); |
| | 17 | 155 | | return new ProgressReporterFactory( |
| | 17 | 156 | | defaultSinks, |
| | 17 | 157 | | sp.GetRequiredService<IProgressSequence>(), |
| | 17 | 158 | | sp.GetRequiredService<IProgressReporterErrorHandler>()); |
| | 681 | 159 | | }); |
| | | 160 | | |
| | 681 | 161 | | services.TryAddSingleton<IAgentFactory>(sp => |
| | 869 | 162 | | sp.GetRequiredService<BuiltAgentFrameworkSyringe>().Value.BuildAgentFactory()); |
| | | 163 | | |
| | 681 | 164 | | services.TryAddSingleton<IChatClientAccessor>(sp => |
| | 681 | 165 | | { |
| | 43 | 166 | | var afSyringe = sp.GetRequiredService<BuiltAgentFrameworkSyringe>().Value; |
| | 43 | 167 | | return new ChatClientAccessor(sp, afSyringe.ConfigureAgentFactory ?? []); |
| | 681 | 168 | | }); |
| | | 169 | | |
| | 681 | 170 | | services.TryAddSingleton<IIterativeAgentLoop>(sp => |
| | 691 | 171 | | new IterativeAgentLoop( |
| | 691 | 172 | | sp.GetRequiredService<IChatClientAccessor>(), |
| | 691 | 173 | | sp.GetService<IAgentDiagnosticsWriter>(), |
| | 691 | 174 | | sp.GetService<IAgentExecutionContextAccessor>(), |
| | 691 | 175 | | sp.GetService<IProgressReporterAccessor>(), |
| | 691 | 176 | | sp.GetService<ITokenBudgetTracker>(), |
| | 691 | 177 | | sp.GetService<IAgentMetrics>())); |
| | | 178 | | |
| | 681 | 179 | | services.TryAddSingleton<IWorkflowFactory>(provider => |
| | 767 | 180 | | new WorkflowFactory(provider.GetRequiredService<IAgentFactory>())); |
| | 681 | 181 | | } |
| | | 182 | | |
| | | 183 | | private static void RegisterAgentFrameworkInfrastructure(IServiceCollection services) |
| | | 184 | | { |
| | 681 | 185 | | services.TryAddSingleton<ITokenBudgetTracker, TokenBudgetTracker>(); |
| | 681 | 186 | | services.TryAddSingleton<IAgentExecutionContextAccessor, AgentExecutionContextAccessor>(); |
| | 681 | 187 | | services.TryAddSingleton<AgentDiagnosticsAccessor>(sp => |
| | 769 | 188 | | new AgentDiagnosticsAccessor( |
| | 769 | 189 | | sp.GetService<ChatCompletionCollectorHolder>(), |
| | 769 | 190 | | sp.GetService<ToolCallCollectorHolder>())); |
| | 753 | 191 | | services.TryAddSingleton<IAgentDiagnosticsAccessor>(sp => sp.GetRequiredService<AgentDiagnosticsAccessor>()); |
| | 721 | 192 | | services.TryAddSingleton<IAgentDiagnosticsWriter>(sp => sp.GetRequiredService<AgentDiagnosticsAccessor>()); |
| | 681 | 193 | | services.TryAddSingleton<IToolMetricsAccessor, ToolMetricsAccessor>(); |
| | 681 | 194 | | services.TryAddSingleton<IAgentMetrics>(sp => |
| | 681 | 195 | | { |
| | 44 | 196 | | var syringe = sp.GetService<BuiltAgentFrameworkSyringe>(); |
| | 44 | 197 | | var options = syringe?.Value.MetricsOptions ?? new AgentFrameworkMetricsOptions(); |
| | 44 | 198 | | return new AgentMetrics(options); |
| | 681 | 199 | | }); |
| | 681 | 200 | | services.TryAddSingleton<ChatCompletionCollectorHolder>(); |
| | 684 | 201 | | services.TryAddSingleton<IChatCompletionCollector>(sp => sp.GetRequiredService<ChatCompletionCollectorHolder>()) |
| | 681 | 202 | | services.TryAddSingleton<ToolCallCollectorHolder>(); |
| | 711 | 203 | | services.TryAddSingleton<IToolCallCollector>(sp => sp.GetRequiredService<ToolCallCollectorHolder>()); |
| | 681 | 204 | | services.TryAddSingleton<IProgressSequence, ProgressSequenceProvider>(); |
| | 681 | 205 | | services.TryAddSingleton<IProgressReporterAccessor, ProgressReporterAccessor>(); |
| | 681 | 206 | | services.TryAddSingleton<IProgressReporterErrorHandler, NullProgressReporterErrorHandler>(); |
| | 681 | 207 | | } |
| | | 208 | | } |