| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.AgentFramework.FunctionScanners; |
| | | 7 | | |
| | | 8 | | [DoNotAutoRegister] |
| | | 9 | | [RequiresUnreferencedCode("Assembly scanning uses reflection to discover types with [AgentFunctionGroup] attributes.")] |
| | | 10 | | [RequiresDynamicCode("Assembly scanning uses reflection APIs that may require dynamic code generation.")] |
| | 10 | 11 | | internal sealed class AssemblyAgentFunctionGroupScanner(IReadOnlyList<Assembly> _assemblies) |
| | | 12 | | { |
| | | 13 | | public IReadOnlyDictionary<string, IReadOnlyList<Type>> ScanForFunctionGroups() |
| | | 14 | | { |
| | 10 | 15 | | var groups = new Dictionary<string, List<Type>>(); |
| | | 16 | | |
| | 50 | 17 | | foreach (var assembly in _assemblies.Where(a => !a.IsDynamic)) |
| | | 18 | | { |
| | | 19 | | IEnumerable<Type> types; |
| | 20 | 20 | | try { types = assembly.GetTypes(); } |
| | 0 | 21 | | catch (ReflectionTypeLoadException ex) { types = ex.Types.Where(t => t is not null)!; } |
| | | 22 | | |
| | 3630 | 23 | | foreach (var type in types.Where(t => t.IsClass && (!t.IsAbstract || t.IsStatic()))) |
| | | 24 | | { |
| | 1320 | 25 | | foreach (AgentFunctionGroupAttribute attr in |
| | 1140 | 26 | | type.GetCustomAttributes<AgentFunctionGroupAttribute>(inherit: false)) |
| | | 27 | | { |
| | 90 | 28 | | if (!groups.TryGetValue(attr.GroupName, out var list)) |
| | 80 | 29 | | groups[attr.GroupName] = list = []; |
| | | 30 | | |
| | 90 | 31 | | if (!list.Contains(type)) |
| | 90 | 32 | | list.Add(type); |
| | | 33 | | } |
| | | 34 | | } |
| | | 35 | | } |
| | | 36 | | |
| | 10 | 37 | | return groups.ToDictionary( |
| | 80 | 38 | | k => k.Key, |
| | 90 | 39 | | v => (IReadOnlyList<Type>)v.Value.AsReadOnly()); |
| | | 40 | | } |
| | | 41 | | } |