Skip to content
Closed
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 @@ -14,7 +14,7 @@ namespace Microsoft.Extensions.AI;
/// Represents a function call request.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class FunctionCallContent : AIContent
public class FunctionCallContent : AIContent
{
/// <summary>
/// Initializes a new instance of the <see cref="FunctionCallContent"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Extensions.AI;
/// Represents the result of a function call.
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class FunctionResultContent : AIContent
public class FunctionResultContent : AIContent
{
/// <summary>
/// Initializes a new instance of the <see cref="FunctionResultContent"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1792,7 +1792,7 @@
]
},
{
"Type": "sealed class Microsoft.Extensions.AI.FunctionCallContent : Microsoft.Extensions.AI.AIContent",
"Type": "class Microsoft.Extensions.AI.FunctionCallContent : Microsoft.Extensions.AI.AIContent",
"Stage": "Stable",
"Methods": [
{
Expand Down Expand Up @@ -1824,7 +1824,7 @@
]
},
{
"Type": "sealed class Microsoft.Extensions.AI.FunctionResultContent : Microsoft.Extensions.AI.AIContent",
"Type": "class Microsoft.Extensions.AI.FunctionResultContent : Microsoft.Extensions.AI.AIContent",
"Stage": "Stable",
"Methods": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Extensions.AI;

public class FunctionCallContentTests
public partial class FunctionCallContentTests
{
[Fact]
public void Constructor_PropsDefault()
Expand Down Expand Up @@ -322,4 +324,60 @@ public static void CreateFromParsedArguments_NullInput_ThrowsArgumentNullExcepti
Assert.Throws<ArgumentNullException>("name", () => FunctionCallContent.CreateFromParsedArguments("{}", "callId", null!, _ => null));
Assert.Throws<ArgumentNullException>("argumentParser", () => FunctionCallContent.CreateFromParsedArguments("{}", "callId", "functionName", null!));
}

[Fact]
public static void DerivedFunctionCallContent_CanBeSerializedAndDeserialized()
{
// Arrange: Register the derived type
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(AIJsonUtilities.DefaultOptions.TypeInfoResolver, DerivedTypesJsonContext.Default),
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
options.AddAIContentType<DerivedFunctionCallContent>("derivedFunctionCall");

var derivedContent = new DerivedFunctionCallContent("callId1", "testFunction", new Dictionary<string, object?> { ["param1"] = "value1" })
{
CustomProperty = "customValue"
};

// Act: Serialize as AIContent to test polymorphic serialization
var json = JsonSerializer.Serialize<AIContent>(derivedContent, options);
var deserialized = JsonSerializer.Deserialize<AIContent>(json, options);

// Assert
Assert.NotNull(deserialized);
Assert.IsType<DerivedFunctionCallContent>(deserialized);
var derivedDeserialized = (DerivedFunctionCallContent)deserialized;
Assert.Equal("callId1", derivedDeserialized.CallId);
Assert.Equal("testFunction", derivedDeserialized.Name);
Assert.NotNull(derivedDeserialized.Arguments);
Assert.Single(derivedDeserialized.Arguments);
Assert.Equal("value1", derivedDeserialized.Arguments["param1"]?.ToString());
Assert.Equal("customValue", derivedDeserialized.CustomProperty);
}

internal sealed class DerivedFunctionCallContent : FunctionCallContent
{
public DerivedFunctionCallContent(string callId, string name, IDictionary<string, object?>? arguments = null)
: base(callId, name, arguments)
{
}

public string? CustomProperty { get; set; }
}

internal sealed class DerivedFunctionResultContent : FunctionResultContent
{
public DerivedFunctionResultContent(string callId, object? result)
: base(callId, result)
{
}

public string? CustomProperty { get; set; }
}

[JsonSerializable(typeof(DerivedFunctionCallContent))]
[JsonSerializable(typeof(DerivedFunctionResultContent))]
internal sealed partial class DerivedTypesJsonContext : JsonSerializerContext;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Xunit;

namespace Microsoft.Extensions.AI;
Expand Down Expand Up @@ -91,4 +93,33 @@ public void ItShouldBeSerializableAndDeserializableWithException()
Assert.Equal(sut.Result, deserializedSut.Result?.ToString());
Assert.Null(deserializedSut.Exception);
}

[Fact]
public void DerivedFunctionResultContent_CanBeSerializedAndDeserialized()
{
// Arrange: Register the derived type
var options = new JsonSerializerOptions
{
TypeInfoResolver = JsonTypeInfoResolver.Combine(AIJsonUtilities.DefaultOptions.TypeInfoResolver, FunctionCallContentTests.DerivedTypesJsonContext.Default),
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
};
options.AddAIContentType<FunctionCallContentTests.DerivedFunctionResultContent>("derivedFunctionResult");

var derivedContent = new FunctionCallContentTests.DerivedFunctionResultContent("callId1", "test result")
{
CustomProperty = "customValue"
};

// Act: Serialize as AIContent to test polymorphic serialization
var json = JsonSerializer.Serialize<AIContent>(derivedContent, options);
var deserialized = JsonSerializer.Deserialize<AIContent>(json, options);

// Assert
Assert.NotNull(deserialized);
Assert.IsType<FunctionCallContentTests.DerivedFunctionResultContent>(deserialized);
var derivedDeserialized = (FunctionCallContentTests.DerivedFunctionResultContent)deserialized;
Assert.Equal("callId1", derivedDeserialized.CallId);
Assert.Equal("test result", derivedDeserialized.Result?.ToString());
Assert.Equal("customValue", derivedDeserialized.CustomProperty);
}
}
Loading