-
Notifications
You must be signed in to change notification settings - Fork 2.2k
.NET: Align file tool descriptions with generated schema argument names #7675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Ruiming Zhao (uuzzrm)
wants to merge
1
commit into
microsoft:main
from
uuzzrm:fix/align-harness-description-schema-names
+255
−5
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
.../Microsoft.Agents.AI.UnitTests/Harness/FileAccess/FileAccessToolDescriptionSchemaTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Moq; | ||
|
|
||
| namespace Microsoft.Agents.AI.UnitTests.Harness.FileAccess; | ||
|
|
||
| /// <summary> | ||
| /// Verifies that each file access tool's description refers to arguments by the same | ||
| /// names that <see cref="AIFunctionFactory"/> exposes in the generated JSON schema. | ||
| /// </summary> | ||
| public class FileAccessToolDescriptionSchemaTests | ||
| { | ||
| [Fact] | ||
| public async Task LsTool_Description_UsesSchemaArgumentNameAsync() | ||
| { | ||
| // Arrange | ||
| AIFunction tool = await GetToolAsync(FileAccessProvider.LsToolName); | ||
|
|
||
| // Act | ||
| List<string> schemaArguments = GetSchemaArgumentNames(tool); | ||
|
|
||
| // Assert — the description and the generated schema agree on the filter argument name. | ||
| Assert.Contains("globPattern", tool.Description); | ||
| Assert.DoesNotContain("glob_pattern", tool.Description); | ||
| Assert.Contains("globPattern", schemaArguments); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReplaceTool_Description_UsesSchemaArgumentNamesAsync() | ||
| { | ||
| // Arrange | ||
| AIFunction tool = await GetToolAsync(FileAccessProvider.ReplaceToolName); | ||
|
|
||
| // Act | ||
| List<string> schemaArguments = GetSchemaArgumentNames(tool); | ||
|
|
||
| // Assert — the description and the generated schema agree on the replace argument names. | ||
| Assert.Contains("oldString", tool.Description); | ||
| Assert.Contains("newString", tool.Description); | ||
| Assert.Contains("replaceAll", tool.Description); | ||
| Assert.DoesNotContain("old_string", tool.Description); | ||
| Assert.DoesNotContain("new_string", tool.Description); | ||
| Assert.DoesNotContain("replace_all", tool.Description); | ||
| Assert.Contains("oldString", schemaArguments); | ||
| Assert.Contains("newString", schemaArguments); | ||
| Assert.Contains("replaceAll", schemaArguments); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReplaceLinesTool_Description_KeepsExplicitJsonPropertyNamesAsync() | ||
| { | ||
| // Arrange | ||
| AIFunction tool = await GetToolAsync(FileAccessProvider.ReplaceLinesToolName); | ||
|
|
||
| // Act | ||
| List<string> schemaArguments = GetSchemaArgumentNames(tool); | ||
|
|
||
| // Assert — line_number/new_line are mapped explicitly via JsonPropertyName, so the | ||
| // description must keep referencing them as-is instead of a camelCase rename. | ||
| Assert.Contains("line_number", tool.Description); | ||
| Assert.Contains("new_line", tool.Description); | ||
| Assert.Contains("line_number", schemaArguments); | ||
| Assert.Contains("new_line", schemaArguments); | ||
| } | ||
|
|
||
| private static async Task<AIFunction> GetToolAsync(string toolName) | ||
| { | ||
| // Arrange | ||
| var provider = new FileAccessProvider(new InMemoryAgentFileStore()); | ||
| var agent = new Mock<AIAgent>().Object; | ||
| var session = new ChatClientAgentSession(); | ||
| #pragma warning disable MAAI001 | ||
| var context = new AIContextProvider.InvokingContext(agent, session, new AIContext()); | ||
| #pragma warning restore MAAI001 | ||
|
|
||
| AIContext result = await provider.InvokingAsync(context); | ||
| return (AIFunction)result.Tools!.First(t => t is AIFunction f && f.Name == toolName); | ||
|
|
||
| } | ||
|
|
||
| private static List<string> GetSchemaArgumentNames(AIFunction tool) | ||
| { | ||
| var names = new List<string>(); | ||
| if (tool.JsonSchema is JsonElement schema) | ||
| { | ||
| CollectPropertyNames(schema, names); | ||
| } | ||
|
|
||
| return names.Distinct().ToList(); | ||
| } | ||
|
|
||
| private static void CollectPropertyNames(JsonElement element, List<string> names) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.Object) | ||
| { | ||
| foreach (JsonProperty property in element.EnumerateObject()) | ||
| { | ||
| if (property.Name == "properties") | ||
| { | ||
| foreach (JsonProperty item in property.Value.EnumerateObject()) | ||
| { | ||
| names.Add(item.Name); | ||
| CollectPropertyNames(item.Value, names); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| CollectPropertyNames(property.Value, names); | ||
| } | ||
| } | ||
| } | ||
| else if (element.ValueKind == JsonValueKind.Array) | ||
| { | ||
| foreach (JsonElement item in element.EnumerateArray()) | ||
| { | ||
| CollectPropertyNames(item, names); | ||
| } | ||
| } | ||
|
Comment on lines
+87
to
+122
|
||
| } | ||
|
Comment on lines
+71
to
+123
|
||
| } | ||
126 changes: 126 additions & 0 deletions
126
.../Microsoft.Agents.AI.UnitTests/Harness/FileMemory/FileMemoryToolDescriptionSchemaTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text.Json; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
| using Moq; | ||
|
|
||
| namespace Microsoft.Agents.AI.UnitTests.Harness.FileMemory; | ||
|
|
||
| /// <summary> | ||
| /// Verifies that each file memory tool's description refers to arguments by the same | ||
| /// names that <see cref="AIFunctionFactory"/> exposes in the generated JSON schema. | ||
| /// </summary> | ||
| public class FileMemoryToolDescriptionSchemaTests | ||
| { | ||
| [Theory] | ||
| [InlineData(FileMemoryProvider.LsToolName)] | ||
| [InlineData(FileMemoryProvider.GrepToolName)] | ||
| public async Task FilteringTool_Description_UsesSchemaArgumentNameAsync(string toolName) | ||
| { | ||
| // Arrange | ||
| AIFunction tool = await GetToolAsync(toolName); | ||
|
|
||
| // Act | ||
| List<string> schemaArguments = GetSchemaArgumentNames(tool); | ||
|
|
||
| // Assert — the description and the generated schema agree on the filter argument name. | ||
| Assert.Contains("globPattern", tool.Description); | ||
| Assert.DoesNotContain("glob_pattern", tool.Description); | ||
| Assert.Contains("globPattern", schemaArguments); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReplaceTool_Description_UsesSchemaArgumentNamesAsync() | ||
| { | ||
| // Arrange | ||
| AIFunction tool = await GetToolAsync(FileMemoryProvider.ReplaceToolName); | ||
|
|
||
| // Act | ||
| List<string> schemaArguments = GetSchemaArgumentNames(tool); | ||
|
|
||
| // Assert — the description and the generated schema agree on the replace argument names. | ||
| Assert.Contains("oldString", tool.Description); | ||
| Assert.Contains("newString", tool.Description); | ||
| Assert.Contains("replaceAll", tool.Description); | ||
| Assert.DoesNotContain("old_string", tool.Description); | ||
| Assert.DoesNotContain("new_string", tool.Description); | ||
| Assert.DoesNotContain("replace_all", tool.Description); | ||
| Assert.Contains("oldString", schemaArguments); | ||
| Assert.Contains("newString", schemaArguments); | ||
| Assert.Contains("replaceAll", schemaArguments); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task ReplaceLinesTool_Description_KeepsExplicitJsonPropertyNamesAsync() | ||
| { | ||
| // Arrange | ||
| AIFunction tool = await GetToolAsync(FileMemoryProvider.ReplaceLinesToolName); | ||
|
|
||
| // Act | ||
| List<string> schemaArguments = GetSchemaArgumentNames(tool); | ||
|
|
||
| // Assert — line_number/new_line are mapped explicitly via JsonPropertyName, so the | ||
| // description must keep referencing them as-is instead of a camelCase rename. | ||
| Assert.Contains("line_number", tool.Description); | ||
| Assert.Contains("new_line", tool.Description); | ||
| Assert.Contains("line_number", schemaArguments); | ||
| Assert.Contains("new_line", schemaArguments); | ||
| } | ||
|
|
||
| private static async Task<AIFunction> GetToolAsync(string toolName) | ||
| { | ||
| // Arrange | ||
| var provider = new FileMemoryProvider(new InMemoryAgentFileStore()); | ||
| var agent = new Mock<AIAgent>().Object; | ||
| var session = new ChatClientAgentSession(); | ||
| #pragma warning disable MAAI001 | ||
| var context = new AIContextProvider.InvokingContext(agent, session, new AIContext()); | ||
| #pragma warning restore MAAI001 | ||
|
|
||
| AIContext result = await provider.InvokingAsync(context); | ||
| return (AIFunction)result.Tools!.First(t => t is AIFunction f && f.Name == toolName); | ||
| } | ||
|
|
||
| private static List<string> GetSchemaArgumentNames(AIFunction tool) | ||
| { | ||
| var names = new List<string>(); | ||
| if (tool.JsonSchema is JsonElement schema) | ||
| { | ||
| CollectPropertyNames(schema, names); | ||
| } | ||
|
|
||
| return names.Distinct().ToList(); | ||
| } | ||
|
|
||
| private static void CollectPropertyNames(JsonElement element, List<string> names) | ||
| { | ||
| if (element.ValueKind == JsonValueKind.Object) | ||
| { | ||
| foreach (JsonProperty property in element.EnumerateObject()) | ||
| { | ||
| if (property.Name == "properties") | ||
| { | ||
| foreach (JsonProperty item in property.Value.EnumerateObject()) | ||
| { | ||
| names.Add(item.Name); | ||
| CollectPropertyNames(item.Value, names); | ||
| } | ||
| } | ||
| else | ||
| { | ||
| CollectPropertyNames(property.Value, names); | ||
| } | ||
| } | ||
| } | ||
| else if (element.ValueKind == JsonValueKind.Array) | ||
| { | ||
| foreach (JsonElement item in element.EnumerateArray()) | ||
| { | ||
| CollectPropertyNames(item, names); | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this fix!
Not sure that we really need these two new tests files though. It's a lot of test code to maintain for something fairly trivial. Let's remove them.