| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NexusLabs.Needlr.AgentFramework; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Runtime bootstrap registry for source-generated Agent Framework components. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// The source generator emits a <c>[ModuleInitializer]</c> in the host assembly that calls |
| | | 10 | | /// <see cref="Register"/> with the generated type providers. |
| | | 11 | | /// <c>UsingAgentFramework()</c> checks this bootstrap |
| | | 12 | | /// and auto-populates function types, groups, and agent types without requiring any explicit |
| | | 13 | | /// <c>Add*FromGenerated()</c> calls. |
| | | 14 | | /// </remarks> |
| | | 15 | | public static class AgentFrameworkGeneratedBootstrap |
| | | 16 | | { |
| | | 17 | | private sealed class Registration |
| | | 18 | | { |
| | 12 | 19 | | public Registration( |
| | 12 | 20 | | Func<IReadOnlyList<Type>> functionTypes, |
| | 12 | 21 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> groupTypes, |
| | 12 | 22 | | Func<IReadOnlyList<Type>> agentTypes, |
| | 12 | 23 | | Func<IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason)>>> handoffTopology, |
| | 12 | 24 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> groupChatGroups, |
| | 12 | 25 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> sequentialTopology) |
| | | 26 | | { |
| | 12 | 27 | | FunctionTypes = functionTypes; |
| | 12 | 28 | | GroupTypes = groupTypes; |
| | 12 | 29 | | AgentTypes = agentTypes; |
| | 12 | 30 | | HandoffTopology = handoffTopology; |
| | 12 | 31 | | GroupChatGroups = groupChatGroups; |
| | 12 | 32 | | SequentialTopology = sequentialTopology; |
| | 12 | 33 | | } |
| | | 34 | | |
| | 1 | 35 | | public Func<IReadOnlyList<Type>> FunctionTypes { get; } |
| | 3 | 36 | | public Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> GroupTypes { get; } |
| | 3 | 37 | | public Func<IReadOnlyList<Type>> AgentTypes { get; } |
| | 4 | 38 | | public Func<IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason)>>> HandoffTopology |
| | 4 | 39 | | public Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> GroupChatGroups { get; } |
| | 4 | 40 | | public Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> SequentialTopology { get; } |
| | | 41 | | } |
| | | 42 | | |
| | 1 | 43 | | private static readonly object _gate = new(); |
| | 1 | 44 | | private static readonly List<Registration> _registrations = []; |
| | 1 | 45 | | private static readonly AsyncLocal<Registration?> _asyncLocalOverride = new(); |
| | | 46 | | private static IAIFunctionProvider? _aiFunctionProvider; |
| | 1 | 47 | | private static readonly AsyncLocal<IAIFunctionProvider?> _asyncLocalProviderOverride = new(); |
| | | 48 | | |
| | | 49 | | private static ( |
| | | 50 | | Func<IReadOnlyList<Type>> Functions, |
| | | 51 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> Groups, |
| | | 52 | | Func<IReadOnlyList<Type>> Agents, |
| | | 53 | | Func<IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason)>>> HandoffTopology, |
| | | 54 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> GroupChatGroups, |
| | | 55 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> SequentialTopology)? _cachedCombined; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Registers the generated <see cref="IAIFunctionProvider"/> for this assembly. |
| | | 59 | | /// Called automatically by the generator-emitted <c>[ModuleInitializer]</c>. |
| | | 60 | | /// </summary> |
| | | 61 | | public static void RegisterAIFunctionProvider(IAIFunctionProvider provider) |
| | | 62 | | { |
| | 1 | 63 | | ArgumentNullException.ThrowIfNull(provider); |
| | 1 | 64 | | _aiFunctionProvider = provider; |
| | 1 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the registered <see cref="IAIFunctionProvider"/>, if one has been registered. |
| | | 69 | | /// </summary> |
| | | 70 | | public static bool TryGetAIFunctionProvider([NotNullWhen(true)] out IAIFunctionProvider? provider) |
| | | 71 | | { |
| | 65 | 72 | | var local = _asyncLocalProviderOverride.Value; |
| | 65 | 73 | | if (local is not null) |
| | | 74 | | { |
| | 2 | 75 | | provider = local; |
| | 2 | 76 | | return true; |
| | | 77 | | } |
| | | 78 | | |
| | 63 | 79 | | provider = _aiFunctionProvider; |
| | 63 | 80 | | return provider is not null; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | /// <summary> |
| | | 84 | | /// Registers the generated type providers for this assembly. |
| | | 85 | | /// Called automatically by the generator-emitted <c>[ModuleInitializer]</c>. |
| | | 86 | | /// </summary> |
| | | 87 | | public static void Register( |
| | | 88 | | Func<IReadOnlyList<Type>> functionTypes, |
| | | 89 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> groupTypes, |
| | | 90 | | Func<IReadOnlyList<Type>> agentTypes, |
| | | 91 | | Func<IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason)>>> handoffTopology, |
| | | 92 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> groupChatGroups, |
| | | 93 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>>? sequentialTopology = null) |
| | | 94 | | { |
| | 1 | 95 | | ArgumentNullException.ThrowIfNull(functionTypes); |
| | 1 | 96 | | ArgumentNullException.ThrowIfNull(groupTypes); |
| | 1 | 97 | | ArgumentNullException.ThrowIfNull(agentTypes); |
| | 1 | 98 | | ArgumentNullException.ThrowIfNull(handoffTopology); |
| | 1 | 99 | | ArgumentNullException.ThrowIfNull(groupChatGroups); |
| | 1 | 100 | | sequentialTopology ??= static () => new Dictionary<string, IReadOnlyList<Type>>(); |
| | | 101 | | |
| | 1 | 102 | | lock (_gate) |
| | | 103 | | { |
| | 1 | 104 | | _registrations.Add(new Registration(functionTypes, groupTypes, agentTypes, handoffTopology, groupChatGroups, |
| | 1 | 105 | | _cachedCombined = null; |
| | 1 | 106 | | } |
| | 1 | 107 | | } |
| | | 108 | | |
| | | 109 | | /// <summary> |
| | | 110 | | /// Gets the combined function type provider from all registered assemblies. |
| | | 111 | | /// </summary> |
| | | 112 | | public static bool TryGetFunctionTypes([NotNullWhen(true)] out Func<IReadOnlyList<Type>>? provider) |
| | | 113 | | { |
| | 37 | 114 | | var local = _asyncLocalOverride.Value; |
| | 37 | 115 | | if (local is not null) |
| | | 116 | | { |
| | 0 | 117 | | provider = local.FunctionTypes; |
| | 0 | 118 | | return true; |
| | | 119 | | } |
| | | 120 | | |
| | 37 | 121 | | lock (_gate) |
| | | 122 | | { |
| | 37 | 123 | | if (_registrations.Count == 0) |
| | | 124 | | { |
| | 0 | 125 | | provider = null; |
| | 0 | 126 | | return false; |
| | | 127 | | } |
| | | 128 | | |
| | 37 | 129 | | EnsureCombined(); |
| | 37 | 130 | | provider = _cachedCombined!.Value.Functions; |
| | 37 | 131 | | return true; |
| | | 132 | | } |
| | 37 | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Gets the combined function group provider from all registered assemblies. |
| | | 137 | | /// </summary> |
| | | 138 | | public static bool TryGetGroupTypes([NotNullWhen(true)] out Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>>? |
| | | 139 | | { |
| | 52 | 140 | | var local = _asyncLocalOverride.Value; |
| | 52 | 141 | | if (local is not null) |
| | | 142 | | { |
| | 2 | 143 | | provider = local.GroupTypes; |
| | 2 | 144 | | return true; |
| | | 145 | | } |
| | | 146 | | |
| | 50 | 147 | | lock (_gate) |
| | | 148 | | { |
| | 50 | 149 | | if (_registrations.Count == 0) |
| | | 150 | | { |
| | 0 | 151 | | provider = null; |
| | 0 | 152 | | return false; |
| | | 153 | | } |
| | | 154 | | |
| | 50 | 155 | | EnsureCombined(); |
| | 50 | 156 | | provider = _cachedCombined!.Value.Groups; |
| | 50 | 157 | | return true; |
| | | 158 | | } |
| | 50 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Gets the combined agent type provider from all registered assemblies. |
| | | 163 | | /// </summary> |
| | | 164 | | public static bool TryGetAgentTypes([NotNullWhen(true)] out Func<IReadOnlyList<Type>>? provider) |
| | | 165 | | { |
| | 36 | 166 | | var local = _asyncLocalOverride.Value; |
| | 36 | 167 | | if (local is not null) |
| | | 168 | | { |
| | 2 | 169 | | provider = local.AgentTypes; |
| | 2 | 170 | | return true; |
| | | 171 | | } |
| | | 172 | | |
| | 34 | 173 | | lock (_gate) |
| | | 174 | | { |
| | 34 | 175 | | if (_registrations.Count == 0) |
| | | 176 | | { |
| | 0 | 177 | | provider = null; |
| | 0 | 178 | | return false; |
| | | 179 | | } |
| | | 180 | | |
| | 34 | 181 | | EnsureCombined(); |
| | 34 | 182 | | provider = _cachedCombined!.Value.Agents; |
| | 34 | 183 | | return true; |
| | | 184 | | } |
| | 34 | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Gets the combined handoff topology provider from all registered assemblies. |
| | | 189 | | /// </summary> |
| | | 190 | | public static bool TryGetHandoffTopology( |
| | | 191 | | [NotNullWhen(true)] out Func<IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason)>>> |
| | | 192 | | { |
| | 9 | 193 | | var local = _asyncLocalOverride.Value; |
| | 9 | 194 | | if (local is not null) |
| | | 195 | | { |
| | 3 | 196 | | provider = local.HandoffTopology; |
| | 3 | 197 | | return true; |
| | | 198 | | } |
| | | 199 | | |
| | 6 | 200 | | lock (_gate) |
| | | 201 | | { |
| | 6 | 202 | | if (_registrations.Count == 0) |
| | | 203 | | { |
| | 0 | 204 | | provider = null; |
| | 0 | 205 | | return false; |
| | | 206 | | } |
| | | 207 | | |
| | 6 | 208 | | EnsureCombined(); |
| | 6 | 209 | | provider = _cachedCombined!.Value.HandoffTopology; |
| | 6 | 210 | | return true; |
| | | 211 | | } |
| | 6 | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Gets the combined group chat groups provider from all registered assemblies. |
| | | 216 | | /// </summary> |
| | | 217 | | public static bool TryGetGroupChatGroups( |
| | | 218 | | [NotNullWhen(true)] out Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>>? provider) |
| | | 219 | | { |
| | 8 | 220 | | var local = _asyncLocalOverride.Value; |
| | 8 | 221 | | if (local is not null) |
| | | 222 | | { |
| | 3 | 223 | | provider = local.GroupChatGroups; |
| | 3 | 224 | | return true; |
| | | 225 | | } |
| | | 226 | | |
| | 5 | 227 | | lock (_gate) |
| | | 228 | | { |
| | 5 | 229 | | if (_registrations.Count == 0) |
| | | 230 | | { |
| | 0 | 231 | | provider = null; |
| | 0 | 232 | | return false; |
| | | 233 | | } |
| | | 234 | | |
| | 5 | 235 | | EnsureCombined(); |
| | 5 | 236 | | provider = _cachedCombined!.Value.GroupChatGroups; |
| | 5 | 237 | | return true; |
| | | 238 | | } |
| | 5 | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Gets the combined sequential topology provider from all registered assemblies. |
| | | 243 | | /// </summary> |
| | | 244 | | public static bool TryGetSequentialTopology( |
| | | 245 | | [NotNullWhen(true)] out Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>>? provider) |
| | | 246 | | { |
| | 7 | 247 | | var local = _asyncLocalOverride.Value; |
| | 7 | 248 | | if (local is not null) |
| | | 249 | | { |
| | 3 | 250 | | provider = local.SequentialTopology; |
| | 3 | 251 | | return true; |
| | | 252 | | } |
| | | 253 | | |
| | 4 | 254 | | lock (_gate) |
| | | 255 | | { |
| | 4 | 256 | | if (_registrations.Count == 0) |
| | | 257 | | { |
| | 0 | 258 | | provider = null; |
| | 0 | 259 | | return false; |
| | | 260 | | } |
| | | 261 | | |
| | 4 | 262 | | EnsureCombined(); |
| | 4 | 263 | | provider = _cachedCombined!.Value.SequentialTopology; |
| | 4 | 264 | | return true; |
| | | 265 | | } |
| | 4 | 266 | | } |
| | | 267 | | |
| | | 268 | | /// <summary> |
| | | 269 | | /// Creates a test-scoped override that replaces bootstrap discovery for the current async context. |
| | | 270 | | /// Dispose the returned scope to restore the previous state. |
| | | 271 | | /// </summary> |
| | | 272 | | internal static IDisposable BeginTestScope( |
| | | 273 | | Func<IReadOnlyList<Type>> functionTypes, |
| | | 274 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> groupTypes, |
| | | 275 | | Func<IReadOnlyList<Type>> agentTypes, |
| | | 276 | | Func<IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason)>>>? handoffTopology = null |
| | | 277 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>>? groupChatGroups = null, |
| | | 278 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>>? sequentialTopology = null, |
| | | 279 | | IAIFunctionProvider? aiFunctionProvider = null) |
| | | 280 | | { |
| | 11 | 281 | | handoffTopology ??= static () => new Dictionary<Type, IReadOnlyList<(Type, string?)>>(); |
| | 11 | 282 | | groupChatGroups ??= static () => new Dictionary<string, IReadOnlyList<Type>>(); |
| | 11 | 283 | | sequentialTopology ??= static () => new Dictionary<string, IReadOnlyList<Type>>(); |
| | 11 | 284 | | var prior = _asyncLocalOverride.Value; |
| | 11 | 285 | | var priorProvider = _asyncLocalProviderOverride.Value; |
| | 11 | 286 | | _asyncLocalOverride.Value = new Registration(functionTypes, groupTypes, agentTypes, handoffTopology, groupChatGr |
| | 11 | 287 | | _asyncLocalProviderOverride.Value = aiFunctionProvider; |
| | 11 | 288 | | return new Scope(prior, priorProvider); |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | private static void EnsureCombined() |
| | | 292 | | { |
| | 136 | 293 | | if (_cachedCombined.HasValue) |
| | 135 | 294 | | return; |
| | | 295 | | |
| | 2 | 296 | | var functionProviders = _registrations.Select(r => r.FunctionTypes).ToArray(); |
| | 2 | 297 | | var groupProviders = _registrations.Select(r => r.GroupTypes).ToArray(); |
| | 2 | 298 | | var agentProviders = _registrations.Select(r => r.AgentTypes).ToArray(); |
| | 2 | 299 | | var topologyProviders = _registrations.Select(r => r.HandoffTopology).ToArray(); |
| | 2 | 300 | | var groupChatProviders = _registrations.Select(r => r.GroupChatGroups).ToArray(); |
| | 2 | 301 | | var sequentialProviders = _registrations.Select(r => r.SequentialTopology).ToArray(); |
| | | 302 | | |
| | 1 | 303 | | Func<IReadOnlyList<Type>> combinedFunctions = () => |
| | 1 | 304 | | { |
| | 37 | 305 | | var result = new List<Type>(); |
| | 37 | 306 | | var seen = new HashSet<Type>(); |
| | 148 | 307 | | foreach (var p in functionProviders) |
| | 74 | 308 | | foreach (var t in p()) |
| | 0 | 309 | | if (seen.Add(t)) |
| | 0 | 310 | | result.Add(t); |
| | 37 | 311 | | return result; |
| | 1 | 312 | | }; |
| | | 313 | | |
| | 1 | 314 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> combinedGroups = () => |
| | 1 | 315 | | { |
| | 50 | 316 | | var merged = new Dictionary<string, List<Type>>(); |
| | 200 | 317 | | foreach (var p in groupProviders) |
| | 100 | 318 | | foreach (var (key, types) in p()) |
| | 1 | 319 | | { |
| | 0 | 320 | | if (!merged.TryGetValue(key, out var list)) |
| | 0 | 321 | | merged[key] = list = []; |
| | 0 | 322 | | foreach (var t in types) |
| | 0 | 323 | | if (!list.Contains(t)) |
| | 0 | 324 | | list.Add(t); |
| | 1 | 325 | | } |
| | 50 | 326 | | return merged.ToDictionary( |
| | 0 | 327 | | kv => kv.Key, |
| | 50 | 328 | | kv => (IReadOnlyList<Type>)kv.Value.AsReadOnly()); |
| | 1 | 329 | | }; |
| | | 330 | | |
| | 1 | 331 | | Func<IReadOnlyList<Type>> combinedAgents = () => |
| | 1 | 332 | | { |
| | 34 | 333 | | var result = new List<Type>(); |
| | 34 | 334 | | var seen = new HashSet<Type>(); |
| | 136 | 335 | | foreach (var p in agentProviders) |
| | 68 | 336 | | foreach (var t in p()) |
| | 0 | 337 | | if (seen.Add(t)) |
| | 0 | 338 | | result.Add(t); |
| | 34 | 339 | | return result; |
| | 1 | 340 | | }; |
| | | 341 | | |
| | 1 | 342 | | Func<IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason)>>> combinedTopology = () = |
| | 1 | 343 | | { |
| | 6 | 344 | | var merged = new Dictionary<Type, List<(Type, string?)>>(); |
| | 24 | 345 | | foreach (var p in topologyProviders) |
| | 12 | 346 | | foreach (var (agentType, targets) in p()) |
| | 1 | 347 | | { |
| | 0 | 348 | | if (!merged.TryGetValue(agentType, out var list)) |
| | 0 | 349 | | merged[agentType] = list = []; |
| | 0 | 350 | | foreach (var target in targets) |
| | 0 | 351 | | if (!list.Contains(target)) |
| | 0 | 352 | | list.Add(target); |
| | 1 | 353 | | } |
| | 6 | 354 | | return merged.ToDictionary( |
| | 0 | 355 | | kv => kv.Key, |
| | 6 | 356 | | kv => (IReadOnlyList<(Type, string?)>)kv.Value.AsReadOnly()); |
| | 1 | 357 | | }; |
| | | 358 | | |
| | 1 | 359 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> combinedGroupChatGroups = () => |
| | 1 | 360 | | { |
| | 5 | 361 | | var merged = new Dictionary<string, List<Type>>(); |
| | 20 | 362 | | foreach (var p in groupChatProviders) |
| | 10 | 363 | | foreach (var (key, types) in p()) |
| | 1 | 364 | | { |
| | 0 | 365 | | if (!merged.TryGetValue(key, out var list)) |
| | 0 | 366 | | merged[key] = list = []; |
| | 0 | 367 | | foreach (var t in types) |
| | 0 | 368 | | if (!list.Contains(t)) |
| | 0 | 369 | | list.Add(t); |
| | 1 | 370 | | } |
| | 5 | 371 | | return merged.ToDictionary( |
| | 0 | 372 | | kv => kv.Key, |
| | 5 | 373 | | kv => (IReadOnlyList<Type>)kv.Value.AsReadOnly()); |
| | 1 | 374 | | }; |
| | | 375 | | |
| | 1 | 376 | | Func<IReadOnlyDictionary<string, IReadOnlyList<Type>>> combinedSequentialTopology = () => |
| | 1 | 377 | | { |
| | 4 | 378 | | var merged = new Dictionary<string, List<Type>>(); |
| | 16 | 379 | | foreach (var p in sequentialProviders) |
| | 8 | 380 | | foreach (var (key, types) in p()) |
| | 1 | 381 | | { |
| | 0 | 382 | | if (!merged.TryGetValue(key, out var list)) |
| | 0 | 383 | | merged[key] = list = []; |
| | 0 | 384 | | foreach (var t in types) |
| | 0 | 385 | | if (!list.Contains(t)) |
| | 0 | 386 | | list.Add(t); |
| | 1 | 387 | | } |
| | 4 | 388 | | return merged.ToDictionary( |
| | 0 | 389 | | kv => kv.Key, |
| | 4 | 390 | | kv => (IReadOnlyList<Type>)kv.Value.AsReadOnly()); |
| | 1 | 391 | | }; |
| | | 392 | | |
| | 1 | 393 | | _cachedCombined = (combinedFunctions, combinedGroups, combinedAgents, combinedTopology, combinedGroupChatGroups, |
| | 1 | 394 | | } |
| | | 395 | | |
| | | 396 | | private sealed class Scope : IDisposable |
| | | 397 | | { |
| | | 398 | | private readonly Registration? _prior; |
| | | 399 | | private readonly IAIFunctionProvider? _priorProvider; |
| | | 400 | | |
| | 11 | 401 | | public Scope(Registration? prior, IAIFunctionProvider? priorProvider) |
| | | 402 | | { |
| | 11 | 403 | | _prior = prior; |
| | 11 | 404 | | _priorProvider = priorProvider; |
| | 11 | 405 | | } |
| | | 406 | | |
| | | 407 | | public void Dispose() |
| | | 408 | | { |
| | 11 | 409 | | _asyncLocalOverride.Value = _prior; |
| | 11 | 410 | | _asyncLocalProviderOverride.Value = _priorProvider; |
| | 11 | 411 | | } |
| | | 412 | | } |
| | | 413 | | } |