| | | 1 | | // Copyright (c) NexusLabs. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Collections.Immutable; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Text; |
| | | 8 | | |
| | | 9 | | namespace NexusLabs.Needlr.AgentFramework.Generators; |
| | | 10 | | |
| | | 11 | | internal static class AIFunctionProviderCodeGenerator |
| | | 12 | | { |
| | | 13 | | public static string GenerateAIFunctionProviderSource( |
| | | 14 | | List<AgentFunctionTypeInfo> types, |
| | | 15 | | string safeAssemblyName) |
| | | 16 | | { |
| | 101 | 17 | | var sb = new StringBuilder(); |
| | 101 | 18 | | sb.AppendLine("// <auto-generated/>"); |
| | 101 | 19 | | sb.AppendLine("#nullable enable"); |
| | 101 | 20 | | sb.AppendLine(); |
| | 101 | 21 | | sb.AppendLine("using global::Microsoft.Extensions.DependencyInjection;"); |
| | 101 | 22 | | sb.AppendLine(); |
| | 101 | 23 | | sb.AppendLine($"namespace {safeAssemblyName}.Generated;"); |
| | 101 | 24 | | sb.AppendLine(); |
| | 101 | 25 | | sb.AppendLine("[global::System.CodeDom.Compiler.GeneratedCodeAttribute(\"NexusLabs.Needlr.AgentFramework.Generat |
| | 101 | 26 | | sb.AppendLine("internal sealed class GeneratedAIFunctionProvider : global::NexusLabs.Needlr.AgentFramework.IAIFu |
| | 101 | 27 | | sb.AppendLine("{"); |
| | 101 | 28 | | sb.AppendLine(" public bool TryGetFunctions("); |
| | 101 | 29 | | sb.AppendLine(" global::System.Type functionType,"); |
| | 101 | 30 | | sb.AppendLine(" global::System.IServiceProvider serviceProvider,"); |
| | 101 | 31 | | sb.AppendLine(" [global::System.Diagnostics.CodeAnalysis.NotNullWhen(true)]"); |
| | 101 | 32 | | sb.AppendLine(" out global::System.Collections.Generic.IReadOnlyList<global::Microsoft.Extensions.AI.AIFu |
| | 101 | 33 | | sb.AppendLine(" {"); |
| | | 34 | | |
| | 101 | 35 | | if (types.Count == 0) |
| | | 36 | | { |
| | 81 | 37 | | sb.AppendLine(" functions = null;"); |
| | 81 | 38 | | sb.AppendLine(" return false;"); |
| | | 39 | | } |
| | | 40 | | else |
| | | 41 | | { |
| | 20 | 42 | | var first = true; |
| | 80 | 43 | | foreach (var type in types) |
| | | 44 | | { |
| | 20 | 45 | | var keyword = first ? "if" : "else if"; |
| | 20 | 46 | | first = false; |
| | 20 | 47 | | sb.AppendLine($" {keyword} (functionType == typeof({type.TypeName}))"); |
| | 20 | 48 | | sb.AppendLine(" {"); |
| | 20 | 49 | | if (!type.IsStatic) |
| | 18 | 50 | | sb.AppendLine($" var typed = serviceProvider.GetRequiredService<{type.TypeName}>();"); |
| | 20 | 51 | | sb.AppendLine(" functions = new global::System.Collections.Generic.List<global::Microsoft.Ext |
| | 20 | 52 | | sb.AppendLine(" {"); |
| | 80 | 53 | | foreach (var m in type.Methods) |
| | | 54 | | { |
| | 20 | 55 | | var nestedName = $"{AgentDiscoveryHelper.GetShortName(type.TypeName)}_{m.MethodName}"; |
| | 20 | 56 | | if (type.IsStatic) |
| | 2 | 57 | | sb.AppendLine($" new {nestedName}(),"); |
| | | 58 | | else |
| | 18 | 59 | | sb.AppendLine($" new {nestedName}(typed),"); |
| | | 60 | | } |
| | 20 | 61 | | sb.AppendLine(" }.AsReadOnly();"); |
| | 20 | 62 | | sb.AppendLine(" return true;"); |
| | 20 | 63 | | sb.AppendLine(" }"); |
| | | 64 | | } |
| | 20 | 65 | | sb.AppendLine(" functions = null;"); |
| | 20 | 66 | | sb.AppendLine(" return false;"); |
| | | 67 | | } |
| | | 68 | | |
| | 101 | 69 | | sb.AppendLine(" }"); |
| | 101 | 70 | | sb.AppendLine(); |
| | | 71 | | |
| | 242 | 72 | | foreach (var type in types) |
| | | 73 | | { |
| | 20 | 74 | | var shortTypeName = AgentDiscoveryHelper.GetShortName(type.TypeName); |
| | 80 | 75 | | foreach (var m in type.Methods) |
| | | 76 | | { |
| | 20 | 77 | | var nestedName = $"{shortTypeName}_{m.MethodName}"; |
| | 20 | 78 | | AppendAIFunctionNestedClass(sb, type, m, nestedName); |
| | | 79 | | } |
| | | 80 | | } |
| | | 81 | | |
| | 101 | 82 | | sb.AppendLine("}"); |
| | 101 | 83 | | return sb.ToString(); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | private static void AppendAIFunctionNestedClass( |
| | | 87 | | StringBuilder sb, |
| | | 88 | | AgentFunctionTypeInfo type, |
| | | 89 | | AgentFunctionMethodInfo method, |
| | | 90 | | string nestedClassName) |
| | | 91 | | { |
| | 20 | 92 | | sb.AppendLine($" private sealed class {nestedClassName} : global::Microsoft.Extensions.AI.AIFunction"); |
| | 20 | 93 | | sb.AppendLine(" {"); |
| | | 94 | | |
| | 20 | 95 | | if (!type.IsStatic) |
| | 18 | 96 | | sb.AppendLine($" private readonly {type.TypeName} _instance;"); |
| | | 97 | | |
| | 20 | 98 | | var schemaJson = BuildJsonSchema(method.Parameters); |
| | 20 | 99 | | sb.AppendLine(" private static readonly global::System.Text.Json.JsonElement _schema ="); |
| | 20 | 100 | | sb.AppendLine($" global::System.Text.Json.JsonDocument.Parse(\"\"\"{schemaJson}\"\"\").RootElement.Cl |
| | | 101 | | |
| | 20 | 102 | | if (!string.IsNullOrEmpty(method.ReturnJsonSchemaType)) |
| | | 103 | | { |
| | 16 | 104 | | var returnSchemaJson = !string.IsNullOrEmpty(method.ReturnObjectSchemaJson) |
| | 16 | 105 | | ? method.ReturnObjectSchemaJson |
| | 16 | 106 | | : $"{{\"type\":\"{method.ReturnJsonSchemaType}\"}}"; |
| | 16 | 107 | | sb.AppendLine(" private static readonly global::System.Text.Json.JsonElement _returnSchema ="); |
| | 16 | 108 | | sb.AppendLine($" global::System.Text.Json.JsonDocument.Parse(\"\"\"{returnSchemaJson}\"\"\").Root |
| | | 109 | | } |
| | | 110 | | |
| | 20 | 111 | | sb.AppendLine(); |
| | | 112 | | |
| | 20 | 113 | | if (!type.IsStatic) |
| | 18 | 114 | | sb.AppendLine($" public {nestedClassName}({type.TypeName} instance) {{ _instance = instance; }}"); |
| | | 115 | | else |
| | 2 | 116 | | sb.AppendLine($" public {nestedClassName}() {{ }}"); |
| | | 117 | | |
| | 20 | 118 | | sb.AppendLine(); |
| | | 119 | | |
| | 20 | 120 | | var escapedName = method.MethodName.Replace("\"", "\\\""); |
| | 20 | 121 | | var escapedDesc = method.Description.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | 20 | 122 | | sb.AppendLine($" public override string Name => \"{escapedName}\";"); |
| | 20 | 123 | | sb.AppendLine($" public override string Description => \"{escapedDesc}\";"); |
| | 20 | 124 | | sb.AppendLine(" public override global::System.Text.Json.JsonElement JsonSchema => _schema;"); |
| | | 125 | | |
| | 20 | 126 | | if (!string.IsNullOrEmpty(method.ReturnJsonSchemaType)) |
| | | 127 | | { |
| | 16 | 128 | | sb.AppendLine(" public override global::System.Text.Json.JsonElement? ReturnJsonSchema => _returnSche |
| | | 129 | | } |
| | | 130 | | |
| | 20 | 131 | | sb.AppendLine(); |
| | | 132 | | |
| | 20 | 133 | | if (method.IsAsync) |
| | 3 | 134 | | sb.AppendLine(" protected override async global::System.Threading.Tasks.ValueTask<object?> InvokeCore |
| | | 135 | | else |
| | 17 | 136 | | sb.AppendLine(" protected override global::System.Threading.Tasks.ValueTask<object?> InvokeCoreAsync( |
| | | 137 | | |
| | 20 | 138 | | sb.AppendLine(" global::Microsoft.Extensions.AI.AIFunctionArguments arguments,"); |
| | 20 | 139 | | sb.AppendLine(" global::System.Threading.CancellationToken ct)"); |
| | 20 | 140 | | sb.AppendLine(" {"); |
| | | 141 | | |
| | 74 | 142 | | foreach (var param in method.Parameters) |
| | | 143 | | { |
| | 17 | 144 | | if (param.IsCancellationToken) |
| | | 145 | | continue; |
| | 16 | 146 | | AppendParameterExtraction(sb, param); |
| | | 147 | | } |
| | | 148 | | |
| | 37 | 149 | | var paramList = string.Join(", ", method.Parameters.Select(p => p.IsCancellationToken ? "ct" : p.Name)); |
| | 20 | 150 | | var instanceExpr = type.IsStatic ? type.TypeName : "_instance"; |
| | | 151 | | |
| | 20 | 152 | | if (method.IsAsync) |
| | | 153 | | { |
| | 3 | 154 | | if (method.IsVoidLike) |
| | | 155 | | { |
| | 1 | 156 | | sb.AppendLine($" await {instanceExpr}.{method.MethodName}({paramList}).ConfigureAwait(false); |
| | 1 | 157 | | sb.AppendLine(" return null;"); |
| | | 158 | | } |
| | | 159 | | else |
| | | 160 | | { |
| | 2 | 161 | | sb.AppendLine($" var result = await {instanceExpr}.{method.MethodName}({paramList}).Configure |
| | 2 | 162 | | sb.AppendLine(" return result;"); |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | else |
| | | 166 | | { |
| | 17 | 167 | | if (method.IsVoidLike) |
| | | 168 | | { |
| | 3 | 169 | | sb.AppendLine($" {instanceExpr}.{method.MethodName}({paramList});"); |
| | 3 | 170 | | sb.AppendLine(" return global::System.Threading.Tasks.ValueTask.FromResult<object?>(null);"); |
| | | 171 | | } |
| | | 172 | | else |
| | | 173 | | { |
| | 14 | 174 | | sb.AppendLine($" var result = {instanceExpr}.{method.MethodName}({paramList});"); |
| | 14 | 175 | | sb.AppendLine(" return global::System.Threading.Tasks.ValueTask.FromResult<object?>(result);" |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | |
| | 20 | 179 | | sb.AppendLine(" }"); |
| | 20 | 180 | | sb.AppendLine(" }"); |
| | 20 | 181 | | sb.AppendLine(); |
| | 20 | 182 | | } |
| | | 183 | | |
| | | 184 | | private static string BuildJsonSchema(ImmutableArray<AgentFunctionParameterInfo> parameters) |
| | | 185 | | { |
| | 37 | 186 | | var nonCtParams = parameters.Where(p => !p.IsCancellationToken).ToList(); |
| | 20 | 187 | | if (nonCtParams.Count == 0) |
| | 6 | 188 | | return "{\"type\":\"object\",\"properties\":{}}"; |
| | | 189 | | |
| | 14 | 190 | | var props = new StringBuilder(); |
| | 14 | 191 | | var required = new List<string>(); |
| | | 192 | | |
| | 14 | 193 | | props.Append("{"); |
| | 14 | 194 | | var firstProp = true; |
| | 60 | 195 | | foreach (var param in nonCtParams) |
| | | 196 | | { |
| | 18 | 197 | | if (!firstProp) props.Append(","); |
| | 16 | 198 | | firstProp = false; |
| | 16 | 199 | | var escapedParamName = param.Name.Replace("\"", "\\\""); |
| | 16 | 200 | | props.Append($"\"{escapedParamName}\":"); |
| | 16 | 201 | | props.Append(BuildJsonSchemaTypeEntry(param)); |
| | 16 | 202 | | if (param.IsRequired) |
| | 15 | 203 | | required.Add(param.Name); |
| | | 204 | | } |
| | 14 | 205 | | props.Append("}"); |
| | | 206 | | |
| | 14 | 207 | | var sb = new StringBuilder(); |
| | 14 | 208 | | sb.Append("{\"type\":\"object\",\"properties\":"); |
| | 14 | 209 | | sb.Append(props); |
| | 14 | 210 | | if (required.Count > 0) |
| | | 211 | | { |
| | 13 | 212 | | sb.Append(",\"required\":["); |
| | 28 | 213 | | sb.Append(string.Join(",", required.Select(r => "\"" + r.Replace("\"", "\\\"") + "\""))); |
| | 13 | 214 | | sb.Append("]"); |
| | | 215 | | } |
| | 14 | 216 | | sb.Append("}"); |
| | 14 | 217 | | return sb.ToString(); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | private static string BuildJsonSchemaTypeEntry(AgentFunctionParameterInfo param) |
| | | 221 | | { |
| | 16 | 222 | | if (string.IsNullOrEmpty(param.JsonSchemaType)) |
| | 0 | 223 | | return "{}"; |
| | | 224 | | |
| | 16 | 225 | | if (param.JsonSchemaType == "array") |
| | | 226 | | { |
| | 4 | 227 | | var desc = string.IsNullOrEmpty(param.Description) |
| | 4 | 228 | | ? "" |
| | 4 | 229 | | : $",\"description\":\"{param.Description!.Replace("\\", "\\\\").Replace("\"", "\\\"")}\""; |
| | | 230 | | |
| | 4 | 231 | | if (param.ItemJsonSchemaType == "object" && !string.IsNullOrEmpty(param.ItemObjectSchemaJson)) |
| | | 232 | | { |
| | | 233 | | // Complex object array items — emit the full object schema |
| | 3 | 234 | | return $"{{\"type\":\"array\",\"items\":{param.ItemObjectSchemaJson}{desc}}}"; |
| | | 235 | | } |
| | | 236 | | |
| | 1 | 237 | | if (!string.IsNullOrEmpty(param.ItemJsonSchemaType)) |
| | 1 | 238 | | return $"{{\"type\":\"array\",\"items\":{{\"type\":\"{param.ItemJsonSchemaType}\"}}{desc}}}"; |
| | | 239 | | |
| | 0 | 240 | | return $"{{\"type\":\"array\"{desc}}}"; |
| | | 241 | | } |
| | | 242 | | |
| | 12 | 243 | | if (param.JsonSchemaType == "object") |
| | | 244 | | { |
| | | 245 | | // Direct complex object parameter (not in an array) |
| | 0 | 246 | | var desc = string.IsNullOrEmpty(param.Description) |
| | 0 | 247 | | ? "" |
| | 0 | 248 | | : $",\"description\":\"{param.Description!.Replace("\\", "\\\\").Replace("\"", "\\\"")}\""; |
| | 0 | 249 | | return $"{{\"type\":\"object\"{desc}}}"; |
| | | 250 | | } |
| | | 251 | | |
| | 12 | 252 | | if (!string.IsNullOrEmpty(param.Description)) |
| | | 253 | | { |
| | 1 | 254 | | var escapedDesc = param.Description!.Replace("\\", "\\\\").Replace("\"", "\\\""); |
| | 1 | 255 | | return $"{{\"type\":\"{param.JsonSchemaType}\",\"description\":\"{escapedDesc}\"}}"; |
| | | 256 | | } |
| | | 257 | | |
| | 11 | 258 | | return $"{{\"type\":\"{param.JsonSchemaType}\"}}"; |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | private static void AppendParameterExtraction(StringBuilder sb, AgentFunctionParameterInfo param) |
| | | 262 | | { |
| | 16 | 263 | | var rawVar = $"_raw_{param.Name}"; |
| | 16 | 264 | | var jVar = $"_j_{param.Name}"; |
| | 16 | 265 | | sb.AppendLine($" arguments.TryGetValue(\"{param.Name}\", out var {rawVar});"); |
| | | 266 | | |
| | 16 | 267 | | switch (param.JsonSchemaType) |
| | | 268 | | { |
| | | 269 | | case "string": |
| | 7 | 270 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? |
| | 7 | 271 | | break; |
| | | 272 | | case "boolean": |
| | | 273 | | { |
| | 0 | 274 | | var bVar = $"_b_{param.Name}"; |
| | 0 | 275 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? |
| | 0 | 276 | | break; |
| | | 277 | | } |
| | | 278 | | case "integer": |
| | 5 | 279 | | AppendIntegerExtraction(sb, param, rawVar, jVar); |
| | 5 | 280 | | break; |
| | | 281 | | case "number": |
| | 0 | 282 | | AppendNumberExtraction(sb, param, rawVar, jVar); |
| | 0 | 283 | | break; |
| | | 284 | | case "array": |
| | | 285 | | { |
| | | 286 | | // AOT-safe array extraction: manually enumerate JsonElement items |
| | | 287 | | // instead of using JsonSerializer.Deserialize<T[]> (which triggers |
| | | 288 | | // IL2026/IL3050 in NativeAOT builds). |
| | 4 | 289 | | var elementType = GetArrayElementType(param.TypeFullName); |
| | 4 | 290 | | sb.AppendLine($" {param.TypeFullName} {param.Name};"); |
| | 4 | 291 | | sb.AppendLine($" if ({rawVar} is global::System.Text.Json.JsonElement {jVar} && {jVar}.ValueK |
| | 4 | 292 | | sb.AppendLine(" {"); |
| | 4 | 293 | | sb.AppendLine($" var _list_{param.Name} = new global::System.Collections.Generic.List<{el |
| | 4 | 294 | | sb.AppendLine($" foreach (var _elem in {jVar}.EnumerateArray())"); |
| | 4 | 295 | | sb.AppendLine(" {"); |
| | | 296 | | |
| | 4 | 297 | | if (param.ItemJsonSchemaType == "object" && param.ItemObjectProperties is { Count: > 0 }) |
| | | 298 | | { |
| | | 299 | | // Complex object: manual property extraction (AOT-safe) |
| | 3 | 300 | | sb.AppendLine($" var _obj = new {elementType}();"); |
| | 18 | 301 | | foreach (var prop in param.ItemObjectProperties) |
| | | 302 | | { |
| | 6 | 303 | | sb.AppendLine($" if (_elem.TryGetProperty(\"{prop.JsonName}\", out var _p_{pr |
| | 6 | 304 | | switch (prop.SchemaType) |
| | | 305 | | { |
| | | 306 | | case "string": |
| | 6 | 307 | | sb.AppendLine($" _obj.{prop.CSharpName} = _p_{prop.JsonName}.GetS |
| | 6 | 308 | | break; |
| | | 309 | | case "integer": |
| | 0 | 310 | | sb.AppendLine($" _obj.{prop.CSharpName} = _p_{prop.JsonName}.GetI |
| | 0 | 311 | | break; |
| | | 312 | | case "number": |
| | 0 | 313 | | sb.AppendLine($" _obj.{prop.CSharpName} = _p_{prop.JsonName}.GetD |
| | 0 | 314 | | break; |
| | | 315 | | case "boolean": |
| | 0 | 316 | | sb.AppendLine($" _obj.{prop.CSharpName} = _p_{prop.JsonName}.GetB |
| | 0 | 317 | | break; |
| | | 318 | | default: |
| | 0 | 319 | | sb.AppendLine($" _obj.{prop.CSharpName} = _p_{prop.JsonName}.GetS |
| | | 320 | | break; |
| | | 321 | | } |
| | | 322 | | } |
| | 3 | 323 | | sb.AppendLine($" _list_{param.Name}.Add(_obj);"); |
| | | 324 | | } |
| | 1 | 325 | | else if (param.ItemJsonSchemaType == "string") |
| | 1 | 326 | | sb.AppendLine($" _list_{param.Name}.Add(_elem.GetString() ?? \"\");"); |
| | 0 | 327 | | else if (param.ItemJsonSchemaType == "integer") |
| | 0 | 328 | | sb.AppendLine($" _list_{param.Name}.Add(_elem.GetInt32());"); |
| | 0 | 329 | | else if (param.ItemJsonSchemaType == "number") |
| | 0 | 330 | | sb.AppendLine($" _list_{param.Name}.Add(_elem.GetDouble());"); |
| | 0 | 331 | | else if (param.ItemJsonSchemaType == "boolean") |
| | 0 | 332 | | sb.AppendLine($" _list_{param.Name}.Add(_elem.GetBoolean());"); |
| | | 333 | | else |
| | 0 | 334 | | sb.AppendLine($" _list_{param.Name}.Add(default!);"); |
| | | 335 | | |
| | 4 | 336 | | sb.AppendLine(" }"); |
| | 4 | 337 | | sb.AppendLine($" {param.Name} = _list_{param.Name}.ToArray();"); |
| | 4 | 338 | | sb.AppendLine(" }"); |
| | 4 | 339 | | sb.AppendLine($" else {{ {param.Name} = {rawVar} as {param.TypeFullName} ?? global::System.Ar |
| | 4 | 340 | | break; |
| | | 341 | | } |
| | | 342 | | case "object": |
| | | 343 | | { |
| | 0 | 344 | | var cVar = $"_c_{param.Name}"; |
| | 0 | 345 | | var nullSuppress = param.IsNullable ? "" : "!"; |
| | 0 | 346 | | sb.AppendLine($" var {param.Name} = {rawVar} is {param.TypeFullName} {cVar} ? {cVar} : defaul |
| | 0 | 347 | | break; |
| | | 348 | | } |
| | | 349 | | default: |
| | | 350 | | { |
| | 0 | 351 | | var cVar = $"_c_{param.Name}"; |
| | 0 | 352 | | var nullSuppress = param.IsNullable ? "" : "!"; |
| | 0 | 353 | | sb.AppendLine($" var {param.Name} = {rawVar} is {param.TypeFullName} {cVar} ? {cVar} : defaul |
| | | 354 | | break; |
| | | 355 | | } |
| | | 356 | | } |
| | 0 | 357 | | } |
| | | 358 | | |
| | | 359 | | private static void AppendIntegerExtraction(StringBuilder sb, AgentFunctionParameterInfo param, string rawVar, strin |
| | | 360 | | { |
| | 5 | 361 | | var typeFqn = param.TypeFullName; |
| | | 362 | | string getMethod, castType, convertMethod; |
| | 5 | 363 | | var iVar = $"_i_{param.Name}"; |
| | | 364 | | |
| | 5 | 365 | | if (typeFqn.Contains("System.Int64")) |
| | 0 | 366 | | { getMethod = "GetInt64()"; castType = "long"; convertMethod = "ToInt64"; } |
| | 5 | 367 | | else if (typeFqn.Contains("System.Int16")) |
| | 0 | 368 | | { getMethod = "GetInt16()"; castType = "short"; convertMethod = "ToInt16"; } |
| | 5 | 369 | | else if (typeFqn.Contains("System.SByte")) |
| | 0 | 370 | | { getMethod = "GetSByte()"; castType = "sbyte"; convertMethod = "ToSByte"; } |
| | 5 | 371 | | else if (typeFqn.Contains("System.Byte")) |
| | 0 | 372 | | { getMethod = "GetByte()"; castType = "byte"; convertMethod = "ToByte"; } |
| | 5 | 373 | | else if (typeFqn.Contains("System.UInt64")) |
| | 0 | 374 | | { getMethod = "GetUInt64()"; castType = "ulong"; convertMethod = "ToUInt64"; } |
| | 5 | 375 | | else if (typeFqn.Contains("System.UInt32")) |
| | 0 | 376 | | { getMethod = "GetUInt32()"; castType = "uint"; convertMethod = "ToUInt32"; } |
| | 5 | 377 | | else if (typeFqn.Contains("System.UInt16")) |
| | 0 | 378 | | { getMethod = "GetUInt16()"; castType = "ushort"; convertMethod = "ToUInt16"; } |
| | | 379 | | else |
| | 15 | 380 | | { getMethod = "GetInt32()"; castType = "int"; convertMethod = "ToInt32"; } |
| | | 381 | | |
| | 5 | 382 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? {jVar}. |
| | 5 | 383 | | } |
| | | 384 | | |
| | | 385 | | private static void AppendNumberExtraction(StringBuilder sb, AgentFunctionParameterInfo param, string rawVar, string |
| | | 386 | | { |
| | 0 | 387 | | var typeFqn = param.TypeFullName; |
| | | 388 | | string getMethod, castType, convertMethod; |
| | 0 | 389 | | var nVar = $"_n_{param.Name}"; |
| | | 390 | | |
| | 0 | 391 | | if (typeFqn.Contains("System.Single")) |
| | 0 | 392 | | { getMethod = "GetSingle()"; castType = "float"; convertMethod = "ToSingle"; } |
| | 0 | 393 | | else if (typeFqn.Contains("System.Decimal")) |
| | 0 | 394 | | { getMethod = "GetDecimal()"; castType = "decimal"; convertMethod = "ToDecimal"; } |
| | | 395 | | else |
| | 0 | 396 | | { getMethod = "GetDouble()"; castType = "double"; convertMethod = "ToDouble"; } |
| | | 397 | | |
| | 0 | 398 | | sb.AppendLine($" var {param.Name} = {rawVar} is global::System.Text.Json.JsonElement {jVar} ? {jVar}. |
| | 0 | 399 | | } |
| | | 400 | | |
| | | 401 | | /// <summary> |
| | | 402 | | /// Extracts the element type from an array type's fully-qualified name. |
| | | 403 | | /// E.g., <c>"global::MyNamespace.FeedbackEntry[]"</c> → <c>"global::MyNamespace.FeedbackEntry"</c>. |
| | | 404 | | /// </summary> |
| | | 405 | | private static string GetArrayElementType(string arrayTypeFullName) |
| | | 406 | | { |
| | | 407 | | // Handle T[] syntax |
| | 4 | 408 | | if (arrayTypeFullName.EndsWith("[]")) |
| | 4 | 409 | | return arrayTypeFullName.Substring(0, arrayTypeFullName.Length - 2); |
| | | 410 | | |
| | | 411 | | // Fallback: return as-is (shouldn't happen for valid array types) |
| | 0 | 412 | | return arrayTypeFullName; |
| | | 413 | | } |
| | | 414 | | } |