| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.AgentFramework.Generators; |
| | | 9 | | |
| | | 10 | | internal static class RegistryCodeGenerator |
| | | 11 | | { |
| | | 12 | | public static string GenerateRegistrySource( |
| | | 13 | | List<AgentFunctionTypeInfo> types, |
| | | 14 | | string safeAssemblyName) |
| | | 15 | | { |
| | 101 | 16 | | var sb = new StringBuilder(); |
| | | 17 | | |
| | 101 | 18 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 19 | | sb.AppendLine("// Needlr AgentFramework Functions"); |
| | 101 | 20 | | sb.AppendLine("#nullable enable"); |
| | 101 | 21 | | sb.AppendLine(); |
| | 101 | 22 | | sb.AppendLine("using System;"); |
| | 101 | 23 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 101 | 24 | | sb.AppendLine(); |
| | 101 | 25 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 26 | | sb.AppendLine(); |
| | 101 | 27 | | sb.AppendLine("/// <summary>"); |
| | 101 | 28 | | sb.AppendLine("/// Generated registry for Microsoft Agent Framework function types discovered at compile time.") |
| | 101 | 29 | | sb.AppendLine("/// Pass <see cref=\"AllFunctionTypes\"/> to"); |
| | 101 | 30 | | sb.AppendLine("/// <c>AgentFrameworkSyringeExtensions.AddAgentFunctionsFromGenerated</c>."); |
| | 101 | 31 | | sb.AppendLine("/// </summary>"); |
| | 101 | 32 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 33 | | sb.AppendLine("public static class AgentFrameworkFunctionRegistry"); |
| | 101 | 34 | | sb.AppendLine("{"); |
| | 101 | 35 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 36 | | sb.AppendLine(" /// All types containing methods decorated with [AgentFunction], discovered at compile time." |
| | 101 | 37 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 38 | | sb.AppendLine(" public static IReadOnlyList<Type> AllFunctionTypes { get; } = new Type[]"); |
| | 101 | 39 | | sb.AppendLine(" {"); |
| | | 40 | | |
| | 242 | 41 | | foreach (var type in types) |
| | | 42 | | { |
| | 20 | 43 | | sb.AppendLine($" typeof({type.TypeName}),"); |
| | | 44 | | } |
| | | 45 | | |
| | 101 | 46 | | sb.AppendLine(" };"); |
| | 101 | 47 | | sb.AppendLine(); |
| | 101 | 48 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 49 | | sb.AppendLine(" /// Gets the number of function types discovered at compile time."); |
| | 101 | 50 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 51 | | sb.AppendLine($" public static int Count => {types.Count};"); |
| | 101 | 52 | | sb.AppendLine("}"); |
| | | 53 | | |
| | 101 | 54 | | return sb.ToString(); |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public static string GenerateGroupRegistrySource( |
| | | 58 | | Dictionary<string, List<string>> groupedByName, |
| | | 59 | | string safeAssemblyName) |
| | | 60 | | { |
| | 101 | 61 | | var sb = new StringBuilder(); |
| | | 62 | | |
| | 101 | 63 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 64 | | sb.AppendLine("// Needlr AgentFramework Function Groups"); |
| | 101 | 65 | | sb.AppendLine("#nullable enable"); |
| | 101 | 66 | | sb.AppendLine(); |
| | 101 | 67 | | sb.AppendLine("using System;"); |
| | 101 | 68 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 101 | 69 | | sb.AppendLine(); |
| | 101 | 70 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 71 | | sb.AppendLine(); |
| | 101 | 72 | | sb.AppendLine("/// <summary>"); |
| | 101 | 73 | | sb.AppendLine("/// Generated registry for Microsoft Agent Framework function groups discovered at compile time." |
| | 101 | 74 | | sb.AppendLine("/// Pass <see cref=\"AllGroups\"/> to"); |
| | 101 | 75 | | sb.AppendLine("/// <c>AgentFrameworkSyringeExtensions.AddAgentFunctionGroupsFromGenerated</c>."); |
| | 101 | 76 | | sb.AppendLine("/// </summary>"); |
| | 101 | 77 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 78 | | sb.AppendLine("public static class AgentFrameworkFunctionGroupRegistry"); |
| | 101 | 79 | | sb.AppendLine("{"); |
| | 101 | 80 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 81 | | sb.AppendLine(" /// All function groups, mapping group name to the types in that group, discovered at compile |
| | 101 | 82 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 83 | | sb.AppendLine(" public static IReadOnlyDictionary<string, IReadOnlyList<Type>> AllGroups { get; } ="); |
| | 101 | 84 | | sb.AppendLine(" new Dictionary<string, IReadOnlyList<Type>>()"); |
| | 101 | 85 | | sb.AppendLine(" {"); |
| | | 86 | | |
| | 241 | 87 | | foreach (var kvp in groupedByName.OrderBy(k => k.Key)) |
| | | 88 | | { |
| | 13 | 89 | | var escapedName = kvp.Key.Replace("\"", "\\\""); |
| | 13 | 90 | | var typeNames = kvp.Value; |
| | 13 | 91 | | sb.AppendLine($" [\"{escapedName}\"] = new Type[]"); |
| | 13 | 92 | | sb.AppendLine(" {"); |
| | 54 | 93 | | foreach (var typeName in typeNames) |
| | | 94 | | { |
| | 14 | 95 | | sb.AppendLine($" typeof({typeName}),"); |
| | | 96 | | } |
| | 13 | 97 | | sb.AppendLine(" },"); |
| | | 98 | | } |
| | | 99 | | |
| | 101 | 100 | | sb.AppendLine(" };"); |
| | 101 | 101 | | sb.AppendLine("}"); |
| | | 102 | | |
| | 101 | 103 | | return sb.ToString(); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public static string GenerateAgentRegistrySource( |
| | | 107 | | List<NeedlrAiAgentTypeInfo> types, |
| | | 108 | | string safeAssemblyName) |
| | | 109 | | { |
| | 101 | 110 | | var sb = new StringBuilder(); |
| | | 111 | | |
| | 101 | 112 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 113 | | sb.AppendLine("// Needlr AgentFramework Agent Registry"); |
| | 101 | 114 | | sb.AppendLine("#nullable enable"); |
| | 101 | 115 | | sb.AppendLine(); |
| | 101 | 116 | | sb.AppendLine("using System;"); |
| | 101 | 117 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 101 | 118 | | sb.AppendLine(); |
| | 101 | 119 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 120 | | sb.AppendLine(); |
| | 101 | 121 | | sb.AppendLine("/// <summary>"); |
| | 101 | 122 | | sb.AppendLine("/// Generated registry for agent types declared with [NeedlrAiAgent], discovered at compile time. |
| | 101 | 123 | | sb.AppendLine("/// </summary>"); |
| | 101 | 124 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 125 | | sb.AppendLine("public static class AgentRegistry"); |
| | 101 | 126 | | sb.AppendLine("{"); |
| | 101 | 127 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 128 | | sb.AppendLine(" /// All types decorated with [NeedlrAiAgent], discovered at compile time."); |
| | 101 | 129 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 130 | | sb.AppendLine(" public static IReadOnlyList<Type> AllAgentTypes { get; } = new Type[]"); |
| | 101 | 131 | | sb.AppendLine(" {"); |
| | | 132 | | |
| | 416 | 133 | | foreach (var type in types) |
| | | 134 | | { |
| | 107 | 135 | | sb.AppendLine($" typeof({type.TypeName}),"); |
| | | 136 | | } |
| | | 137 | | |
| | 101 | 138 | | sb.AppendLine(" };"); |
| | 101 | 139 | | sb.AppendLine(); |
| | 101 | 140 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 141 | | sb.AppendLine(" /// Gets the number of agent types discovered at compile time."); |
| | 101 | 142 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 143 | | sb.AppendLine($" public static int Count => {types.Count};"); |
| | 101 | 144 | | sb.AppendLine("}"); |
| | | 145 | | |
| | 101 | 146 | | return sb.ToString(); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public static string GenerateHandoffTopologyRegistrySource( |
| | | 150 | | Dictionary<(string InitialAgentTypeName, string InitialAgentClassName), List<(string TargetAgentTypeName, string |
| | | 151 | | string safeAssemblyName) |
| | | 152 | | { |
| | 101 | 153 | | var sb = new StringBuilder(); |
| | 101 | 154 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 155 | | sb.AppendLine("// Needlr AgentFramework Handoff Topology Registry"); |
| | 101 | 156 | | sb.AppendLine("#nullable enable"); |
| | 101 | 157 | | sb.AppendLine(); |
| | 101 | 158 | | sb.AppendLine("using System;"); |
| | 101 | 159 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 101 | 160 | | sb.AppendLine(); |
| | 101 | 161 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 162 | | sb.AppendLine(); |
| | 101 | 163 | | sb.AppendLine("/// <summary>"); |
| | 101 | 164 | | sb.AppendLine("/// Generated registry of agent handoff topology declared via [AgentHandoffsTo] attributes."); |
| | 101 | 165 | | sb.AppendLine("/// </summary>"); |
| | 101 | 166 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 167 | | sb.AppendLine("public static class AgentHandoffTopologyRegistry"); |
| | 101 | 168 | | sb.AppendLine("{"); |
| | 101 | 169 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 170 | | sb.AppendLine(" /// All handoff relationships, mapping initial agent type to its declared handoff targets."); |
| | 101 | 171 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 172 | | sb.AppendLine(" public static IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason |
| | 101 | 173 | | sb.AppendLine(" new Dictionary<Type, IReadOnlyList<(Type, string?)>>()"); |
| | 101 | 174 | | sb.AppendLine(" {"); |
| | | 175 | | |
| | 217 | 176 | | foreach (var kvp in handoffByInitialAgent.OrderBy(k => k.Key.InitialAgentClassName)) |
| | | 177 | | { |
| | 5 | 178 | | sb.AppendLine($" [typeof({kvp.Key.InitialAgentTypeName})] = new (Type, string?)[]"); |
| | 5 | 179 | | sb.AppendLine(" {"); |
| | 20 | 180 | | foreach (var (targetType, reason) in kvp.Value) |
| | | 181 | | { |
| | 5 | 182 | | var reasonLiteral = reason is null ? "null" : $"\"{reason.Replace("\"", "\\\"")}\""; |
| | 5 | 183 | | sb.AppendLine($" (typeof({targetType}), {reasonLiteral}),"); |
| | | 184 | | } |
| | 5 | 185 | | sb.AppendLine(" },"); |
| | | 186 | | } |
| | | 187 | | |
| | 101 | 188 | | sb.AppendLine(" };"); |
| | 101 | 189 | | sb.AppendLine("}"); |
| | 101 | 190 | | return sb.ToString(); |
| | | 191 | | } |
| | | 192 | | |
| | | 193 | | public static string GenerateGroupChatRegistrySource( |
| | | 194 | | Dictionary<string, List<string>> groupChatByGroupName, |
| | | 195 | | string safeAssemblyName) |
| | | 196 | | { |
| | 101 | 197 | | var sb = new StringBuilder(); |
| | 101 | 198 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 199 | | sb.AppendLine("// Needlr AgentFramework Group Chat Registry"); |
| | 101 | 200 | | sb.AppendLine("#nullable enable"); |
| | 101 | 201 | | sb.AppendLine(); |
| | 101 | 202 | | sb.AppendLine("using System;"); |
| | 101 | 203 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 101 | 204 | | sb.AppendLine(); |
| | 101 | 205 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 206 | | sb.AppendLine(); |
| | 101 | 207 | | sb.AppendLine("/// <summary>"); |
| | 101 | 208 | | sb.AppendLine("/// Generated registry of agent group chat memberships declared via [AgentGroupChatMember] attrib |
| | 101 | 209 | | sb.AppendLine("/// </summary>"); |
| | 101 | 210 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 211 | | sb.AppendLine("public static class AgentGroupChatRegistry"); |
| | 101 | 212 | | sb.AppendLine("{"); |
| | 101 | 213 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 214 | | sb.AppendLine(" /// All group chat groups, mapping group name to the agent types in that group."); |
| | 101 | 215 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 216 | | sb.AppendLine(" public static IReadOnlyDictionary<string, IReadOnlyList<Type>> AllGroups { get; } ="); |
| | 101 | 217 | | sb.AppendLine(" new Dictionary<string, IReadOnlyList<Type>>()"); |
| | 101 | 218 | | sb.AppendLine(" {"); |
| | | 219 | | |
| | 214 | 220 | | foreach (var kvp in groupChatByGroupName.OrderBy(k => k.Key)) |
| | | 221 | | { |
| | 4 | 222 | | var escapedName = kvp.Key.Replace("\"", "\\\""); |
| | 4 | 223 | | sb.AppendLine($" [\"{escapedName}\"] = new Type[]"); |
| | 4 | 224 | | sb.AppendLine(" {"); |
| | 24 | 225 | | foreach (var typeName in kvp.Value) |
| | 8 | 226 | | sb.AppendLine($" typeof({typeName}),"); |
| | 4 | 227 | | sb.AppendLine(" },"); |
| | | 228 | | } |
| | | 229 | | |
| | 101 | 230 | | sb.AppendLine(" };"); |
| | 101 | 231 | | sb.AppendLine("}"); |
| | 101 | 232 | | return sb.ToString(); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | public static string GenerateSequentialTopologyRegistrySource( |
| | | 236 | | Dictionary<string, List<string>> sequenceByPipelineName, |
| | | 237 | | string safeAssemblyName) |
| | | 238 | | { |
| | 101 | 239 | | var sb = new StringBuilder(); |
| | 101 | 240 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 241 | | sb.AppendLine("// Needlr AgentFramework Sequential Pipeline Registry"); |
| | 101 | 242 | | sb.AppendLine("#nullable enable"); |
| | 101 | 243 | | sb.AppendLine(); |
| | 101 | 244 | | sb.AppendLine("using System;"); |
| | 101 | 245 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 101 | 246 | | sb.AppendLine(); |
| | 101 | 247 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 248 | | sb.AppendLine(); |
| | 101 | 249 | | sb.AppendLine("/// <summary>"); |
| | 101 | 250 | | sb.AppendLine("/// Generated registry of sequential pipeline memberships declared via [AgentSequenceMember] attr |
| | 101 | 251 | | sb.AppendLine("/// Agents within each pipeline are stored in ascending order value order."); |
| | 101 | 252 | | sb.AppendLine("/// </summary>"); |
| | 101 | 253 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 254 | | sb.AppendLine("public static class AgentSequentialTopologyRegistry"); |
| | 101 | 255 | | sb.AppendLine("{"); |
| | 101 | 256 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 257 | | sb.AppendLine(" /// All sequential pipelines, mapping pipeline name to the ordered agent types."); |
| | 101 | 258 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 259 | | sb.AppendLine(" public static IReadOnlyDictionary<string, IReadOnlyList<Type>> AllPipelines { get; } ="); |
| | 101 | 260 | | sb.AppendLine(" new Dictionary<string, IReadOnlyList<Type>>()"); |
| | 101 | 261 | | sb.AppendLine(" {"); |
| | | 262 | | |
| | 241 | 263 | | foreach (var kvp in sequenceByPipelineName.OrderBy(k => k.Key)) |
| | | 264 | | { |
| | 13 | 265 | | var escapedName = kvp.Key.Replace("\"", "\\\""); |
| | 13 | 266 | | sb.AppendLine($" [\"{escapedName}\"] = new Type[]"); |
| | 13 | 267 | | sb.AppendLine(" {"); |
| | 74 | 268 | | foreach (var typeName in kvp.Value) |
| | 24 | 269 | | sb.AppendLine($" typeof({typeName}),"); |
| | 13 | 270 | | sb.AppendLine(" },"); |
| | | 271 | | } |
| | | 272 | | |
| | 101 | 273 | | sb.AppendLine(" };"); |
| | 101 | 274 | | sb.AppendLine("}"); |
| | 101 | 275 | | return sb.ToString(); |
| | | 276 | | } |
| | | 277 | | |
| | | 278 | | public static string GenerateGraphTopologyRegistrySource( |
| | | 279 | | Dictionary<string, GraphData> graphDataByName, |
| | | 280 | | string safeAssemblyName) |
| | | 281 | | { |
| | 101 | 282 | | var sb = new StringBuilder(); |
| | 101 | 283 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 284 | | sb.AppendLine("// Needlr AgentFramework Graph Topology Registry"); |
| | 101 | 285 | | sb.AppendLine("#nullable enable"); |
| | 101 | 286 | | sb.AppendLine(); |
| | 101 | 287 | | sb.AppendLine("using System;"); |
| | 101 | 288 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 101 | 289 | | sb.AppendLine(); |
| | 101 | 290 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 291 | | sb.AppendLine(); |
| | 101 | 292 | | sb.AppendLine("/// <summary>"); |
| | 101 | 293 | | sb.AppendLine("/// Generated registry of graph topologies declared via [AgentGraphEntry], [AgentGraphEdge],"); |
| | 101 | 294 | | sb.AppendLine("/// [AgentGraphNode], and [AgentGraphReducer] attributes."); |
| | 101 | 295 | | sb.AppendLine("/// </summary>"); |
| | 101 | 296 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 297 | | sb.AppendLine("public static class AgentGraphTopologyRegistry"); |
| | 101 | 298 | | sb.AppendLine("{"); |
| | 101 | 299 | | sb.AppendLine(" /// <summary>"); |
| | 101 | 300 | | sb.AppendLine(" /// All graph topologies, mapping graph name to its registration data."); |
| | 101 | 301 | | sb.AppendLine(" /// </summary>"); |
| | 101 | 302 | | sb.AppendLine(" public static IReadOnlyDictionary<string, global::NexusLabs.Needlr.AgentFramework.GraphTopolo |
| | 101 | 303 | | sb.AppendLine(" new Dictionary<string, global::NexusLabs.Needlr.AgentFramework.GraphTopologyRegistration> |
| | 101 | 304 | | sb.AppendLine(" {"); |
| | | 305 | | |
| | 265 | 306 | | foreach (var kvp in graphDataByName.OrderBy(k => k.Key)) |
| | | 307 | | { |
| | 21 | 308 | | var escapedName = kvp.Key.Replace("\"", "\\\""); |
| | 21 | 309 | | var graphData = kvp.Value; |
| | | 310 | | |
| | | 311 | | // The AgentGraphEntryPointAnalyzer (NDLRMAF017/018) enforces exactly one |
| | | 312 | | // entry point per graph. We take FirstOrDefault here because the analyzer |
| | | 313 | | // guarantees the constraint; the generator doesn't need to re-validate. |
| | 21 | 314 | | var entryPoint = graphData.EntryPoints.FirstOrDefault(); |
| | 21 | 315 | | var entryTypeExpr = entryPoint.AgentTypeName is not null |
| | 21 | 316 | | ? $"typeof({entryPoint.AgentTypeName})" |
| | 21 | 317 | | : "null"; |
| | 21 | 318 | | var routingMode = entryPoint.AgentTypeName is not null |
| | 21 | 319 | | ? entryPoint.RoutingMode |
| | 21 | 320 | | : 0; |
| | | 321 | | |
| | 21 | 322 | | sb.AppendLine($" [\"{escapedName}\"] = new global::NexusLabs.Needlr.AgentFramework.GraphTopologyR |
| | 21 | 323 | | sb.AppendLine($" entryType: {entryTypeExpr},"); |
| | 21 | 324 | | sb.AppendLine($" routingMode: {routingMode},"); |
| | | 325 | | |
| | | 326 | | // Edges |
| | 21 | 327 | | sb.AppendLine(" edges: new (Type, Type, string?, bool, int?)[]"); |
| | 21 | 328 | | sb.AppendLine(" {"); |
| | 88 | 329 | | foreach (var edge in graphData.Edges) |
| | | 330 | | { |
| | 23 | 331 | | var conditionLiteral = edge.Condition is null ? "null" : $"\"{edge.Condition.Replace("\"", "\\\"")}\""; |
| | 23 | 332 | | var nodeRoutingLiteral = edge.NodeRoutingMode is null ? "(int?)null" : $"(int?){edge.NodeRoutingMode.Val |
| | 23 | 333 | | sb.AppendLine($" (typeof({edge.SourceAgentTypeName}), typeof({edge.TargetAgentTypeNam |
| | | 334 | | } |
| | 21 | 335 | | sb.AppendLine(" },"); |
| | | 336 | | |
| | | 337 | | // Nodes |
| | 21 | 338 | | sb.AppendLine(" nodes: new (Type, int)[]"); |
| | 21 | 339 | | sb.AppendLine(" {"); |
| | 46 | 340 | | foreach (var node in graphData.Nodes) |
| | | 341 | | { |
| | 2 | 342 | | sb.AppendLine($" (typeof({node.AgentTypeName}), {node.JoinMode}),"); |
| | | 343 | | } |
| | 21 | 344 | | sb.AppendLine(" },"); |
| | | 345 | | |
| | | 346 | | // The AgentGraphEntryPointAnalyzer (NDLRMAF017/018) enforces at most one |
| | | 347 | | // reducer per graph. We take FirstOrDefault here because the analyzer |
| | | 348 | | // guarantees the constraint; the generator doesn't need to re-validate. |
| | 21 | 349 | | var reducer = graphData.Reducers.FirstOrDefault(); |
| | 21 | 350 | | if (reducer.AgentTypeName is not null) |
| | | 351 | | { |
| | 3 | 352 | | var methodLiteral = $"\"{reducer.ReducerMethod.Replace("\"", "\\\"")}\""; |
| | 3 | 353 | | sb.AppendLine($" reducer: (typeof({reducer.AgentTypeName}), {methodLiteral})),"); |
| | | 354 | | } |
| | | 355 | | else |
| | | 356 | | { |
| | 18 | 357 | | sb.AppendLine(" reducer: null),"); |
| | | 358 | | } |
| | | 359 | | } |
| | | 360 | | |
| | 101 | 361 | | sb.AppendLine(" };"); |
| | 101 | 362 | | sb.AppendLine("}"); |
| | 101 | 363 | | return sb.ToString(); |
| | | 364 | | } |
| | | 365 | | } |