| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.Immutable; |
| | | 7 | | using System.Linq; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading; |
| | | 10 | | |
| | | 11 | | using Microsoft.CodeAnalysis; |
| | | 12 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 13 | | using Microsoft.CodeAnalysis.Text; |
| | | 14 | | |
| | | 15 | | namespace NexusLabs.Needlr.AgentFramework.Generators; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Source generator for Microsoft Agent Framework functions. |
| | | 19 | | /// Discovers classes with [AgentFunction] methods and generates a compile-time type registry. |
| | | 20 | | /// Also discovers classes with [AgentFunctionGroup] attributes and generates a group registry. |
| | | 21 | | /// Also discovers classes with [NeedlrAiAgent] attributes and generates an agent registry. |
| | | 22 | | /// Always emits a [ModuleInitializer] that auto-registers all discovered types with |
| | | 23 | | /// AgentFrameworkGeneratedBootstrap on assembly load. |
| | | 24 | | /// </summary> |
| | | 25 | | [Generator] |
| | | 26 | | public class AgentFrameworkFunctionRegistryGenerator : IIncrementalGenerator |
| | | 27 | | { |
| | | 28 | | private const string AgentFunctionAttributeName = "NexusLabs.Needlr.AgentFramework.AgentFunctionAttribute"; |
| | | 29 | | private const string AgentFunctionGroupAttributeName = "NexusLabs.Needlr.AgentFramework.AgentFunctionGroupAttribute" |
| | | 30 | | private const string NeedlrAiAgentAttributeName = "NexusLabs.Needlr.AgentFramework.NeedlrAiAgentAttribute"; |
| | | 31 | | private const string AgentHandoffsToAttributeName = "NexusLabs.Needlr.AgentFramework.AgentHandoffsToAttribute"; |
| | | 32 | | private const string AgentGroupChatMemberAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGroupChatMemberAttrib |
| | | 33 | | private const string AgentSequenceMemberAttributeName = "NexusLabs.Needlr.AgentFramework.AgentSequenceMemberAttribut |
| | | 34 | | private const string WorkflowRunTerminationConditionAttributeName = "NexusLabs.Needlr.AgentFramework.WorkflowRunTerm |
| | | 35 | | |
| | | 36 | | public void Initialize(IncrementalGeneratorInitializationContext context) |
| | | 37 | | { |
| | | 38 | | // [AgentFunction] method-bearing classes → AgentFrameworkFunctionRegistry |
| | 63 | 39 | | var functionClasses = context.SyntaxProvider |
| | 63 | 40 | | .CreateSyntaxProvider( |
| | 41371 | 41 | | predicate: static (s, _) => s is ClassDeclarationSyntax, |
| | 835 | 42 | | transform: static (ctx, ct) => GetAgentFunctionTypeInfo(ctx, ct)) |
| | 898 | 43 | | .Where(static m => m is not null); |
| | | 44 | | |
| | | 45 | | // [AgentFunctionGroup] class-level annotations → AgentFrameworkFunctionGroupRegistry |
| | 63 | 46 | | var groupClasses = context.SyntaxProvider |
| | 63 | 47 | | .CreateSyntaxProvider( |
| | 41371 | 48 | | predicate: static (s, _) => s is ClassDeclarationSyntax, |
| | 835 | 49 | | transform: static (ctx, ct) => GetAgentFunctionGroupEntries(ctx, ct)) |
| | 898 | 50 | | .Where(static arr => arr.Length > 0); |
| | | 51 | | |
| | | 52 | | // [NeedlrAiAgent] declared agent types → AgentRegistry + partial companions |
| | 63 | 53 | | var agentClasses = context.SyntaxProvider |
| | 63 | 54 | | .ForAttributeWithMetadataName( |
| | 63 | 55 | | NeedlrAiAgentAttributeName, |
| | 58 | 56 | | predicate: static (s, _) => s is ClassDeclarationSyntax, |
| | 58 | 57 | | transform: static (ctx, ct) => GetNeedlrAiAgentTypeInfo(ctx, ct)) |
| | 121 | 58 | | .Where(static m => m is not null); |
| | | 59 | | |
| | | 60 | | // [AgentHandoffsTo] annotations → handoff topology registry |
| | 63 | 61 | | var handoffEntries = context.SyntaxProvider |
| | 63 | 62 | | .ForAttributeWithMetadataName( |
| | 63 | 63 | | AgentHandoffsToAttributeName, |
| | 5 | 64 | | predicate: static (s, _) => s is ClassDeclarationSyntax, |
| | 5 | 65 | | transform: static (ctx, ct) => GetHandoffEntries(ctx, ct)) |
| | 68 | 66 | | .Where(static arr => arr.Length > 0); |
| | | 67 | | |
| | | 68 | | // [AgentGroupChatMember] annotations → group chat registry |
| | 63 | 69 | | var groupChatEntries = context.SyntaxProvider |
| | 63 | 70 | | .ForAttributeWithMetadataName( |
| | 63 | 71 | | AgentGroupChatMemberAttributeName, |
| | 8 | 72 | | predicate: static (s, _) => s is ClassDeclarationSyntax, |
| | 8 | 73 | | transform: static (ctx, ct) => GetGroupChatEntries(ctx, ct)) |
| | 71 | 74 | | .Where(static arr => arr.Length > 0); |
| | | 75 | | |
| | | 76 | | // [AgentSequenceMember] annotations → sequential pipeline registry |
| | 63 | 77 | | var sequenceEntries = context.SyntaxProvider |
| | 63 | 78 | | .ForAttributeWithMetadataName( |
| | 63 | 79 | | AgentSequenceMemberAttributeName, |
| | 24 | 80 | | predicate: static (s, _) => s is ClassDeclarationSyntax, |
| | 24 | 81 | | transform: static (ctx, ct) => GetSequenceEntries(ctx, ct)) |
| | 87 | 82 | | .Where(static arr => arr.Length > 0); |
| | | 83 | | |
| | | 84 | | // [WorkflowRunTerminationCondition] → termination conditions per agent |
| | 63 | 85 | | var terminationConditionEntries = context.SyntaxProvider |
| | 63 | 86 | | .ForAttributeWithMetadataName( |
| | 63 | 87 | | WorkflowRunTerminationConditionAttributeName, |
| | 8 | 88 | | predicate: static (s, _) => s is ClassDeclarationSyntax, |
| | 8 | 89 | | transform: static (ctx, ct) => GetTerminationConditionEntries(ctx, ct)) |
| | 71 | 90 | | .Where(static arr => arr.Length > 0); |
| | | 91 | | |
| | | 92 | | // Unified output: all seven pipelines combined with compilation metadata and build config. |
| | | 93 | | // Always emits all registries + [ModuleInitializer] bootstrap, even when empty. |
| | 63 | 94 | | var combined = functionClasses.Collect() |
| | 63 | 95 | | .Combine(groupClasses.Collect()) |
| | 63 | 96 | | .Combine(agentClasses.Collect()) |
| | 63 | 97 | | .Combine(handoffEntries.Collect()) |
| | 63 | 98 | | .Combine(groupChatEntries.Collect()) |
| | 63 | 99 | | .Combine(sequenceEntries.Collect()) |
| | 63 | 100 | | .Combine(terminationConditionEntries.Collect()) |
| | 63 | 101 | | .Combine(context.CompilationProvider) |
| | 63 | 102 | | .Combine(context.AnalyzerConfigOptionsProvider); |
| | | 103 | | |
| | 63 | 104 | | context.RegisterSourceOutput(combined, |
| | 63 | 105 | | static (spc, data) => |
| | 63 | 106 | | { |
| | 63 | 107 | | var ((((((((functionData, groupData), agentData), handoffData), groupChatData), sequenceData), terminati |
| | 63 | 108 | | ExecuteAll(functionData, groupData, agentData, handoffData, groupChatData, sequenceData, terminationData |
| | 126 | 109 | | }); |
| | 63 | 110 | | } |
| | | 111 | | |
| | | 112 | | private static AgentFunctionTypeInfo? GetAgentFunctionTypeInfo( |
| | | 113 | | GeneratorSyntaxContext context, |
| | | 114 | | CancellationToken cancellationToken) |
| | | 115 | | { |
| | 835 | 116 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | 835 | 117 | | var typeSymbol = context.SemanticModel |
| | 835 | 118 | | .GetDeclaredSymbol(classDeclaration, cancellationToken) as INamedTypeSymbol; |
| | | 119 | | |
| | 835 | 120 | | if (typeSymbol is null) |
| | 0 | 121 | | return null; |
| | | 122 | | |
| | 835 | 123 | | return TryGetTypeInfo(typeSymbol); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private static AgentFunctionTypeInfo? TryGetTypeInfo(INamedTypeSymbol typeSymbol) |
| | | 127 | | { |
| | 835 | 128 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 0 | 129 | | return null; |
| | | 130 | | |
| | 835 | 131 | | if (!IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 132 | | return null; |
| | | 133 | | |
| | 835 | 134 | | if (!typeSymbol.IsStatic && typeSymbol.IsAbstract) |
| | 0 | 135 | | return null; |
| | | 136 | | |
| | 835 | 137 | | bool hasAgentFunction = false; |
| | 6483 | 138 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 139 | | { |
| | 2411 | 140 | | if (member is not IMethodSymbol method) |
| | | 141 | | continue; |
| | | 142 | | |
| | 1529 | 143 | | if (method.MethodKind != MethodKind.Ordinary) |
| | | 144 | | continue; |
| | | 145 | | |
| | 136 | 146 | | if (method.DeclaredAccessibility != Accessibility.Public) |
| | | 147 | | continue; |
| | | 148 | | |
| | 281 | 149 | | foreach (var attribute in method.GetAttributes()) |
| | | 150 | | { |
| | 9 | 151 | | if (attribute.AttributeClass?.ToDisplayString() == AgentFunctionAttributeName) |
| | | 152 | | { |
| | 9 | 153 | | hasAgentFunction = true; |
| | 9 | 154 | | break; |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 136 | 158 | | if (hasAgentFunction) |
| | | 159 | | break; |
| | | 160 | | } |
| | | 161 | | |
| | 835 | 162 | | if (!hasAgentFunction) |
| | 826 | 163 | | return null; |
| | | 164 | | |
| | 9 | 165 | | var methodInfos = ImmutableArray.CreateBuilder<AgentFunctionMethodInfo>(); |
| | 52 | 166 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 167 | | { |
| | 17 | 168 | | if (member is not IMethodSymbol method) |
| | | 169 | | continue; |
| | | 170 | | |
| | 17 | 171 | | if (method.MethodKind != MethodKind.Ordinary) |
| | | 172 | | continue; |
| | | 173 | | |
| | 9 | 174 | | if (method.DeclaredAccessibility != Accessibility.Public) |
| | | 175 | | continue; |
| | | 176 | | |
| | 9 | 177 | | bool isAgentFunction = false; |
| | 27 | 178 | | foreach (var attribute in method.GetAttributes()) |
| | | 179 | | { |
| | 9 | 180 | | if (attribute.AttributeClass?.ToDisplayString() == AgentFunctionAttributeName) |
| | | 181 | | { |
| | 9 | 182 | | isAgentFunction = true; |
| | 9 | 183 | | break; |
| | | 184 | | } |
| | | 185 | | } |
| | | 186 | | |
| | 9 | 187 | | if (!isAgentFunction) |
| | | 188 | | continue; |
| | | 189 | | |
| | 9 | 190 | | var returnType = method.ReturnType; |
| | 9 | 191 | | bool isVoid = returnType.SpecialType == SpecialType.System_Void; |
| | 9 | 192 | | bool isTask = returnType is INamedTypeSymbol nt && |
| | 9 | 193 | | nt.ContainingNamespace?.ToDisplayString() == "System.Threading.Tasks" && |
| | 9 | 194 | | (nt.MetadataName == "Task" || nt.MetadataName == "ValueTask"); |
| | 9 | 195 | | bool isTaskOfT = returnType is INamedTypeSymbol nt2 && |
| | 9 | 196 | | nt2.ContainingNamespace?.ToDisplayString() == "System.Threading.Tasks" && |
| | 9 | 197 | | (nt2.MetadataName == "Task`1" || nt2.MetadataName == "ValueTask`1") && |
| | 9 | 198 | | nt2.TypeArguments.Length == 1; |
| | 9 | 199 | | bool isAsync = isTask || isTaskOfT; |
| | 9 | 200 | | bool isVoidLike = isVoid || isTask; |
| | 9 | 201 | | string? returnValueTypeFQN = isVoidLike ? null : isTaskOfT |
| | 9 | 202 | | ? GetFullyQualifiedName(((INamedTypeSymbol)returnType).TypeArguments[0]) |
| | 9 | 203 | | : GetFullyQualifiedName(returnType); |
| | | 204 | | |
| | 9 | 205 | | string? methodDesc = GetDescriptionFromAttributes(method.GetAttributes()); |
| | | 206 | | |
| | 9 | 207 | | var parameters = ImmutableArray.CreateBuilder<AgentFunctionParameterInfo>(); |
| | 36 | 208 | | foreach (var param in method.Parameters) |
| | | 209 | | { |
| | 9 | 210 | | bool isCancellationToken = param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "glob |
| | 9 | 211 | | bool isNullable = param.NullableAnnotation == NullableAnnotation.Annotated || |
| | 9 | 212 | | (param.Type is INamedTypeSymbol pnt && pnt.ConstructedFrom.SpecialType == SpecialType.System_Nullabl |
| | 9 | 213 | | bool hasDefault = param.HasExplicitDefaultValue; |
| | 9 | 214 | | string? paramDesc = GetDescriptionFromAttributes(param.GetAttributes()); |
| | 9 | 215 | | string jsonSchemaType = GetJsonSchemaType(param.Type, out string? itemJsonSchemaType); |
| | 9 | 216 | | string typeFullName = GetFullyQualifiedName(param.Type); |
| | | 217 | | |
| | 9 | 218 | | parameters.Add(new AgentFunctionParameterInfo( |
| | 9 | 219 | | param.Name, typeFullName, jsonSchemaType, itemJsonSchemaType, |
| | 9 | 220 | | isCancellationToken, isNullable, hasDefault, paramDesc)); |
| | | 221 | | } |
| | | 222 | | |
| | 9 | 223 | | methodInfos.Add(new AgentFunctionMethodInfo( |
| | 9 | 224 | | method.Name, isAsync, isVoidLike, returnValueTypeFQN, |
| | 9 | 225 | | parameters.ToImmutable(), methodDesc ?? "")); |
| | | 226 | | } |
| | | 227 | | |
| | 9 | 228 | | return new AgentFunctionTypeInfo( |
| | 9 | 229 | | GetFullyQualifiedName(typeSymbol), |
| | 9 | 230 | | typeSymbol.ContainingAssembly?.Name ?? "Unknown", |
| | 9 | 231 | | typeSymbol.IsStatic, |
| | 9 | 232 | | methodInfos.ToImmutable()); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | private static ImmutableArray<AgentFunctionGroupEntry> GetAgentFunctionGroupEntries( |
| | | 236 | | GeneratorSyntaxContext context, |
| | | 237 | | CancellationToken cancellationToken) |
| | | 238 | | { |
| | 835 | 239 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | 835 | 240 | | var typeSymbol = context.SemanticModel |
| | 835 | 241 | | .GetDeclaredSymbol(classDeclaration, cancellationToken) as INamedTypeSymbol; |
| | | 242 | | |
| | 835 | 243 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 244 | | return ImmutableArray<AgentFunctionGroupEntry>.Empty; |
| | | 245 | | |
| | 835 | 246 | | var entries = ImmutableArray.CreateBuilder<AgentFunctionGroupEntry>(); |
| | | 247 | | |
| | 2778 | 248 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | | 249 | | { |
| | 554 | 250 | | if (attr.AttributeClass?.ToDisplayString() != AgentFunctionGroupAttributeName) |
| | | 251 | | continue; |
| | | 252 | | |
| | 10 | 253 | | if (attr.ConstructorArguments.Length != 1) |
| | | 254 | | continue; |
| | | 255 | | |
| | 10 | 256 | | var groupName = attr.ConstructorArguments[0].Value as string; |
| | 10 | 257 | | if (string.IsNullOrWhiteSpace(groupName)) |
| | | 258 | | continue; |
| | | 259 | | |
| | 10 | 260 | | entries.Add(new AgentFunctionGroupEntry(GetFullyQualifiedName(typeSymbol), groupName!)); |
| | | 261 | | } |
| | | 262 | | |
| | 835 | 263 | | return entries.ToImmutable(); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | private static NeedlrAiAgentTypeInfo? GetNeedlrAiAgentTypeInfo( |
| | | 267 | | GeneratorAttributeSyntaxContext context, |
| | | 268 | | CancellationToken cancellationToken) |
| | | 269 | | { |
| | 58 | 270 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 58 | 271 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 272 | | return null; |
| | | 273 | | |
| | 58 | 274 | | var classDeclaration = context.TargetNode as ClassDeclarationSyntax; |
| | 122 | 275 | | var isPartial = classDeclaration?.Modifiers.Any(m => m.ValueText == "partial") ?? false; |
| | | 276 | | |
| | 58 | 277 | | var namespaceName = typeSymbol.ContainingNamespace?.IsGlobalNamespace == true |
| | 58 | 278 | | ? null |
| | 58 | 279 | | : typeSymbol.ContainingNamespace?.ToDisplayString(); |
| | | 280 | | |
| | 58 | 281 | | var functionGroupNames = ImmutableArray<string>.Empty; |
| | 58 | 282 | | var explicitFunctionTypeFQNs = ImmutableArray<string>.Empty; |
| | 58 | 283 | | var hasExplicitFunctionTypes = false; |
| | | 284 | | |
| | 58 | 285 | | var agentAttr = context.Attributes.FirstOrDefault(); |
| | 58 | 286 | | if (agentAttr is not null) |
| | | 287 | | { |
| | 63 | 288 | | var groupsArg = agentAttr.NamedArguments.FirstOrDefault(a => a.Key == "FunctionGroups"); |
| | 58 | 289 | | if (groupsArg.Key is not null && groupsArg.Value.Kind == TypedConstantKind.Array) |
| | | 290 | | { |
| | 2 | 291 | | functionGroupNames = groupsArg.Value.Values |
| | 2 | 292 | | .Select(v => v.Value as string) |
| | 2 | 293 | | .Where(s => !string.IsNullOrWhiteSpace(s)) |
| | 2 | 294 | | .Select(s => s!) |
| | 2 | 295 | | .ToImmutableArray(); |
| | | 296 | | } |
| | | 297 | | |
| | 63 | 298 | | var typesArg = agentAttr.NamedArguments.FirstOrDefault(a => a.Key == "FunctionTypes"); |
| | 58 | 299 | | if (typesArg.Key is not null && typesArg.Value.Kind == TypedConstantKind.Array) |
| | | 300 | | { |
| | 2 | 301 | | hasExplicitFunctionTypes = true; |
| | 2 | 302 | | explicitFunctionTypeFQNs = typesArg.Value.Values |
| | 1 | 303 | | .Where(v => v.Kind == TypedConstantKind.Type && v.Value is INamedTypeSymbol) |
| | 1 | 304 | | .Select(v => GetFullyQualifiedName((INamedTypeSymbol)v.Value!)) |
| | 2 | 305 | | .ToImmutableArray(); |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | |
| | 58 | 309 | | return new NeedlrAiAgentTypeInfo( |
| | 58 | 310 | | GetFullyQualifiedName(typeSymbol), |
| | 58 | 311 | | typeSymbol.Name, |
| | 58 | 312 | | namespaceName, |
| | 58 | 313 | | isPartial, |
| | 58 | 314 | | functionGroupNames, |
| | 58 | 315 | | explicitFunctionTypeFQNs, |
| | 58 | 316 | | hasExplicitFunctionTypes); |
| | | 317 | | } |
| | | 318 | | |
| | | 319 | | private static ImmutableArray<HandoffEntry> GetHandoffEntries( |
| | | 320 | | GeneratorAttributeSyntaxContext context, |
| | | 321 | | CancellationToken cancellationToken) |
| | | 322 | | { |
| | 5 | 323 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 5 | 324 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 325 | | return ImmutableArray<HandoffEntry>.Empty; |
| | | 326 | | |
| | 5 | 327 | | var initialTypeName = GetFullyQualifiedName(typeSymbol); |
| | 5 | 328 | | var entries = ImmutableArray.CreateBuilder<HandoffEntry>(); |
| | | 329 | | |
| | 20 | 330 | | foreach (var attr in context.Attributes) |
| | | 331 | | { |
| | 5 | 332 | | if (attr.ConstructorArguments.Length < 1) |
| | | 333 | | continue; |
| | | 334 | | |
| | 5 | 335 | | var typeArg = attr.ConstructorArguments[0]; |
| | 5 | 336 | | if (typeArg.Kind != TypedConstantKind.Type || typeArg.Value is not INamedTypeSymbol targetTypeSymbol) |
| | | 337 | | continue; |
| | | 338 | | |
| | 5 | 339 | | var targetTypeName = GetFullyQualifiedName(targetTypeSymbol); |
| | 5 | 340 | | var reason = attr.ConstructorArguments.Length > 1 ? attr.ConstructorArguments[1].Value as string : null; |
| | | 341 | | |
| | 5 | 342 | | entries.Add(new HandoffEntry(initialTypeName, typeSymbol.Name, targetTypeName, reason)); |
| | | 343 | | } |
| | | 344 | | |
| | 5 | 345 | | return entries.ToImmutable(); |
| | | 346 | | } |
| | | 347 | | |
| | | 348 | | private static ImmutableArray<GroupChatEntry> GetGroupChatEntries( |
| | | 349 | | GeneratorAttributeSyntaxContext context, |
| | | 350 | | CancellationToken cancellationToken) |
| | | 351 | | { |
| | 8 | 352 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 8 | 353 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 354 | | return ImmutableArray<GroupChatEntry>.Empty; |
| | | 355 | | |
| | 8 | 356 | | var agentTypeName = GetFullyQualifiedName(typeSymbol); |
| | 8 | 357 | | var entries = ImmutableArray.CreateBuilder<GroupChatEntry>(); |
| | | 358 | | |
| | 32 | 359 | | foreach (var attr in context.Attributes) |
| | | 360 | | { |
| | 8 | 361 | | if (attr.ConstructorArguments.Length < 1) |
| | | 362 | | continue; |
| | | 363 | | |
| | 8 | 364 | | var groupName = attr.ConstructorArguments[0].Value as string; |
| | 8 | 365 | | if (string.IsNullOrWhiteSpace(groupName)) |
| | | 366 | | continue; |
| | | 367 | | |
| | 8 | 368 | | entries.Add(new GroupChatEntry(agentTypeName, groupName!)); |
| | | 369 | | } |
| | | 370 | | |
| | 8 | 371 | | return entries.ToImmutable(); |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | private static ImmutableArray<SequenceEntry> GetSequenceEntries( |
| | | 375 | | GeneratorAttributeSyntaxContext context, |
| | | 376 | | CancellationToken cancellationToken) |
| | | 377 | | { |
| | 24 | 378 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 24 | 379 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 380 | | return ImmutableArray<SequenceEntry>.Empty; |
| | | 381 | | |
| | 24 | 382 | | var agentTypeName = GetFullyQualifiedName(typeSymbol); |
| | 24 | 383 | | var entries = ImmutableArray.CreateBuilder<SequenceEntry>(); |
| | | 384 | | |
| | 96 | 385 | | foreach (var attr in context.Attributes) |
| | | 386 | | { |
| | 24 | 387 | | if (attr.ConstructorArguments.Length < 2) |
| | | 388 | | continue; |
| | | 389 | | |
| | 24 | 390 | | var pipelineName = attr.ConstructorArguments[0].Value as string; |
| | 24 | 391 | | if (string.IsNullOrWhiteSpace(pipelineName)) |
| | | 392 | | continue; |
| | | 393 | | |
| | 24 | 394 | | if (attr.ConstructorArguments[1].Value is not int order) |
| | | 395 | | continue; |
| | | 396 | | |
| | 24 | 397 | | entries.Add(new SequenceEntry(agentTypeName, pipelineName!, order)); |
| | | 398 | | } |
| | | 399 | | |
| | 24 | 400 | | return entries.ToImmutable(); |
| | | 401 | | } |
| | | 402 | | |
| | | 403 | | private static ImmutableArray<TerminationConditionEntry> GetTerminationConditionEntries( |
| | | 404 | | GeneratorAttributeSyntaxContext context, |
| | | 405 | | CancellationToken cancellationToken) |
| | | 406 | | { |
| | 8 | 407 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 8 | 408 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 409 | | return ImmutableArray<TerminationConditionEntry>.Empty; |
| | | 410 | | |
| | 8 | 411 | | var agentTypeName = GetFullyQualifiedName(typeSymbol); |
| | 8 | 412 | | var entries = ImmutableArray.CreateBuilder<TerminationConditionEntry>(); |
| | | 413 | | |
| | 32 | 414 | | foreach (var attr in context.Attributes) |
| | | 415 | | { |
| | 8 | 416 | | if (attr.ConstructorArguments.Length < 1) |
| | | 417 | | continue; |
| | | 418 | | |
| | 8 | 419 | | var typeArg = attr.ConstructorArguments[0]; |
| | 8 | 420 | | if (typeArg.Kind != TypedConstantKind.Type || typeArg.Value is not INamedTypeSymbol condTypeSymbol) |
| | | 421 | | continue; |
| | | 422 | | |
| | 8 | 423 | | var condTypeFQN = GetFullyQualifiedName(condTypeSymbol); |
| | 8 | 424 | | var ctorArgLiterals = ImmutableArray<string>.Empty; |
| | | 425 | | |
| | 8 | 426 | | if (attr.ConstructorArguments.Length > 1) |
| | | 427 | | { |
| | 8 | 428 | | var paramsArg = attr.ConstructorArguments[1]; |
| | 8 | 429 | | if (paramsArg.Kind == TypedConstantKind.Array) |
| | | 430 | | { |
| | 8 | 431 | | ctorArgLiterals = paramsArg.Values |
| | 8 | 432 | | .Select(SerializeTypedConstant) |
| | 8 | 433 | | .Where(s => s is not null) |
| | 8 | 434 | | .Select(s => s!) |
| | 8 | 435 | | .ToImmutableArray(); |
| | | 436 | | } |
| | | 437 | | } |
| | | 438 | | |
| | 8 | 439 | | entries.Add(new TerminationConditionEntry(agentTypeName, condTypeFQN, ctorArgLiterals)); |
| | | 440 | | } |
| | | 441 | | |
| | 8 | 442 | | return entries.ToImmutable(); |
| | | 443 | | } |
| | | 444 | | |
| | | 445 | | private static string? SerializeTypedConstant(TypedConstant constant) |
| | | 446 | | { |
| | 8 | 447 | | return constant.Kind switch |
| | 8 | 448 | | { |
| | 8 | 449 | | TypedConstantKind.Primitive when constant.Value is string s => |
| | 8 | 450 | | "\"" + s.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"", |
| | 0 | 451 | | TypedConstantKind.Primitive when constant.Value is int i => i.ToString(), |
| | 0 | 452 | | TypedConstantKind.Primitive when constant.Value is long l => l.ToString() + "L", |
| | 0 | 453 | | TypedConstantKind.Primitive when constant.Value is bool b => b ? "true" : "false", |
| | 0 | 454 | | TypedConstantKind.Primitive when constant.Value is null => "null", |
| | 0 | 455 | | TypedConstantKind.Type when constant.Value is ITypeSymbol ts => |
| | 0 | 456 | | $"typeof({GetFullyQualifiedName(ts)})", |
| | 0 | 457 | | _ => null, |
| | 8 | 458 | | }; |
| | | 459 | | } |
| | | 460 | | |
| | | 461 | | private static string? GetDescriptionFromAttributes(ImmutableArray<AttributeData> attributes) |
| | | 462 | | { |
| | 55 | 463 | | foreach (var attr in attributes) |
| | | 464 | | { |
| | 10 | 465 | | if (attr.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == |
| | 10 | 466 | | "global::System.ComponentModel.DescriptionAttribute" && |
| | 10 | 467 | | attr.ConstructorArguments.Length == 1 && |
| | 10 | 468 | | attr.ConstructorArguments[0].Value is string desc) |
| | 1 | 469 | | return desc; |
| | | 470 | | } |
| | 17 | 471 | | return null; |
| | | 472 | | } |
| | | 473 | | |
| | | 474 | | private static string GetJsonSchemaType(ITypeSymbol type, out string? itemType) |
| | | 475 | | { |
| | 9 | 476 | | itemType = null; |
| | 9 | 477 | | if (type is INamedTypeSymbol nullable && nullable.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T) |
| | 0 | 478 | | type = nullable.TypeArguments[0]; |
| | | 479 | | |
| | 9 | 480 | | switch (type.SpecialType) |
| | | 481 | | { |
| | 3 | 482 | | case SpecialType.System_String: return "string"; |
| | 0 | 483 | | case SpecialType.System_Boolean: return "boolean"; |
| | | 484 | | case SpecialType.System_Byte: |
| | | 485 | | case SpecialType.System_SByte: |
| | | 486 | | case SpecialType.System_Int16: |
| | | 487 | | case SpecialType.System_UInt16: |
| | | 488 | | case SpecialType.System_Int32: |
| | | 489 | | case SpecialType.System_UInt32: |
| | | 490 | | case SpecialType.System_Int64: |
| | 5 | 491 | | case SpecialType.System_UInt64: return "integer"; |
| | | 492 | | case SpecialType.System_Single: |
| | | 493 | | case SpecialType.System_Double: |
| | 0 | 494 | | case SpecialType.System_Decimal: return "number"; |
| | | 495 | | } |
| | | 496 | | |
| | 1 | 497 | | if (type is IArrayTypeSymbol arrayType) |
| | | 498 | | { |
| | 0 | 499 | | itemType = GetJsonSchemaType(arrayType.ElementType, out _); |
| | 0 | 500 | | return "array"; |
| | | 501 | | } |
| | | 502 | | |
| | 1 | 503 | | if (type is INamedTypeSymbol named && named.IsGenericType && named.TypeArguments.Length == 1) |
| | | 504 | | { |
| | 0 | 505 | | var baseName = named.ConstructedFrom.ToDisplayString(); |
| | 0 | 506 | | if (baseName == "System.Collections.Generic.IEnumerable<T>" || |
| | 0 | 507 | | baseName == "System.Collections.Generic.List<T>" || |
| | 0 | 508 | | baseName == "System.Collections.Generic.IReadOnlyList<T>" || |
| | 0 | 509 | | baseName == "System.Collections.Generic.ICollection<T>" || |
| | 0 | 510 | | baseName == "System.Collections.Generic.IList<T>") |
| | | 511 | | { |
| | 0 | 512 | | itemType = GetJsonSchemaType(named.TypeArguments[0], out _); |
| | 0 | 513 | | return "array"; |
| | | 514 | | } |
| | | 515 | | } |
| | | 516 | | |
| | 1 | 517 | | return ""; |
| | | 518 | | } |
| | | 519 | | |
| | | 520 | | private static bool IsAccessibleFromGeneratedCode(INamedTypeSymbol typeSymbol) |
| | | 521 | | { |
| | 1773 | 522 | | if (typeSymbol.DeclaredAccessibility == Accessibility.Private || |
| | 1773 | 523 | | typeSymbol.DeclaredAccessibility == Accessibility.Protected) |
| | 0 | 524 | | return false; |
| | | 525 | | |
| | 1773 | 526 | | var current = typeSymbol.ContainingType; |
| | 1773 | 527 | | while (current != null) |
| | | 528 | | { |
| | 0 | 529 | | if (current.DeclaredAccessibility == Accessibility.Private) |
| | 0 | 530 | | return false; |
| | 0 | 531 | | current = current.ContainingType; |
| | | 532 | | } |
| | | 533 | | |
| | 1773 | 534 | | return true; |
| | | 535 | | } |
| | | 536 | | |
| | | 537 | | private static string GetFullyQualifiedName(ITypeSymbol typeSymbol) => |
| | 152 | 538 | | "global::" + typeSymbol.ToDisplayString( |
| | 152 | 539 | | SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle( |
| | 152 | 540 | | SymbolDisplayGlobalNamespaceStyle.Omitted)); |
| | | 541 | | |
| | | 542 | | private static string SanitizeIdentifier(string name) |
| | | 543 | | { |
| | 63 | 544 | | if (string.IsNullOrEmpty(name)) |
| | 0 | 545 | | return "Generated"; |
| | | 546 | | |
| | 63 | 547 | | var sb = new StringBuilder(name.Length); |
| | 1638 | 548 | | foreach (var c in name) |
| | | 549 | | { |
| | 756 | 550 | | if (char.IsLetterOrDigit(c) || c == '_') |
| | 756 | 551 | | sb.Append(c); |
| | 0 | 552 | | else if (c == '.' || c == '-' || c == ' ') |
| | 0 | 553 | | sb.Append(c == '.' ? '.' : '_'); |
| | | 554 | | } |
| | | 555 | | |
| | 63 | 556 | | var result = sb.ToString(); |
| | 63 | 557 | | var segments = result.Split('.'); |
| | 252 | 558 | | for (int i = 0; i < segments.Length; i++) |
| | | 559 | | { |
| | 63 | 560 | | if (segments[i].Length > 0 && char.IsDigit(segments[i][0])) |
| | 0 | 561 | | segments[i] = "_" + segments[i]; |
| | | 562 | | } |
| | | 563 | | |
| | 126 | 564 | | return string.Join(".", segments.Where(s => s.Length > 0)); |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | private static void ExecuteAll( |
| | | 568 | | ImmutableArray<AgentFunctionTypeInfo?> functionData, |
| | | 569 | | ImmutableArray<ImmutableArray<AgentFunctionGroupEntry>> groupData, |
| | | 570 | | ImmutableArray<NeedlrAiAgentTypeInfo?> agentData, |
| | | 571 | | ImmutableArray<ImmutableArray<HandoffEntry>> handoffData, |
| | | 572 | | ImmutableArray<ImmutableArray<GroupChatEntry>> groupChatData, |
| | | 573 | | ImmutableArray<ImmutableArray<SequenceEntry>> sequenceData, |
| | | 574 | | ImmutableArray<ImmutableArray<TerminationConditionEntry>> terminationData, |
| | | 575 | | Compilation compilation, |
| | | 576 | | Microsoft.CodeAnalysis.Diagnostics.AnalyzerConfigOptionsProvider configOptions, |
| | | 577 | | SourceProductionContext spc) |
| | | 578 | | { |
| | 63 | 579 | | var assemblyName = compilation.AssemblyName ?? "UnknownAssembly"; |
| | 63 | 580 | | var safeAssemblyName = SanitizeIdentifier(assemblyName); |
| | | 581 | | |
| | 63 | 582 | | var validFunctionTypes = functionData |
| | 9 | 583 | | .Where(t => t.HasValue) |
| | 9 | 584 | | .Select(t => t!.Value) |
| | 63 | 585 | | .ToList(); |
| | | 586 | | |
| | 73 | 587 | | var allGroupEntries = groupData.SelectMany(a => a).ToList(); |
| | 63 | 588 | | var groupedByName = allGroupEntries |
| | 10 | 589 | | .GroupBy(e => e.GroupName) |
| | 91 | 590 | | .ToDictionary(g => g.Key, g => g.Select(e => e.TypeName).Distinct().ToList()); |
| | | 591 | | |
| | 63 | 592 | | var validAgentTypes = agentData |
| | 58 | 593 | | .Where(t => t.HasValue) |
| | 58 | 594 | | .Select(t => t!.Value) |
| | 63 | 595 | | .ToList(); |
| | | 596 | | |
| | 68 | 597 | | var allHandoffEntries = handoffData.SelectMany(a => a).ToList(); |
| | 63 | 598 | | var handoffByInitialAgent = allHandoffEntries |
| | 5 | 599 | | .GroupBy(e => (e.InitialAgentTypeName, e.InitialAgentClassName)) |
| | 63 | 600 | | .ToDictionary( |
| | 5 | 601 | | g => g.Key, |
| | 73 | 602 | | g => g.Select(e => (e.TargetAgentTypeName, e.HandoffReason)).ToList()); |
| | | 603 | | |
| | 71 | 604 | | var allGroupChatEntries = groupChatData.SelectMany(a => a).ToList(); |
| | 63 | 605 | | var groupChatByGroupName = allGroupChatEntries |
| | 8 | 606 | | .GroupBy(e => e.GroupName) |
| | 79 | 607 | | .ToDictionary(g => g.Key, g => g.Select(e => e.AgentTypeName).Distinct().ToList()); |
| | | 608 | | |
| | 87 | 609 | | var allSequenceEntries = sequenceData.SelectMany(a => a).ToList(); |
| | 63 | 610 | | var sequenceByPipelineName = allSequenceEntries |
| | 24 | 611 | | .GroupBy(e => e.PipelineName) |
| | 63 | 612 | | .ToDictionary( |
| | 13 | 613 | | g => g.Key, |
| | 124 | 614 | | g => g.OrderBy(e => e.Order).Select(e => e.AgentTypeName).ToList()); |
| | | 615 | | |
| | 63 | 616 | | var conditionsByAgentTypeName = terminationData |
| | 8 | 617 | | .SelectMany(a => a) |
| | 8 | 618 | | .GroupBy(e => e.AgentTypeName) |
| | 79 | 619 | | .ToDictionary(g => g.Key, g => g.ToList()); |
| | | 620 | | |
| | | 621 | | // Always emit all registries (may be empty) and the bootstrap |
| | 63 | 622 | | spc.AddSource("AgentFrameworkFunctions.g.cs", |
| | 63 | 623 | | SourceText.From(GenerateRegistrySource(validFunctionTypes, safeAssemblyName), Encoding.UTF8)); |
| | | 624 | | |
| | 63 | 625 | | spc.AddSource("AgentFrameworkFunctionGroups.g.cs", |
| | 63 | 626 | | SourceText.From(GenerateGroupRegistrySource(groupedByName, safeAssemblyName), Encoding.UTF8)); |
| | | 627 | | |
| | 63 | 628 | | spc.AddSource("AgentRegistry.g.cs", |
| | 63 | 629 | | SourceText.From(GenerateAgentRegistrySource(validAgentTypes, safeAssemblyName), Encoding.UTF8)); |
| | | 630 | | |
| | 63 | 631 | | spc.AddSource("AgentHandoffTopologyRegistry.g.cs", |
| | 63 | 632 | | SourceText.From(GenerateHandoffTopologyRegistrySource(handoffByInitialAgent, safeAssemblyName), Encoding.UTF |
| | | 633 | | |
| | 63 | 634 | | spc.AddSource("AgentGroupChatRegistry.g.cs", |
| | 63 | 635 | | SourceText.From(GenerateGroupChatRegistrySource(groupChatByGroupName, safeAssemblyName), Encoding.UTF8)); |
| | | 636 | | |
| | 63 | 637 | | spc.AddSource("AgentSequentialTopologyRegistry.g.cs", |
| | 63 | 638 | | SourceText.From(GenerateSequentialTopologyRegistrySource(sequenceByPipelineName, safeAssemblyName), Encoding |
| | | 639 | | |
| | 63 | 640 | | spc.AddSource("NeedlrAgentFrameworkBootstrap.g.cs", |
| | 63 | 641 | | SourceText.From(GenerateBootstrapSource(safeAssemblyName), Encoding.UTF8)); |
| | | 642 | | |
| | 63 | 643 | | spc.AddSource("WorkflowFactoryExtensions.g.cs", |
| | 63 | 644 | | SourceText.From(GenerateWorkflowFactoryExtensionsSource( |
| | 63 | 645 | | handoffByInitialAgent, groupChatByGroupName, sequenceByPipelineName, |
| | 63 | 646 | | conditionsByAgentTypeName, safeAssemblyName), Encoding.UTF8)); |
| | | 647 | | |
| | 63 | 648 | | spc.AddSource("AgentFactoryExtensions.g.cs", |
| | 63 | 649 | | SourceText.From(GenerateAgentFactoryExtensionsSource(validAgentTypes, safeAssemblyName), Encoding.UTF8)); |
| | | 650 | | |
| | 63 | 651 | | spc.AddSource("AgentTopologyConstants.g.cs", |
| | 63 | 652 | | SourceText.From(GenerateAgentTopologyConstantsSource(validAgentTypes, allGroupEntries, sequenceByPipelineNam |
| | | 653 | | |
| | 63 | 654 | | spc.AddSource("AgentFrameworkSyringeExtensions.g.cs", |
| | 63 | 655 | | SourceText.From(GenerateSyringeExtensionsSource(allGroupEntries, safeAssemblyName), Encoding.UTF8)); |
| | | 656 | | |
| | 63 | 657 | | spc.AddSource("GeneratedAIFunctionProvider.g.cs", |
| | 63 | 658 | | SourceText.From(GenerateAIFunctionProviderSource(validFunctionTypes, safeAssemblyName), Encoding.UTF8)); |
| | | 659 | | |
| | 63 | 660 | | configOptions.GlobalOptions.TryGetValue("build_property.NeedlrDiagnostics", out var diagValue); |
| | 63 | 661 | | if (string.Equals(diagValue, "true", StringComparison.OrdinalIgnoreCase)) |
| | | 662 | | { |
| | 0 | 663 | | var mermaid = GenerateMermaidDiagram(handoffByInitialAgent, groupChatByGroupName, sequenceByPipelineName); |
| | | 664 | | |
| | 0 | 665 | | spc.AddSource("AgentTopologyGraph.g.cs", |
| | 0 | 666 | | SourceText.From(GenerateTopologyGraphSource(mermaid, safeAssemblyName), Encoding.UTF8)); |
| | | 667 | | } |
| | | 668 | | |
| | | 669 | | // Partial companions for [NeedlrAiAgent] classes declared as partial |
| | 196 | 670 | | foreach (var agentType in validAgentTypes.Where(a => a.IsPartial)) |
| | | 671 | | { |
| | 6 | 672 | | var safeTypeName = agentType.TypeName |
| | 6 | 673 | | .Replace("global::", "") |
| | 6 | 674 | | .Replace(".", "_") |
| | 6 | 675 | | .Replace("<", "_") |
| | 6 | 676 | | .Replace(">", "_"); |
| | | 677 | | |
| | 6 | 678 | | spc.AddSource($"{safeTypeName}.NeedlrAiAgent.g.cs", |
| | 6 | 679 | | SourceText.From(GeneratePartialCompanionSource(agentType, groupedByName), Encoding.UTF8)); |
| | | 680 | | } |
| | 63 | 681 | | } |
| | | 682 | | |
| | | 683 | | private static string GenerateTopologyGraphSource( |
| | | 684 | | string diagram, |
| | | 685 | | string safeAssemblyName) |
| | | 686 | | { |
| | 0 | 687 | | var escaped = diagram.Replace("\"", "\"\""); |
| | | 688 | | |
| | 0 | 689 | | var sb = new StringBuilder(); |
| | 0 | 690 | | sb.AppendLine("// <auto-generated/>"); |
| | 0 | 691 | | sb.AppendLine("#nullable enable"); |
| | 0 | 692 | | sb.AppendLine(); |
| | 0 | 693 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 0 | 694 | | sb.AppendLine(); |
| | 0 | 695 | | sb.AppendLine("internal static class AgentTopologyGraphDiagnostics"); |
| | 0 | 696 | | sb.AppendLine("{"); |
| | 0 | 697 | | sb.AppendLine($" public const string AgentTopologyGraph = @\"{escaped}\";"); |
| | 0 | 698 | | sb.AppendLine("}"); |
| | 0 | 699 | | return sb.ToString(); |
| | | 700 | | } |
| | | 701 | | |
| | | 702 | | private static string GenerateMermaidDiagram( |
| | | 703 | | Dictionary<(string InitialAgentTypeName, string InitialAgentClassName), List<(string TargetAgentTypeName, string |
| | | 704 | | Dictionary<string, List<string>> groupChatByGroupName, |
| | | 705 | | Dictionary<string, List<string>> sequenceByPipelineName) |
| | | 706 | | { |
| | 0 | 707 | | var sb = new StringBuilder(); |
| | 0 | 708 | | sb.AppendLine("graph TD"); |
| | | 709 | | |
| | 0 | 710 | | foreach (var kvp in handoffByInitialAgent.OrderBy(k => k.Key.InitialAgentClassName)) |
| | | 711 | | { |
| | 0 | 712 | | var sourceClass = kvp.Key.InitialAgentClassName; |
| | 0 | 713 | | foreach (var (targetFqn, reason) in kvp.Value) |
| | | 714 | | { |
| | 0 | 715 | | var targetClass = GetShortName(targetFqn); |
| | 0 | 716 | | if (string.IsNullOrWhiteSpace(reason)) |
| | 0 | 717 | | sb.AppendLine($" {sourceClass} --> {targetClass}"); |
| | | 718 | | else |
| | 0 | 719 | | sb.AppendLine($" {sourceClass} -->|\"{EscapeMermaidLabel(reason!)}\"| {targetClass}"); |
| | | 720 | | } |
| | | 721 | | } |
| | | 722 | | |
| | 0 | 723 | | foreach (var kvp in groupChatByGroupName.OrderBy(k => k.Key)) |
| | | 724 | | { |
| | 0 | 725 | | sb.AppendLine($" subgraph GroupChat_{SanitizeMermaidId(kvp.Key)}"); |
| | 0 | 726 | | foreach (var memberFqn in kvp.Value) |
| | 0 | 727 | | sb.AppendLine($" {GetShortName(memberFqn)}"); |
| | 0 | 728 | | sb.AppendLine(" end"); |
| | | 729 | | } |
| | | 730 | | |
| | 0 | 731 | | foreach (var kvp in sequenceByPipelineName.OrderBy(k => k.Key)) |
| | | 732 | | { |
| | 0 | 733 | | sb.AppendLine($" subgraph Sequential_{SanitizeMermaidId(kvp.Key)}"); |
| | 0 | 734 | | var agents = kvp.Value; |
| | 0 | 735 | | if (agents.Count == 1) |
| | | 736 | | { |
| | 0 | 737 | | sb.AppendLine($" {GetShortName(agents[0])}"); |
| | | 738 | | } |
| | | 739 | | else |
| | | 740 | | { |
| | 0 | 741 | | for (int i = 0; i < agents.Count - 1; i++) |
| | | 742 | | { |
| | 0 | 743 | | var cur = GetShortName(agents[i]); |
| | 0 | 744 | | var next = GetShortName(agents[i + 1]); |
| | 0 | 745 | | sb.AppendLine($" {cur} -->|\"{i + 1}\"| {next}"); |
| | | 746 | | } |
| | | 747 | | } |
| | 0 | 748 | | sb.AppendLine(" end"); |
| | | 749 | | } |
| | | 750 | | |
| | 0 | 751 | | return sb.ToString(); |
| | | 752 | | } |
| | | 753 | | |
| | | 754 | | private static string GetShortName(string fqn) |
| | | 755 | | { |
| | 18 | 756 | | var stripped = fqn.StartsWith("global::", StringComparison.Ordinal) ? fqn.Substring(8) : fqn; |
| | 18 | 757 | | var lastDot = stripped.LastIndexOf('.'); |
| | 18 | 758 | | return lastDot >= 0 ? stripped.Substring(lastDot + 1) : stripped; |
| | | 759 | | } |
| | | 760 | | |
| | | 761 | | private static string SanitizeMermaidId(string name) |
| | | 762 | | { |
| | 0 | 763 | | var sb = new StringBuilder(); |
| | 0 | 764 | | foreach (var c in name) |
| | 0 | 765 | | sb.Append(char.IsLetterOrDigit(c) ? c : '_'); |
| | 0 | 766 | | return sb.ToString(); |
| | | 767 | | } |
| | | 768 | | |
| | | 769 | | private static string EscapeMermaidLabel(string label) => |
| | 0 | 770 | | label.Replace("\"", """); |
| | | 771 | | |
| | | 772 | | private static string GenerateRegistrySource( |
| | | 773 | | List<AgentFunctionTypeInfo> types, |
| | | 774 | | string safeAssemblyName) |
| | | 775 | | { |
| | 63 | 776 | | var sb = new StringBuilder(); |
| | | 777 | | |
| | 63 | 778 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 779 | | sb.AppendLine("// Needlr AgentFramework Functions"); |
| | 63 | 780 | | sb.AppendLine("#nullable enable"); |
| | 63 | 781 | | sb.AppendLine(); |
| | 63 | 782 | | sb.AppendLine("using System;"); |
| | 63 | 783 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 63 | 784 | | sb.AppendLine(); |
| | 63 | 785 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 786 | | sb.AppendLine(); |
| | 63 | 787 | | sb.AppendLine("/// <summary>"); |
| | 63 | 788 | | sb.AppendLine("/// Generated registry for Microsoft Agent Framework function types discovered at compile time.") |
| | 63 | 789 | | sb.AppendLine("/// Pass <see cref=\"AllFunctionTypes\"/> to"); |
| | 63 | 790 | | sb.AppendLine("/// <c>AgentFrameworkSyringeExtensions.AddAgentFunctionsFromGenerated</c>."); |
| | 63 | 791 | | sb.AppendLine("/// </summary>"); |
| | 63 | 792 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 793 | | sb.AppendLine("public static class AgentFrameworkFunctionRegistry"); |
| | 63 | 794 | | sb.AppendLine("{"); |
| | 63 | 795 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 796 | | sb.AppendLine(" /// All types containing methods decorated with [AgentFunction], discovered at compile time." |
| | 63 | 797 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 798 | | sb.AppendLine(" public static IReadOnlyList<Type> AllFunctionTypes { get; } = new Type[]"); |
| | 63 | 799 | | sb.AppendLine(" {"); |
| | | 800 | | |
| | 144 | 801 | | foreach (var type in types) |
| | | 802 | | { |
| | 9 | 803 | | sb.AppendLine($" typeof({type.TypeName}),"); |
| | | 804 | | } |
| | | 805 | | |
| | 63 | 806 | | sb.AppendLine(" };"); |
| | 63 | 807 | | sb.AppendLine(); |
| | 63 | 808 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 809 | | sb.AppendLine(" /// Gets the number of function types discovered at compile time."); |
| | 63 | 810 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 811 | | sb.AppendLine($" public static int Count => {types.Count};"); |
| | 63 | 812 | | sb.AppendLine("}"); |
| | | 813 | | |
| | 63 | 814 | | return sb.ToString(); |
| | | 815 | | } |
| | | 816 | | |
| | | 817 | | private static string GenerateGroupRegistrySource( |
| | | 818 | | Dictionary<string, List<string>> groupedByName, |
| | | 819 | | string safeAssemblyName) |
| | | 820 | | { |
| | 63 | 821 | | var sb = new StringBuilder(); |
| | | 822 | | |
| | 63 | 823 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 824 | | sb.AppendLine("// Needlr AgentFramework Function Groups"); |
| | 63 | 825 | | sb.AppendLine("#nullable enable"); |
| | 63 | 826 | | sb.AppendLine(); |
| | 63 | 827 | | sb.AppendLine("using System;"); |
| | 63 | 828 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 63 | 829 | | sb.AppendLine(); |
| | 63 | 830 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 831 | | sb.AppendLine(); |
| | 63 | 832 | | sb.AppendLine("/// <summary>"); |
| | 63 | 833 | | sb.AppendLine("/// Generated registry for Microsoft Agent Framework function groups discovered at compile time." |
| | 63 | 834 | | sb.AppendLine("/// Pass <see cref=\"AllGroups\"/> to"); |
| | 63 | 835 | | sb.AppendLine("/// <c>AgentFrameworkSyringeExtensions.AddAgentFunctionGroupsFromGenerated</c>."); |
| | 63 | 836 | | sb.AppendLine("/// </summary>"); |
| | 63 | 837 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 838 | | sb.AppendLine("public static class AgentFrameworkFunctionGroupRegistry"); |
| | 63 | 839 | | sb.AppendLine("{"); |
| | 63 | 840 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 841 | | sb.AppendLine(" /// All function groups, mapping group name to the types in that group, discovered at compile |
| | 63 | 842 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 843 | | sb.AppendLine(" public static IReadOnlyDictionary<string, IReadOnlyList<Type>> AllGroups { get; } ="); |
| | 63 | 844 | | sb.AppendLine(" new Dictionary<string, IReadOnlyList<Type>>()"); |
| | 63 | 845 | | sb.AppendLine(" {"); |
| | | 846 | | |
| | 153 | 847 | | foreach (var kvp in groupedByName.OrderBy(k => k.Key)) |
| | | 848 | | { |
| | 9 | 849 | | var escapedName = kvp.Key.Replace("\"", "\\\""); |
| | 9 | 850 | | var typeNames = kvp.Value; |
| | 9 | 851 | | sb.AppendLine($" [\"{escapedName}\"] = new Type[]"); |
| | 9 | 852 | | sb.AppendLine(" {"); |
| | 38 | 853 | | foreach (var typeName in typeNames) |
| | | 854 | | { |
| | 10 | 855 | | sb.AppendLine($" typeof({typeName}),"); |
| | | 856 | | } |
| | 9 | 857 | | sb.AppendLine(" },"); |
| | | 858 | | } |
| | | 859 | | |
| | 63 | 860 | | sb.AppendLine(" };"); |
| | 63 | 861 | | sb.AppendLine("}"); |
| | | 862 | | |
| | 63 | 863 | | return sb.ToString(); |
| | | 864 | | } |
| | | 865 | | |
| | | 866 | | private static string GenerateAgentRegistrySource( |
| | | 867 | | List<NeedlrAiAgentTypeInfo> types, |
| | | 868 | | string safeAssemblyName) |
| | | 869 | | { |
| | 63 | 870 | | var sb = new StringBuilder(); |
| | | 871 | | |
| | 63 | 872 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 873 | | sb.AppendLine("// Needlr AgentFramework Agent Registry"); |
| | 63 | 874 | | sb.AppendLine("#nullable enable"); |
| | 63 | 875 | | sb.AppendLine(); |
| | 63 | 876 | | sb.AppendLine("using System;"); |
| | 63 | 877 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 63 | 878 | | sb.AppendLine(); |
| | 63 | 879 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 880 | | sb.AppendLine(); |
| | 63 | 881 | | sb.AppendLine("/// <summary>"); |
| | 63 | 882 | | sb.AppendLine("/// Generated registry for agent types declared with [NeedlrAiAgent], discovered at compile time. |
| | 63 | 883 | | sb.AppendLine("/// </summary>"); |
| | 63 | 884 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 885 | | sb.AppendLine("public static class AgentRegistry"); |
| | 63 | 886 | | sb.AppendLine("{"); |
| | 63 | 887 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 888 | | sb.AppendLine(" /// All types decorated with [NeedlrAiAgent], discovered at compile time."); |
| | 63 | 889 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 890 | | sb.AppendLine(" public static IReadOnlyList<Type> AllAgentTypes { get; } = new Type[]"); |
| | 63 | 891 | | sb.AppendLine(" {"); |
| | | 892 | | |
| | 242 | 893 | | foreach (var type in types) |
| | | 894 | | { |
| | 58 | 895 | | sb.AppendLine($" typeof({type.TypeName}),"); |
| | | 896 | | } |
| | | 897 | | |
| | 63 | 898 | | sb.AppendLine(" };"); |
| | 63 | 899 | | sb.AppendLine(); |
| | 63 | 900 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 901 | | sb.AppendLine(" /// Gets the number of agent types discovered at compile time."); |
| | 63 | 902 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 903 | | sb.AppendLine($" public static int Count => {types.Count};"); |
| | 63 | 904 | | sb.AppendLine("}"); |
| | | 905 | | |
| | 63 | 906 | | return sb.ToString(); |
| | | 907 | | } |
| | | 908 | | |
| | | 909 | | private static string GenerateHandoffTopologyRegistrySource( |
| | | 910 | | Dictionary<(string InitialAgentTypeName, string InitialAgentClassName), List<(string TargetAgentTypeName, string |
| | | 911 | | string safeAssemblyName) |
| | | 912 | | { |
| | 63 | 913 | | var sb = new StringBuilder(); |
| | 63 | 914 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 915 | | sb.AppendLine("// Needlr AgentFramework Handoff Topology Registry"); |
| | 63 | 916 | | sb.AppendLine("#nullable enable"); |
| | 63 | 917 | | sb.AppendLine(); |
| | 63 | 918 | | sb.AppendLine("using System;"); |
| | 63 | 919 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 63 | 920 | | sb.AppendLine(); |
| | 63 | 921 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 922 | | sb.AppendLine(); |
| | 63 | 923 | | sb.AppendLine("/// <summary>"); |
| | 63 | 924 | | sb.AppendLine("/// Generated registry of agent handoff topology declared via [AgentHandoffsTo] attributes."); |
| | 63 | 925 | | sb.AppendLine("/// </summary>"); |
| | 63 | 926 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 927 | | sb.AppendLine("public static class AgentHandoffTopologyRegistry"); |
| | 63 | 928 | | sb.AppendLine("{"); |
| | 63 | 929 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 930 | | sb.AppendLine(" /// All handoff relationships, mapping initial agent type to its declared handoff targets."); |
| | 63 | 931 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 932 | | sb.AppendLine(" public static IReadOnlyDictionary<Type, IReadOnlyList<(Type TargetType, string? HandoffReason |
| | 63 | 933 | | sb.AppendLine(" new Dictionary<Type, IReadOnlyList<(Type, string?)>>()"); |
| | 63 | 934 | | sb.AppendLine(" {"); |
| | | 935 | | |
| | 141 | 936 | | foreach (var kvp in handoffByInitialAgent.OrderBy(k => k.Key.InitialAgentClassName)) |
| | | 937 | | { |
| | 5 | 938 | | sb.AppendLine($" [typeof({kvp.Key.InitialAgentTypeName})] = new (Type, string?)[]"); |
| | 5 | 939 | | sb.AppendLine(" {"); |
| | 20 | 940 | | foreach (var (targetType, reason) in kvp.Value) |
| | | 941 | | { |
| | 5 | 942 | | var reasonLiteral = reason is null ? "null" : $"\"{reason.Replace("\"", "\\\"")}\""; |
| | 5 | 943 | | sb.AppendLine($" (typeof({targetType}), {reasonLiteral}),"); |
| | | 944 | | } |
| | 5 | 945 | | sb.AppendLine(" },"); |
| | | 946 | | } |
| | | 947 | | |
| | 63 | 948 | | sb.AppendLine(" };"); |
| | 63 | 949 | | sb.AppendLine("}"); |
| | 63 | 950 | | return sb.ToString(); |
| | | 951 | | } |
| | | 952 | | |
| | | 953 | | private static string GenerateGroupChatRegistrySource( |
| | | 954 | | Dictionary<string, List<string>> groupChatByGroupName, |
| | | 955 | | string safeAssemblyName) |
| | | 956 | | { |
| | 63 | 957 | | var sb = new StringBuilder(); |
| | 63 | 958 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 959 | | sb.AppendLine("// Needlr AgentFramework Group Chat Registry"); |
| | 63 | 960 | | sb.AppendLine("#nullable enable"); |
| | 63 | 961 | | sb.AppendLine(); |
| | 63 | 962 | | sb.AppendLine("using System;"); |
| | 63 | 963 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 63 | 964 | | sb.AppendLine(); |
| | 63 | 965 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 966 | | sb.AppendLine(); |
| | 63 | 967 | | sb.AppendLine("/// <summary>"); |
| | 63 | 968 | | sb.AppendLine("/// Generated registry of agent group chat memberships declared via [AgentGroupChatMember] attrib |
| | 63 | 969 | | sb.AppendLine("/// </summary>"); |
| | 63 | 970 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 971 | | sb.AppendLine("public static class AgentGroupChatRegistry"); |
| | 63 | 972 | | sb.AppendLine("{"); |
| | 63 | 973 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 974 | | sb.AppendLine(" /// All group chat groups, mapping group name to the agent types in that group."); |
| | 63 | 975 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 976 | | sb.AppendLine(" public static IReadOnlyDictionary<string, IReadOnlyList<Type>> AllGroups { get; } ="); |
| | 63 | 977 | | sb.AppendLine(" new Dictionary<string, IReadOnlyList<Type>>()"); |
| | 63 | 978 | | sb.AppendLine(" {"); |
| | | 979 | | |
| | 138 | 980 | | foreach (var kvp in groupChatByGroupName.OrderBy(k => k.Key)) |
| | | 981 | | { |
| | 4 | 982 | | var escapedName = kvp.Key.Replace("\"", "\\\""); |
| | 4 | 983 | | sb.AppendLine($" [\"{escapedName}\"] = new Type[]"); |
| | 4 | 984 | | sb.AppendLine(" {"); |
| | 24 | 985 | | foreach (var typeName in kvp.Value) |
| | 8 | 986 | | sb.AppendLine($" typeof({typeName}),"); |
| | 4 | 987 | | sb.AppendLine(" },"); |
| | | 988 | | } |
| | | 989 | | |
| | 63 | 990 | | sb.AppendLine(" };"); |
| | 63 | 991 | | sb.AppendLine("}"); |
| | 63 | 992 | | return sb.ToString(); |
| | | 993 | | } |
| | | 994 | | |
| | | 995 | | private static string GenerateSequentialTopologyRegistrySource( |
| | | 996 | | Dictionary<string, List<string>> sequenceByPipelineName, |
| | | 997 | | string safeAssemblyName) |
| | | 998 | | { |
| | 63 | 999 | | var sb = new StringBuilder(); |
| | 63 | 1000 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 1001 | | sb.AppendLine("// Needlr AgentFramework Sequential Pipeline Registry"); |
| | 63 | 1002 | | sb.AppendLine("#nullable enable"); |
| | 63 | 1003 | | sb.AppendLine(); |
| | 63 | 1004 | | sb.AppendLine("using System;"); |
| | 63 | 1005 | | sb.AppendLine("using System.Collections.Generic;"); |
| | 63 | 1006 | | sb.AppendLine(); |
| | 63 | 1007 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 1008 | | sb.AppendLine(); |
| | 63 | 1009 | | sb.AppendLine("/// <summary>"); |
| | 63 | 1010 | | sb.AppendLine("/// Generated registry of sequential pipeline memberships declared via [AgentSequenceMember] attr |
| | 63 | 1011 | | sb.AppendLine("/// Agents within each pipeline are stored in ascending order value order."); |
| | 63 | 1012 | | sb.AppendLine("/// </summary>"); |
| | 63 | 1013 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1014 | | sb.AppendLine("public static class AgentSequentialTopologyRegistry"); |
| | 63 | 1015 | | sb.AppendLine("{"); |
| | 63 | 1016 | | sb.AppendLine(" /// <summary>"); |
| | 63 | 1017 | | sb.AppendLine(" /// All sequential pipelines, mapping pipeline name to the ordered agent types."); |
| | 63 | 1018 | | sb.AppendLine(" /// </summary>"); |
| | 63 | 1019 | | sb.AppendLine(" public static IReadOnlyDictionary<string, IReadOnlyList<Type>> AllPipelines { get; } ="); |
| | 63 | 1020 | | sb.AppendLine(" new Dictionary<string, IReadOnlyList<Type>>()"); |
| | 63 | 1021 | | sb.AppendLine(" {"); |
| | | 1022 | | |
| | 165 | 1023 | | foreach (var kvp in sequenceByPipelineName.OrderBy(k => k.Key)) |
| | | 1024 | | { |
| | 13 | 1025 | | var escapedName = kvp.Key.Replace("\"", "\\\""); |
| | 13 | 1026 | | sb.AppendLine($" [\"{escapedName}\"] = new Type[]"); |
| | 13 | 1027 | | sb.AppendLine(" {"); |
| | 74 | 1028 | | foreach (var typeName in kvp.Value) |
| | 24 | 1029 | | sb.AppendLine($" typeof({typeName}),"); |
| | 13 | 1030 | | sb.AppendLine(" },"); |
| | | 1031 | | } |
| | | 1032 | | |
| | 63 | 1033 | | sb.AppendLine(" };"); |
| | 63 | 1034 | | sb.AppendLine("}"); |
| | 63 | 1035 | | return sb.ToString(); |
| | | 1036 | | } |
| | | 1037 | | |
| | | 1038 | | private static string GenerateWorkflowFactoryExtensionsSource( |
| | | 1039 | | Dictionary<(string InitialAgentTypeName, string InitialAgentClassName), List<(string TargetAgentTypeName, string |
| | | 1040 | | Dictionary<string, List<string>> groupChatByGroupName, |
| | | 1041 | | Dictionary<string, List<string>> sequenceByPipelineName, |
| | | 1042 | | Dictionary<string, List<TerminationConditionEntry>> conditionsByAgentTypeName, |
| | | 1043 | | string safeAssemblyName) |
| | | 1044 | | { |
| | 63 | 1045 | | var sb = new StringBuilder(); |
| | 63 | 1046 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 1047 | | sb.AppendLine("// Needlr AgentFramework IWorkflowFactory Extension Methods"); |
| | 63 | 1048 | | sb.AppendLine("#nullable enable"); |
| | 63 | 1049 | | sb.AppendLine(); |
| | 63 | 1050 | | sb.AppendLine("using Microsoft.Agents.AI.Workflows;"); |
| | 63 | 1051 | | sb.AppendLine("using NexusLabs.Needlr.AgentFramework;"); |
| | 63 | 1052 | | sb.AppendLine(); |
| | 63 | 1053 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 1054 | | sb.AppendLine(); |
| | 63 | 1055 | | sb.AppendLine("/// <summary>"); |
| | 63 | 1056 | | sb.AppendLine("/// Generated strongly-typed extension methods on <see cref=\"IWorkflowFactory\"/>."); |
| | 63 | 1057 | | sb.AppendLine("/// Each method encapsulates an agent type or group name so the composition root"); |
| | 63 | 1058 | | sb.AppendLine("/// requires no direct agent type references or magic strings."); |
| | 63 | 1059 | | sb.AppendLine("/// </summary>"); |
| | 63 | 1060 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1061 | | sb.AppendLine("public static partial class WorkflowFactoryExtensions"); |
| | 63 | 1062 | | sb.AppendLine("{"); |
| | | 1063 | | |
| | 141 | 1064 | | foreach (var kvp in handoffByInitialAgent.OrderBy(k => k.Key.InitialAgentClassName)) |
| | | 1065 | | { |
| | 5 | 1066 | | var className = kvp.Key.InitialAgentClassName; |
| | 5 | 1067 | | var typeName = kvp.Key.InitialAgentTypeName; |
| | 5 | 1068 | | var methodName = $"Create{StripAgentSuffix(className)}HandoffWorkflow"; |
| | | 1069 | | |
| | 5 | 1070 | | sb.AppendLine($" /// <summary>"); |
| | 5 | 1071 | | sb.AppendLine($" /// Creates a handoff workflow starting from <see cref=\"{typeName.Replace("global::", " |
| | 5 | 1072 | | sb.AppendLine($" /// </summary>"); |
| | 5 | 1073 | | sb.AppendLine($" /// <remarks>"); |
| | 5 | 1074 | | sb.AppendLine($" /// Handoff targets declared via <see cref=\"global::NexusLabs.Needlr.AgentFramework.Age |
| | 5 | 1075 | | sb.AppendLine($" /// <list type=\"bullet\">"); |
| | 20 | 1076 | | foreach (var (targetTypeName, reason) in kvp.Value) |
| | | 1077 | | { |
| | 5 | 1078 | | var cref = targetTypeName.Replace("global::", ""); |
| | 5 | 1079 | | if (string.IsNullOrEmpty(reason)) |
| | 4 | 1080 | | sb.AppendLine($" /// <item><description><see cref=\"{cref}\"/></description></item>"); |
| | | 1081 | | else |
| | 1 | 1082 | | sb.AppendLine($" /// <item><description><see cref=\"{cref}\"/> — {reason}</description></item>"); |
| | | 1083 | | } |
| | 5 | 1084 | | sb.AppendLine($" /// </list>"); |
| | 5 | 1085 | | sb.AppendLine($" /// </remarks>"); |
| | 5 | 1086 | | sb.AppendLine($" public static Workflow {methodName}(this IWorkflowFactory workflowFactory)"); |
| | 5 | 1087 | | sb.AppendLine($" => workflowFactory.CreateHandoffWorkflow<{typeName}>();"); |
| | 5 | 1088 | | sb.AppendLine(); |
| | | 1089 | | |
| | 5 | 1090 | | var allHandoffAgents = new[] { typeName } |
| | 5 | 1091 | | .Concat(kvp.Value.Select(v => v.TargetAgentTypeName)) |
| | 5 | 1092 | | .ToList(); |
| | 5 | 1093 | | var handoffConditions = allHandoffAgents |
| | 10 | 1094 | | .SelectMany(t => conditionsByAgentTypeName.TryGetValue(t, out var c) ? c : Enumerable.Empty<TerminationC |
| | 5 | 1095 | | .ToList(); |
| | | 1096 | | |
| | 5 | 1097 | | if (handoffConditions.Count > 0) |
| | | 1098 | | { |
| | 1 | 1099 | | var runMethodName = $"Run{StripAgentSuffix(className)}HandoffWorkflowAsync"; |
| | 1 | 1100 | | sb.AppendLine($" /// <summary>"); |
| | 1 | 1101 | | sb.AppendLine($" /// Creates and runs the handoff workflow starting from <see cref=\"{typeName.Replac |
| | 1 | 1102 | | sb.AppendLine($" /// </summary>"); |
| | 1 | 1103 | | sb.AppendLine($" /// <remarks>"); |
| | 1 | 1104 | | sb.AppendLine($" /// Termination conditions are evaluated after each completed agent turn (Layer 2)." |
| | 1 | 1105 | | sb.AppendLine($" /// The workflow stops early when any condition is satisfied."); |
| | 1 | 1106 | | sb.AppendLine($" /// </remarks>"); |
| | 1 | 1107 | | sb.AppendLine($" /// <param name=\"workflowFactory\">The workflow factory.</param>"); |
| | 1 | 1108 | | sb.AppendLine($" /// <param name=\"message\">The input message to start the workflow.</param>"); |
| | 1 | 1109 | | sb.AppendLine($" /// <param name=\"cancellationToken\">Optional cancellation token.</param>"); |
| | 1 | 1110 | | sb.AppendLine($" /// <returns>A dictionary mapping executor IDs to their response text.</returns>"); |
| | 1 | 1111 | | sb.AppendLine($" public static async global::System.Threading.Tasks.Task<global::System.Collections.G |
| | 1 | 1112 | | sb.AppendLine($" this IWorkflowFactory workflowFactory,"); |
| | 1 | 1113 | | sb.AppendLine($" string message,"); |
| | 1 | 1114 | | sb.AppendLine($" global::System.Threading.CancellationToken cancellationToken = default)"); |
| | 1 | 1115 | | sb.AppendLine($" {{"); |
| | 1 | 1116 | | sb.AppendLine($" var workflow = workflowFactory.{methodName}();"); |
| | 1 | 1117 | | sb.AppendLine($" global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.AgentF |
| | 1 | 1118 | | sb.AppendLine($" new global::System.Collections.Generic.List<global::NexusLabs.Needlr.AgentFr |
| | 1 | 1119 | | sb.AppendLine($" {{"); |
| | 4 | 1120 | | foreach (var cond in handoffConditions) |
| | | 1121 | | { |
| | 1 | 1122 | | var args = cond.CtorArgLiterals.IsEmpty ? "" : string.Join(", ", cond.CtorArgLiterals); |
| | 1 | 1123 | | sb.AppendLine($" new {cond.ConditionTypeFQN}({args}),"); |
| | | 1124 | | } |
| | 1 | 1125 | | sb.AppendLine($" }};"); |
| | 1 | 1126 | | sb.AppendLine($" return await global::NexusLabs.Needlr.AgentFramework.Workflows.StreamingRunWorkf |
| | 1 | 1127 | | sb.AppendLine($" }}"); |
| | 1 | 1128 | | sb.AppendLine(); |
| | | 1129 | | } |
| | | 1130 | | } |
| | | 1131 | | |
| | 138 | 1132 | | foreach (var kvp in groupChatByGroupName.OrderBy(k => k.Key)) |
| | | 1133 | | { |
| | 4 | 1134 | | var groupName = kvp.Key; |
| | 4 | 1135 | | var methodSuffix = GroupNameToPascalCase(groupName); |
| | 4 | 1136 | | var methodName = $"Create{methodSuffix}GroupChatWorkflow"; |
| | 4 | 1137 | | var escapedGroupName = groupName.Replace("\"", "\\\""); |
| | | 1138 | | |
| | 4 | 1139 | | sb.AppendLine($" /// <summary>"); |
| | 4 | 1140 | | sb.AppendLine($" /// Creates a round-robin group chat workflow for the \"{escapedGroupName}\" group."); |
| | 4 | 1141 | | sb.AppendLine($" /// </summary>"); |
| | 4 | 1142 | | sb.AppendLine($" /// <remarks>"); |
| | 4 | 1143 | | sb.AppendLine($" /// Participants declared via <see cref=\"global::NexusLabs.Needlr.AgentFramework.AgentG |
| | 4 | 1144 | | sb.AppendLine($" /// <list type=\"bullet\">"); |
| | 24 | 1145 | | foreach (var participantTypeName in kvp.Value) |
| | | 1146 | | { |
| | 8 | 1147 | | var cref = participantTypeName.Replace("global::", ""); |
| | 8 | 1148 | | sb.AppendLine($" /// <item><description><see cref=\"{cref}\"/></description></item>"); |
| | | 1149 | | } |
| | 4 | 1150 | | sb.AppendLine($" /// </list>"); |
| | 4 | 1151 | | sb.AppendLine($" /// </remarks>"); |
| | 4 | 1152 | | sb.AppendLine($" public static Workflow {methodName}(this IWorkflowFactory workflowFactory, int maxIterat |
| | 4 | 1153 | | sb.AppendLine($" => workflowFactory.CreateGroupChatWorkflow(\"{escapedGroupName}\", maxIterations);") |
| | 4 | 1154 | | sb.AppendLine(); |
| | | 1155 | | |
| | 4 | 1156 | | var gcConditions = kvp.Value |
| | 8 | 1157 | | .SelectMany(t => conditionsByAgentTypeName.TryGetValue(t, out var c) ? c : Enumerable.Empty<TerminationC |
| | 4 | 1158 | | .ToList(); |
| | | 1159 | | |
| | 4 | 1160 | | if (gcConditions.Count > 0) |
| | | 1161 | | { |
| | 1 | 1162 | | var runMethodName = $"Run{methodSuffix}GroupChatWorkflowAsync"; |
| | 1 | 1163 | | sb.AppendLine($" /// <summary>"); |
| | 1 | 1164 | | sb.AppendLine($" /// Creates and runs the \"{escapedGroupName}\" group chat workflow, applying declar |
| | 1 | 1165 | | sb.AppendLine($" /// </summary>"); |
| | 1 | 1166 | | sb.AppendLine($" /// <remarks>"); |
| | 1 | 1167 | | sb.AppendLine($" /// Termination conditions are evaluated after each completed agent turn (Layer 2)." |
| | 1 | 1168 | | sb.AppendLine($" /// The workflow stops early when any condition is satisfied."); |
| | 1 | 1169 | | sb.AppendLine($" /// </remarks>"); |
| | 1 | 1170 | | sb.AppendLine($" /// <param name=\"workflowFactory\">The workflow factory.</param>"); |
| | 1 | 1171 | | sb.AppendLine($" /// <param name=\"message\">The input message to start the workflow.</param>"); |
| | 1 | 1172 | | sb.AppendLine($" /// <param name=\"maxIterations\">Maximum number of group chat iterations.</param>") |
| | 1 | 1173 | | sb.AppendLine($" /// <param name=\"cancellationToken\">Optional cancellation token.</param>"); |
| | 1 | 1174 | | sb.AppendLine($" /// <returns>A dictionary mapping executor IDs to their response text.</returns>"); |
| | 1 | 1175 | | sb.AppendLine($" public static async global::System.Threading.Tasks.Task<global::System.Collections.G |
| | 1 | 1176 | | sb.AppendLine($" this IWorkflowFactory workflowFactory,"); |
| | 1 | 1177 | | sb.AppendLine($" string message,"); |
| | 1 | 1178 | | sb.AppendLine($" int maxIterations = 10,"); |
| | 1 | 1179 | | sb.AppendLine($" global::System.Threading.CancellationToken cancellationToken = default)"); |
| | 1 | 1180 | | sb.AppendLine($" {{"); |
| | 1 | 1181 | | sb.AppendLine($" var workflow = workflowFactory.{methodName}(maxIterations);"); |
| | 1 | 1182 | | sb.AppendLine($" global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.AgentF |
| | 1 | 1183 | | sb.AppendLine($" new global::System.Collections.Generic.List<global::NexusLabs.Needlr.AgentFr |
| | 1 | 1184 | | sb.AppendLine($" {{"); |
| | 4 | 1185 | | foreach (var cond in gcConditions) |
| | | 1186 | | { |
| | 1 | 1187 | | var args = cond.CtorArgLiterals.IsEmpty ? "" : string.Join(", ", cond.CtorArgLiterals); |
| | 1 | 1188 | | sb.AppendLine($" new {cond.ConditionTypeFQN}({args}),"); |
| | | 1189 | | } |
| | 1 | 1190 | | sb.AppendLine($" }};"); |
| | 1 | 1191 | | sb.AppendLine($" return await global::NexusLabs.Needlr.AgentFramework.Workflows.StreamingRunWorkf |
| | 1 | 1192 | | sb.AppendLine($" }}"); |
| | 1 | 1193 | | sb.AppendLine(); |
| | | 1194 | | } |
| | | 1195 | | } |
| | | 1196 | | |
| | 165 | 1197 | | foreach (var kvp in sequenceByPipelineName.OrderBy(k => k.Key)) |
| | | 1198 | | { |
| | 13 | 1199 | | var pipelineName = kvp.Key; |
| | 13 | 1200 | | var methodSuffix = GroupNameToPascalCase(pipelineName); |
| | 13 | 1201 | | var methodName = $"Create{methodSuffix}SequentialWorkflow"; |
| | 13 | 1202 | | var escapedPipelineName = pipelineName.Replace("\"", "\\\""); |
| | | 1203 | | |
| | 13 | 1204 | | sb.AppendLine($" /// <summary>"); |
| | 13 | 1205 | | sb.AppendLine($" /// Creates a sequential pipeline workflow for the \"{escapedPipelineName}\" pipeline.") |
| | 13 | 1206 | | sb.AppendLine($" /// </summary>"); |
| | 13 | 1207 | | sb.AppendLine($" /// <remarks>"); |
| | 13 | 1208 | | sb.AppendLine($" /// Agents declared via <see cref=\"global::NexusLabs.Needlr.AgentFramework.AgentSequenc |
| | 13 | 1209 | | sb.AppendLine($" /// <list type=\"number\">"); |
| | 74 | 1210 | | foreach (var memberTypeName in kvp.Value) |
| | | 1211 | | { |
| | 24 | 1212 | | var cref = memberTypeName.Replace("global::", ""); |
| | 24 | 1213 | | sb.AppendLine($" /// <item><description><see cref=\"{cref}\"/></description></item>"); |
| | | 1214 | | } |
| | 13 | 1215 | | sb.AppendLine($" /// </list>"); |
| | 13 | 1216 | | sb.AppendLine($" /// </remarks>"); |
| | 13 | 1217 | | sb.AppendLine($" public static Workflow {methodName}(this IWorkflowFactory workflowFactory)"); |
| | 13 | 1218 | | sb.AppendLine($" => workflowFactory.CreateSequentialWorkflow(\"{escapedPipelineName}\");"); |
| | 13 | 1219 | | sb.AppendLine(); |
| | | 1220 | | |
| | 13 | 1221 | | var seqConditions = kvp.Value |
| | 24 | 1222 | | .SelectMany(t => conditionsByAgentTypeName.TryGetValue(t, out var c) ? c : Enumerable.Empty<TerminationC |
| | 13 | 1223 | | .ToList(); |
| | | 1224 | | |
| | 13 | 1225 | | if (seqConditions.Count > 0) |
| | | 1226 | | { |
| | 5 | 1227 | | var runMethodName = $"Run{methodSuffix}SequentialWorkflowAsync"; |
| | 5 | 1228 | | sb.AppendLine($" /// <summary>"); |
| | 5 | 1229 | | sb.AppendLine($" /// Creates and runs the \"{escapedPipelineName}\" sequential workflow, applying dec |
| | 5 | 1230 | | sb.AppendLine($" /// </summary>"); |
| | 5 | 1231 | | sb.AppendLine($" /// <remarks>"); |
| | 5 | 1232 | | sb.AppendLine($" /// Termination conditions are evaluated after each completed agent turn (Layer 2)." |
| | 5 | 1233 | | sb.AppendLine($" /// The workflow stops early when any condition is satisfied."); |
| | 5 | 1234 | | sb.AppendLine($" /// </remarks>"); |
| | 5 | 1235 | | sb.AppendLine($" /// <param name=\"workflowFactory\">The workflow factory.</param>"); |
| | 5 | 1236 | | sb.AppendLine($" /// <param name=\"message\">The input message to start the workflow.</param>"); |
| | 5 | 1237 | | sb.AppendLine($" /// <param name=\"cancellationToken\">Optional cancellation token.</param>"); |
| | 5 | 1238 | | sb.AppendLine($" /// <returns>A dictionary mapping executor IDs to their response text.</returns>"); |
| | 5 | 1239 | | sb.AppendLine($" public static async global::System.Threading.Tasks.Task<global::System.Collections.G |
| | 5 | 1240 | | sb.AppendLine($" this IWorkflowFactory workflowFactory,"); |
| | 5 | 1241 | | sb.AppendLine($" string message,"); |
| | 5 | 1242 | | sb.AppendLine($" global::System.Threading.CancellationToken cancellationToken = default)"); |
| | 5 | 1243 | | sb.AppendLine($" {{"); |
| | 5 | 1244 | | sb.AppendLine($" var workflow = workflowFactory.{methodName}();"); |
| | 5 | 1245 | | sb.AppendLine($" global::System.Collections.Generic.IReadOnlyList<global::NexusLabs.Needlr.AgentF |
| | 5 | 1246 | | sb.AppendLine($" new global::System.Collections.Generic.List<global::NexusLabs.Needlr.AgentFr |
| | 5 | 1247 | | sb.AppendLine($" {{"); |
| | 22 | 1248 | | foreach (var cond in seqConditions) |
| | | 1249 | | { |
| | 6 | 1250 | | var args = cond.CtorArgLiterals.IsEmpty ? "" : string.Join(", ", cond.CtorArgLiterals); |
| | 6 | 1251 | | sb.AppendLine($" new {cond.ConditionTypeFQN}({args}),"); |
| | | 1252 | | } |
| | 5 | 1253 | | sb.AppendLine($" }};"); |
| | 5 | 1254 | | sb.AppendLine($" return await global::NexusLabs.Needlr.AgentFramework.Workflows.StreamingRunWorkf |
| | 5 | 1255 | | sb.AppendLine($" }}"); |
| | 5 | 1256 | | sb.AppendLine(); |
| | | 1257 | | } |
| | | 1258 | | } |
| | | 1259 | | |
| | 63 | 1260 | | sb.AppendLine("}"); |
| | 63 | 1261 | | return sb.ToString(); |
| | | 1262 | | } |
| | | 1263 | | |
| | | 1264 | | private static string GenerateAgentFactoryExtensionsSource( |
| | | 1265 | | List<NeedlrAiAgentTypeInfo> agents, |
| | | 1266 | | string safeAssemblyName) |
| | | 1267 | | { |
| | 63 | 1268 | | var sb = new StringBuilder(); |
| | 63 | 1269 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 1270 | | sb.AppendLine("// Needlr AgentFramework IAgentFactory Extension Methods"); |
| | 63 | 1271 | | sb.AppendLine("#nullable enable"); |
| | 63 | 1272 | | sb.AppendLine(); |
| | 63 | 1273 | | sb.AppendLine("using Microsoft.Agents.AI;"); |
| | 63 | 1274 | | sb.AppendLine("using NexusLabs.Needlr.AgentFramework;"); |
| | 63 | 1275 | | sb.AppendLine(); |
| | 63 | 1276 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 1277 | | sb.AppendLine(); |
| | 63 | 1278 | | sb.AppendLine("/// <summary>"); |
| | 63 | 1279 | | sb.AppendLine("/// Generated strongly-typed extension methods on <see cref=\"IAgentFactory\"/>."); |
| | 63 | 1280 | | sb.AppendLine("/// Each method creates an agent from its <c>[NeedlrAiAgent]</c> declaration,"); |
| | 63 | 1281 | | sb.AppendLine("/// eliminating magic strings and direct type references at the composition root."); |
| | 63 | 1282 | | sb.AppendLine("/// </summary>"); |
| | 63 | 1283 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1284 | | sb.AppendLine("public static class GeneratedAgentFactoryExtensions"); |
| | 63 | 1285 | | sb.AppendLine("{"); |
| | | 1286 | | |
| | 300 | 1287 | | foreach (var agent in agents.OrderBy(a => a.ClassName)) |
| | | 1288 | | { |
| | 58 | 1289 | | sb.AppendLine($" /// <summary>"); |
| | 58 | 1290 | | sb.AppendLine($" /// Creates an <see cref=\"AIAgent\"/> configured for <see cref=\"{agent.TypeName.Replac |
| | 58 | 1291 | | sb.AppendLine($" /// </summary>"); |
| | 58 | 1292 | | sb.AppendLine($" public static AIAgent Create{agent.ClassName}(this IAgentFactory factory)"); |
| | 58 | 1293 | | sb.AppendLine($" => factory.CreateAgent<{agent.TypeName}>();"); |
| | 58 | 1294 | | sb.AppendLine(); |
| | | 1295 | | } |
| | | 1296 | | |
| | 63 | 1297 | | sb.AppendLine("}"); |
| | 63 | 1298 | | return sb.ToString(); |
| | | 1299 | | } |
| | | 1300 | | |
| | | 1301 | | private static string GenerateAgentTopologyConstantsSource( |
| | | 1302 | | List<NeedlrAiAgentTypeInfo> agents, |
| | | 1303 | | List<AgentFunctionGroupEntry> groupEntries, |
| | | 1304 | | Dictionary<string, List<string>> sequenceByPipelineName, |
| | | 1305 | | string safeAssemblyName) |
| | | 1306 | | { |
| | 63 | 1307 | | var sb = new StringBuilder(); |
| | 63 | 1308 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 1309 | | sb.AppendLine("// Needlr AgentFramework Topology Constants"); |
| | 63 | 1310 | | sb.AppendLine("#nullable enable"); |
| | 63 | 1311 | | sb.AppendLine(); |
| | 63 | 1312 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 1313 | | sb.AppendLine(); |
| | | 1314 | | |
| | 63 | 1315 | | sb.AppendLine("/// <summary>String constants for agent type names discovered at compile time.</summary>"); |
| | 63 | 1316 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1317 | | sb.AppendLine("public static class AgentNames"); |
| | 63 | 1318 | | sb.AppendLine("{"); |
| | 300 | 1319 | | foreach (var agent in agents.OrderBy(a => a.ClassName)) |
| | | 1320 | | { |
| | 58 | 1321 | | sb.AppendLine($" /// <summary>The name of <see cref=\"{agent.TypeName.Replace("global::", "")}\"/>.</summ |
| | 58 | 1322 | | sb.AppendLine($" public const string {agent.ClassName} = \"{agent.ClassName}\";"); |
| | | 1323 | | } |
| | 63 | 1324 | | sb.AppendLine("}"); |
| | 63 | 1325 | | sb.AppendLine(); |
| | | 1326 | | |
| | 77 | 1327 | | var groupNames = groupEntries.Select(e => e.GroupName).Distinct().OrderBy(g => g).ToList(); |
| | 63 | 1328 | | sb.AppendLine("/// <summary>String constants for function group names discovered at compile time.</summary>"); |
| | 63 | 1329 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1330 | | sb.AppendLine("public static class GroupNames"); |
| | 63 | 1331 | | sb.AppendLine("{"); |
| | 144 | 1332 | | foreach (var gn in groupNames) |
| | | 1333 | | { |
| | 9 | 1334 | | var propName = GroupNameToPascalCase(gn); |
| | 9 | 1335 | | var escaped = gn.Replace("\"", "\\\""); |
| | 9 | 1336 | | sb.AppendLine($" /// <summary>The group name <c>\"{escaped}\"</c>.</summary>"); |
| | 9 | 1337 | | sb.AppendLine($" public const string {propName} = \"{escaped}\";"); |
| | | 1338 | | } |
| | 63 | 1339 | | sb.AppendLine("}"); |
| | 63 | 1340 | | sb.AppendLine(); |
| | | 1341 | | |
| | 63 | 1342 | | sb.AppendLine("/// <summary>String constants for sequential pipeline names discovered at compile time.</summary> |
| | 63 | 1343 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1344 | | sb.AppendLine("public static class PipelineNames"); |
| | 63 | 1345 | | sb.AppendLine("{"); |
| | 165 | 1346 | | foreach (var pn in sequenceByPipelineName.Keys.OrderBy(k => k)) |
| | | 1347 | | { |
| | 13 | 1348 | | var propName = GroupNameToPascalCase(pn); |
| | 13 | 1349 | | var escaped = pn.Replace("\"", "\\\""); |
| | 13 | 1350 | | sb.AppendLine($" /// <summary>The pipeline name <c>\"{escaped}\"</c>.</summary>"); |
| | 13 | 1351 | | sb.AppendLine($" public const string {propName} = \"{escaped}\";"); |
| | | 1352 | | } |
| | 63 | 1353 | | sb.AppendLine("}"); |
| | | 1354 | | |
| | 63 | 1355 | | return sb.ToString(); |
| | | 1356 | | } |
| | | 1357 | | |
| | | 1358 | | private static string GenerateSyringeExtensionsSource( |
| | | 1359 | | List<AgentFunctionGroupEntry> groupEntries, |
| | | 1360 | | string safeAssemblyName) |
| | | 1361 | | { |
| | 63 | 1362 | | var sb = new StringBuilder(); |
| | 63 | 1363 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 1364 | | sb.AppendLine("// Needlr AgentFramework AgentFrameworkSyringe Extension Methods"); |
| | 63 | 1365 | | sb.AppendLine("#nullable enable"); |
| | 63 | 1366 | | sb.AppendLine(); |
| | 63 | 1367 | | sb.AppendLine("using System.Diagnostics.CodeAnalysis;"); |
| | 63 | 1368 | | sb.AppendLine("using NexusLabs.Needlr.AgentFramework;"); |
| | 63 | 1369 | | sb.AppendLine(); |
| | 63 | 1370 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 1371 | | sb.AppendLine(); |
| | 63 | 1372 | | sb.AppendLine("/// <summary>"); |
| | 63 | 1373 | | sb.AppendLine("/// Generated strongly-typed extension methods on <see cref=\"AgentFrameworkSyringe\"/>."); |
| | 63 | 1374 | | sb.AppendLine("/// Each method registers a named function group without requiring direct type references."); |
| | 63 | 1375 | | sb.AppendLine("/// </summary>"); |
| | 63 | 1376 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1377 | | sb.AppendLine("public static class GeneratedAgentFrameworkSyringeExtensions"); |
| | 63 | 1378 | | sb.AppendLine("{"); |
| | | 1379 | | |
| | 63 | 1380 | | var byGroupName = groupEntries |
| | 10 | 1381 | | .GroupBy(e => e.GroupName) |
| | 72 | 1382 | | .OrderBy(g => g.Key); |
| | | 1383 | | |
| | 144 | 1384 | | foreach (var grp in byGroupName) |
| | | 1385 | | { |
| | 9 | 1386 | | var methodName = $"With{GroupNameToPascalCase(grp.Key)}"; |
| | 9 | 1387 | | var escapedGroupName = grp.Key.Replace("\"", "\\\""); |
| | 19 | 1388 | | var types = grp.Select(e => e.TypeName).Distinct().ToList(); |
| | | 1389 | | |
| | 9 | 1390 | | sb.AppendLine($" /// <summary>Registers the '{escapedGroupName}' function group with the syringe.</summar |
| | 9 | 1391 | | sb.AppendLine($" [RequiresUnreferencedCode(\"AgentFramework function setup uses reflection to discover [A |
| | 9 | 1392 | | sb.AppendLine($" [RequiresDynamicCode(\"AgentFramework function setup uses reflection APIs that require d |
| | 9 | 1393 | | sb.AppendLine($" public static AgentFrameworkSyringe {methodName}(this AgentFrameworkSyringe syringe)"); |
| | 19 | 1394 | | sb.AppendLine($" => syringe.AddAgentFunctions(new global::System.Type[] {{ {string.Join(", ", types.S |
| | 9 | 1395 | | sb.AppendLine(); |
| | | 1396 | | } |
| | | 1397 | | |
| | 63 | 1398 | | sb.AppendLine("}"); |
| | 63 | 1399 | | return sb.ToString(); |
| | | 1400 | | } |
| | | 1401 | | |
| | | 1402 | | private static string GenerateAIFunctionProviderSource( |
| | | 1403 | | List<AgentFunctionTypeInfo> types, |
| | | 1404 | | string safeAssemblyName) |
| | | 1405 | | { |
| | 63 | 1406 | | var sb = new StringBuilder(); |
| | 63 | 1407 | | sb.AppendLine("// <auto-generated/>"); |
| | 63 | 1408 | | sb.AppendLine("#nullable enable"); |
| | 63 | 1409 | | sb.AppendLine(); |
| | 63 | 1410 | | sb.AppendLine("using global::Microsoft.Extensions.DependencyInjection;"); |
| | 63 | 1411 | | sb.AppendLine(); |
| | 63 | 1412 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 63 | 1413 | | sb.AppendLine(); |
| | 63 | 1414 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 63 | 1415 | | sb.AppendLine("internal sealed class GeneratedAIFunctionProvider : global::NexusLabs.Needlr.AgentFramework.IAIFu |
| | 63 | 1416 | | sb.AppendLine("{"); |
| | 63 | 1417 | | sb.AppendLine(" public bool TryGetFunctions("); |
| | 63 | 1418 | | sb.AppendLine(" global::System.Type functionType,"); |
| | 63 | 1419 | | sb.AppendLine(" global::System.IServiceProvider serviceProvider,"); |
| | 63 | 1420 | | sb.AppendLine(" [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]"); |
| | 63 | 1421 | | sb.AppendLine(" out global::System.Collections.Generic.IReadOnlyList<global::Microsoft.Extensions.AI.AIFu |
| | 63 | 1422 | | sb.AppendLine(" {"); |
| | | 1423 | | |
| | 63 | 1424 | | if (types.Count == 0) |
| | | 1425 | | { |
| | 54 | 1426 | | sb.AppendLine(" functions = null;"); |
| | 54 | 1427 | | sb.AppendLine(" return false;"); |
| | | 1428 | | } |
| | | 1429 | | else |
| | | 1430 | | { |
| | 9 | 1431 | | var first = true; |
| | 36 | 1432 | | foreach (var type in types) |
| | | 1433 | | { |
| | 9 | 1434 | | var keyword = first ? "if" : "else if"; |
| | 9 | 1435 | | first = false; |
| | 9 | 1436 | | sb.AppendLine($" {keyword} (functionType == typeof({type.TypeName}))"); |
| | 9 | 1437 | | sb.AppendLine(" {"); |
| | 9 | 1438 | | if (!type.IsStatic) |
| | 8 | 1439 | | sb.AppendLine($" var typed = serviceProvider.GetRequiredService<{type.TypeName}>();"); |
| | 9 | 1440 | | sb.AppendLine(" functions = new global::System.Collections.Generic.List<global::Microsoft.Ext |
| | 9 | 1441 | | sb.AppendLine(" {"); |
| | 36 | 1442 | | foreach (var m in type.Methods) |
| | | 1443 | | { |
| | 9 | 1444 | | var nestedName = $"{GetShortName(type.TypeName)}_{m.MethodName}"; |
| | 9 | 1445 | | if (type.IsStatic) |
| | 1 | 1446 | | sb.AppendLine($" new {nestedName}(),"); |
| | | 1447 | | else |
| | 8 | 1448 | | sb.AppendLine($" new {nestedName}(typed),"); |
| | | 1449 | | } |
| | 9 | 1450 | | sb.AppendLine(" }.AsReadOnly();"); |
| | 9 | 1451 | | sb.AppendLine(" return true;"); |
| | 9 | 1452 | | sb.AppendLine(" }"); |
| | | 1453 | | } |
| | 9 | 1454 | | sb.AppendLine(" functions = null;"); |
| | 9 | 1455 | | sb.AppendLine(" return false;"); |
| | | 1456 | | } |
| | | 1457 | | |
| | 63 | 1458 | | sb.AppendLine(" }"); |
| | 63 | 1459 | | sb.AppendLine(); |
| | | 1460 | | |
| | 144 | 1461 | | foreach (var type in types) |
| | | 1462 | | { |
| | 9 | 1463 | | var shortTypeName = GetShortName(type.TypeName); |
| | 36 | 1464 | | foreach (var m in type.Methods) |
| | | 1465 | | { |
| | 9 | 1466 | | var nestedName = $"{shortTypeName}_{m.MethodName}"; |
| | 9 | 1467 | | AppendAIFunctionNestedClass(sb, type, m, nestedName); |
| | | 1468 | | } |
| | | 1469 | | } |
| | | 1470 | | |
| | 63 | 1471 | | sb.AppendLine("}"); |
| | 63 | 1472 | | return sb.ToString(); |
| | | 1473 | | } |
| | | 1474 | | |
| | | 1475 | | private static void AppendAIFunctionNestedClass( |
| | | 1476 | | StringBuilder sb, |
| | | 1477 | | AgentFunctionTypeInfo type, |
| | | 1478 | | AgentFunctionMethodInfo method, |
| | | 1479 | | string nestedClassName) |
| | | 1480 | | { |
| | 9 | 1481 | | sb.AppendLine($" private sealed class {nestedClassName} : global::Microsoft.Extensions.AI.AIFunction"); |
| | 9 | 1482 | | sb.AppendLine(" {"); |
| | | 1483 | | |
| | 9 | 1484 | | if (!type.IsStatic) |
| | 8 | 1485 | | sb.AppendLine($" private readonly {type.TypeName} _instance;"); |
| | | 1486 | | |
| | 9 | 1487 | | var schemaJson = BuildJsonSchema(method.Parameters); |
| | 9 | 1488 | | sb.AppendLine(" private static readonly global::System.Text.Json.JsonElement _schema ="); |
| | 9 | 1489 | | sb.AppendLine($" global::System.Text.Json.JsonDocument.Parse(\"\"\"{schemaJson}\"\"\").RootElement.Cl |
| | 9 | 1490 | | sb.AppendLine(); |
| | | 1491 | | |
| | 9 | 1492 | | if (!type.IsStatic) |
| | 8 | 1493 | | sb.AppendLine($" public {nestedClassName}({type.TypeName} instance) {{ _instance = instance; }}"); |
| | | 1494 | | else |
| | 1 | 1495 | | sb.AppendLine($" public {nestedClassName}() {{ }}"); |
| | | 1496 | | |
| | 9 | 1497 | | sb.AppendLine(); |
| | | 1498 | | |
| | 9 | 1499 | | var escapedName = method.MethodName.Replace("\"", "\\\""); |
| | 9 | 1500 | | var escapedDesc = method.Description.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | 9 | 1501 | | sb.AppendLine($" public override string Name => \"{escapedName}\";"); |
| | 9 | 1502 | | sb.AppendLine($" public override string Description => \"{escapedDesc}\";"); |
| | 9 | 1503 | | sb.AppendLine(" public override global::System.Text.Json.JsonElement JsonSchema => _schema;"); |
| | 9 | 1504 | | sb.AppendLine(); |
| | | 1505 | | |
| | 9 | 1506 | | if (method.IsAsync) |
| | 1 | 1507 | | sb.AppendLine(" protected override async global::System.Threading.Tasks.ValueTask<object?> InvokeCore |
| | | 1508 | | else |
| | 8 | 1509 | | sb.AppendLine(" protected override global::System.Threading.Tasks.ValueTask<object?> InvokeCoreAsync( |
| | | 1510 | | |
| | 9 | 1511 | | sb.AppendLine(" global::Microsoft.Extensions.AI.AIFunctionArguments arguments,"); |
| | 9 | 1512 | | sb.AppendLine(" global::System.Threading.CancellationToken ct)"); |
| | 9 | 1513 | | sb.AppendLine(" {"); |
| | | 1514 | | |
| | 36 | 1515 | | foreach (var param in method.Parameters) |
| | | 1516 | | { |
| | 9 | 1517 | | if (param.IsCancellationToken) |
| | | 1518 | | continue; |
| | 8 | 1519 | | AppendParameterExtraction(sb, param); |
| | | 1520 | | } |
| | | 1521 | | |
| | 18 | 1522 | | var paramList = string.Join(", ", method.Parameters.Select(p => p.IsCancellationToken ? "ct" : p.Name)); |
| | 9 | 1523 | | var instanceExpr = type.IsStatic ? type.TypeName : "_instance"; |
| | | 1524 | | |
| | 9 | 1525 | | if (method.IsAsync) |
| | | 1526 | | { |
| | 1 | 1527 | | if (method.IsVoidLike) |
| | | 1528 | | { |
| | 0 | 1529 | | sb.AppendLine($" await {instanceExpr}.{method.MethodName}({paramList}).ConfigureAwait(false); |
| | 0 | 1530 | | sb.AppendLine(" return null;"); |
| | | 1531 | | } |
| | | 1532 | | else |
| | | 1533 | | { |
| | 1 | 1534 | | sb.AppendLine($" var result = await {instanceExpr}.{method.MethodName}({paramList}).Configure |
| | 1 | 1535 | | sb.AppendLine(" return result;"); |
| | | 1536 | | } |
| | | 1537 | | } |
| | | 1538 | | else |
| | | 1539 | | { |
| | 8 | 1540 | | if (method.IsVoidLike) |
| | | 1541 | | { |
| | 2 | 1542 | | sb.AppendLine($" {instanceExpr}.{method.MethodName}({paramList});"); |
| | 2 | 1543 | | sb.AppendLine(" return global::System.Threading.Tasks.ValueTask.FromResult<object?>(null);"); |
| | | 1544 | | } |
| | | 1545 | | else |
| | | 1546 | | { |
| | 6 | 1547 | | sb.AppendLine($" var result = {instanceExpr}.{method.MethodName}({paramList});"); |
| | 6 | 1548 | | sb.AppendLine(" return global::System.Threading.Tasks.ValueTask.FromResult<object?>(result);" |
| | | 1549 | | } |
| | | 1550 | | } |
| | | 1551 | | |
| | 9 | 1552 | | sb.AppendLine(" }"); |
| | 9 | 1553 | | sb.AppendLine(" }"); |
| | 9 | 1554 | | sb.AppendLine(); |
| | 9 | 1555 | | } |
| | | 1556 | | |
| | | 1557 | | private static string BuildJsonSchema(ImmutableArray<AgentFunctionParameterInfo> parameters) |
| | | 1558 | | { |
| | 18 | 1559 | | var nonCtParams = parameters.Where(p => !p.IsCancellationToken).ToList(); |
| | 9 | 1560 | | if (nonCtParams.Count == 0) |
| | 3 | 1561 | | return "{\"type\":\"object\",\"properties\":{}}"; |
| | | 1562 | | |
| | 6 | 1563 | | var props = new StringBuilder(); |
| | 6 | 1564 | | var required = new List<string>(); |
| | | 1565 | | |
| | 6 | 1566 | | props.Append("{"); |
| | 6 | 1567 | | var firstProp = true; |
| | 28 | 1568 | | foreach (var param in nonCtParams) |
| | | 1569 | | { |
| | 10 | 1570 | | if (!firstProp) props.Append(","); |
| | 8 | 1571 | | firstProp = false; |
| | 8 | 1572 | | var escapedParamName = param.Name.Replace("\"", "\\\""); |
| | 8 | 1573 | | props.Append($"\"{escapedParamName}\":"); |
| | 8 | 1574 | | props.Append(BuildJsonSchemaTypeEntry(param)); |
| | 8 | 1575 | | if (param.IsRequired) |
| | 7 | 1576 | | required.Add(param.Name); |
| | | 1577 | | } |
| | 6 | 1578 | | props.Append("}"); |
| | | 1579 | | |
| | 6 | 1580 | | var sb = new StringBuilder(); |
| | 6 | 1581 | | sb.Append("{\"type\":\"object\",\"properties\":"); |
| | 6 | 1582 | | sb.Append(props); |
| | 6 | 1583 | | if (required.Count > 0) |
| | | 1584 | | { |
| | 5 | 1585 | | sb.Append(",\"required\":["); |
| | 12 | 1586 | | sb.Append(string.Join(",", required.Select(r => "\"" + r.Replace("\"", "\\\"") + "\""))); |
| | 5 | 1587 | | sb.Append("]"); |
| | | 1588 | | } |
| | 6 | 1589 | | sb.Append("}"); |
| | 6 | 1590 | | return sb.ToString(); |
| | | 1591 | | } |
| | | 1592 | | |
| | | 1593 | | private static string BuildJsonSchemaTypeEntry(AgentFunctionParameterInfo param) |
| | | 1594 | | { |
| | 8 | 1595 | | if (string.IsNullOrEmpty(param.JsonSchemaType)) |
| | 0 | 1596 | | return "{}"; |
| | | 1597 | | |
| | 8 | 1598 | | if (param.JsonSchemaType == "array") |
| | | 1599 | | { |
| | 0 | 1600 | | var desc = string.IsNullOrEmpty(param.Description) |
| | 0 | 1601 | | ? "" |
| | 0 | 1602 | | : $",\"description\":\"{param.Description!.Replace("\\", "\\\\").Replace("\"", "\\\"")}\""; |
| | 0 | 1603 | | if (!string.IsNullOrEmpty(param.ItemJsonSchemaType)) |
| | 0 | 1604 | | return $"{{\"type\":\"array\",\"items\":{{\"type\":\"{param.ItemJsonSchemaType}\"}}{desc}}}"; |
| | 0 | 1605 | | return $"{{\"type\":\"array\"{desc}}}"; |
| | | 1606 | | } |
| | | 1607 | | |
| | 8 | 1608 | | if (!string.IsNullOrEmpty(param.Description)) |
| | | 1609 | | { |
| | 1 | 1610 | | var escapedDesc = param.Description!.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | 1 | 1611 | | return $"{{\"type\":\"{param.JsonSchemaType}\",\"description\":\"{escapedDesc}\"}}"; |
| | | 1612 | | } |
| | | 1613 | | |
| | 7 | 1614 | | return $"{{\"type\":\"{param.JsonSchemaType}\"}}"; |
| | | 1615 | | } |
| | | 1616 | | |
| | | 1617 | | private static void AppendParameterExtraction(StringBuilder sb, AgentFunctionParameterInfo param) |
| | | 1618 | | { |
| | 8 | 1619 | | var rawVar = $"_raw_{param.Name}"; |
| | 8 | 1620 | | var jVar = $"_j_{param.Name}"; |
| | 8 | 1621 | | sb.AppendLine($" arguments.TryGetValue(\"{param.Name}\", out var {rawVar});"); |
| | | 1622 | | |
| | 8 | 1623 | | switch (param.JsonSchemaType) |
| | | 1624 | | { |
| | | 1625 | | case "string": |
| | 3 | 1626 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? |
| | 3 | 1627 | | break; |
| | | 1628 | | case "boolean": |
| | | 1629 | | { |
| | 0 | 1630 | | var bVar = $"_b_{param.Name}"; |
| | 0 | 1631 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? |
| | 0 | 1632 | | break; |
| | | 1633 | | } |
| | | 1634 | | case "integer": |
| | 5 | 1635 | | AppendIntegerExtraction(sb, param, rawVar, jVar); |
| | 5 | 1636 | | break; |
| | | 1637 | | case "number": |
| | 0 | 1638 | | AppendNumberExtraction(sb, param, rawVar, jVar); |
| | 0 | 1639 | | break; |
| | | 1640 | | default: |
| | | 1641 | | { |
| | 0 | 1642 | | var cVar = $"_c_{param.Name}"; |
| | 0 | 1643 | | var nullSuppress = param.IsNullable ? "" : "!"; |
| | 0 | 1644 | | sb.AppendLine($" var {param.Name} = {rawVar} is {param.TypeFullName} {cVar} ? {cVar} : defaul |
| | | 1645 | | break; |
| | | 1646 | | } |
| | | 1647 | | } |
| | 0 | 1648 | | } |
| | | 1649 | | |
| | | 1650 | | private static void AppendIntegerExtraction(StringBuilder sb, AgentFunctionParameterInfo param, string rawVar, strin |
| | | 1651 | | { |
| | 5 | 1652 | | var typeFqn = param.TypeFullName; |
| | | 1653 | | string getMethod, castType, convertMethod; |
| | 5 | 1654 | | var iVar = $"_i_{param.Name}"; |
| | | 1655 | | |
| | 5 | 1656 | | if (typeFqn.Contains("System.Int64")) |
| | 0 | 1657 | | { getMethod = "GetInt64()"; castType = "long"; convertMethod = "ToInt64"; } |
| | 5 | 1658 | | else if (typeFqn.Contains("System.Int16")) |
| | 0 | 1659 | | { getMethod = "GetInt16()"; castType = "short"; convertMethod = "ToInt16"; } |
| | 5 | 1660 | | else if (typeFqn.Contains("System.SByte")) |
| | 0 | 1661 | | { getMethod = "GetSByte()"; castType = "sbyte"; convertMethod = "ToSByte"; } |
| | 5 | 1662 | | else if (typeFqn.Contains("System.Byte")) |
| | 0 | 1663 | | { getMethod = "GetByte()"; castType = "byte"; convertMethod = "ToByte"; } |
| | 5 | 1664 | | else if (typeFqn.Contains("System.UInt64")) |
| | 0 | 1665 | | { getMethod = "GetUInt64()"; castType = "ulong"; convertMethod = "ToUInt64"; } |
| | 5 | 1666 | | else if (typeFqn.Contains("System.UInt32")) |
| | 0 | 1667 | | { getMethod = "GetUInt32()"; castType = "uint"; convertMethod = "ToUInt32"; } |
| | 5 | 1668 | | else if (typeFqn.Contains("System.UInt16")) |
| | 0 | 1669 | | { getMethod = "GetUInt16()"; castType = "ushort"; convertMethod = "ToUInt16"; } |
| | | 1670 | | else |
| | 15 | 1671 | | { getMethod = "GetInt32()"; castType = "int"; convertMethod = "ToInt32"; } |
| | | 1672 | | |
| | 5 | 1673 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? {jVar}. |
| | 5 | 1674 | | } |
| | | 1675 | | |
| | | 1676 | | private static void AppendNumberExtraction(StringBuilder sb, AgentFunctionParameterInfo param, string rawVar, string |
| | | 1677 | | { |
| | 0 | 1678 | | var typeFqn = param.TypeFullName; |
| | | 1679 | | string getMethod, castType, convertMethod; |
| | 0 | 1680 | | var nVar = $"_n_{param.Name}"; |
| | | 1681 | | |
| | 0 | 1682 | | if (typeFqn.Contains("System.Single")) |
| | 0 | 1683 | | { getMethod = "GetSingle()"; castType = "float"; convertMethod = "ToSingle"; } |
| | 0 | 1684 | | else if (typeFqn.Contains("System.Decimal")) |
| | 0 | 1685 | | { getMethod = "GetDecimal()"; castType = "decimal"; convertMethod = "ToDecimal"; } |
| | | 1686 | | else |
| | 0 | 1687 | | { getMethod = "GetDouble()"; castType = "double"; convertMethod = "ToDouble"; } |
| | | 1688 | | |
| | 0 | 1689 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? {jVar}. |
| | 0 | 1690 | | } |
| | | 1691 | | |
| | | 1692 | | private static string StripAgentSuffix(string className) |
| | | 1693 | | { |
| | | 1694 | | const string suffix = "Agent"; |
| | 6 | 1695 | | return className.EndsWith(suffix, StringComparison.Ordinal) && className.Length > suffix.Length |
| | 6 | 1696 | | ? className.Substring(0, className.Length - suffix.Length) |
| | 6 | 1697 | | : className; |
| | | 1698 | | } |
| | | 1699 | | |
| | | 1700 | | private static string GroupNameToPascalCase(string groupName) |
| | | 1701 | | { |
| | 48 | 1702 | | var sb = new StringBuilder(); |
| | 48 | 1703 | | var capitalizeNext = true; |
| | 1236 | 1704 | | foreach (var c in groupName) |
| | | 1705 | | { |
| | 570 | 1706 | | if (c == '-' || c == '_' || c == ' ') |
| | | 1707 | | { |
| | 44 | 1708 | | capitalizeNext = true; |
| | | 1709 | | } |
| | 526 | 1710 | | else if (char.IsLetterOrDigit(c)) |
| | | 1711 | | { |
| | 526 | 1712 | | sb.Append(capitalizeNext ? char.ToUpperInvariant(c) : c); |
| | 526 | 1713 | | capitalizeNext = false; |
| | | 1714 | | } |
| | | 1715 | | } |
| | 48 | 1716 | | return sb.ToString(); |
| | | 1717 | | } |
| | | 1718 | | |
| | | 1719 | | private static string GenerateBootstrapSource(string safeAssemblyName) |
| | | 1720 | | { |
| | 63 | 1721 | | return $@"// <auto-generated/> |
| | 63 | 1722 | | // Needlr AgentFramework Module Initializer |
| | 63 | 1723 | | #nullable enable |
| | 63 | 1724 | | |
| | 63 | 1725 | | using System.Runtime.CompilerServices; |
| | 63 | 1726 | | |
| | 63 | 1727 | | namespace {safeAssemblyName}.Generated; |
| | 63 | 1728 | | |
| | 63 | 1729 | | /// <summary> |
| | 63 | 1730 | | /// Auto-registers source-generated Agent Framework types with the Needlr bootstrap. |
| | 63 | 1731 | | /// Fires automatically when the assembly loads — no explicit <c>Add*FromGenerated()</c> calls needed. |
| | 63 | 1732 | | /// </summary> |
| | 63 | 1733 | | [global::System.CodeDom.Compiler.GeneratedCodeAttribute(""NexusLabs.Needlr.AgentFramework.Generators"", ""1.0.0"")] |
| | 63 | 1734 | | internal static class NeedlrAgentFrameworkModuleInitializer |
| | 63 | 1735 | | {{ |
| | 63 | 1736 | | [ModuleInitializer] |
| | 63 | 1737 | | internal static void Initialize() |
| | 63 | 1738 | | {{ |
| | 63 | 1739 | | global::NexusLabs.Needlr.AgentFramework.AgentFrameworkGeneratedBootstrap.Register( |
| | 63 | 1740 | | () => AgentFrameworkFunctionRegistry.AllFunctionTypes, |
| | 63 | 1741 | | () => AgentFrameworkFunctionGroupRegistry.AllGroups, |
| | 63 | 1742 | | () => AgentRegistry.AllAgentTypes, |
| | 63 | 1743 | | () => AgentHandoffTopologyRegistry.AllHandoffs, |
| | 63 | 1744 | | () => AgentGroupChatRegistry.AllGroups, |
| | 63 | 1745 | | () => AgentSequentialTopologyRegistry.AllPipelines); |
| | 63 | 1746 | | global::NexusLabs.Needlr.AgentFramework.AgentFrameworkGeneratedBootstrap.RegisterAIFunctionProvider( |
| | 63 | 1747 | | new global::{safeAssemblyName}.Generated.GeneratedAIFunctionProvider()); |
| | 63 | 1748 | | }} |
| | 63 | 1749 | | }} |
| | 63 | 1750 | | "; |
| | | 1751 | | } |
| | | 1752 | | |
| | | 1753 | | private static string GeneratePartialCompanionSource( |
| | | 1754 | | NeedlrAiAgentTypeInfo agentType, |
| | | 1755 | | Dictionary<string, List<string>> groupedByName) |
| | | 1756 | | { |
| | 6 | 1757 | | var sb = new StringBuilder(); |
| | 6 | 1758 | | sb.AppendLine("// <auto-generated/>"); |
| | 6 | 1759 | | sb.AppendLine("#nullable enable"); |
| | 6 | 1760 | | sb.AppendLine(); |
| | | 1761 | | |
| | 6 | 1762 | | if (agentType.NamespaceName is not null) |
| | | 1763 | | { |
| | 6 | 1764 | | sb.AppendLine($"namespace {agentType.NamespaceName};"); |
| | 6 | 1765 | | sb.AppendLine(); |
| | | 1766 | | } |
| | | 1767 | | |
| | 6 | 1768 | | sb.AppendLine($"partial class {agentType.ClassName}"); |
| | 6 | 1769 | | sb.AppendLine("{"); |
| | | 1770 | | |
| | 6 | 1771 | | sb.AppendLine(" /// <summary>The declared name of this agent, equal to the class name.</summary>"); |
| | | 1772 | | |
| | 6 | 1773 | | var toolsDocLines = BuildToolsDocComment(agentType, groupedByName); |
| | 54 | 1774 | | foreach (var line in toolsDocLines) |
| | 21 | 1775 | | sb.AppendLine(line); |
| | | 1776 | | |
| | 6 | 1777 | | sb.AppendLine($" public static string AgentName => nameof({agentType.ClassName});"); |
| | 6 | 1778 | | sb.AppendLine("}"); |
| | | 1779 | | |
| | 6 | 1780 | | return sb.ToString(); |
| | | 1781 | | } |
| | | 1782 | | |
| | | 1783 | | private static List<string> BuildToolsDocComment( |
| | | 1784 | | NeedlrAiAgentTypeInfo agentType, |
| | | 1785 | | Dictionary<string, List<string>> groupedByName) |
| | | 1786 | | { |
| | | 1787 | | static string ShortName(string fqn) |
| | | 1788 | | { |
| | 2 | 1789 | | var clean = fqn.StartsWith("global::") ? fqn.Substring(8) : fqn; |
| | 2 | 1790 | | var dot = clean.LastIndexOf('.'); |
| | 2 | 1791 | | return dot >= 0 ? clean.Substring(dot + 1) : clean; |
| | | 1792 | | } |
| | | 1793 | | |
| | 6 | 1794 | | var lines = new List<string>(); |
| | | 1795 | | |
| | | 1796 | | // FunctionTypes = new Type[0] — explicitly no tools |
| | 6 | 1797 | | if (agentType.HasExplicitFunctionTypes && agentType.ExplicitFunctionTypeFQNs.IsEmpty |
| | 6 | 1798 | | && agentType.FunctionGroupNames.IsEmpty) |
| | | 1799 | | { |
| | 1 | 1800 | | lines.Add(" /// <remarks>This agent has no tools assigned (declared with an empty <c>FunctionTypes</c>).< |
| | 1 | 1801 | | return lines; |
| | | 1802 | | } |
| | | 1803 | | |
| | | 1804 | | // Neither set — uses all registered function types |
| | 5 | 1805 | | if (!agentType.HasExplicitFunctionTypes && agentType.FunctionGroupNames.IsEmpty) |
| | | 1806 | | { |
| | 2 | 1807 | | lines.Add(" /// <remarks>This agent uses all registered function types.</remarks>"); |
| | 2 | 1808 | | return lines; |
| | | 1809 | | } |
| | | 1810 | | |
| | | 1811 | | // Build the resolved list of function types |
| | 3 | 1812 | | var entries = new List<(string displayName, string? groupSource)>(); |
| | | 1813 | | |
| | 10 | 1814 | | foreach (var group in agentType.FunctionGroupNames) |
| | | 1815 | | { |
| | 2 | 1816 | | if (groupedByName.TryGetValue(group, out var types)) |
| | | 1817 | | { |
| | 4 | 1818 | | foreach (var typeFqn in types) |
| | 1 | 1819 | | entries.Add((ShortName(typeFqn), group)); |
| | | 1820 | | } |
| | | 1821 | | else |
| | | 1822 | | { |
| | 1 | 1823 | | entries.Add(($"(unresolved group \"{group}\")", group)); |
| | | 1824 | | } |
| | | 1825 | | } |
| | | 1826 | | |
| | 8 | 1827 | | foreach (var typeFqn in agentType.ExplicitFunctionTypeFQNs) |
| | | 1828 | | { |
| | 1 | 1829 | | var shortName = ShortName(typeFqn); |
| | 1 | 1830 | | if (!entries.Any(e => e.displayName == shortName)) |
| | 1 | 1831 | | entries.Add((shortName, null)); |
| | | 1832 | | } |
| | | 1833 | | |
| | 3 | 1834 | | lines.Add(" /// <remarks>"); |
| | 3 | 1835 | | lines.Add(" /// <para>Agent tools:</para>"); |
| | 3 | 1836 | | lines.Add(" /// <list type=\"bullet\">"); |
| | 12 | 1837 | | foreach (var (displayName, groupSource) in entries) |
| | | 1838 | | { |
| | 3 | 1839 | | var source = groupSource is not null ? $" (group <c>\"{groupSource}\"</c>)" : " (explicit type)"; |
| | 3 | 1840 | | lines.Add($" /// <item><term><see cref=\"{displayName}\"/>{source}</term></item>"); |
| | | 1841 | | } |
| | 3 | 1842 | | lines.Add(" /// </list>"); |
| | 3 | 1843 | | lines.Add(" /// </remarks>"); |
| | | 1844 | | |
| | 3 | 1845 | | return lines; |
| | | 1846 | | } |
| | | 1847 | | |
| | | 1848 | | private readonly struct AgentFunctionTypeInfo |
| | | 1849 | | { |
| | | 1850 | | public AgentFunctionTypeInfo(string typeName, string assemblyName, bool isStatic, ImmutableArray<AgentFunctionMe |
| | | 1851 | | { |
| | 36 | 1852 | | TypeName = typeName; AssemblyName = assemblyName; IsStatic = isStatic; Methods = methods; |
| | 9 | 1853 | | } |
| | | 1854 | | |
| | 61 | 1855 | | public string TypeName { get; } |
| | 0 | 1856 | | public string AssemblyName { get; } |
| | 45 | 1857 | | public bool IsStatic { get; } |
| | 18 | 1858 | | public ImmutableArray<AgentFunctionMethodInfo> Methods { get; } |
| | | 1859 | | } |
| | | 1860 | | |
| | | 1861 | | private readonly struct AgentFunctionGroupEntry |
| | | 1862 | | { |
| | | 1863 | | public AgentFunctionGroupEntry(string typeName, string groupName) |
| | | 1864 | | { |
| | 10 | 1865 | | TypeName = typeName; |
| | 10 | 1866 | | GroupName = groupName; |
| | 10 | 1867 | | } |
| | | 1868 | | |
| | 20 | 1869 | | public string TypeName { get; } |
| | 30 | 1870 | | public string GroupName { get; } |
| | | 1871 | | } |
| | | 1872 | | |
| | | 1873 | | private readonly struct NeedlrAiAgentTypeInfo |
| | | 1874 | | { |
| | | 1875 | | public NeedlrAiAgentTypeInfo( |
| | | 1876 | | string typeName, |
| | | 1877 | | string className, |
| | | 1878 | | string? namespaceName, |
| | | 1879 | | bool isPartial, |
| | | 1880 | | ImmutableArray<string> functionGroupNames, |
| | | 1881 | | ImmutableArray<string> explicitFunctionTypeFQNs, |
| | | 1882 | | bool hasExplicitFunctionTypes) |
| | | 1883 | | { |
| | 58 | 1884 | | TypeName = typeName; |
| | 58 | 1885 | | ClassName = className; |
| | 58 | 1886 | | NamespaceName = namespaceName; |
| | 58 | 1887 | | IsPartial = isPartial; |
| | 58 | 1888 | | FunctionGroupNames = functionGroupNames; |
| | 58 | 1889 | | ExplicitFunctionTypeFQNs = explicitFunctionTypeFQNs; |
| | 58 | 1890 | | HasExplicitFunctionTypes = hasExplicitFunctionTypes; |
| | 58 | 1891 | | } |
| | | 1892 | | |
| | 238 | 1893 | | public string TypeName { get; } |
| | 302 | 1894 | | public string ClassName { get; } |
| | 12 | 1895 | | public string? NamespaceName { get; } |
| | 58 | 1896 | | public bool IsPartial { get; } |
| | 8 | 1897 | | public ImmutableArray<string> FunctionGroupNames { get; } |
| | 5 | 1898 | | public ImmutableArray<string> ExplicitFunctionTypeFQNs { get; } |
| | 11 | 1899 | | public bool HasExplicitFunctionTypes { get; } |
| | | 1900 | | } |
| | | 1901 | | |
| | | 1902 | | private readonly struct HandoffEntry |
| | | 1903 | | { |
| | | 1904 | | public HandoffEntry(string initialAgentTypeName, string initialAgentClassName, string targetAgentTypeName, strin |
| | | 1905 | | { |
| | 5 | 1906 | | InitialAgentTypeName = initialAgentTypeName; |
| | 5 | 1907 | | InitialAgentClassName = initialAgentClassName; |
| | 5 | 1908 | | TargetAgentTypeName = targetAgentTypeName; |
| | 5 | 1909 | | HandoffReason = handoffReason; |
| | 5 | 1910 | | } |
| | | 1911 | | |
| | 5 | 1912 | | public string InitialAgentTypeName { get; } |
| | 5 | 1913 | | public string InitialAgentClassName { get; } |
| | 5 | 1914 | | public string TargetAgentTypeName { get; } |
| | 5 | 1915 | | public string? HandoffReason { get; } |
| | | 1916 | | } |
| | | 1917 | | |
| | | 1918 | | private readonly struct GroupChatEntry |
| | | 1919 | | { |
| | | 1920 | | public GroupChatEntry(string agentTypeName, string groupName) |
| | | 1921 | | { |
| | 8 | 1922 | | AgentTypeName = agentTypeName; |
| | 8 | 1923 | | GroupName = groupName; |
| | 8 | 1924 | | } |
| | | 1925 | | |
| | 8 | 1926 | | public string AgentTypeName { get; } |
| | 8 | 1927 | | public string GroupName { get; } |
| | | 1928 | | } |
| | | 1929 | | |
| | | 1930 | | private readonly struct SequenceEntry |
| | | 1931 | | { |
| | | 1932 | | public SequenceEntry(string agentTypeName, string pipelineName, int order) |
| | | 1933 | | { |
| | 24 | 1934 | | AgentTypeName = agentTypeName; |
| | 24 | 1935 | | PipelineName = pipelineName; |
| | 24 | 1936 | | Order = order; |
| | 24 | 1937 | | } |
| | | 1938 | | |
| | 24 | 1939 | | public string AgentTypeName { get; } |
| | 24 | 1940 | | public string PipelineName { get; } |
| | 24 | 1941 | | public int Order { get; } |
| | | 1942 | | } |
| | | 1943 | | |
| | | 1944 | | private readonly struct TerminationConditionEntry |
| | | 1945 | | { |
| | | 1946 | | public TerminationConditionEntry(string agentTypeName, string conditionTypeFQN, ImmutableArray<string> ctorArgLi |
| | | 1947 | | { |
| | 8 | 1948 | | AgentTypeName = agentTypeName; |
| | 8 | 1949 | | ConditionTypeFQN = conditionTypeFQN; |
| | 8 | 1950 | | CtorArgLiterals = ctorArgLiterals; |
| | 8 | 1951 | | } |
| | | 1952 | | |
| | 8 | 1953 | | public string AgentTypeName { get; } |
| | 8 | 1954 | | public string ConditionTypeFQN { get; } |
| | 16 | 1955 | | public ImmutableArray<string> CtorArgLiterals { get; } |
| | | 1956 | | } |
| | | 1957 | | |
| | | 1958 | | private readonly struct AgentFunctionParameterInfo |
| | | 1959 | | { |
| | | 1960 | | public AgentFunctionParameterInfo( |
| | | 1961 | | string name, string typeFullName, |
| | | 1962 | | string jsonSchemaType, string? itemJsonSchemaType, |
| | | 1963 | | bool isCancellationToken, bool isNullable, bool hasDefault, string? description) |
| | | 1964 | | { |
| | 18 | 1965 | | Name = name; TypeFullName = typeFullName; |
| | 18 | 1966 | | JsonSchemaType = jsonSchemaType; ItemJsonSchemaType = itemJsonSchemaType; |
| | 18 | 1967 | | IsCancellationToken = isCancellationToken; IsNullable = isNullable; |
| | 18 | 1968 | | HasDefault = hasDefault; Description = description; |
| | 9 | 1969 | | } |
| | | 1970 | | |
| | 60 | 1971 | | public string Name { get; } |
| | 5 | 1972 | | public string TypeFullName { get; } |
| | 32 | 1973 | | public string JsonSchemaType { get; } |
| | 0 | 1974 | | public string? ItemJsonSchemaType { get; } |
| | 35 | 1975 | | public bool IsCancellationToken { get; } |
| | 8 | 1976 | | public bool IsNullable { get; } |
| | 8 | 1977 | | public bool HasDefault { get; } |
| | 9 | 1978 | | public string? Description { get; } |
| | 8 | 1979 | | public bool IsRequired => !IsCancellationToken && !IsNullable && !HasDefault; |
| | | 1980 | | } |
| | | 1981 | | |
| | | 1982 | | private readonly struct AgentFunctionMethodInfo |
| | | 1983 | | { |
| | | 1984 | | public AgentFunctionMethodInfo( |
| | | 1985 | | string methodName, bool isAsync, bool isVoidLike, |
| | | 1986 | | string? returnValueTypeFQN, ImmutableArray<AgentFunctionParameterInfo> parameters, |
| | | 1987 | | string description) |
| | | 1988 | | { |
| | 27 | 1989 | | MethodName = methodName; IsAsync = isAsync; IsVoidLike = isVoidLike; |
| | 27 | 1990 | | ReturnValueTypeFQN = returnValueTypeFQN; Parameters = parameters; Description = description; |
| | 9 | 1991 | | } |
| | | 1992 | | |
| | 36 | 1993 | | public string MethodName { get; } |
| | 18 | 1994 | | public bool IsAsync { get; } |
| | 9 | 1995 | | public bool IsVoidLike { get; } |
| | 0 | 1996 | | public string? ReturnValueTypeFQN { get; } |
| | 27 | 1997 | | public ImmutableArray<AgentFunctionParameterInfo> Parameters { get; } |
| | 9 | 1998 | | public string Description { get; } |
| | | 1999 | | } |
| | | 2000 | | } |