| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 3 | | using Microsoft.SemanticKernel; |
| | | 4 | | |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace 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 | | [DoNotAutoRegister] |
| | | 18 | | [RequiresUnreferencedCode("SemanticKernel plugin setup uses reflection to discover [KernelFunction] methods.")] |
| | | 19 | | [RequiresDynamicCode("SemanticKernel plugin setup uses reflection APIs that require dynamic code generation.")] |
| | | 20 | | public sealed record SemanticKernelSyringe |
| | | 21 | | { |
| | 109 | 22 | | public required IServiceProvider ServiceProvider { get; init; } |
| | | 23 | | |
| | 69 | 24 | | internal List<Action<KernelFactoryOptions>>? ConfigureKernelFactory { get; init; } = []; |
| | | 25 | | |
| | 115 | 26 | | internal List<Type>? PluginTypes { get; init; } = []; |
| | | 27 | | |
| | | 28 | | public IKernelFactory BuildKernelFactory( |
| | | 29 | | Action<KernelFactoryOptions>? configure = null) |
| | | 30 | | { |
| | 32 | 31 | | var pluginFactory = ServiceProvider.GetRequiredService<IPluginFactory>(); |
| | 32 | 32 | | KernelFactory kernelFactory = new( |
| | 32 | 33 | | _serviceProvider: ServiceProvider, |
| | 32 | 34 | | _pluginFactory: pluginFactory, |
| | 32 | 35 | | _configure: options => |
| | 32 | 36 | | { |
| | 31 | 37 | | SetupPlugins( |
| | 31 | 38 | | options.KernelBuilder, |
| | 31 | 39 | | PluginTypes ?? []); |
| | 32 | 40 | | |
| | 68 | 41 | | foreach (var callback in ConfigureKernelFactory ?? []) |
| | 32 | 42 | | { |
| | 3 | 43 | | callback?.Invoke(options); |
| | 32 | 44 | | } |
| | 32 | 45 | | |
| | 31 | 46 | | configure?.Invoke(options); |
| | 32 | 47 | | }); |
| | 32 | 48 | | return kernelFactory; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private static void SetupPlugins( |
| | | 52 | | IKernelBuilder kernelBuilder, |
| | | 53 | | IReadOnlyList<Type> pluginTypes) |
| | | 54 | | { |
| | 310 | 55 | | foreach (var pluginType in pluginTypes) |
| | | 56 | | { |
| | 124 | 57 | | if (pluginType.IsStatic()) |
| | | 58 | | { |
| | 24 | 59 | | var funcs = pluginType.GetMethods(BindingFlags.Public | BindingFlags.Static) |
| | 24 | 60 | | .Where(m => m.IsDefined(typeof(KernelFunctionAttribute), inherit: true)) |
| | 24 | 61 | | .Select(m => KernelFunctionFactory.CreateFromMethod(m, target: null)) |
| | 24 | 62 | | .ToList(); |
| | | 63 | | |
| | 24 | 64 | | if (funcs.Count > 0) |
| | | 65 | | { |
| | 24 | 66 | | var plugin = KernelPluginFactory.CreateFromFunctions(pluginType.Name, funcs); |
| | 24 | 67 | | kernelBuilder.Plugins.Add(plugin); |
| | | 68 | | } |
| | | 69 | | |
| | 24 | 70 | | continue; |
| | | 71 | | } |
| | | 72 | | |
| | 200 | 73 | | kernelBuilder.Plugins.Services.AddSingleton(sp => BuildPluginFromType(pluginType, sp)); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | static KernelPlugin BuildPluginFromType( |
| | | 77 | | Type pluginType, |
| | | 78 | | IServiceProvider serviceProvider) |
| | | 79 | | { |
| | 100 | 80 | | var plugin = KernelPluginFactory.CreateFromType( |
| | 100 | 81 | | instanceType: pluginType, |
| | 100 | 82 | | pluginName: pluginType.Name, |
| | 100 | 83 | | serviceProvider: serviceProvider); |
| | 100 | 84 | | return plugin; |
| | | 85 | | } |
| | 31 | 86 | | } |
| | | 87 | | } |