-
Notifications
You must be signed in to change notification settings - Fork 306
Fix foundry agents connect serialization bug #1205
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
Open
JasonYeMSFT
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
JasonYeMSFT:chuye/fix-foundry-agents-connect-serialization-bug
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
22 changes: 22 additions & 0 deletions
22
tools/Azure.Mcp.Tools.Foundry/src/Models/AgentFileSearchResult.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,22 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Text.Json.Serialization; | ||
| using Azure.AI.Agents.Persistent; | ||
|
|
||
| namespace Azure.Mcp.Tools.Foundry.Models; | ||
|
|
||
| internal class AgentFileSearchResult | ||
| { | ||
| [JsonPropertyName("fileId")] | ||
| public string? FileId { get; set; } | ||
|
|
||
| [JsonPropertyName("fileName")] | ||
| public string? FileName { get; set; } | ||
|
|
||
| [JsonPropertyName("score")] | ||
| public float Score { get; set; } | ||
|
|
||
| [JsonPropertyName("content")] | ||
| public IReadOnlyList<FileSearchToolCallContent>? Content { get; set; } | ||
| } |
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1257,26 +1257,10 @@ private List<ToolDefinitionAIFunction> ConvertToolDefinitionsFromString(string? | |||||
| { | ||||||
| foreach (RunStepToolCall toolCall in details.ToolCalls) | ||||||
| { | ||||||
| Microsoft.Extensions.AI.ChatMessage CreateRequestMessage(string name, Dictionary<string, object?> arguments) => | ||||||
| new(ChatRole.Assistant, [new FunctionCallContent(toolCall.Id, name, arguments)]) | ||||||
| { | ||||||
| AuthorName = step.AssistantId, | ||||||
| MessageId = step.Id, | ||||||
| RawRepresentation = step, | ||||||
| }; | ||||||
|
|
||||||
| Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(object result) => | ||||||
| new(ChatRole.Tool, [new FunctionResultContent(toolCall.Id, result)]) | ||||||
| { | ||||||
| AuthorName = step.AssistantId, | ||||||
| MessageId = step.Id, | ||||||
| RawRepresentation = step, | ||||||
| }; | ||||||
|
|
||||||
| switch (toolCall) | ||||||
| { | ||||||
| case RunStepFunctionToolCall function: | ||||||
| yield return CreateRequestMessage(function.Name, Parse(function.Arguments) ?? []); | ||||||
| yield return CreateRequestMessage(toolCall.Id, function.Name, Parse(function.Arguments) ?? [], step); | ||||||
| // TODO: output doesn't appear to be available in the API | ||||||
|
|
||||||
| static Dictionary<string, object?>? Parse(string arguments) | ||||||
|
|
@@ -1288,43 +1272,44 @@ Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(object result) => | |||||
| break; | ||||||
|
|
||||||
| case RunStepCodeInterpreterToolCall code: | ||||||
| yield return CreateRequestMessage("code_interpreter", new() { ["input"] = code.Input }); | ||||||
| yield return CreateResponseMessage(string.Concat(code.Outputs.OfType<RunStepCodeInterpreterLogOutput>().Select(o => o.Logs))); | ||||||
| yield return CreateRequestMessage(toolCall.Id, "code_interpreter", new() { ["input"] = code.Input }, step); | ||||||
| yield return CreateResponseMessage(toolCall.Id, string.Concat(code.Outputs.OfType<RunStepCodeInterpreterLogOutput>().Select(o => o.Logs)), step); | ||||||
| break; | ||||||
|
|
||||||
| case RunStepBingGroundingToolCall bing: | ||||||
| yield return CreateRequestMessage("bing_grounding", new() { ["requesturl"] = bing.BingGrounding["requesturl"] }); | ||||||
| yield return CreateRequestMessage(toolCall.Id, "bing_grounding", new() { ["requesturl"] = bing.BingGrounding["requesturl"] }, step); | ||||||
| break; | ||||||
|
|
||||||
| case RunStepFileSearchToolCall fileSearch: | ||||||
| yield return CreateRequestMessage("file_search", new() | ||||||
| yield return CreateRequestMessage(toolCall.Id, "file_search", new() | ||||||
| { | ||||||
| ["ranking_options"] = JsonSerializer.SerializeToElement(new() | ||||||
| { | ||||||
| ["ranker"] = fileSearch.FileSearch.RankingOptions.Ranker, | ||||||
| ["score_threshold"] = fileSearch.FileSearch.RankingOptions.ScoreThreshold, | ||||||
| }, DictionaryTypeInfo) | ||||||
| }); | ||||||
| yield return CreateResponseMessage(fileSearch.FileSearch.Results.Select(r => new | ||||||
| }, step); | ||||||
| List<AgentFileSearchResult> fileSearchResults = fileSearch.FileSearch.Results.Select(r => new AgentFileSearchResult | ||||||
| { | ||||||
| file_id = r.FileId, | ||||||
| file_name = r.FileName, | ||||||
| score = r.Score, | ||||||
| content = r.Content, | ||||||
| }).ToList()); | ||||||
| FileId = r.FileId, | ||||||
| FileName = r.FileName, | ||||||
| Score = r.Score, | ||||||
| Content = r.Content, | ||||||
| }).ToList(); | ||||||
| yield return CreateResponseMessage(toolCall.Id, fileSearchResults, step); | ||||||
| break; | ||||||
|
|
||||||
| case RunStepAzureAISearchToolCall aiSearch: | ||||||
| yield return CreateRequestMessage("azure_ai_search", new() { ["input"] = aiSearch.AzureAISearch["input"] }); | ||||||
| yield return CreateResponseMessage(new Dictionary<string, object?> | ||||||
| yield return CreateRequestMessage(toolCall.Id, "azure_ai_search", new() { ["input"] = aiSearch.AzureAISearch["input"] }, step); | ||||||
| yield return CreateResponseMessage(toolCall.Id, new Dictionary<string, string?> | ||||||
| { | ||||||
| ["output"] = aiSearch.AzureAISearch["output"] | ||||||
JasonYeMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| }); | ||||||
| }, step); | ||||||
| break; | ||||||
|
|
||||||
| case RunStepMicrosoftFabricToolCall fabric: | ||||||
| yield return CreateRequestMessage("fabric_dataagent", new() { ["input"] = fabric.MicrosoftFabric["input"] }); | ||||||
| yield return CreateResponseMessage(fabric.MicrosoftFabric["output"]); | ||||||
| yield return CreateRequestMessage(toolCall.Id, "fabric_dataagent", new() { ["input"] = fabric.MicrosoftFabric["input"] }, step); | ||||||
| yield return CreateResponseMessage(toolCall.Id, fabric.MicrosoftFabric["output"], step); | ||||||
JasonYeMSFT marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -1688,4 +1673,54 @@ public string SerializeToolDefinition() | |||||
| return Encoding.UTF8.GetString(bytes.GetBuffer(), 0, (int)bytes.Length); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| internal static Microsoft.Extensions.AI.ChatMessage CreateRequestMessage(string toolCallId, string name, Dictionary<string, object?> arguments, RunStep step) | ||||||
| { | ||||||
| return new(ChatRole.Assistant, [new FunctionCallContent(toolCallId, name, arguments)]) | ||||||
| { | ||||||
| AuthorName = step.AssistantId, | ||||||
| MessageId = step.Id, | ||||||
| RawRepresentation = step, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, List<AgentFileSearchResult> result, RunStep step) | ||||||
| { | ||||||
| return new(ChatRole.Tool, [new FunctionResultContent(toolCallId, result)]) | ||||||
| { | ||||||
| AuthorName = step.AssistantId, | ||||||
| MessageId = step.Id, | ||||||
| RawRepresentation = step, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, JsonElement result, RunStep step) | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need all these different static methods given
Suggested change
|
||||||
| { | ||||||
| return new(ChatRole.Tool, [new FunctionResultContent(toolCallId, result)]) | ||||||
| { | ||||||
| AuthorName = step.AssistantId, | ||||||
| MessageId = step.Id, | ||||||
| RawRepresentation = step, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, string result, RunStep step) | ||||||
| { | ||||||
| return new(ChatRole.Tool, [new FunctionResultContent(toolCallId, result)]) | ||||||
| { | ||||||
| AuthorName = step.AssistantId, | ||||||
| MessageId = step.Id, | ||||||
| RawRepresentation = step, | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| internal static Microsoft.Extensions.AI.ChatMessage CreateResponseMessage(string toolCallId, IDictionary<string, string?> result, RunStep step) | ||||||
| { | ||||||
| return new(ChatRole.Tool, [new FunctionResultContent(toolCallId, result)]) | ||||||
| { | ||||||
| AuthorName = step.AssistantId, | ||||||
| MessageId = step.Id, | ||||||
| RawRepresentation = step, | ||||||
| }; | ||||||
| } | ||||||
| } | ||||||
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.
The
AgentFileSearchResulttype should be added to the JSON serialization context. WhileList<AgentFileSearchResult>is registered on line 67, the individual type needs to be registered as well for proper AOT serialization. Add[JsonSerializable(typeof(AgentFileSearchResult))]before line 67.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.
Is Copilot right? I don't think it's necessary.