| | | 1 | | using System.Collections.Immutable; |
| | | 2 | | |
| | | 3 | | using Microsoft.CodeAnalysis; |
| | | 4 | | using Microsoft.CodeAnalysis.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace NexusLabs.Needlr.AgentFramework.Analyzers; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Analyzer that detects types listed in <c>FunctionTypes</c> on <c>[NeedlrAiAgent]</c> that |
| | | 10 | | /// have no <c>[AgentFunction]</c> methods, causing the agent to silently receive zero tools. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <b>NDLRMAF014</b> (Warning): A type in <c>FunctionTypes</c> has no <c>[AgentFunction]</c> methods. |
| | | 14 | | /// </remarks> |
| | | 15 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 16 | | public sealed class AgentFunctionTypesMiswiredAnalyzer : DiagnosticAnalyzer |
| | | 17 | | { |
| | | 18 | | private const string NeedlrAiAgentAttributeName = "NexusLabs.Needlr.AgentFramework.NeedlrAiAgentAttribute"; |
| | | 19 | | private const string AgentFunctionAttributeName = "NexusLabs.Needlr.AgentFramework.AgentFunctionAttribute"; |
| | | 20 | | |
| | | 21 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 155 | 22 | | ImmutableArray.Create(MafDiagnosticDescriptors.AgentFunctionTypesMiswired); |
| | | 23 | | |
| | | 24 | | public override void Initialize(AnalysisContext context) |
| | | 25 | | { |
| | 15 | 26 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 15 | 27 | | context.EnableConcurrentExecution(); |
| | 15 | 28 | | context.RegisterSymbolAction(AnalyzeNamedType, SymbolKind.NamedType); |
| | 15 | 29 | | } |
| | | 30 | | |
| | | 31 | | private static void AnalyzeNamedType(SymbolAnalysisContext context) |
| | | 32 | | { |
| | 103 | 33 | | var typeSymbol = (INamedTypeSymbol)context.Symbol; |
| | | 34 | | |
| | 372 | 35 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | | 36 | | { |
| | 83 | 37 | | if (attr.AttributeClass?.ToDisplayString() != NeedlrAiAgentAttributeName) |
| | | 38 | | continue; |
| | | 39 | | |
| | 11 | 40 | | var functionTypesArg = attr.NamedArguments |
| | 21 | 41 | | .FirstOrDefault(n => n.Key == "FunctionTypes"); |
| | | 42 | | |
| | 11 | 43 | | if (functionTypesArg.Key is null) |
| | | 44 | | continue; |
| | | 45 | | |
| | 10 | 46 | | if (functionTypesArg.Value.Kind != TypedConstantKind.Array) |
| | | 47 | | continue; |
| | | 48 | | |
| | 42 | 49 | | foreach (var element in functionTypesArg.Value.Values) |
| | | 50 | | { |
| | 11 | 51 | | if (element.Kind != TypedConstantKind.Type) |
| | | 52 | | continue; |
| | | 53 | | |
| | 11 | 54 | | if (element.Value is not INamedTypeSymbol functionType) |
| | | 55 | | continue; |
| | | 56 | | |
| | 11 | 57 | | var hasAgentFunction = HasAnyAgentFunctionMethod(functionType); |
| | 11 | 58 | | if (hasAgentFunction) |
| | | 59 | | continue; |
| | | 60 | | |
| | 8 | 61 | | var location = attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 8 | 62 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 8 | 63 | | : typeSymbol.Locations[0]; |
| | | 64 | | |
| | 8 | 65 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 8 | 66 | | MafDiagnosticDescriptors.AgentFunctionTypesMiswired, |
| | 8 | 67 | | location, |
| | 8 | 68 | | functionType.Name, |
| | 8 | 69 | | typeSymbol.Name)); |
| | | 70 | | } |
| | | 71 | | } |
| | 103 | 72 | | } |
| | | 73 | | |
| | | 74 | | private static bool HasAnyAgentFunctionMethod(INamedTypeSymbol type) |
| | | 75 | | { |
| | 11 | 76 | | return type.GetMembers() |
| | 11 | 77 | | .OfType<IMethodSymbol>() |
| | 26 | 78 | | .Any(m => m.GetAttributes() |
| | 29 | 79 | | .Any(a => a.AttributeClass?.ToDisplayString() == AgentFunctionAttributeName)); |
| | | 80 | | } |
| | | 81 | | } |