Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading;
using System.Threading.Tasks;
using Azure.AI.AgentServer.Responses;
Expand Down Expand Up @@ -46,6 +48,9 @@ public static implicit operator OutputConverterItem(ResponseStreamEvent value) =
/// </summary>
internal static class OutputConverter
{
// Keep non-ASCII characters as-is while continuing to escape HTML-sensitive characters.
private static readonly JsonSerializerOptions s_jsonOptions = new() { Encoder = JavaScriptEncoder.Create(UnicodeRanges.All) };

/// <summary>
/// Converts a stream of <see cref="AgentResponseUpdate"/> into a stream of
/// <see cref="ResponseStreamEvent"/> using the SDK builder pattern.
Expand Down Expand Up @@ -209,7 +214,7 @@ public static async IAsyncEnumerable<OutputConverterItem> ConvertUpdatesToItemsA
previousMessageId = null;

var arguments = functionCall.Arguments is not null
? JsonSerializer.Serialize(functionCall.Arguments)
? JsonSerializer.Serialize(functionCall.Arguments, s_jsonOptions)
: "{}";

var fcBuilder = stream.AddOutputItemFunctionCall(functionCall.Name, functionCall.CallId);
Expand Down Expand Up @@ -328,7 +333,7 @@ public static async IAsyncEnumerable<OutputConverterItem> ConvertUpdatesToItemsA
var wireId = ToolApprovalIdMap.ComputeWireId(approvalRequest.RequestId);

var approvalArguments = approvalFunctionCall.Arguments is not null
? JsonSerializer.Serialize(approvalFunctionCall.Arguments)
? JsonSerializer.Serialize(approvalFunctionCall.Arguments, s_jsonOptions)
: "{}";

ToolApprovalIdMap.Record(
Expand Down Expand Up @@ -763,13 +768,13 @@ internal static string EncodeFunctionResultAsJsonStringPayload(object? result)
string s => s,
JsonElement je => je.ValueKind == JsonValueKind.String
? (je.GetString() ?? string.Empty)
: je.GetRawText(),
: JsonSerializer.Serialize(je, s_jsonOptions),
JsonDocument jd => jd.RootElement.ValueKind == JsonValueKind.String
? (jd.RootElement.GetString() ?? string.Empty)
: jd.RootElement.GetRawText(),
_ => JsonSerializer.Serialize(result),
: JsonSerializer.Serialize(jd.RootElement, s_jsonOptions),
_ => JsonSerializer.Serialize(result, s_jsonOptions),
Comment thread
westey-m marked this conversation as resolved.
Resolved
};

return JsonSerializer.Serialize(innerText);
return JsonSerializer.Serialize(innerText, s_jsonOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public static ChoiceMessageToolCall ToChoiceMessageToolCall(this FunctionCallCon
Function = new()
{
Name = functionCall.Name,
Arguments = JsonSerializer.Serialize(functionCall.Arguments, ChatCompletionsJsonContext.Default.DictionaryStringObject)
Arguments = JsonSerializer.Serialize(functionCall.Arguments, OpenAIHostingJsonUtilities.DefaultOptions.GetTypeInfo(typeof(IDictionary<string, object?>)))
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Unicode;
using Microsoft.Agents.AI.Hosting.OpenAI.Conversations.Models;
using Microsoft.Agents.AI.Hosting.OpenAI.Models;
using Microsoft.Agents.AI.Hosting.OpenAI.Responses.Models;
Expand Down Expand Up @@ -31,6 +33,9 @@ private static JsonSerializerOptions CreateDefaultOptions()
options.TypeInfoResolverChain.Add(AgentAbstractionsJsonUtilities.DefaultOptions.TypeInfoResolver!);
options.TypeInfoResolverChain.Add(OpenAIHostingJsonContext.Default.Options.TypeInfoResolver!);

// Keep non-ASCII characters as-is while continuing to escape HTML-sensitive characters.
options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);

options.MakeReadOnly();
return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,110 @@ public async Task ConvertUpdatesToEventsAsync_FunctionCallWithNullArguments_Emit
Assert.IsType<ResponseCompletedEvent>(events[^1]);
}

[Fact]
public async Task ConvertUpdatesToEventsAsync_FunctionCallNonAsciiArguments_PreservesCharactersAsync()
{
// Arrange
var (stream, _) = CreateTestStream();
var update = new AgentResponseUpdate
{
Contents = [new FunctionCallContent("call_1", "get_weather", new Dictionary<string, object?> { ["location"] = "Ереван" })]
};

// Act
var events = new List<ResponseStreamEvent>();
await foreach (var evt in OutputConverter.ConvertUpdatesToEventsAsync(ToAsync(new[] { update }), stream))
{
events.Add(evt);
}

// Assert
var done = Assert.Single(events.OfType<ResponseFunctionCallArgumentsDoneEvent>());
Assert.Equal("""{"location":"Ереван"}""", done.Arguments);
}

[Fact]
public async Task ConvertUpdatesToEventsAsync_FunctionCallHtmlSensitiveArguments_EscapesCharactersAsync()
{
// Arrange
var (stream, _) = CreateTestStream();
var update = new AgentResponseUpdate
{
Contents = [new FunctionCallContent("call_1", "search", new Dictionary<string, object?> { ["query"] = "<tag>&" })]
};

// Act
var events = new List<ResponseStreamEvent>();
await foreach (var evt in OutputConverter.ConvertUpdatesToEventsAsync(ToAsync(new[] { update }), stream))
{
events.Add(evt);
}

// Assert
var done = Assert.Single(events.OfType<ResponseFunctionCallArgumentsDoneEvent>());
Assert.Equal("""{"query":"\u003Ctag\u003E\u0026"}""", done.Arguments);
}

[Fact]
public async Task ConvertUpdatesToEventsAsync_ToolApprovalRequestNonAsciiArguments_PreservesCharactersAsync()
{
// Arrange
var (stream, _) = CreateTestStream();
var functionCall = new FunctionCallContent("call_1", "get_weather", new Dictionary<string, object?> { ["location"] = "Ереван" });
var update = new AgentResponseUpdate { Contents = [new ToolApprovalRequestContent("af_request_1", functionCall)] };

// Act
var events = new List<ResponseStreamEvent>();
await foreach (var evt in OutputConverter.ConvertUpdatesToEventsAsync(ToAsync(new[] { update }), stream, new AgentSessionStateBag()))
{
events.Add(evt);
}

// Assert
var added = Assert.Single(events.OfType<ResponseOutputItemAddedEvent>());
var item = Assert.IsType<OutputItemMcpApprovalRequest>(added.Item);
Assert.Equal("""{"location":"Ереван"}""", item.Arguments);
}

[Fact]
public void EncodeFunctionResultAsJsonStringPayload_NonAsciiObject_PreservesCharacters()
{
// Arrange
var result = new { city = "Ереван" };

// Act
string payload = OutputConverter.EncodeFunctionResultAsJsonStringPayload(result);

// Assert
Assert.Equal("""{"city":"Ереван"}""", System.Text.Json.JsonSerializer.Deserialize<string>(payload));
}

[Fact]
public void EncodeFunctionResultAsJsonStringPayload_JsonElementWithEscapedUnicode_DecodesCharacters()
{
// Arrange
using System.Text.Json.JsonDocument document = System.Text.Json.JsonDocument.Parse("""{"city":"\u0415"}""");

// Act
string payload = OutputConverter.EncodeFunctionResultAsJsonStringPayload(document.RootElement);

// Assert
Assert.Equal("""{"city":"Е"}""", System.Text.Json.JsonSerializer.Deserialize<string>(payload));
}

[Fact]
public void EncodeFunctionResultAsJsonStringPayload_JsonDocumentWithEscapedUnicode_DecodesCharacters()
{
// Arrange
using System.Text.Json.JsonDocument document = System.Text.Json.JsonDocument.Parse("""{"city":"\u0415"}""");

// Act
string payload = OutputConverter.EncodeFunctionResultAsJsonStringPayload(document);

// Assert
Assert.Equal("""{"city":"Е"}""", System.Text.Json.JsonSerializer.Deserialize<string>(payload));
}

// G-04
[Fact]
public async Task ConvertUpdatesToEventsAsync_FunctionCallWithEmptyCallId_DoesNotEmitWireItemAsync()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Text.Json;
using Microsoft.Agents.AI.Hosting.OpenAI.ChatCompletions.Models;
using Microsoft.Agents.AI.Hosting.OpenAI.Tests;
using Microsoft.Extensions.AI;
using ChatCompletionsAgentResponseExtensions = Microsoft.Agents.AI.Hosting.OpenAI.ChatCompletions.AgentResponseExtensions;

namespace Microsoft.Agents.AI.Hosting.OpenAI.UnitTests;

Expand Down Expand Up @@ -385,6 +387,24 @@ public void Deserialize_ResponseRoundTrip_PreservesData()

#endregion

#region Function Call Conversion Tests

[Fact]
public void ToChoiceMessageToolCall_NonAsciiArguments_PreservesCharacters()
{
// Arrange
var functionCall = new FunctionCallContent("call_1", "get_weather", new Dictionary<string, object?> { ["location"] = "Ереван" });

// Act
ChoiceMessageToolCall toolCall = ChatCompletionsAgentResponseExtensions.ToChoiceMessageToolCall(functionCall);

// Assert
Assert.NotNull(toolCall.Function);
Assert.Equal("""{"location":"Ереван"}""", toolCall.Function.Arguments);
}

#endregion

#region Streaming Chunk Deserialization Tests

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Text.Json;
using Microsoft.Agents.AI.Hosting.OpenAI.Responses.Models;
using Microsoft.Agents.AI.Hosting.OpenAI.Tests;
using Microsoft.Extensions.AI;
using ResponsesAgentResponseExtensions = Microsoft.Agents.AI.Hosting.OpenAI.Responses.AgentResponseExtensions;

namespace Microsoft.Agents.AI.Hosting.OpenAI.UnitTests;

Expand Down Expand Up @@ -766,6 +768,36 @@ public void Deserialize_ResponseRoundTrip_PreservesData()

#endregion

#region Function Call Conversion Tests

[Fact]
public void ToFunctionToolCallItemResource_NonAsciiArguments_PreservesCharacters()
{
// Arrange
var functionCall = new FunctionCallContent("call_1", "get_weather", new Dictionary<string, object?> { ["location"] = "Ереван" });

// Act
FunctionToolCallItemResource resource = ResponsesAgentResponseExtensions.ToFunctionToolCallItemResource(functionCall, "fc_1", OpenAIHostingJsonUtilities.DefaultOptions);

// Assert
Assert.Equal("""{"location":"Ереван"}""", resource.Arguments);
}

[Fact]
public void ToFunctionToolCallItemResource_HtmlSensitiveArguments_EscapesCharacters()
{
// Arrange
var functionCall = new FunctionCallContent("call_1", "search", new Dictionary<string, object?> { ["query"] = "<tag>&" });

// Act
FunctionToolCallItemResource resource = ResponsesAgentResponseExtensions.ToFunctionToolCallItemResource(functionCall, "fc_1", OpenAIHostingJsonUtilities.DefaultOptions);

// Assert
Assert.Equal("""{"query":"\u003Ctag\u003E\u0026"}""", resource.Arguments);
}

#endregion

#region Streaming Event Deserialization Tests

[Fact]
Expand Down
Loading