| | | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | | 2 | | |
| | | 3 | | using NexusLabs.Needlr; |
| | | 4 | | |
| | | 5 | | using System.Diagnostics.CodeAnalysis; |
| | | 6 | | using System.Reflection; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.AgentFramework.FunctionScanners; |
| | | 9 | | |
| | | 10 | | [DoNotAutoRegister] |
| | | 11 | | [RequiresUnreferencedCode("Service provider scanning uses reflection to discover types with [AgentFunction] methods.")] |
| | | 12 | | [RequiresDynamicCode("Service provider scanning uses reflection APIs that may require dynamic code generation.")] |
| | 0 | 13 | | internal sealed class ServiceProviderAgentFunctionScanner( |
| | 0 | 14 | | IServiceProvider _root) : |
| | | 15 | | IAgentFrameworkFunctionScanner |
| | | 16 | | { |
| | | 17 | | public IReadOnlyList<Type> ScanForFunctionTypes() |
| | | 18 | | { |
| | 0 | 19 | | var serviceCollection = _root.GetServiceCollection(); |
| | 0 | 20 | | return serviceCollection |
| | 0 | 21 | | .Select(sd => sd.ServiceType) |
| | 0 | 22 | | .Where(t => t is { IsClass: true } && !t.IsAbstract) |
| | 0 | 23 | | .Where(HasAgentFunctions) |
| | 0 | 24 | | .Distinct() |
| | 0 | 25 | | .ToArray(); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | private static bool HasAgentFunctions(Type t) => |
| | 0 | 29 | | t.GetMethods(BindingFlags.Instance | BindingFlags.Public) |
| | 0 | 30 | | .Any(m => m.IsDefined(typeof(AgentFunctionAttribute), inherit: true)); |
| | | 31 | | } |