| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Reflection; |
| | | 3 | | |
| | | 4 | | using NexusLabs.Needlr.AgentFramework; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.AgentFramework.Workflows; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Discovers and caches <see cref="GraphTopology"/> from attributes declared |
| | | 10 | | /// on agent types. Cached per graph name for the lifetime of the provider. |
| | | 11 | | /// </summary> |
| | | 12 | | internal sealed class GraphTopologyProvider |
| | | 13 | | { |
| | 40 | 14 | | private readonly ConcurrentDictionary<string, GraphTopology> _cache = new(); |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the topology for the named graph, scanning assemblies on first access |
| | | 18 | | /// and caching the result for subsequent calls. |
| | | 19 | | /// </summary> |
| | | 20 | | public GraphTopology GetTopology(string graphName) => |
| | 62 | 21 | | _cache.GetOrAdd(graphName, static name => DiscoverTopology(name)); |
| | | 22 | | |
| | | 23 | | private static GraphTopology DiscoverTopology(string graphName) |
| | | 24 | | { |
| | 31 | 25 | | Type? entryType = null; |
| | 31 | 26 | | GraphRoutingMode graphRoutingMode = GraphRoutingMode.Deterministic; |
| | 31 | 27 | | var edgeDetails = new List<GraphEdgeDetail>(); |
| | 31 | 28 | | var joinModes = new Dictionary<Type, GraphJoinMode>(); |
| | 31 | 29 | | var allTypes = new HashSet<Type>(); |
| | 31 | 30 | | Func<IReadOnlyList<string>, string>? reducerFunc = null; |
| | 31 | 31 | | Type? reducerType = null; |
| | | 32 | | |
| | 5728 | 33 | | foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) |
| | | 34 | | { |
| | | 35 | | Type[] types; |
| | 5666 | 36 | | try { types = assembly.GetTypes(); } |
| | 0 | 37 | | catch (ReflectionTypeLoadException) { continue; } |
| | 0 | 38 | | catch (FileNotFoundException) { continue; } |
| | | 39 | | |
| | 1071704 | 40 | | foreach (var type in types) |
| | | 41 | | { |
| | 1067836 | 42 | | foreach (var attr in type.GetCustomAttributes<AgentGraphEntryAttribute>()) |
| | | 43 | | { |
| | 899 | 44 | | if (string.Equals(attr.GraphName, graphName, StringComparison.Ordinal)) |
| | | 45 | | { |
| | 31 | 46 | | entryType = type; |
| | 31 | 47 | | graphRoutingMode = attr.RoutingMode; |
| | 31 | 48 | | allTypes.Add(type); |
| | | 49 | | } |
| | | 50 | | } |
| | | 51 | | |
| | 1071618 | 52 | | foreach (var attr in type.GetCustomAttributes<AgentGraphEdgeAttribute>()) |
| | | 53 | | { |
| | 2790 | 54 | | if (string.Equals(attr.GraphName, graphName, StringComparison.Ordinal)) |
| | | 55 | | { |
| | 103 | 56 | | edgeDetails.Add(new GraphEdgeDetail( |
| | 103 | 57 | | type, |
| | 103 | 58 | | attr.TargetAgentType, |
| | 103 | 59 | | attr.Condition, |
| | 103 | 60 | | attr.IsRequired, |
| | 103 | 61 | | attr.HasNodeRoutingMode ? attr.NodeRoutingMode : null)); |
| | 103 | 62 | | allTypes.Add(type); |
| | 103 | 63 | | allTypes.Add(attr.TargetAgentType); |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | 1067340 | 67 | | foreach (var attr in type.GetCustomAttributes<AgentGraphNodeAttribute>()) |
| | | 68 | | { |
| | 651 | 69 | | if (string.Equals(attr.GraphName, graphName, StringComparison.Ordinal)) |
| | | 70 | | { |
| | 26 | 71 | | joinModes[type] = attr.JoinMode; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | 1066286 | 75 | | foreach (var attr in type.GetCustomAttributes<AgentGraphReducerAttribute>()) |
| | | 76 | | { |
| | 124 | 77 | | if (!string.Equals(attr.GraphName, graphName, StringComparison.Ordinal)) |
| | | 78 | | continue; |
| | 5 | 79 | | if (string.IsNullOrWhiteSpace(attr.ReducerMethod)) |
| | | 80 | | continue; |
| | | 81 | | |
| | 5 | 82 | | var method = type.GetMethod( |
| | 5 | 83 | | attr.ReducerMethod, |
| | 5 | 84 | | BindingFlags.Public | BindingFlags.Static, |
| | 5 | 85 | | null, |
| | 5 | 86 | | [typeof(IReadOnlyList<string>)], |
| | 5 | 87 | | null); |
| | | 88 | | |
| | 5 | 89 | | if (method is not null && method.ReturnType == typeof(string)) |
| | | 90 | | { |
| | 5 | 91 | | reducerType = type; |
| | 5 | 92 | | var captured = method; |
| | 10 | 93 | | reducerFunc = inputs => (string)captured.Invoke(null, [inputs])!; |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | |
| | 31 | 99 | | var incomingTypes = new Dictionary<Type, List<Type>>(); |
| | 31 | 100 | | var inboundEdges = new Dictionary<Type, List<Type>>(); |
| | 31 | 101 | | var outboundEdges = new Dictionary<Type, List<Type>>(); |
| | | 102 | | |
| | 290 | 103 | | foreach (var type in allTypes) |
| | | 104 | | { |
| | 114 | 105 | | incomingTypes[type] = []; |
| | 114 | 106 | | inboundEdges[type] = []; |
| | 114 | 107 | | outboundEdges[type] = []; |
| | | 108 | | } |
| | | 109 | | |
| | 268 | 110 | | foreach (var edge in edgeDetails) |
| | | 111 | | { |
| | 103 | 112 | | incomingTypes[edge.Target].Add(edge.Source); |
| | 103 | 113 | | inboundEdges[edge.Target].Add(edge.Source); |
| | 103 | 114 | | outboundEdges[edge.Source].Add(edge.Target); |
| | | 115 | | } |
| | | 116 | | |
| | 31 | 117 | | var outgoingEdgesBySource = new Dictionary<Type, List<GraphEdgeDetail>>(); |
| | 268 | 118 | | foreach (var edge in edgeDetails) |
| | | 119 | | { |
| | 103 | 120 | | if (!outgoingEdgesBySource.TryGetValue(edge.Source, out var list)) |
| | | 121 | | { |
| | 75 | 122 | | list = []; |
| | 75 | 123 | | outgoingEdgesBySource[edge.Source] = list; |
| | | 124 | | } |
| | | 125 | | |
| | 103 | 126 | | list.Add(edge); |
| | | 127 | | } |
| | | 128 | | |
| | 31 | 129 | | var effectiveRoutingModes = new Dictionary<Type, GraphRoutingMode>(); |
| | 212 | 130 | | foreach (var (sourceType, sourceEdges) in outgoingEdgesBySource) |
| | | 131 | | { |
| | 75 | 132 | | var nodeOverride = sourceEdges |
| | 102 | 133 | | .Select(e => e.NodeRoutingModeOverride) |
| | 177 | 134 | | .FirstOrDefault(m => m is not null); |
| | 75 | 135 | | effectiveRoutingModes[sourceType] = nodeOverride ?? graphRoutingMode; |
| | | 136 | | } |
| | | 137 | | |
| | 31 | 138 | | var edgeIsRequired = new Dictionary<(Type Source, Type Target), bool>(); |
| | 268 | 139 | | foreach (var edge in edgeDetails) |
| | | 140 | | { |
| | 103 | 141 | | edgeIsRequired[(edge.Source, edge.Target)] = edge.IsRequired; |
| | | 142 | | } |
| | | 143 | | |
| | 31 | 144 | | return new GraphTopology( |
| | 31 | 145 | | entryType, |
| | 31 | 146 | | allTypes, |
| | 31 | 147 | | joinModes, |
| | 31 | 148 | | incomingTypes, |
| | 228 | 149 | | inboundEdges.ToDictionary(kv => kv.Key, kv => (IReadOnlyList<Type>)kv.Value), |
| | 228 | 150 | | outboundEdges.ToDictionary(kv => kv.Key, kv => (IReadOnlyList<Type>)kv.Value), |
| | 31 | 151 | | graphRoutingMode, |
| | 31 | 152 | | outgoingEdgesBySource, |
| | 31 | 153 | | effectiveRoutingModes, |
| | 31 | 154 | | edgeIsRequired, |
| | 31 | 155 | | reducerFunc, |
| | 31 | 156 | | reducerType); |
| | | 157 | | } |
| | | 158 | | } |