| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Immutable; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Text; |
| | | 7 | | using System.Threading; |
| | | 8 | | |
| | | 9 | | using Microsoft.CodeAnalysis; |
| | | 10 | | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| | | 11 | | |
| | | 12 | | namespace NexusLabs.Needlr.AgentFramework.Generators; |
| | | 13 | | |
| | | 14 | | internal static class AgentDiscoveryHelper |
| | | 15 | | { |
| | | 16 | | private const string AgentFunctionAttributeName = "NexusLabs.Needlr.AgentFramework.AgentFunctionAttribute"; |
| | | 17 | | private const string AgentFunctionGroupAttributeName = "NexusLabs.Needlr.AgentFramework.AgentFunctionGroupAttribute" |
| | | 18 | | |
| | | 19 | | public static AgentFunctionTypeInfo? GetAgentFunctionTypeInfo( |
| | | 20 | | GeneratorSyntaxContext context, |
| | | 21 | | CancellationToken cancellationToken) |
| | | 22 | | { |
| | 1966 | 23 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | 1966 | 24 | | var typeSymbol = context.SemanticModel |
| | 1966 | 25 | | .GetDeclaredSymbol(classDeclaration, cancellationToken) as INamedTypeSymbol; |
| | | 26 | | |
| | 1966 | 27 | | if (typeSymbol is null) |
| | 0 | 28 | | return null; |
| | | 29 | | |
| | 1966 | 30 | | return TryGetTypeInfo(typeSymbol); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public static AgentFunctionTypeInfo? TryGetTypeInfo(INamedTypeSymbol typeSymbol) |
| | | 34 | | { |
| | 1966 | 35 | | if (typeSymbol.TypeKind != TypeKind.Class) |
| | 0 | 36 | | return null; |
| | | 37 | | |
| | 1966 | 38 | | if (!IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 39 | | return null; |
| | | 40 | | |
| | 1966 | 41 | | if (!typeSymbol.IsStatic && typeSymbol.IsAbstract) |
| | 0 | 42 | | return null; |
| | | 43 | | |
| | 1966 | 44 | | bool hasAgentFunction = false; |
| | 22674 | 45 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 46 | | { |
| | 9381 | 47 | | if (member is not IMethodSymbol method) |
| | | 48 | | continue; |
| | | 49 | | |
| | 5323 | 50 | | if (method.MethodKind != MethodKind.Ordinary) |
| | | 51 | | continue; |
| | | 52 | | |
| | 430 | 53 | | if (method.DeclaredAccessibility != Accessibility.Public) |
| | | 54 | | continue; |
| | | 55 | | |
| | 880 | 56 | | foreach (var attribute in method.GetAttributes()) |
| | | 57 | | { |
| | 20 | 58 | | if (attribute.AttributeClass?.ToDisplayString() == AgentFunctionAttributeName) |
| | | 59 | | { |
| | 20 | 60 | | hasAgentFunction = true; |
| | 20 | 61 | | break; |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | 430 | 65 | | if (hasAgentFunction) |
| | | 66 | | break; |
| | | 67 | | } |
| | | 68 | | |
| | 1966 | 69 | | if (!hasAgentFunction) |
| | 1946 | 70 | | return null; |
| | | 71 | | |
| | 20 | 72 | | var methodInfos = ImmutableArray.CreateBuilder<AgentFunctionMethodInfo>(); |
| | 116 | 73 | | foreach (var member in typeSymbol.GetMembers()) |
| | | 74 | | { |
| | 38 | 75 | | if (member is not IMethodSymbol method) |
| | | 76 | | continue; |
| | | 77 | | |
| | 38 | 78 | | if (method.MethodKind != MethodKind.Ordinary) |
| | | 79 | | continue; |
| | | 80 | | |
| | 20 | 81 | | if (method.DeclaredAccessibility != Accessibility.Public) |
| | | 82 | | continue; |
| | | 83 | | |
| | 20 | 84 | | bool isAgentFunction = false; |
| | 60 | 85 | | foreach (var attribute in method.GetAttributes()) |
| | | 86 | | { |
| | 20 | 87 | | if (attribute.AttributeClass?.ToDisplayString() == AgentFunctionAttributeName) |
| | | 88 | | { |
| | 20 | 89 | | isAgentFunction = true; |
| | 20 | 90 | | break; |
| | | 91 | | } |
| | | 92 | | } |
| | | 93 | | |
| | 20 | 94 | | if (!isAgentFunction) |
| | | 95 | | continue; |
| | | 96 | | |
| | 20 | 97 | | var returnType = method.ReturnType; |
| | 20 | 98 | | bool isVoid = returnType.SpecialType == SpecialType.System_Void; |
| | 20 | 99 | | bool isTask = returnType is INamedTypeSymbol nt && |
| | 20 | 100 | | nt.ContainingNamespace?.ToDisplayString() == "System.Threading.Tasks" && |
| | 20 | 101 | | (nt.MetadataName == "Task" || nt.MetadataName == "ValueTask"); |
| | 20 | 102 | | bool isTaskOfT = returnType is INamedTypeSymbol nt2 && |
| | 20 | 103 | | nt2.ContainingNamespace?.ToDisplayString() == "System.Threading.Tasks" && |
| | 20 | 104 | | (nt2.MetadataName == "Task`1" || nt2.MetadataName == "ValueTask`1") && |
| | 20 | 105 | | nt2.TypeArguments.Length == 1; |
| | 20 | 106 | | bool isAsync = isTask || isTaskOfT; |
| | 20 | 107 | | bool isVoidLike = isVoid || isTask; |
| | 20 | 108 | | string? returnValueTypeFQN = isVoidLike ? null : isTaskOfT |
| | 20 | 109 | | ? GetFullyQualifiedName(((INamedTypeSymbol)returnType).TypeArguments[0]) |
| | 20 | 110 | | : GetFullyQualifiedName(returnType); |
| | | 111 | | |
| | 20 | 112 | | string? returnJsonSchemaType = null; |
| | 20 | 113 | | string? returnObjectSchemaJson = null; |
| | 20 | 114 | | if (!isVoidLike) |
| | | 115 | | { |
| | 16 | 116 | | var unwrappedReturnType = isTaskOfT |
| | 16 | 117 | | ? ((INamedTypeSymbol)returnType).TypeArguments[0] |
| | 16 | 118 | | : returnType; |
| | 16 | 119 | | returnJsonSchemaType = GetJsonSchemaType(unwrappedReturnType, out _); |
| | 16 | 120 | | if (returnJsonSchemaType == "object") |
| | | 121 | | { |
| | 1 | 122 | | returnObjectSchemaJson = BuildObjectSchemaJson(unwrappedReturnType); |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | |
| | 20 | 126 | | string? methodDesc = GetDescriptionFromAttributes(method.GetAttributes()); |
| | | 127 | | |
| | 20 | 128 | | var parameters = ImmutableArray.CreateBuilder<AgentFunctionParameterInfo>(); |
| | 74 | 129 | | foreach (var param in method.Parameters) |
| | | 130 | | { |
| | 17 | 131 | | bool isCancellationToken = param.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == "glob |
| | 17 | 132 | | bool isNullable = param.NullableAnnotation == NullableAnnotation.Annotated || |
| | 17 | 133 | | (param.Type is INamedTypeSymbol pnt && pnt.ConstructedFrom.SpecialType == SpecialType.System_Nullabl |
| | 17 | 134 | | bool hasDefault = param.HasExplicitDefaultValue; |
| | 17 | 135 | | string? paramDesc = GetDescriptionFromAttributes(param.GetAttributes()); |
| | 17 | 136 | | string jsonSchemaType = GetJsonSchemaType(param.Type, out string? itemJsonSchemaType); |
| | 17 | 137 | | string typeFullName = GetFullyQualifiedName(param.Type); |
| | | 138 | | |
| | | 139 | | // Build object schema for complex array items (e.g., FaqEntry[] → properties of FaqEntry) |
| | 17 | 140 | | string? itemObjectSchemaJson = null; |
| | 17 | 141 | | IReadOnlyList<ObjectPropertyInfo>? itemObjectProperties = null; |
| | 17 | 142 | | if (jsonSchemaType == "array" && itemJsonSchemaType == "object") |
| | | 143 | | { |
| | 3 | 144 | | ITypeSymbol? elementType = null; |
| | 3 | 145 | | if (param.Type is IArrayTypeSymbol arrSym) |
| | 3 | 146 | | elementType = arrSym.ElementType; |
| | 0 | 147 | | else if (param.Type is INamedTypeSymbol namedSym && namedSym.IsGenericType && namedSym.TypeArguments |
| | 0 | 148 | | elementType = namedSym.TypeArguments[0]; |
| | | 149 | | |
| | 3 | 150 | | if (elementType != null) |
| | | 151 | | { |
| | 3 | 152 | | itemObjectSchemaJson = BuildObjectSchemaJson(elementType); |
| | 3 | 153 | | itemObjectProperties = BuildObjectPropertyInfos(elementType); |
| | | 154 | | } |
| | | 155 | | } |
| | | 156 | | |
| | 17 | 157 | | parameters.Add(new AgentFunctionParameterInfo( |
| | 17 | 158 | | param.Name, typeFullName, jsonSchemaType, itemJsonSchemaType, |
| | 17 | 159 | | itemObjectSchemaJson, itemObjectProperties, |
| | 17 | 160 | | isCancellationToken, isNullable, hasDefault, paramDesc)); |
| | | 161 | | } |
| | | 162 | | |
| | 20 | 163 | | methodInfos.Add(new AgentFunctionMethodInfo( |
| | 20 | 164 | | method.Name, isAsync, isVoidLike, returnValueTypeFQN, |
| | 20 | 165 | | returnJsonSchemaType, returnObjectSchemaJson, |
| | 20 | 166 | | parameters.ToImmutable(), methodDesc ?? "")); |
| | | 167 | | } |
| | | 168 | | |
| | 20 | 169 | | return new AgentFunctionTypeInfo( |
| | 20 | 170 | | GetFullyQualifiedName(typeSymbol), |
| | 20 | 171 | | typeSymbol.ContainingAssembly?.Name ?? "Unknown", |
| | 20 | 172 | | typeSymbol.IsStatic, |
| | 20 | 173 | | methodInfos.ToImmutable()); |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | public static ImmutableArray<AgentFunctionGroupEntry> GetAgentFunctionGroupEntries( |
| | | 177 | | GeneratorSyntaxContext context, |
| | | 178 | | CancellationToken cancellationToken) |
| | | 179 | | { |
| | 1966 | 180 | | var classDeclaration = (ClassDeclarationSyntax)context.Node; |
| | 1966 | 181 | | var typeSymbol = context.SemanticModel |
| | 1966 | 182 | | .GetDeclaredSymbol(classDeclaration, cancellationToken) as INamedTypeSymbol; |
| | | 183 | | |
| | 1966 | 184 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 185 | | return ImmutableArray<AgentFunctionGroupEntry>.Empty; |
| | | 186 | | |
| | 1966 | 187 | | var entries = ImmutableArray.CreateBuilder<AgentFunctionGroupEntry>(); |
| | | 188 | | |
| | 6794 | 189 | | foreach (var attr in typeSymbol.GetAttributes()) |
| | | 190 | | { |
| | 1431 | 191 | | if (attr.AttributeClass?.ToDisplayString() != AgentFunctionGroupAttributeName) |
| | | 192 | | continue; |
| | | 193 | | |
| | 14 | 194 | | if (attr.ConstructorArguments.Length != 1) |
| | | 195 | | continue; |
| | | 196 | | |
| | 14 | 197 | | var groupName = attr.ConstructorArguments[0].Value as string; |
| | 14 | 198 | | if (string.IsNullOrWhiteSpace(groupName)) |
| | | 199 | | continue; |
| | | 200 | | |
| | 14 | 201 | | entries.Add(new AgentFunctionGroupEntry(GetFullyQualifiedName(typeSymbol), groupName!)); |
| | | 202 | | } |
| | | 203 | | |
| | 1966 | 204 | | return entries.ToImmutable(); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | public static NeedlrAiAgentTypeInfo? GetNeedlrAiAgentTypeInfo( |
| | | 208 | | GeneratorAttributeSyntaxContext context, |
| | | 209 | | CancellationToken cancellationToken) |
| | | 210 | | { |
| | 107 | 211 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 107 | 212 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 213 | | return null; |
| | | 214 | | |
| | 107 | 215 | | var classDeclaration = context.TargetNode as ClassDeclarationSyntax; |
| | 225 | 216 | | var isPartial = classDeclaration?.Modifiers.Any(m => m.ValueText == "partial") ?? false; |
| | | 217 | | |
| | 107 | 218 | | var namespaceName = typeSymbol.ContainingNamespace?.IsGlobalNamespace == true |
| | 107 | 219 | | ? null |
| | 107 | 220 | | : typeSymbol.ContainingNamespace?.ToDisplayString(); |
| | | 221 | | |
| | 107 | 222 | | var functionGroupNames = ImmutableArray<string>.Empty; |
| | 107 | 223 | | var explicitFunctionTypeFQNs = ImmutableArray<string>.Empty; |
| | 107 | 224 | | var hasExplicitFunctionTypes = false; |
| | | 225 | | |
| | 107 | 226 | | var agentAttr = context.Attributes.FirstOrDefault(); |
| | 107 | 227 | | if (agentAttr is not null) |
| | | 228 | | { |
| | 117 | 229 | | var groupsArg = agentAttr.NamedArguments.FirstOrDefault(a => a.Key == "FunctionGroups"); |
| | 107 | 230 | | if (groupsArg.Key is not null && groupsArg.Value.Kind == TypedConstantKind.Array) |
| | | 231 | | { |
| | 2 | 232 | | functionGroupNames = groupsArg.Value.Values |
| | 2 | 233 | | .Select(v => v.Value as string) |
| | 2 | 234 | | .Where(s => !string.IsNullOrWhiteSpace(s)) |
| | 2 | 235 | | .Select(s => s!) |
| | 2 | 236 | | .ToImmutableArray(); |
| | | 237 | | } |
| | | 238 | | |
| | 117 | 239 | | var typesArg = agentAttr.NamedArguments.FirstOrDefault(a => a.Key == "FunctionTypes"); |
| | 107 | 240 | | if (typesArg.Key is not null && typesArg.Value.Kind == TypedConstantKind.Array) |
| | | 241 | | { |
| | 2 | 242 | | hasExplicitFunctionTypes = true; |
| | 2 | 243 | | explicitFunctionTypeFQNs = typesArg.Value.Values |
| | 1 | 244 | | .Where(v => v.Kind == TypedConstantKind.Type && v.Value is INamedTypeSymbol) |
| | 1 | 245 | | .Select(v => GetFullyQualifiedName((INamedTypeSymbol)v.Value!)) |
| | 2 | 246 | | .ToImmutableArray(); |
| | | 247 | | } |
| | | 248 | | } |
| | | 249 | | |
| | 107 | 250 | | return new NeedlrAiAgentTypeInfo( |
| | 107 | 251 | | GetFullyQualifiedName(typeSymbol), |
| | 107 | 252 | | typeSymbol.Name, |
| | 107 | 253 | | namespaceName, |
| | 107 | 254 | | isPartial, |
| | 107 | 255 | | functionGroupNames, |
| | 107 | 256 | | explicitFunctionTypeFQNs, |
| | 107 | 257 | | hasExplicitFunctionTypes); |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | public static ImmutableArray<HandoffEntry> GetHandoffEntries( |
| | | 261 | | GeneratorAttributeSyntaxContext context, |
| | | 262 | | CancellationToken cancellationToken) |
| | | 263 | | { |
| | 5 | 264 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 5 | 265 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 266 | | return ImmutableArray<HandoffEntry>.Empty; |
| | | 267 | | |
| | 5 | 268 | | var initialTypeName = GetFullyQualifiedName(typeSymbol); |
| | 5 | 269 | | var entries = ImmutableArray.CreateBuilder<HandoffEntry>(); |
| | | 270 | | |
| | 20 | 271 | | foreach (var attr in context.Attributes) |
| | | 272 | | { |
| | 5 | 273 | | if (attr.ConstructorArguments.Length < 1) |
| | | 274 | | continue; |
| | | 275 | | |
| | 5 | 276 | | var typeArg = attr.ConstructorArguments[0]; |
| | 5 | 277 | | if (typeArg.Kind != TypedConstantKind.Type || typeArg.Value is not INamedTypeSymbol targetTypeSymbol) |
| | | 278 | | continue; |
| | | 279 | | |
| | 5 | 280 | | var targetTypeName = GetFullyQualifiedName(targetTypeSymbol); |
| | 5 | 281 | | var reason = attr.ConstructorArguments.Length > 1 ? attr.ConstructorArguments[1].Value as string : null; |
| | | 282 | | |
| | 5 | 283 | | entries.Add(new HandoffEntry(initialTypeName, typeSymbol.Name, targetTypeName, reason)); |
| | | 284 | | } |
| | | 285 | | |
| | 5 | 286 | | return entries.ToImmutable(); |
| | | 287 | | } |
| | | 288 | | |
| | | 289 | | public static ImmutableArray<GroupChatEntry> GetGroupChatEntries( |
| | | 290 | | GeneratorAttributeSyntaxContext context, |
| | | 291 | | CancellationToken cancellationToken) |
| | | 292 | | { |
| | 8 | 293 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 8 | 294 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 295 | | return ImmutableArray<GroupChatEntry>.Empty; |
| | | 296 | | |
| | 8 | 297 | | var agentTypeName = GetFullyQualifiedName(typeSymbol); |
| | 8 | 298 | | var entries = ImmutableArray.CreateBuilder<GroupChatEntry>(); |
| | | 299 | | |
| | 32 | 300 | | foreach (var attr in context.Attributes) |
| | | 301 | | { |
| | 8 | 302 | | if (attr.ConstructorArguments.Length < 1) |
| | | 303 | | continue; |
| | | 304 | | |
| | 8 | 305 | | var groupName = attr.ConstructorArguments[0].Value as string; |
| | 8 | 306 | | if (string.IsNullOrWhiteSpace(groupName)) |
| | | 307 | | continue; |
| | | 308 | | |
| | 8 | 309 | | var order = 0; |
| | 16 | 310 | | foreach (var named in attr.NamedArguments) |
| | | 311 | | { |
| | 0 | 312 | | if (named.Key == "Order" && named.Value.Value is int orderValue) |
| | | 313 | | { |
| | 0 | 314 | | order = orderValue; |
| | 0 | 315 | | break; |
| | | 316 | | } |
| | | 317 | | } |
| | | 318 | | |
| | 8 | 319 | | entries.Add(new GroupChatEntry(agentTypeName, groupName!, order)); |
| | | 320 | | } |
| | | 321 | | |
| | 8 | 322 | | return entries.ToImmutable(); |
| | | 323 | | } |
| | | 324 | | |
| | | 325 | | public static ImmutableArray<SequenceEntry> GetSequenceEntries( |
| | | 326 | | GeneratorAttributeSyntaxContext context, |
| | | 327 | | CancellationToken cancellationToken) |
| | | 328 | | { |
| | 24 | 329 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 24 | 330 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 331 | | return ImmutableArray<SequenceEntry>.Empty; |
| | | 332 | | |
| | 24 | 333 | | var agentTypeName = GetFullyQualifiedName(typeSymbol); |
| | 24 | 334 | | var entries = ImmutableArray.CreateBuilder<SequenceEntry>(); |
| | | 335 | | |
| | 96 | 336 | | foreach (var attr in context.Attributes) |
| | | 337 | | { |
| | 24 | 338 | | if (attr.ConstructorArguments.Length < 2) |
| | | 339 | | continue; |
| | | 340 | | |
| | 24 | 341 | | var pipelineName = attr.ConstructorArguments[0].Value as string; |
| | 24 | 342 | | if (string.IsNullOrWhiteSpace(pipelineName)) |
| | | 343 | | continue; |
| | | 344 | | |
| | 24 | 345 | | if (attr.ConstructorArguments[1].Value is not int order) |
| | | 346 | | continue; |
| | | 347 | | |
| | 24 | 348 | | entries.Add(new SequenceEntry(agentTypeName, pipelineName!, order)); |
| | | 349 | | } |
| | | 350 | | |
| | 24 | 351 | | return entries.ToImmutable(); |
| | | 352 | | } |
| | | 353 | | |
| | | 354 | | public static ImmutableArray<TerminationConditionEntry> GetTerminationConditionEntries( |
| | | 355 | | GeneratorAttributeSyntaxContext context, |
| | | 356 | | CancellationToken cancellationToken) |
| | | 357 | | { |
| | 8 | 358 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 8 | 359 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 360 | | return ImmutableArray<TerminationConditionEntry>.Empty; |
| | | 361 | | |
| | 8 | 362 | | var agentTypeName = GetFullyQualifiedName(typeSymbol); |
| | 8 | 363 | | var entries = ImmutableArray.CreateBuilder<TerminationConditionEntry>(); |
| | | 364 | | |
| | 32 | 365 | | foreach (var attr in context.Attributes) |
| | | 366 | | { |
| | 8 | 367 | | if (attr.ConstructorArguments.Length < 1) |
| | | 368 | | continue; |
| | | 369 | | |
| | 8 | 370 | | var typeArg = attr.ConstructorArguments[0]; |
| | 8 | 371 | | if (typeArg.Kind != TypedConstantKind.Type || typeArg.Value is not INamedTypeSymbol condTypeSymbol) |
| | | 372 | | continue; |
| | | 373 | | |
| | 8 | 374 | | var condTypeFQN = GetFullyQualifiedName(condTypeSymbol); |
| | 8 | 375 | | var ctorArgLiterals = ImmutableArray<string>.Empty; |
| | | 376 | | |
| | 8 | 377 | | if (attr.ConstructorArguments.Length > 1) |
| | | 378 | | { |
| | 8 | 379 | | var paramsArg = attr.ConstructorArguments[1]; |
| | 8 | 380 | | if (paramsArg.Kind == TypedConstantKind.Array) |
| | | 381 | | { |
| | 8 | 382 | | ctorArgLiterals = paramsArg.Values |
| | 8 | 383 | | .Select(SerializeTypedConstant) |
| | 8 | 384 | | .Where(s => s is not null) |
| | 8 | 385 | | .Select(s => s!) |
| | 8 | 386 | | .ToImmutableArray(); |
| | | 387 | | } |
| | | 388 | | } |
| | | 389 | | |
| | 8 | 390 | | entries.Add(new TerminationConditionEntry(agentTypeName, condTypeFQN, ctorArgLiterals)); |
| | | 391 | | } |
| | | 392 | | |
| | 8 | 393 | | return entries.ToImmutable(); |
| | | 394 | | } |
| | | 395 | | |
| | | 396 | | public static string? SerializeTypedConstant(TypedConstant constant) |
| | | 397 | | { |
| | 8 | 398 | | return constant.Kind switch |
| | 8 | 399 | | { |
| | 8 | 400 | | TypedConstantKind.Primitive when constant.Value is string s => |
| | 8 | 401 | | "\"" + s.Replace("\\", "\\\\").Replace("\"", "\\\"") + "\"", |
| | 0 | 402 | | TypedConstantKind.Primitive when constant.Value is int i => i.ToString(), |
| | 0 | 403 | | TypedConstantKind.Primitive when constant.Value is long l => l.ToString() + "L", |
| | 0 | 404 | | TypedConstantKind.Primitive when constant.Value is bool b => b ? "true" : "false", |
| | 0 | 405 | | TypedConstantKind.Primitive when constant.Value is null => "null", |
| | 0 | 406 | | TypedConstantKind.Type when constant.Value is ITypeSymbol ts => |
| | 0 | 407 | | $"typeof({GetFullyQualifiedName(ts)})", |
| | 0 | 408 | | _ => null, |
| | 8 | 409 | | }; |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | public static ImmutableArray<ProgressSinksEntry> GetProgressSinksEntries( |
| | | 413 | | GeneratorAttributeSyntaxContext context, |
| | | 414 | | CancellationToken cancellationToken) |
| | | 415 | | { |
| | 4 | 416 | | var typeSymbol = context.TargetSymbol as INamedTypeSymbol; |
| | 4 | 417 | | if (typeSymbol is null || !IsAccessibleFromGeneratedCode(typeSymbol)) |
| | 0 | 418 | | return ImmutableArray<ProgressSinksEntry>.Empty; |
| | | 419 | | |
| | 4 | 420 | | var agentTypeName = GetFullyQualifiedName(typeSymbol); |
| | | 421 | | |
| | 12 | 422 | | foreach (var attr in context.Attributes) |
| | | 423 | | { |
| | | 424 | | // params Type[] is a single constructor arg of TypedConstantKind.Array |
| | 4 | 425 | | if (attr.ConstructorArguments.Length < 1) |
| | | 426 | | continue; |
| | | 427 | | |
| | 4 | 428 | | var firstArg = attr.ConstructorArguments[0]; |
| | 4 | 429 | | if (firstArg.Kind != TypedConstantKind.Array) |
| | | 430 | | continue; |
| | | 431 | | |
| | 4 | 432 | | var sinkFQNs = ImmutableArray.CreateBuilder<string>(); |
| | 16 | 433 | | foreach (var element in firstArg.Values) |
| | | 434 | | { |
| | 4 | 435 | | if (element.Kind == TypedConstantKind.Type && element.Value is INamedTypeSymbol sinkType) |
| | | 436 | | { |
| | 4 | 437 | | sinkFQNs.Add(GetFullyQualifiedName(sinkType)); |
| | | 438 | | } |
| | | 439 | | } |
| | | 440 | | |
| | 4 | 441 | | if (sinkFQNs.Count > 0) |
| | | 442 | | { |
| | 4 | 443 | | return ImmutableArray.Create( |
| | 4 | 444 | | new ProgressSinksEntry(agentTypeName, typeSymbol.Name, sinkFQNs.ToImmutable())); |
| | | 445 | | } |
| | | 446 | | } |
| | | 447 | | |
| | 0 | 448 | | return ImmutableArray<ProgressSinksEntry>.Empty; |
| | | 449 | | } |
| | | 450 | | |
| | | 451 | | public static string? GetDescriptionFromAttributes(ImmutableArray<AttributeData> attributes) |
| | | 452 | | { |
| | 123 | 453 | | foreach (var attr in attributes) |
| | | 454 | | { |
| | 29 | 455 | | if (attr.AttributeClass?.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == |
| | 29 | 456 | | "global::System.ComponentModel.DescriptionAttribute" && |
| | 29 | 457 | | attr.ConstructorArguments.Length == 1 && |
| | 29 | 458 | | attr.ConstructorArguments[0].Value is string desc) |
| | 9 | 459 | | return desc; |
| | | 460 | | } |
| | 28 | 461 | | return null; |
| | | 462 | | } |
| | | 463 | | |
| | | 464 | | public static string GetJsonSchemaType(ITypeSymbol type, out string? itemType) |
| | | 465 | | { |
| | 52 | 466 | | itemType = null; |
| | 52 | 467 | | if (type is INamedTypeSymbol nullable && nullable.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T) |
| | 0 | 468 | | type = nullable.TypeArguments[0]; |
| | | 469 | | |
| | 52 | 470 | | switch (type.SpecialType) |
| | | 471 | | { |
| | 33 | 472 | | case SpecialType.System_String: return "string"; |
| | 2 | 473 | | case SpecialType.System_Boolean: return "boolean"; |
| | | 474 | | case SpecialType.System_Byte: |
| | | 475 | | case SpecialType.System_SByte: |
| | | 476 | | case SpecialType.System_Int16: |
| | | 477 | | case SpecialType.System_UInt16: |
| | | 478 | | case SpecialType.System_Int32: |
| | | 479 | | case SpecialType.System_UInt32: |
| | | 480 | | case SpecialType.System_Int64: |
| | 8 | 481 | | case SpecialType.System_UInt64: return "integer"; |
| | | 482 | | case SpecialType.System_Single: |
| | | 483 | | case SpecialType.System_Double: |
| | 0 | 484 | | case SpecialType.System_Decimal: return "number"; |
| | | 485 | | } |
| | | 486 | | |
| | 9 | 487 | | if (type is IArrayTypeSymbol arrayType) |
| | | 488 | | { |
| | 4 | 489 | | itemType = GetJsonSchemaType(arrayType.ElementType, out _); |
| | 4 | 490 | | if (string.IsNullOrEmpty(itemType)) |
| | 0 | 491 | | itemType = "object"; |
| | 4 | 492 | | return "array"; |
| | | 493 | | } |
| | | 494 | | |
| | 5 | 495 | | if (type is INamedTypeSymbol named && named.IsGenericType && named.TypeArguments.Length == 1) |
| | | 496 | | { |
| | 0 | 497 | | var baseName = named.ConstructedFrom.ToDisplayString(); |
| | 0 | 498 | | if (baseName == "System.Collections.Generic.IEnumerable<T>" || |
| | 0 | 499 | | baseName == "System.Collections.Generic.List<T>" || |
| | 0 | 500 | | baseName == "System.Collections.Generic.IReadOnlyList<T>" || |
| | 0 | 501 | | baseName == "System.Collections.Generic.ICollection<T>" || |
| | 0 | 502 | | baseName == "System.Collections.Generic.IList<T>") |
| | | 503 | | { |
| | 0 | 504 | | itemType = GetJsonSchemaType(named.TypeArguments[0], out _); |
| | 0 | 505 | | if (string.IsNullOrEmpty(itemType)) |
| | 0 | 506 | | itemType = "object"; |
| | 0 | 507 | | return "array"; |
| | | 508 | | } |
| | | 509 | | } |
| | | 510 | | |
| | | 511 | | // Enum types map to string (LLMs pass enum values as strings) |
| | 5 | 512 | | if (type.TypeKind == TypeKind.Enum) |
| | 0 | 513 | | return "string"; |
| | | 514 | | |
| | | 515 | | // Complex types (classes, records, structs) → "object" |
| | 5 | 516 | | if (type.TypeKind == TypeKind.Class || type.TypeKind == TypeKind.Struct) |
| | 5 | 517 | | return "object"; |
| | | 518 | | |
| | 0 | 519 | | return ""; |
| | | 520 | | } |
| | | 521 | | |
| | | 522 | | /// <summary> |
| | | 523 | | /// Builds a JSON schema string for a complex object type's properties. |
| | | 524 | | /// Returns <see langword="null"/> if the type has no public properties or is not a complex type. |
| | | 525 | | /// </summary> |
| | | 526 | | public static string? BuildObjectSchemaJson(ITypeSymbol type) |
| | | 527 | | { |
| | 4 | 528 | | if (type is INamedTypeSymbol nullable && nullable.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T) |
| | 0 | 529 | | type = nullable.TypeArguments[0]; |
| | | 530 | | |
| | | 531 | | // Get public instance properties (including inherited) |
| | 4 | 532 | | var properties = type.GetMembers() |
| | 4 | 533 | | .OfType<IPropertySymbol>() |
| | 9 | 534 | | .Where(p => p.DeclaredAccessibility == Accessibility.Public && |
| | 9 | 535 | | !p.IsStatic && !p.IsIndexer && |
| | 9 | 536 | | p.GetMethod != null) |
| | 4 | 537 | | .ToList(); |
| | | 538 | | |
| | 4 | 539 | | if (properties.Count == 0) |
| | 0 | 540 | | return null; |
| | | 541 | | |
| | 4 | 542 | | var sb = new StringBuilder(); |
| | 4 | 543 | | sb.Append("{\"type\":\"object\",\"properties\":{"); |
| | | 544 | | |
| | 4 | 545 | | var required = new List<string>(); |
| | 4 | 546 | | var first = true; |
| | 26 | 547 | | foreach (var prop in properties) |
| | | 548 | | { |
| | 14 | 549 | | if (!first) sb.Append(","); |
| | 9 | 550 | | first = false; |
| | | 551 | | |
| | | 552 | | // Use camelCase for JSON property names (standard convention) |
| | 9 | 553 | | var propName = char.ToLowerInvariant(prop.Name[0]) + prop.Name.Substring(1); |
| | 9 | 554 | | var propSchemaType = GetJsonSchemaType(prop.Type, out _); |
| | 9 | 555 | | if (string.IsNullOrEmpty(propSchemaType)) |
| | 0 | 556 | | propSchemaType = "string"; // fallback |
| | | 557 | | |
| | | 558 | | // Check for [Description] attribute on the property |
| | 9 | 559 | | string? propDesc = null; |
| | 24 | 560 | | foreach (var attr in prop.GetAttributes()) |
| | | 561 | | { |
| | 6 | 562 | | if (attr.AttributeClass?.Name == "DescriptionAttribute") |
| | | 563 | | { |
| | 6 | 564 | | propDesc = attr.ConstructorArguments.FirstOrDefault().Value?.ToString(); |
| | 6 | 565 | | break; |
| | | 566 | | } |
| | | 567 | | } |
| | | 568 | | |
| | 9 | 569 | | sb.Append($"\"{propName}\":{{\"type\":\"{propSchemaType}\""); |
| | 9 | 570 | | if (!string.IsNullOrEmpty(propDesc)) |
| | | 571 | | { |
| | 6 | 572 | | var escapedDesc = propDesc!.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | 6 | 573 | | sb.Append($",\"description\":\"{escapedDesc}\""); |
| | | 574 | | } |
| | 9 | 575 | | sb.Append("}"); |
| | | 576 | | |
| | | 577 | | // Non-nullable value types and non-nullable reference types are required |
| | 9 | 578 | | if (!prop.Type.IsValueType && prop.NullableAnnotation != NullableAnnotation.Annotated) |
| | 7 | 579 | | required.Add(propName); |
| | 2 | 580 | | else if (prop.Type.IsValueType && prop.NullableAnnotation != NullableAnnotation.Annotated |
| | 2 | 581 | | && prop.Type is INamedTypeSymbol nt && nt.ConstructedFrom.SpecialType != SpecialType.System_Nullabl |
| | 2 | 582 | | required.Add(propName); |
| | | 583 | | } |
| | | 584 | | |
| | 4 | 585 | | sb.Append("}"); |
| | 4 | 586 | | if (required.Count > 0) |
| | | 587 | | { |
| | 4 | 588 | | sb.Append(",\"required\":["); |
| | 13 | 589 | | sb.Append(string.Join(",", required.Select(r => "\"" + r + "\""))); |
| | 4 | 590 | | sb.Append("]"); |
| | | 591 | | } |
| | 4 | 592 | | sb.Append("}"); |
| | | 593 | | |
| | 4 | 594 | | return sb.ToString(); |
| | | 595 | | } |
| | | 596 | | |
| | | 597 | | /// <summary> |
| | | 598 | | /// Builds a list of <see cref="ObjectPropertyInfo"/> for manual property extraction |
| | | 599 | | /// from JsonElement. Used in generated code for AOT-safe deserialization of |
| | | 600 | | /// complex array element types. |
| | | 601 | | /// </summary> |
| | | 602 | | public static IReadOnlyList<ObjectPropertyInfo>? BuildObjectPropertyInfos(ITypeSymbol type) |
| | | 603 | | { |
| | 3 | 604 | | if (type is INamedTypeSymbol nullable && nullable.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T) |
| | 0 | 605 | | type = nullable.TypeArguments[0]; |
| | | 606 | | |
| | 3 | 607 | | var properties = type.GetMembers() |
| | 3 | 608 | | .OfType<IPropertySymbol>() |
| | 6 | 609 | | .Where(p => p.DeclaredAccessibility == Accessibility.Public && |
| | 6 | 610 | | !p.IsStatic && !p.IsIndexer && |
| | 6 | 611 | | p.GetMethod != null && p.SetMethod != null) |
| | 3 | 612 | | .ToList(); |
| | | 613 | | |
| | 3 | 614 | | if (properties.Count == 0) |
| | 0 | 615 | | return null; |
| | | 616 | | |
| | 3 | 617 | | var result = new List<ObjectPropertyInfo>(); |
| | 18 | 618 | | foreach (var prop in properties) |
| | | 619 | | { |
| | 6 | 620 | | var jsonName = char.ToLowerInvariant(prop.Name[0]) + prop.Name.Substring(1); |
| | 6 | 621 | | var schemaType = GetJsonSchemaType(prop.Type, out _); |
| | 6 | 622 | | if (string.IsNullOrEmpty(schemaType)) |
| | 0 | 623 | | schemaType = "string"; |
| | 6 | 624 | | var isNullable = prop.NullableAnnotation == NullableAnnotation.Annotated || |
| | 6 | 625 | | (prop.Type is INamedTypeSymbol pnt && pnt.ConstructedFrom.SpecialType == SpecialType.System_Nullable_T); |
| | 6 | 626 | | result.Add(new ObjectPropertyInfo(prop.Name, jsonName, schemaType, isNullable)); |
| | | 627 | | } |
| | | 628 | | |
| | 3 | 629 | | return result; |
| | | 630 | | } |
| | | 631 | | |
| | | 632 | | public static bool IsAccessibleFromGeneratedCode(INamedTypeSymbol typeSymbol) |
| | | 633 | | { |
| | 4133 | 634 | | if (typeSymbol.DeclaredAccessibility == Accessibility.Private || |
| | 4133 | 635 | | typeSymbol.DeclaredAccessibility == Accessibility.Protected) |
| | 0 | 636 | | return false; |
| | | 637 | | |
| | 4133 | 638 | | var current = typeSymbol.ContainingType; |
| | 4133 | 639 | | while (current != null) |
| | | 640 | | { |
| | 0 | 641 | | if (current.DeclaredAccessibility == Accessibility.Private) |
| | 0 | 642 | | return false; |
| | 0 | 643 | | current = current.ContainingType; |
| | | 644 | | } |
| | | 645 | | |
| | 4133 | 646 | | return true; |
| | | 647 | | } |
| | | 648 | | |
| | | 649 | | public static string GetFullyQualifiedName(ITypeSymbol typeSymbol) => |
| | 309 | 650 | | "global::" + typeSymbol.ToDisplayString( |
| | 309 | 651 | | SymbolDisplayFormat.FullyQualifiedFormat |
| | 309 | 652 | | .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted) |
| | 309 | 653 | | .WithMiscellaneousOptions( |
| | 309 | 654 | | SymbolDisplayFormat.FullyQualifiedFormat.MiscellaneousOptions |
| | 309 | 655 | | & ~SymbolDisplayMiscellaneousOptions.UseSpecialTypes)); |
| | | 656 | | |
| | | 657 | | public static string SanitizeIdentifier(string name) |
| | | 658 | | { |
| | 101 | 659 | | if (string.IsNullOrEmpty(name)) |
| | 0 | 660 | | return "Generated"; |
| | | 661 | | |
| | 101 | 662 | | var sb = new StringBuilder(name.Length); |
| | 2626 | 663 | | foreach (var c in name) |
| | | 664 | | { |
| | 1212 | 665 | | if (char.IsLetterOrDigit(c) || c == '_') |
| | 1212 | 666 | | sb.Append(c); |
| | 0 | 667 | | else if (c == '.' || c == '-' || c == ' ') |
| | 0 | 668 | | sb.Append(c == '.' ? '.' : '_'); |
| | | 669 | | } |
| | | 670 | | |
| | 101 | 671 | | var result = sb.ToString(); |
| | 101 | 672 | | var segments = result.Split('.'); |
| | 404 | 673 | | for (int i = 0; i < segments.Length; i++) |
| | | 674 | | { |
| | 101 | 675 | | if (segments[i].Length > 0 && char.IsDigit(segments[i][0])) |
| | 0 | 676 | | segments[i] = "_" + segments[i]; |
| | | 677 | | } |
| | | 678 | | |
| | 202 | 679 | | return string.Join(".", segments.Where(s => s.Length > 0)); |
| | | 680 | | } |
| | | 681 | | |
| | | 682 | | public static string GetShortName(string fqn) |
| | | 683 | | { |
| | 93 | 684 | | var stripped = fqn.StartsWith("global::", System.StringComparison.Ordinal) ? fqn.Substring(8) : fqn; |
| | 93 | 685 | | var lastDot = stripped.LastIndexOf('.'); |
| | 93 | 686 | | return lastDot >= 0 ? stripped.Substring(lastDot + 1) : stripped; |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | public static string StripAgentSuffix(string className) |
| | | 690 | | { |
| | | 691 | | const string suffix = "Agent"; |
| | 6 | 692 | | return className.EndsWith(suffix, System.StringComparison.Ordinal) && className.Length > suffix.Length |
| | 6 | 693 | | ? className.Substring(0, className.Length - suffix.Length) |
| | 6 | 694 | | : className; |
| | | 695 | | } |
| | | 696 | | |
| | | 697 | | public static string GroupNameToPascalCase(string groupName) |
| | | 698 | | { |
| | 98 | 699 | | var sb = new StringBuilder(); |
| | 98 | 700 | | var capitalizeNext = true; |
| | 1884 | 701 | | foreach (var c in groupName) |
| | | 702 | | { |
| | 844 | 703 | | if (c == '-' || c == '_' || c == ' ') |
| | | 704 | | { |
| | 44 | 705 | | capitalizeNext = true; |
| | | 706 | | } |
| | 800 | 707 | | else if (char.IsLetterOrDigit(c)) |
| | | 708 | | { |
| | 800 | 709 | | sb.Append(capitalizeNext ? char.ToUpperInvariant(c) : c); |
| | 800 | 710 | | capitalizeNext = false; |
| | | 711 | | } |
| | | 712 | | } |
| | 98 | 713 | | return sb.ToString(); |
| | | 714 | | } |
| | | 715 | | } |