fix: address methods that return void or task in those cases return null
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
using AnthropicClient.Json;
|
using AnthropicClient.Json;
|
||||||
|
|
||||||
@@ -65,7 +66,9 @@ public class ToolCall
|
|||||||
|
|
||||||
const string resultPropertyName = "Result";
|
const string resultPropertyName = "Result";
|
||||||
var resultProperty = task.GetType().GetProperty(resultPropertyName);
|
var resultProperty = task.GetType().GetProperty(resultPropertyName);
|
||||||
result = resultProperty is not null ? (T)resultProperty.GetValue(task) : default;
|
var isVoidTaskResult = resultProperty.PropertyType.FullName.Contains("VoidTaskResult");
|
||||||
|
|
||||||
|
result = resultProperty is not null && isVoidTaskResult is false ? (T)resultProperty.GetValue(task) : default;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ namespace AnthropicClient.Models;
|
|||||||
public class ToolCallResult<T>
|
public class ToolCallResult<T>
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The value of the tool call result.
|
/// The value of the tool call result. Can be null if the call failed, the call was successful but the return type is void or Task, or the call was successful but the return value is null
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public T? Value { get; }
|
public T? Value { get; }
|
||||||
|
|
||||||
|
|||||||
@@ -221,6 +221,40 @@ public class ToolCallTests : SerializationTest
|
|||||||
result.Value.Should().Be("John");
|
result.Value.Should().Be("John");
|
||||||
result.Error.Should().BeNull();
|
result.Error.Should().BeNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvokeAsync_WhenCalledAndToolReturnsVoid_ItShouldReturnSuccessResult()
|
||||||
|
{
|
||||||
|
var func = (int i) => { };
|
||||||
|
var anthropicFunction = new AnthropicFunction(func.Method, func.Target);
|
||||||
|
var tool = new Tool("tool", "description", anthropicFunction);
|
||||||
|
|
||||||
|
var input = new Dictionary<string, object?> { { "i", 42 } };
|
||||||
|
var toolCall = new ToolCall(tool, new ToolUseContent { Input = input });
|
||||||
|
|
||||||
|
var result = await toolCall.InvokeAsync();
|
||||||
|
|
||||||
|
result.IsSuccess.Should().BeTrue();
|
||||||
|
result.Value.Should().BeNull();
|
||||||
|
result.Error.Should().BeNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task InvokeAsync_WhenCalledAndToolReturnsTask_ItShouldReturnSuccessResult()
|
||||||
|
{
|
||||||
|
var func = async (int i) => await Task.CompletedTask;
|
||||||
|
var anthropicFunction = new AnthropicFunction(func.Method, func.Target);
|
||||||
|
var tool = new Tool("tool", "description", anthropicFunction);
|
||||||
|
|
||||||
|
var input = new Dictionary<string, object?> { { "i", 42 } };
|
||||||
|
var toolCall = new ToolCall(tool, new ToolUseContent { Input = input });
|
||||||
|
|
||||||
|
var result = await toolCall.InvokeAsync();
|
||||||
|
|
||||||
|
result.IsSuccess.Should().BeTrue();
|
||||||
|
result.Value.Should().BeNull();
|
||||||
|
result.Error.Should().BeNull();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Person
|
class Person
|
||||||
|
|||||||
Reference in New Issue
Block a user