Skip to content

A programming framework for agentic AI

[.Net] Fix #2687 by adding global:: keyword in generated code (#2689)

Xiaoyun Zhange1afff20f643
diff --git a/dotnet/src/AutoGen.SourceGenerator/AutoGen.SourceGenerator.csproj b/dotnet/src/AutoGen.SourceGenerator/AutoGen.SourceGenerator.csproj
index 455816072..37f344ed1 100644
--- a/dotnet/src/AutoGen.SourceGenerator/AutoGen.SourceGenerator.csproj
+++ b/dotnet/src/AutoGen.SourceGenerator/AutoGen.SourceGenerator.csproj
@@ -14,7 +14,7 @@
</PropertyGroup>
<Import Project="$(RepoRoot)/nuget/nuget-package.props" />
-
+
<PropertyGroup>
<Title>AutoGen.SourceGenerator</Title>
@@ -50,6 +50,10 @@
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>
+ <ItemGroup>
+ <InternalsVisibleTo Include="AutoGen.SourceGenerator.Tests"/>
+ </ItemGroup>
+
<ItemGroup>
<Compile Update="Template\FunctionCallTemplate.cs">
<DesignTime>True</DesignTime>
diff --git a/dotnet/src/AutoGen.SourceGenerator/FunctionCallGenerator.cs b/dotnet/src/AutoGen.SourceGenerator/FunctionCallGenerator.cs
index 50bdc03f0..cd0141618 100644
--- a/dotnet/src/AutoGen.SourceGenerator/FunctionCallGenerator.cs
+++ b/dotnet/src/AutoGen.SourceGenerator/FunctionCallGenerator.cs
@@ -144,7 +144,7 @@ namespace AutoGen.SourceGenerator
private class PartialClassOutput
{
- public PartialClassOutput(string fullClassName, ClassDeclarationSyntax classDeclarationSyntax, IEnumerable<FunctionContract> functionContracts)
+ public PartialClassOutput(string fullClassName, ClassDeclarationSyntax classDeclarationSyntax, IEnumerable<SourceGeneratorFunctionContract> functionContracts)
{
FullClassName = fullClassName;
ClassDeclarationSyntax = classDeclarationSyntax;
@@ -155,10 +155,10 @@ namespace AutoGen.SourceGenerator
public ClassDeclarationSyntax ClassDeclarationSyntax { get; }
- public IEnumerable<FunctionContract> FunctionContracts { get; }
+ public IEnumerable<SourceGeneratorFunctionContract> FunctionContracts { get; }
}
- private FunctionContract CreateFunctionContract(MethodDeclarationSyntax method, string? className, string? namespaceName)
+ private SourceGeneratorFunctionContract CreateFunctionContract(MethodDeclarationSyntax method, string? className, string? namespaceName)
{
// get function_call attribute
var functionCallAttribute = method.AttributeLists.SelectMany(attributeList => attributeList.Attributes)
@@ -208,7 +208,7 @@ namespace AutoGen.SourceGenerator
description = System.Text.RegularExpressions.Regex.Replace(description, @"[^\S\r\n]+\/[\/]+\s*", string.Empty);
}
var jsonItemType = parameter.Type!.ToString().EndsWith("[]") ? parameter.Type!.ToString().Substring(0, parameter.Type!.ToString().Length - 2) : null;
- return new ParameterContract
+ return new SourceGeneratorParameterContract
{
Name = parameter.Identifier.ToString(),
JsonType = parameter.Type!.ToString() switch
@@ -234,7 +234,7 @@ namespace AutoGen.SourceGenerator
};
});
- return new FunctionContract
+ return new SourceGeneratorFunctionContract
{
ClassName = className,
Namespace = namespaceName,
diff --git a/dotnet/src/AutoGen.SourceGenerator/FunctionExtension.cs b/dotnet/src/AutoGen.SourceGenerator/FunctionExtension.cs
index a56e4cb54..cfb77d26a 100644
--- a/dotnet/src/AutoGen.SourceGenerator/FunctionExtension.cs
+++ b/dotnet/src/AutoGen.SourceGenerator/FunctionExtension.cs
@@ -5,27 +5,27 @@ using AutoGen.SourceGenerator;
internal static class FunctionExtension
{
- public static string GetFunctionName(this FunctionContract function)
+ public static string GetFunctionName(this SourceGeneratorFunctionContract function)
{
return function.Name ?? string.Empty;
}
- public static string GetFunctionSchemaClassName(this FunctionContract function)
+ public static string GetFunctionSchemaClassName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}Schema";
}
- public static string GetFunctionDefinitionName(this FunctionContract function)
+ public static string GetFunctionDefinitionName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}Function";
}
- public static string GetFunctionWrapperName(this FunctionContract function)
+ public static string GetFunctionWrapperName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}Wrapper";
}
- public static string GetFunctionContractName(this FunctionContract function)
+ public static string GetFunctionContractName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}FunctionContract";
}
diff --git a/dotnet/src/AutoGen.SourceGenerator/FunctionContract.cs b/dotnet/src/AutoGen.SourceGenerator/SourceGeneratorFunctionContract.cs
similarity index 81%
rename from dotnet/src/AutoGen.SourceGenerator/FunctionContract.cs
rename to dotnet/src/AutoGen.SourceGenerator/SourceGeneratorFunctionContract.cs
index 2f2635217..24e42affa 100644
--- a/dotnet/src/AutoGen.SourceGenerator/FunctionContract.cs
+++ b/dotnet/src/AutoGen.SourceGenerator/SourceGeneratorFunctionContract.cs
@@ -3,7 +3,7 @@
namespace AutoGen.SourceGenerator
{
- internal class FunctionContract
+ internal class SourceGeneratorFunctionContract
{
public string? Namespace { get; set; }
@@ -15,12 +15,12 @@ namespace AutoGen.SourceGenerator
public string? ReturnDescription { get; set; }
- public ParameterContract[]? Parameters { get; set; }
+ public SourceGeneratorParameterContract[]? Parameters { get; set; }
public string? ReturnType { get; set; }
}
- internal class ParameterContract
+ internal class SourceGeneratorParameterContract
{
public string? Name { get; set; }
diff --git a/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.cs b/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.cs
index 1d455bd30..e56db112e 100644
--- a/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.cs
+++ b/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.cs
@@ -31,7 +31,6 @@ namespace AutoGen.SourceGenerator.Template
// This code was generated by a tool.
// </auto-generated>
//----------------------
-using Azure.AI.OpenAI;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
@@ -152,7 +151,8 @@ if (parameter.DefaultValue != null) {
}
this.Write(" },\r\n");
}
- this.Write(" };\r\n }\r\n\r\n public Azure.AI.OpenAI.FunctionDefinition ");
+ this.Write(" };\r\n }\r\n\r\n public global::Azure.AI.OpenAI.FunctionDefin" +
+ "ition ");
this.Write(this.ToStringHelper.ToStringWithCulture(functionContract.GetFunctionDefinitionName()));
this.Write("\r\n {\r\n get => this.");
this.Write(this.ToStringHelper.ToStringWithCulture(functionContract.GetFunctionContractName()));
@@ -168,7 +168,7 @@ if (!String.IsNullOrEmpty(NameSpace)) {
public string NameSpace {get; set;}
public string ClassName {get; set;}
-public IEnumerable<FunctionContract> FunctionContracts {get; set;}
+public IEnumerable<SourceGeneratorFunctionContract> FunctionContracts {get; set;}
public bool IsStatic {get; set;} = false;
}
diff --git a/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.tt b/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.tt
index baa2a680f..526dfe400 100644
--- a/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.tt
+++ b/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.tt
@@ -8,7 +8,6 @@
// This code was generated by a tool.
// </auto-generated>
//----------------------
-using Azure.AI.OpenAI;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
@@ -98,7 +97,7 @@ namespace <#=NameSpace#>
};
}
- public Azure.AI.OpenAI.FunctionDefinition <#=functionContract.GetFunctionDefinitionName()#>
+ public global::Azure.AI.OpenAI.FunctionDefinition <#=functionContract.GetFunctionDefinitionName()#>
{
get => this.<#=functionContract.GetFunctionContractName()#>.ToOpenAIFunctionDefinition();
}
diff --git a/dotnet/test/AutoGen.SourceGenerator.Tests/ApprovalTests/FunctionCallTemplateTests.TestFunctionCallTemplate.approved.txt b/dotnet/test/AutoGen.SourceGenerator.Tests/ApprovalTests/FunctionCallTemplateTests.TestFunctionCallTemplate.approved.txt
new file mode 100644
index 000000000..feab4ebd6
--- /dev/null
+++ b/dotnet/test/AutoGen.SourceGenerator.Tests/ApprovalTests/FunctionCallTemplateTests.TestFunctionCallTemplate.approved.txt
@@ -0,0 +1,71 @@
+//----------------------
+// <auto-generated>
+// This code was generated by a tool.
+// </auto-generated>
+//----------------------
+using System.Text.Json;
+using System.Text.Json.Serialization;
+using System.Threading.Tasks;
+using System;
+using AutoGen.Core;
+using AutoGen.OpenAI.Extension;
+
+namespace AutoGen.SourceGenerator.Tests
+{
+ public partial class FunctionExamples
+ {
+
+ private class AddAsyncSchema
+ {
+ [JsonPropertyName(@"a")]
+ public System.Int32 a {get; set;}
+ [JsonPropertyName(@"b")]
+ public System.Int32 b {get; set;}
+ }
+
+ public System.Threading.Tasks.Task`1[System.String] AddAsyncWrapper(string arguments)
+ {
+ var schema = JsonSerializer.Deserialize<AddAsyncSchema>(
+ arguments,
+ new JsonSerializerOptions
+ {
+ PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
+ });
+
+ return AddAsync(schema.a, schema.b);
+ }
+
+ public FunctionContract AddAsyncFunctionContract
+ {
+ get => new FunctionContract
+ {
+ Name = @"AddAsync",
+ Description = @"Add two numbers.",
+ ReturnType = typeof(System.Threading.Tasks.Task`1[System.String]),
+ Parameters = new []
+ {
+ new FunctionParameterContract
+ {
+ Name = @"a",
+ Description = @"The first number.",
+ ParameterType = typeof(System.Int32),
+ IsRequired = true,
+ },
+ new FunctionParameterContract
+ {
+ Name = @"b",
+ Description = @"The second number.",
+ ParameterType = typeof(System.Int32),
+ IsRequired = true,
+ },
+ },
+ };
+ }
+
+ public global::Azure.AI.OpenAI.FunctionDefinition AddAsyncFunction
+ {
+ get => this.AddAsyncFunctionContract.ToOpenAIFunctionDefinition();
+ }
+ }
+}
+
diff --git a/dotnet/test/AutoGen.SourceGenerator.Tests/AutoGen.SourceGenerator.Tests.csproj b/dotnet/test/AutoGen.SourceGenerator.Tests/AutoGen.SourceGenerator.Tests.csproj
index c4b2a8aa8..0d0d91e05 100644
--- a/dotnet/test/AutoGen.SourceGenerator.Tests/AutoGen.SourceGenerator.Tests.csproj
+++ b/dotnet/test/AutoGen.SourceGenerator.Tests/AutoGen.SourceGenerator.Tests.csproj
@@ -17,7 +17,7 @@
</ItemGroup>
<ItemGroup>
- <ProjectReference Include="..\..\src\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
+ <ProjectReference Include="..\..\src\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\..\src\AutoGen\AutoGen.csproj" />
</ItemGroup>
diff --git a/dotnet/test/AutoGen.SourceGenerator.Tests/FunctionCallTemplateTests.cs b/dotnet/test/AutoGen.SourceGenerator.Tests/FunctionCallTemplateTests.cs
new file mode 100644
index 000000000..3c1e6c8ed
--- /dev/null
+++ b/dotnet/test/AutoGen.SourceGenerator.Tests/FunctionCallTemplateTests.cs
@@ -0,0 +1,46 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// FunctionCallTemplateTests.cs
+
+using ApprovalTests;
+using ApprovalTests.Namers;
+using ApprovalTests.Reporters;
+using AutoGen.SourceGenerator.Template;
+using Xunit;
+
+namespace AutoGen.SourceGenerator.Tests;
+
+public class FunctionCallTemplateTests
+{
+ [Fact]
+ [UseReporter(typeof(DiffReporter))]
+ [UseApprovalSubdirectory("ApprovalTests")]
+ public void TestFunctionCallTemplate()
+ {
+ var functionExample = new FunctionExamples();
+ var function = functionExample.AddAsyncFunctionContract;
+ var functionCallTemplate = new FunctionCallTemplate()
+ {
+ ClassName = function.ClassName,
+ NameSpace = function.Namespace,
+ FunctionContracts = [new SourceGeneratorFunctionContract()
+ {
+ Name = function.Name,
+ Description = function.Description,
+ ReturnType = function.ReturnType!.ToString(),
+ ReturnDescription = function.ReturnDescription,
+ Parameters = function.Parameters!.Select(p => new SourceGeneratorParameterContract()
+ {
+ Name = p.Name,
+ Description = p.Description,
+ Type = p.ParameterType!.ToString(),
+ IsOptional = !p.IsRequired,
+ JsonType = p.ParameterType!.ToString(),
+ }).ToArray()
+ }]
+ };
+
+ var actual = functionCallTemplate.TransformText();
+
+ Approvals.Verify(actual);
+ }
+}