< Summary

Information
Class: NexusLabs.Needlr.AgentFramework.AgentFrameworkSyringe
Assembly: NexusLabs.Needlr.AgentFramework
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework/AgentFrameworkSyringe.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 71
Line coverage: 100%
Branch coverage
50%
Covered branches: 4
Total branches: 8
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_ServiceProvider()100%11100%
get_ConfigureAgentFactory()100%11100%
get_FunctionTypes()100%11100%
get_FunctionGroupMap()100%11100%
get_AgentTypes()100%11100%
BuildAgentFactory()50%88100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.AgentFramework/AgentFrameworkSyringe.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.DependencyInjection.Extensions;
 3
 4namespace 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&lt;CustomerSupportAgent&gt;();
 33/// </code>
 34/// </example>
 35[DoNotAutoRegister]
 36public sealed record AgentFrameworkSyringe
 37{
 13138    public required IServiceProvider ServiceProvider { get; init; }
 39
 25240    internal List<Action<AgentFrameworkConfigureOptions>>? ConfigureAgentFactory { get; init; } = [];
 41
 35342    internal List<Type>? FunctionTypes { get; init; } = [];
 43
 28844    internal IReadOnlyDictionary<string, IReadOnlyList<Type>>? FunctionGroupMap { get; init; }
 45
 52446    internal List<Type>? AgentTypes { get; init; } = [];
 47
 48    public IAgentFactory BuildAgentFactory()
 49    {
 6550        var groupTypes = (FunctionGroupMap ?? new Dictionary<string, IReadOnlyList<Type>>())
 14951            .SelectMany(kvp => kvp.Value);
 52
 6553        var allFunctionTypes = (FunctionTypes ?? [])
 6554            .Concat(groupTypes)
 6555            .Distinct()
 6556            .ToList();
 57
 6558        var agentTypeMap = (AgentTypes ?? [])
 29559            .ToDictionary(t => t.Name, t => t);
 60
 6561        AgentFrameworkGeneratedBootstrap.TryGetAIFunctionProvider(out var generatedProvider);
 62
 6563        return new AgentFactory(
 6564            serviceProvider: ServiceProvider,
 6565            configureCallbacks: ConfigureAgentFactory ?? [],
 6566            functionTypes: allFunctionTypes,
 6567            functionGroupMap: FunctionGroupMap,
 6568            agentTypeMap: agentTypeMap,
 6569            generatedProvider: generatedProvider);
 70    }
 71}