< Summary

Information
Class: NexusLabs.Needlr.SemanticKernel.SemanticKernelSyringe
Assembly: NexusLabs.Needlr.SemanticKernel
File(s): /home/runner/work/needlr/needlr/src/NexusLabs.Needlr.SemanticKernel/SemanticKernelSyringe.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 101
Line coverage: 100%
Branch coverage
75%
Covered branches: 12
Total branches: 16
Branch coverage: 75%
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_ConfigureKernelFactory()100%11100%
get_PluginTypes()100%11100%
BuildKernelFactory(...)60%1010100%
SetupPlugins(...)100%66100%
BuildPluginFromType()100%11100%

File(s)

/home/runner/work/needlr/needlr/src/NexusLabs.Needlr.SemanticKernel/SemanticKernelSyringe.cs

#LineLine coverage
 1using Microsoft.Extensions.DependencyInjection;
 2using Microsoft.Extensions.DependencyInjection.Extensions;
 3using Microsoft.SemanticKernel;
 4
 5using System.Diagnostics.CodeAnalysis;
 6using System.Reflection;
 7
 8namespace NexusLabs.Needlr.SemanticKernel;
 9
 10/// <summary>
 11/// Fluent builder for configuring Semantic Kernel with Needlr plugin discovery.
 12/// </summary>
 13/// <remarks>
 14/// This class uses reflection to discover methods with <see cref="KernelFunctionAttribute"/>.
 15/// For AOT/trimmed applications, consider registering kernel functions explicitly.
 16/// </remarks>
 17/// <example>
 18/// <code>
 19/// // Obtained from SyringeExtensionsForSemanticKernel.UsingSemanticKernel()
 20/// SemanticKernelSyringe syringe = app.Services.UsingSemanticKernel();
 21///
 22/// // Register plugins and build the kernel factory
 23/// IKernelFactory kernelFactory = syringe
 24///     .AddSemanticKernelPluginsFromGenerated(GeneratedSemanticKernelPlugins.AllPluginTypes)
 25///     .BuildKernelFactory();
 26///
 27/// // Create a Kernel instance from the factory
 28/// Kernel kernel = kernelFactory.CreateKernel();
 29/// </code>
 30/// </example>
 31[DoNotAutoRegister]
 32[RequiresUnreferencedCode("SemanticKernel plugin setup uses reflection to discover [KernelFunction] methods.")]
 33[RequiresDynamicCode("SemanticKernel plugin setup uses reflection APIs that require dynamic code generation.")]
 34public sealed record SemanticKernelSyringe
 35{
 10936    public required IServiceProvider ServiceProvider { get; init; }
 37
 6938    internal List<Action<KernelFactoryOptions>>? ConfigureKernelFactory { get; init; } = [];
 39
 11540    internal List<Type>? PluginTypes { get; init; } = [];
 41
 42    public IKernelFactory BuildKernelFactory(
 43        Action<KernelFactoryOptions>? configure = null)
 44    {
 3245        var pluginFactory = ServiceProvider.GetRequiredService<IPluginFactory>();
 3246        KernelFactory kernelFactory = new(
 3247            _serviceProvider: ServiceProvider,
 3248            _pluginFactory: pluginFactory,
 3249            _configure: options =>
 3250            {
 3151                SetupPlugins(
 3152                    options.KernelBuilder,
 3153                    PluginTypes ?? []);
 3254
 6855                foreach (var callback in ConfigureKernelFactory ?? [])
 3256                {
 357                    callback?.Invoke(options);
 3258                }
 3259
 3160                configure?.Invoke(options);
 3261            });
 3262        return kernelFactory;
 63    }
 64
 65    private static void SetupPlugins(
 66        IKernelBuilder kernelBuilder,
 67        IReadOnlyList<Type> pluginTypes)
 68    {
 31069        foreach (var pluginType in pluginTypes)
 70        {
 12471            if (pluginType.IsStatic())
 72            {
 2473                var funcs = pluginType.GetMethods(BindingFlags.Public | BindingFlags.Static)
 2474                    .Where(m => m.IsDefined(typeof(KernelFunctionAttribute), inherit: true))
 2475                    .Select(m => KernelFunctionFactory.CreateFromMethod(m, target: null))
 2476                    .ToList();
 77
 2478                if (funcs.Count > 0)
 79                {
 2480                    var plugin = KernelPluginFactory.CreateFromFunctions(pluginType.Name, funcs);
 2481                    kernelBuilder.Plugins.Add(plugin);
 82                }
 83
 2484                continue;
 85            }
 86
 20087            kernelBuilder.Plugins.Services.AddSingleton(sp => BuildPluginFromType(pluginType, sp));
 88        }
 89
 90        static KernelPlugin BuildPluginFromType(
 91            Type pluginType,
 92            IServiceProvider serviceProvider)
 93        {
 10094            var plugin = KernelPluginFactory.CreateFromType(
 10095                instanceType: pluginType,
 10096                pluginName: pluginType.Name,
 10097                serviceProvider: serviceProvider);
 10098            return plugin;
 99        }
 31100    }
 101}