| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | |
| | | 4 | | using Microsoft.CodeAnalysis; |
| | | 5 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Analyzer that validates <c>[AgentGroupChatMember]</c> group declarations. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <b>NDLRMAF002</b> (Error): A named group chat has fewer than two members in this compilation. |
| | | 14 | | /// <c>IWorkflowFactory.CreateGroupChatWorkflow</c> throws at runtime when this condition is met. |
| | | 15 | | /// </remarks> |
| | | 16 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 17 | | public sealed class AgentGroupChatSingletonAnalyzer : DiagnosticAnalyzer |
| | | 18 | | { |
| | | 19 | | private const string AgentGroupChatMemberAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGroupChatMemberAttrib |
| | | 20 | | |
| | | 21 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 89 | 22 | | ImmutableArray.Create(MafDiagnosticDescriptors.GroupChatTooFewMembers); |
| | | 23 | | |
| | | 24 | | public override void Initialize(AnalysisContext context) |
| | | 25 | | { |
| | 12 | 26 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 12 | 27 | | context.EnableConcurrentExecution(); |
| | | 28 | | |
| | 12 | 29 | | context.RegisterCompilationStartAction(compilationContext => |
| | 12 | 30 | | { |
| | 12 | 31 | | // group name → list of (type symbol, attribute location) |
| | 7 | 32 | | var groupMembers = new ConcurrentDictionary<string, ConcurrentBag<(INamedTypeSymbol Type, Location Location) |
| | 7 | 33 | | StringComparer.Ordinal); |
| | 12 | 34 | | |
| | 7 | 35 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 7 | 36 | | { |
| | 77 | 37 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 7 | 38 | | |
| | 320 | 39 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 7 | 40 | | { |
| | 83 | 41 | | if (attr.AttributeClass?.ToDisplayString() != AgentGroupChatMemberAttributeName) |
| | 7 | 42 | | continue; |
| | 7 | 43 | | |
| | 13 | 44 | | if (attr.ConstructorArguments.Length < 1) |
| | 7 | 45 | | continue; |
| | 7 | 46 | | |
| | 13 | 47 | | var groupName = attr.ConstructorArguments[0].Value as string; |
| | 13 | 48 | | if (string.IsNullOrWhiteSpace(groupName)) |
| | 7 | 49 | | continue; |
| | 7 | 50 | | |
| | 13 | 51 | | var attrLocation = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 13 | 52 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 13 | 53 | | : typeSymbol.Locations[0]; |
| | 7 | 54 | | |
| | 21 | 55 | | groupMembers.GetOrAdd(groupName!, _ => new ConcurrentBag<(INamedTypeSymbol, Location)>()) |
| | 13 | 56 | | .Add((typeSymbol, attrLocation)); |
| | 7 | 57 | | } |
| | 84 | 58 | | }, SymbolKind.NamedType); |
| | 12 | 59 | | |
| | 7 | 60 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 7 | 61 | | { |
| | 30 | 62 | | foreach (var kvp in groupMembers) |
| | 7 | 63 | | { |
| | 8 | 64 | | var members = kvp.Value.ToList(); |
| | 8 | 65 | | if (members.Count >= 2) |
| | 7 | 66 | | continue; |
| | 7 | 67 | | |
| | 7 | 68 | | // Report on every contributing class so the squiggle appears where the attribute is |
| | 16 | 69 | | foreach (var (_, location) in members) |
| | 7 | 70 | | { |
| | 4 | 71 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 4 | 72 | | MafDiagnosticDescriptors.GroupChatTooFewMembers, |
| | 4 | 73 | | location, |
| | 4 | 74 | | kvp.Key, |
| | 4 | 75 | | members.Count)); |
| | 7 | 76 | | } |
| | 7 | 77 | | } |
| | 14 | 78 | | }); |
| | 19 | 79 | | }); |
| | 12 | 80 | | } |
| | | 81 | | } |