| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | |
| | | 4 | | namespace NexusLabs.Needlr.AgentFramework; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Fluent builder for configuring the Microsoft Agent Framework with Needlr function discovery. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <para> |
| | | 11 | | /// When the Needlr source generator is active (the common case), this class uses pre-built |
| | | 12 | | /// <see cref="IAIFunctionProvider"/> instances registered by the generated <c>[ModuleInitializer]</c>. |
| | | 13 | | /// No reflection is required in that path. |
| | | 14 | | /// </para> |
| | | 15 | | /// <para> |
| | | 16 | | /// When the source generator is not used, this class falls back to reflection to discover |
| | | 17 | | /// methods decorated with <see cref="AgentFunctionAttribute"/>. That path carries |
| | | 18 | | /// <c>[RequiresDynamicCode]</c> and is not NativeAOT-compatible. |
| | | 19 | | /// </para> |
| | | 20 | | /// </remarks> |
| | | 21 | | /// <example> |
| | | 22 | | /// <code> |
| | | 23 | | /// // Obtained from SyringeAgentFrameworkExtensions.UsingAgentFramework() |
| | | 24 | | /// AgentFrameworkSyringe syringe = app.Services.UsingAgentFramework(); |
| | | 25 | | /// |
| | | 26 | | /// // Register function types and build the factory |
| | | 27 | | /// IAgentFactory factory = syringe |
| | | 28 | | /// .AddAgentFunctionsFromGenerated(GeneratedAgentFunctions.AllFunctionTypes) |
| | | 29 | | /// .BuildAgentFactory(); |
| | | 30 | | /// |
| | | 31 | | /// // Create agents from the factory |
| | | 32 | | /// var supportAgent = factory.CreateAgent<CustomerSupportAgent>(); |
| | | 33 | | /// </code> |
| | | 34 | | /// </example> |
| | | 35 | | [DoNotAutoRegister] |
| | | 36 | | public sealed record AgentFrameworkSyringe |
| | | 37 | | { |
| | 131 | 38 | | public required IServiceProvider ServiceProvider { get; init; } |
| | | 39 | | |
| | 252 | 40 | | internal List<Action<AgentFrameworkConfigureOptions>>? ConfigureAgentFactory { get; init; } = []; |
| | | 41 | | |
| | 353 | 42 | | internal List<Type>? FunctionTypes { get; init; } = []; |
| | | 43 | | |
| | 288 | 44 | | internal IReadOnlyDictionary<string, IReadOnlyList<Type>>? FunctionGroupMap { get; init; } |
| | | 45 | | |
| | 524 | 46 | | internal List<Type>? AgentTypes { get; init; } = []; |
| | | 47 | | |
| | | 48 | | public IAgentFactory BuildAgentFactory() |
| | | 49 | | { |
| | 65 | 50 | | var groupTypes = (FunctionGroupMap ?? new Dictionary<string, IReadOnlyList<Type>>()) |
| | 149 | 51 | | .SelectMany(kvp => kvp.Value); |
| | | 52 | | |
| | 65 | 53 | | var allFunctionTypes = (FunctionTypes ?? []) |
| | 65 | 54 | | .Concat(groupTypes) |
| | 65 | 55 | | .Distinct() |
| | 65 | 56 | | .ToList(); |
| | | 57 | | |
| | 65 | 58 | | var agentTypeMap = (AgentTypes ?? []) |
| | 295 | 59 | | .ToDictionary(t => t.Name, t => t); |
| | | 60 | | |
| | 65 | 61 | | AgentFrameworkGeneratedBootstrap.TryGetAIFunctionProvider(out var generatedProvider); |
| | | 62 | | |
| | 65 | 63 | | return new AgentFactory( |
| | 65 | 64 | | serviceProvider: ServiceProvider, |
| | 65 | 65 | | configureCallbacks: ConfigureAgentFactory ?? [], |
| | 65 | 66 | | functionTypes: allFunctionTypes, |
| | 65 | 67 | | functionGroupMap: FunctionGroupMap, |
| | 65 | 68 | | agentTypeMap: agentTypeMap, |
| | 65 | 69 | | generatedProvider: generatedProvider); |
| | | 70 | | } |
| | | 71 | | } |