| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr.Injection; |
| | | 4 | | |
| | | 5 | | namespace NexusLabs.Needlr.AgentFramework; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Extension methods for <see cref="ConfiguredSyringe"/> that enable registering |
| | | 9 | | /// Microsoft Agent Framework infrastructure (namely <see cref="IAgentFactory"/>) |
| | | 10 | | /// as part of the Needlr build pipeline. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <para> |
| | | 14 | | /// These helpers defer service registration using the Syringe |
| | | 15 | | /// post-plugin registration callback so that function discovery and |
| | | 16 | | /// registration are completed before the agent factory is added. |
| | | 17 | | /// </para> |
| | | 18 | | /// <para> |
| | | 19 | | /// When the Needlr source generator is active, the generated <c>[ModuleInitializer]</c> registers |
| | | 20 | | /// an <see cref="IAIFunctionProvider"/> that is used instead of reflection. This makes the |
| | | 21 | | /// integration NativeAOT-compatible without any code changes. |
| | | 22 | | /// </para> |
| | | 23 | | /// <para> |
| | | 24 | | /// When the source generator is not used, the integration falls back to reflection-based |
| | | 25 | | /// <see cref="Microsoft.Extensions.AI.AIFunction"/> schema generation, which requires dynamic code. |
| | | 26 | | /// </para> |
| | | 27 | | /// </remarks> |
| | | 28 | | public static class SyringeExtensionsForAgentFramework |
| | | 29 | | { |
| | | 30 | | /// <summary> |
| | | 31 | | /// Registers an <see cref="IAgentFactory"/> built via a |
| | | 32 | | /// <see cref="AgentFrameworkSyringe"/> instance. |
| | | 33 | | /// </summary> |
| | | 34 | | /// <param name="syringe"> |
| | | 35 | | /// The <see cref="ConfiguredSyringe"/> to augment with the registration. |
| | | 36 | | /// </param> |
| | | 37 | | /// <returns> |
| | | 38 | | /// A new <see cref="ConfiguredSyringe"/> instance containing the registration. |
| | | 39 | | /// </returns> |
| | | 40 | | public static ConfiguredSyringe UsingAgentFramework( |
| | | 41 | | this ConfiguredSyringe syringe) |
| | | 42 | | { |
| | 2 | 43 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 4 | 44 | | return syringe.UsingAgentFramework(s => s); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Registers an <see cref="IAgentFactory"/> built via a configurable |
| | | 49 | | /// <see cref="AgentFrameworkSyringe"/> instance. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="syringe"> |
| | | 52 | | /// The <see cref="ConfiguredSyringe"/> to augment with the registration. |
| | | 53 | | /// </param> |
| | | 54 | | /// <param name="configure"> |
| | | 55 | | /// A delegate that receives a pre-initialized <see cref="AgentFrameworkSyringe"/> |
| | | 56 | | /// (with its <see cref="AgentFrameworkSyringe.ServiceProvider"/> set) and |
| | | 57 | | /// returns the configured instance used to build the agent factory. |
| | | 58 | | /// </param> |
| | | 59 | | /// <returns> |
| | | 60 | | /// A new <see cref="ConfiguredSyringe"/> instance containing the registration. |
| | | 61 | | /// </returns> |
| | | 62 | | public static ConfiguredSyringe UsingAgentFramework( |
| | | 63 | | this ConfiguredSyringe syringe, |
| | | 64 | | Func<AgentFrameworkSyringe, AgentFrameworkSyringe> configure) |
| | | 65 | | { |
| | 65 | 66 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 65 | 67 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 68 | | |
| | 65 | 69 | | return syringe.UsingPostPluginRegistrationCallback(services => |
| | 65 | 70 | | { |
| | 65 | 71 | | services.AddSingleton<IAgentFactory>(provider => |
| | 65 | 72 | | { |
| | 65 | 73 | | AgentFrameworkSyringe afSyringe = new() |
| | 65 | 74 | | { |
| | 65 | 75 | | ServiceProvider = provider, |
| | 65 | 76 | | }; |
| | 65 | 77 | | afSyringe = configure.Invoke(afSyringe); |
| | 65 | 78 | | |
| | 65 | 79 | | // Auto-populate from [ModuleInitializer] bootstrap if nothing was explicitly configured. |
| | 65 | 80 | | // Explicit calls (Add*FromGenerated, AddAgentFunctions*, etc.) take precedence. |
| | 65 | 81 | | if ((afSyringe.FunctionTypes is null || afSyringe.FunctionTypes.Count == 0) && |
| | 65 | 82 | | AgentFrameworkGeneratedBootstrap.TryGetFunctionTypes(out var functionProvider)) |
| | 65 | 83 | | { |
| | 37 | 84 | | afSyringe = afSyringe with { FunctionTypes = functionProvider().ToList() }; |
| | 65 | 85 | | } |
| | 65 | 86 | | |
| | 65 | 87 | | if ((afSyringe.FunctionGroupMap is null || afSyringe.FunctionGroupMap.Count == 0) && |
| | 65 | 88 | | AgentFrameworkGeneratedBootstrap.TryGetGroupTypes(out var groupProvider)) |
| | 65 | 89 | | { |
| | 52 | 90 | | afSyringe = afSyringe with { FunctionGroupMap = groupProvider() }; |
| | 65 | 91 | | } |
| | 65 | 92 | | |
| | 65 | 93 | | if ((afSyringe.AgentTypes is null || afSyringe.AgentTypes.Count == 0) && |
| | 65 | 94 | | AgentFrameworkGeneratedBootstrap.TryGetAgentTypes(out var agentProvider)) |
| | 65 | 95 | | { |
| | 36 | 96 | | afSyringe = afSyringe with { AgentTypes = agentProvider().ToList() }; |
| | 65 | 97 | | } |
| | 65 | 98 | | |
| | 65 | 99 | | return afSyringe.BuildAgentFactory(); |
| | 65 | 100 | | }); |
| | 65 | 101 | | |
| | 65 | 102 | | services.AddSingleton<IWorkflowFactory>(provider => |
| | 86 | 103 | | new WorkflowFactory(provider.GetRequiredService<IAgentFactory>())); |
| | 130 | 104 | | }); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Registers an <see cref="IAgentFactory"/> built via an |
| | | 109 | | /// <see cref="AgentFrameworkSyringe"/> created by the supplied delegate. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="syringe"> |
| | | 112 | | /// The <see cref="ConfiguredSyringe"/> to augment with the registration. |
| | | 113 | | /// </param> |
| | | 114 | | /// <param name="configure"> |
| | | 115 | | /// A factory that creates a fully-configured <see cref="AgentFrameworkSyringe"/> |
| | | 116 | | /// used to build the agent factory. Useful when configuration does |
| | | 117 | | /// not need the service provider. |
| | | 118 | | /// </param> |
| | | 119 | | /// <returns> |
| | | 120 | | /// A new <see cref="ConfiguredSyringe"/> instance containing the registration. |
| | | 121 | | /// </returns> |
| | | 122 | | public static ConfiguredSyringe UsingAgentFramework( |
| | | 123 | | this ConfiguredSyringe syringe, |
| | | 124 | | Func<AgentFrameworkSyringe> configure) |
| | | 125 | | { |
| | 0 | 126 | | ArgumentNullException.ThrowIfNull(syringe); |
| | 0 | 127 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 128 | | |
| | 0 | 129 | | return syringe.UsingPostPluginRegistrationCallback(services => |
| | 0 | 130 | | { |
| | 0 | 131 | | services.AddSingleton<IAgentFactory>(provider => |
| | 0 | 132 | | { |
| | 0 | 133 | | var afSyringe = configure.Invoke(); |
| | 0 | 134 | | return afSyringe.BuildAgentFactory(); |
| | 0 | 135 | | }); |
| | 0 | 136 | | |
| | 0 | 137 | | services.AddSingleton<IWorkflowFactory>(provider => |
| | 0 | 138 | | new WorkflowFactory(provider.GetRequiredService<IAgentFactory>())); |
| | 0 | 139 | | }); |
| | | 140 | | } |
| | | 141 | | } |