Skip to content

Commit

Permalink
Remove unnecessary ctors from FunctionResultContent (#5536)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Oct 22, 2024
1 parent cd4dc68 commit 1622413
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,6 @@ public FunctionResultContent(string callId, string name, object? result)
Result = result;
}

/// <summary>
/// Initializes a new instance of the <see cref="FunctionResultContent"/> class.
/// </summary>
/// <param name="callId">The function call ID for which this is the result.</param>
/// <param name="name">The function name that produced the result.</param>
/// <param name="result">
/// This may be <see langword="null"/> if the function returned <see langword="null"/>, if the function was void-returning
/// and thus had no result, or if the function call failed. Typically, however, in order to provide meaningfully representative
/// information to an AI service, a human-readable representation of those conditions should be supplied.
/// </param>
/// <param name="exception">Any exception that occurred when invoking the function.</param>
public FunctionResultContent(string callId, string name, object? result, Exception? exception)
{
CallId = Throw.IfNull(callId);
Name = Throw.IfNull(name);
Result = result;
Exception = exception;
}

/// <summary>
/// Initializes a new instance of the <see cref="FunctionResultContent"/> class.
/// </summary>
/// <param name="functionCall">The function call for which this is the result.</param>
/// <param name="result">
/// This may be <see langword="null"/> if the function returned <see langword="null"/>, if the function was void-returning
/// and thus had no result, or if the function call failed. Typically, however, in order to provide meaningfully representative
/// information to an AI service, a human-readable representation of those conditions should be supplied.
/// </param>
/// <param name="exception">Any exception that occurred when invoking the function.</param>
public FunctionResultContent(FunctionCallContent functionCall, object? result, Exception? exception = null)
: this(Throw.IfNull(functionCall).CallId, functionCall.Name, result, exception)
{
}

/// <summary>
/// Gets or sets the ID of the function call for which this is the result.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ FunctionResultContent CreateFunctionResultContent(FunctionInvocationResult resul
functionResult = message;
}

return new FunctionResultContent(result.CallContent.CallId, result.CallContent.Name, functionResult, result.Exception);
return new FunctionResultContent(result.CallContent.CallId, result.CallContent.Name, functionResult) { Exception = result.Exception };
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void Text_GetSet_UsesFirstTextContent()
new FunctionCallContent("callId1", "fc1"),
new TextContent("text-1"),
new TextContent("text-2"),
new FunctionResultContent(new FunctionCallContent("callId1", "fc2"), "result"),
new FunctionResultContent("callId1", "fc2", "result"),
]);

TextContent textContent = Assert.IsType<TextContent>(message.Contents[3]);
Expand Down Expand Up @@ -291,7 +291,7 @@ public void ItCanBeSerializeAndDeserialized()
AdditionalProperties = new() { ["metadata-key-6"] = "metadata-value-6" }
},
new FunctionCallContent("function-id", "plugin-name-function-name", new Dictionary<string, object?> { ["parameter"] = "argument" }),
new FunctionResultContent(new FunctionCallContent("function-id", "plugin-name-function-name"), "function-result"),
new FunctionResultContent("function-id", "plugin-name-function-name", "function-result"),
];

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void Text_GetSet_UsesFirstTextContent()
new FunctionCallContent("callId1", "fc1"),
new TextContent("text-1"),
new TextContent("text-2"),
new FunctionResultContent(new FunctionCallContent("callId1", "fc2"), "result"),
new FunctionResultContent("callId1", "fc2", "result"),
],
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,14 @@ public void Constructor_PropsDefault()
[Fact]
public void Constructor_String_PropsRoundtrip()
{
Exception e = new();

FunctionResultContent c = new("id", "name", "result", e);
FunctionResultContent c = new("id", "name", "result");
Assert.Null(c.RawRepresentation);
Assert.Null(c.ModelId);
Assert.Null(c.AdditionalProperties);
Assert.Equal("name", c.Name);
Assert.Equal("id", c.CallId);
Assert.Equal("result", c.Result);
Assert.Same(e, c.Exception);
}

[Fact]
public void Constructor_FunctionCallContent_PropsRoundtrip()
{
Exception e = new();

FunctionResultContent c = new(new FunctionCallContent("id", "name"), "result", e);
Assert.Null(c.RawRepresentation);
Assert.Null(c.ModelId);
Assert.Null(c.AdditionalProperties);
Assert.Equal("id", c.CallId);
Assert.Equal("result", c.Result);
Assert.Same(e, c.Exception);
Assert.Null(c.Exception);
}

[Fact]
Expand Down Expand Up @@ -88,7 +72,7 @@ public void Constructor_PropsRoundtrip()
public void ItShouldBeSerializableAndDeserializable()
{
// Arrange
var sut = new FunctionResultContent(new FunctionCallContent("id", "p1-f1"), "result");
var sut = new FunctionResultContent("id", "p1-f1", "result");

// Act
var json = JsonSerializer.Serialize(sut, TestJsonSerializerContext.Default.Options);
Expand All @@ -106,7 +90,7 @@ public void ItShouldBeSerializableAndDeserializable()
public void ItShouldBeSerializableAndDeserializableWithException()
{
// Arrange
var sut = new FunctionResultContent("callId1", "functionName", null, new InvalidOperationException("hello"));
var sut = new FunctionResultContent("callId1", "functionName", null) { Exception = new InvalidOperationException("hello") };

// Act
var json = JsonSerializer.Serialize(sut, TestJsonSerializerContext.Default.Options);
Expand Down

0 comments on commit 1622413

Please sign in to comment.