| | | 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 validates termination condition declarations on agent classes. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// <b>NDLRMAF009</b> (Warning): <c>[WorkflowRunTerminationCondition]</c> is declared on a class |
| | | 13 | | /// that is not also decorated with <c>[NeedlrAiAgent]</c>.<br/> |
| | | 14 | | /// <b>NDLRMAF010</b> (Error): The <c>conditionType</c> passed to |
| | | 15 | | /// <c>[WorkflowRunTerminationCondition]</c> or <c>[AgentTerminationCondition]</c> does not |
| | | 16 | | /// implement <c>IWorkflowTerminationCondition</c>.<br/> |
| | | 17 | | /// <b>NDLRMAF011</b> (Info): <c>[WorkflowRunTerminationCondition]</c> is declared on a |
| | | 18 | | /// <c>[AgentGroupChatMember]</c>; prefer <c>[AgentTerminationCondition]</c> for group chats. |
| | | 19 | | /// </remarks> |
| | | 20 | | [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| | | 21 | | public sealed class TerminationConditionAnalyzer : DiagnosticAnalyzer |
| | | 22 | | { |
| | | 23 | | private const string NeedlrAiAgentAttributeName = "NexusLabs.Needlr.AgentFramework.NeedlrAiAgentAttribute"; |
| | | 24 | | private const string AgentGroupChatMemberAttributeName = "NexusLabs.Needlr.AgentFramework.AgentGroupChatMemberAttrib |
| | | 25 | | private const string WorkflowRunTerminationConditionAttributeName = "NexusLabs.Needlr.AgentFramework.WorkflowRunTerm |
| | | 26 | | private const string AgentTerminationConditionAttributeName = "NexusLabs.Needlr.AgentFramework.AgentTerminationCondi |
| | | 27 | | private const string IWorkflowTerminationConditionName = "NexusLabs.Needlr.AgentFramework.IWorkflowTerminationCondit |
| | | 28 | | |
| | | 29 | | public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| | 448 | 30 | | ImmutableArray.Create( |
| | 448 | 31 | | MafDiagnosticDescriptors.WorkflowRunTerminationConditionOnNonAgent, |
| | 448 | 32 | | MafDiagnosticDescriptors.TerminationConditionTypeInvalid, |
| | 448 | 33 | | MafDiagnosticDescriptors.PreferAgentTerminationConditionForGroupChat); |
| | | 34 | | |
| | | 35 | | public override void Initialize(AnalysisContext context) |
| | | 36 | | { |
| | 34 | 37 | | context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| | 34 | 38 | | context.EnableConcurrentExecution(); |
| | | 39 | | |
| | 34 | 40 | | context.RegisterSymbolAction(AnalyzeType, SymbolKind.NamedType); |
| | 34 | 41 | | } |
| | | 42 | | |
| | | 43 | | private static void AnalyzeType(SymbolAnalysisContext context) |
| | | 44 | | { |
| | 242 | 45 | | var typeSymbol = (INamedTypeSymbol)context.Symbol; |
| | | 46 | | |
| | 242 | 47 | | bool isAgent = false; |
| | 242 | 48 | | bool isGroupChatMember = false; |
| | 242 | 49 | | var workflowRunConditionAttrs = new List<AttributeData>(); |
| | 242 | 50 | | var agentTerminationConditionAttrs = new List<AttributeData>(); |
| | | 51 | | |
| | 970 | 52 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | | 53 | | { |
| | 243 | 54 | | var attrName = attr.AttributeClass?.ToDisplayString(); |
| | | 55 | | |
| | 243 | 56 | | if (attrName == NeedlrAiAgentAttributeName) |
| | 24 | 57 | | isAgent = true; |
| | 219 | 58 | | else if (attrName == AgentGroupChatMemberAttributeName) |
| | 16 | 59 | | isGroupChatMember = true; |
| | 203 | 60 | | else if (attrName == WorkflowRunTerminationConditionAttributeName) |
| | 23 | 61 | | workflowRunConditionAttrs.Add(attr); |
| | 180 | 62 | | else if (attrName == AgentTerminationConditionAttributeName) |
| | 4 | 63 | | agentTerminationConditionAttrs.Add(attr); |
| | | 64 | | } |
| | | 65 | | |
| | 242 | 66 | | if (workflowRunConditionAttrs.Count == 0 && agentTerminationConditionAttrs.Count == 0) |
| | 221 | 67 | | return; |
| | | 68 | | |
| | 21 | 69 | | var terminationInterface = context.Compilation.GetTypeByMetadataName(IWorkflowTerminationConditionName); |
| | | 70 | | |
| | 88 | 71 | | foreach (var attr in workflowRunConditionAttrs) |
| | | 72 | | { |
| | 23 | 73 | | var attrLocation = GetAttributeLocation(attr, typeSymbol); |
| | | 74 | | |
| | | 75 | | // NDLRMAF009: [WorkflowRunTerminationCondition] on a non-agent class |
| | 23 | 76 | | if (!isAgent) |
| | | 77 | | { |
| | 8 | 78 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 8 | 79 | | MafDiagnosticDescriptors.WorkflowRunTerminationConditionOnNonAgent, |
| | 8 | 80 | | attrLocation, |
| | 8 | 81 | | typeSymbol.Name)); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | // NDLRMAF010: conditionType doesn't implement IWorkflowTerminationCondition |
| | 23 | 85 | | if (TryGetConditionType(attr, out var conditionType) && terminationInterface is not null) |
| | | 86 | | { |
| | 23 | 87 | | if (!ImplementsInterface(conditionType!, terminationInterface)) |
| | | 88 | | { |
| | 8 | 89 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 8 | 90 | | MafDiagnosticDescriptors.TerminationConditionTypeInvalid, |
| | 8 | 91 | | attrLocation, |
| | 8 | 92 | | conditionType!.Name, |
| | 8 | 93 | | typeSymbol.Name)); |
| | | 94 | | } |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // NDLRMAF011: [WorkflowRunTerminationCondition] on [AgentGroupChatMember] |
| | 23 | 98 | | if (isGroupChatMember) |
| | | 99 | | { |
| | 6 | 100 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 6 | 101 | | MafDiagnosticDescriptors.PreferAgentTerminationConditionForGroupChat, |
| | 6 | 102 | | attrLocation, |
| | 6 | 103 | | typeSymbol.Name)); |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | 50 | 107 | | foreach (var attr in agentTerminationConditionAttrs) |
| | | 108 | | { |
| | | 109 | | // NDLRMAF010: conditionType doesn't implement IWorkflowTerminationCondition |
| | 4 | 110 | | if (TryGetConditionType(attr, out var conditionType) && terminationInterface is not null) |
| | | 111 | | { |
| | 4 | 112 | | if (!ImplementsInterface(conditionType!, terminationInterface)) |
| | | 113 | | { |
| | 2 | 114 | | var attrLocation = GetAttributeLocation(attr, typeSymbol); |
| | 2 | 115 | | context.ReportDiagnostic(Diagnostic.Create( |
| | 2 | 116 | | MafDiagnosticDescriptors.TerminationConditionTypeInvalid, |
| | 2 | 117 | | attrLocation, |
| | 2 | 118 | | conditionType!.Name, |
| | 2 | 119 | | typeSymbol.Name)); |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | } |
| | 21 | 123 | | } |
| | | 124 | | |
| | | 125 | | private static bool TryGetConditionType(AttributeData attr, out INamedTypeSymbol? conditionType) |
| | | 126 | | { |
| | 27 | 127 | | conditionType = null; |
| | | 128 | | |
| | 27 | 129 | | if (attr.ConstructorArguments.Length >= 1 |
| | 27 | 130 | | && attr.ConstructorArguments[0].Kind == TypedConstantKind.Type |
| | 27 | 131 | | && attr.ConstructorArguments[0].Value is INamedTypeSymbol namedType) |
| | | 132 | | { |
| | 27 | 133 | | conditionType = namedType; |
| | 27 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | |
| | 0 | 137 | | return false; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | private static bool ImplementsInterface(INamedTypeSymbol type, INamedTypeSymbol interfaceSymbol) |
| | | 141 | | { |
| | 71 | 142 | | foreach (var iface in type.AllInterfaces) |
| | | 143 | | { |
| | 17 | 144 | | if (SymbolEqualityComparer.Default.Equals(iface, interfaceSymbol)) |
| | 17 | 145 | | return true; |
| | | 146 | | } |
| | | 147 | | |
| | 10 | 148 | | return false; |
| | | 149 | | } |
| | | 150 | | |
| | | 151 | | private static Location GetAttributeLocation(AttributeData attr, INamedTypeSymbol fallback) |
| | | 152 | | { |
| | 25 | 153 | | return attr.ApplicationSyntaxReference?.SyntaxTree is { } tree |
| | 25 | 154 | | ? Location.Create(tree, attr.ApplicationSyntaxReference.Span) |
| | 25 | 155 | | : fallback.Locations[0]; |
| | | 156 | | } |
| | | 157 | | } |