microsoft / autogen

A programming framework for agentic AI. Discord: https://aka.ms/autogen-dc. Roadmap: https://aka.ms/autogen-roadmap
https://microsoft.github.io/autogen/
Creative Commons Attribution 4.0 International
28.12k stars 4.1k forks source link

[.Net][Bug]: AutoGen.SourceGenerator doesn't encode `"` in structural comments #2872

Open LittleLittleCloud opened 4 weeks ago

LittleLittleCloud commented 4 weeks ago

Describe the bug

When the content in structural comments contains ", the generated code won't get compiled because the " breaks the string.

Steps to reproduce

No response

Model Used

No response

Expected Behavior

No response

Screenshots and logs

No response

Additional Information

No response

prithvi2226 commented 4 weeks ago

Hi @LittleLittleCloud! This task seems to be interesting! Can I work on this task?

LittleLittleCloud commented 3 weeks ago

@prithvi2226 that would be awesome!

prithvi2226 commented 3 weeks ago

@LittleLittleCloud If possible, could you please assign this task to me? However, if this action doesn't make a significant difference, I am fine with it. The main priority is to ensure the issue is resolved and the repository progresses smoothly.

LittleLittleCloud commented 2 weeks ago

@prithvi2226 Any update on this issue

prithvi2226 commented 1 week ago

@prithvi2226 Any update on this issue

Hey @LittleLittleCloud , I am still working on it, been remote for a while, I can give you an update by Tuesday!? If that's okay?

LittleLittleCloud commented 5 days ago

@prithvi2226 Do you need more time on this one? Also please let me know if you need help!

prithvi2226 commented 4 days ago

Hi @LittleLittleCloud! Thank you very much for checking up on me, and thank you very much for taking the time to help me out with this. I am very sorry from my side, for not giving an update, to solve this bug.

Please Correct me if I am wrong, I have decided to work on the file dotnet/src/AutoGen.SourceGenerator/DocumentCommentExtension.cs, and DocumentCommentExtension class contains methods for handling documentation comments, and it's where I am going to add the logic to escape double quotes in the comments. And, in the method 'GetParameterDescriptionFromDocumentationCommentTriviaSyntax' looks like it processes descriptions for parameters from documentation comments. I hope I am on the right track! Thank you very much!

LittleLittleCloud commented 3 days ago

@prithvi2226 Thanks for your response.

IMO you don't need to go that further into DocumentCommentExtension.cs, the quickest solution is to encode " properly in https://github.com/microsoft/autogen/blob/80ecbf900ca9c2bc7db519702ae132ec6b6c6a3f/dotnet/src/AutoGen.SourceGenerator/Template/FunctionCallTemplate.tt#L84. (" -> "")

For example, when the comment contains "

/// <summary>
/// I have " in the summary and the generated code is breaking!
/// </summary>
/// <param name="city"></param>
/// <returns></returns>
[Function]
public async Task<string> GetWeatherAsync(string city)
{
    return await Task.FromResult($"The weather in {city} is sunny.");
}

The generated code is image

As you can see, " breaks the generated code because FunctionTemplate.tt encodes description using @"...." and didn't process the " in the comment description.

The quickest fix is to encode " in description by doubling it (""). This is how " is encoded as well in verbatim string.

e.g. image

prithvi2226 commented 3 days ago

Hi @LittleLittleCloud ,

Thank you very much for your guidance. Based on your suggestion, I will implement the fix directly in the FunctionCallTemplate.tt file. Here are the steps I plan to take:

Planned Changes:

  1. Modify the FunctionCallTemplate.tt File:

Locating the line that assigns the description (around line 84):

Description = @"<#=parameter.Description#>",

  1. I will modify this line to properly encode double quotes by doubling them (""), like so:

    Description = @"<#= parameter.Description.Replace("\"", "\"\"") #>",

This approach should correctly handle double quotes in comments and prevent the generated code from breaking.

Please let me know if this solution looks good to you, and I will proceed with making these changes.

Thank you once again for your assistance!

Best regards, Prithvi

LittleLittleCloud commented 2 days ago

You nailed it!

prithvi2226 commented 2 days ago

Hi @LittleLittleCloud ! I have created a pull request, let me know what you think about it. Thank you very much for your guidance and support. I would love to connect with you on Github or Slack to continue supporting this project.