| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using System.Collections.Immutable; |
| | | 3 | | using System.Linq; |
| | | 4 | | |
| | | 5 | | using Microsoft.CodeAnalysis; |
| | | 6 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 7 | | |
| | | 8 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Analyzer that validates <c>FunctionGroups</c> references in <c>[NeedlrAiAgent]</c> declarations. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// <b>NDLRMAF005</b> (Warning): An agent declares a <c>FunctionGroups</c> entry whose name does not |
| | | 15 | | /// match any class decorated with <c>[AgentFunctionGroup]</c> in this compilation. The agent will |
| | | 16 | | /// silently receive zero tools from that group at runtime. |
| | | 17 | | /// </remarks> |
| | | 18 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 19 | | public sealed class AgentFunctionGroupReferenceAnalyzer : DiagnosticAnalyzer |
| | | 20 | | { |
| | | 21 | | private const string NeedlrAiAgentAttributeName = "NexusLabs.Needlr.AgentFramework.NeedlrAiAgentAttribute"; |
| | | 22 | | private const string AgentFunctionGroupAttributeName = "NexusLabs.Needlr.AgentFramework.AgentFunctionGroupAttribute" |
| | | 23 | | |
| | | 24 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 165 | 25 | | ImmutableArray.Create(MafDiagnosticDescriptors.UnresolvedFunctionGroupReference); |
| | | 26 | | |
| | | 27 | | public override void Initialize(AnalysisContext context) |
| | | 28 | | { |
| | 19 | 29 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 19 | 30 | | context.EnableConcurrentExecution(); |
| | | 31 | | |
| | 19 | 32 | | context.RegisterCompilationStartAction(compilationContext => |
| | 19 | 33 | | { |
| | 11 | 34 | | var knownGroups = new ConcurrentBag<string>(); |
| | 11 | 35 | | var references = new ConcurrentBag<(string AgentName, string GroupName, Location Location)>(); |
| | 19 | 36 | | |
| | 11 | 37 | | compilationContext.RegisterSymbolAction(symbolContext => |
| | 11 | 38 | | { |
| | 120 | 39 | | var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| | 11 | 40 | | |
| | 460 | 41 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | 11 | 42 | | { |
| | 110 | 43 | | var attrName = attr.AttributeClass?.ToDisplayString(); |
| | 11 | 44 | | |
| | 110 | 45 | | if (attrName == AgentFunctionGroupAttributeName |
| | 110 | 46 | | && attr.ConstructorArguments.Length >= 1 |
| | 110 | 47 | | && attr.ConstructorArguments[0].Value is string groupName |
| | 110 | 48 | | && !string.IsNullOrWhiteSpace(groupName)) |
| | 11 | 49 | | { |
| | 9 | 50 | | knownGroups.Add(groupName); |
| | 11 | 51 | | } |
| | 11 | 52 | | |
| | 110 | 53 | | if (attrName == NeedlrAiAgentAttributeName) |
| | 11 | 54 | | { |
| | 25 | 55 | | var functionGroupsArg = attr.NamedArguments.FirstOrDefault(a => a.Key == "FunctionGroups"); |
| | 13 | 56 | | if (functionGroupsArg.Key is null) |
| | 11 | 57 | | continue; |
| | 11 | 58 | | |
| | 12 | 59 | | var arrayConstant = functionGroupsArg.Value; |
| | 12 | 60 | | if (arrayConstant.Kind != TypedConstantKind.Array) |
| | 11 | 61 | | continue; |
| | 11 | 62 | | |
| | 12 | 63 | | var attrLocation = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 12 | 64 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 12 | 65 | | : typeSymbol.Locations[0]; |
| | 11 | 66 | | |
| | 52 | 67 | | foreach (var item in arrayConstant.Values) |
| | 11 | 68 | | { |
| | 14 | 69 | | if (item.Value is string name && !string.IsNullOrWhiteSpace(name)) |
| | 14 | 70 | | references.Add((typeSymbol.Name, name, attrLocation)); |
| | 11 | 71 | | } |
| | 11 | 72 | | } |
| | 11 | 73 | | } |
| | 131 | 74 | | }, SymbolKind.NamedType); |
| | 19 | 75 | | |
| | 11 | 76 | | compilationContext.RegisterCompilationEndAction(endContext => |
| | 11 | 77 | | { |
| | 11 | 78 | | var knownGroupSet = new HashSet<string>(knownGroups, StringComparer.Ordinal); |
| | 11 | 79 | | |
| | 50 | 80 | | foreach (var (agentName, groupName, location) in references) |
| | 11 | 81 | | { |
| | 14 | 82 | | if (!knownGroupSet.Contains(groupName)) |
| | 11 | 83 | | { |
| | 8 | 84 | | endContext.ReportDiagnostic(Diagnostic.Create( |
| | 8 | 85 | | MafDiagnosticDescriptors.UnresolvedFunctionGroupReference, |
| | 8 | 86 | | location, |
| | 8 | 87 | | agentName, |
| | 8 | 88 | | groupName)); |
| | 11 | 89 | | } |
| | 11 | 90 | | } |
| | 22 | 91 | | }); |
| | 30 | 92 | | }); |
| | 19 | 93 | | } |
| | | 94 | | } |