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