chore: run dotnet format
This commit is contained in:
@@ -3,8 +3,8 @@ using System.Text;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
|
||||||
using AnthropicClient.Json;
|
using AnthropicClient.Json;
|
||||||
using AnthropicClient.Utils;
|
|
||||||
using AnthropicClient.Models;
|
using AnthropicClient.Models;
|
||||||
|
using AnthropicClient.Utils;
|
||||||
|
|
||||||
namespace AnthropicClient;
|
namespace AnthropicClient;
|
||||||
|
|
||||||
@@ -82,12 +82,12 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
}
|
}
|
||||||
|
|
||||||
var chatResponse = Deserialize<ChatResponse>(responseContent) ?? new ChatResponse();
|
var chatResponse = Deserialize<ChatResponse>(responseContent) ?? new ChatResponse();
|
||||||
|
|
||||||
if (request.Tools is not null && request.Tools.Count > 0)
|
if (request.Tools is not null && request.Tools.Count > 0)
|
||||||
{
|
{
|
||||||
chatResponse.ToolCall = GetToolCall(chatResponse, request.Tools);
|
chatResponse.ToolCall = GetToolCall(chatResponse, request.Tools);
|
||||||
}
|
}
|
||||||
|
|
||||||
return AnthropicResult<ChatResponse>.Success(chatResponse, anthropicHeaders);
|
return AnthropicResult<ChatResponse>.Success(chatResponse, anthropicHeaders);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,13 +108,13 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
do
|
do
|
||||||
{
|
{
|
||||||
var line = await streamReader.ReadLineAsync();
|
var line = await streamReader.ReadLineAsync();
|
||||||
|
|
||||||
// I know...this is not pretty, but here is why...
|
// I know...this is not pretty, but here is why...
|
||||||
// as events are being yielded I want to also
|
// as events are being yielded I want to also
|
||||||
// build up the complete chat response
|
// build up the complete chat response
|
||||||
// so I can yield it as a special event to make tool
|
// so I can yield it as a special event to make tool
|
||||||
// calling easier to handle
|
// calling easier to handle
|
||||||
|
|
||||||
// initialize chat response on message start
|
// initialize chat response on message start
|
||||||
if (currentEvent.Type is EventType.MessageStart && currentEvent.Data is MessageStartEventData msgStartData)
|
if (currentEvent.Type is EventType.MessageStart && currentEvent.Data is MessageStartEventData msgStartData)
|
||||||
{
|
{
|
||||||
@@ -173,7 +173,7 @@ public class AnthropicApiClient : IAnthropicApiClient
|
|||||||
|
|
||||||
// update chat response with message delta data
|
// update chat response with message delta data
|
||||||
if (
|
if (
|
||||||
currentEvent.Type is EventType.MessageDelta &&
|
currentEvent.Type is EventType.MessageDelta &&
|
||||||
currentEvent.Data is MessageDeltaEventData msgDeltaData &&
|
currentEvent.Data is MessageDeltaEventData msgDeltaData &&
|
||||||
chatResponse is not null
|
chatResponse is not null
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -70,48 +70,48 @@ public class AnthropicHeaders
|
|||||||
RequestId = headers.TryGetValues(RequestIdHeaderKey, out var requestIdValues)
|
RequestId = headers.TryGetValues(RequestIdHeaderKey, out var requestIdValues)
|
||||||
? requestIdValues.FirstOrDefault()
|
? requestIdValues.FirstOrDefault()
|
||||||
: string.Empty;
|
: string.Empty;
|
||||||
|
|
||||||
RateLimitRequestsLimit = int.Parse(
|
RateLimitRequestsLimit = int.Parse(
|
||||||
headers.TryGetValues(RateLimitRequestsLimitHeaderKey, out var requestLimitValues)
|
headers.TryGetValues(RateLimitRequestsLimitHeaderKey, out var requestLimitValues)
|
||||||
? requestLimitValues.FirstOrDefault()
|
? requestLimitValues.FirstOrDefault()
|
||||||
: "0"
|
: "0"
|
||||||
);
|
);
|
||||||
|
|
||||||
RateLimitRequestsRemaining = int.Parse(
|
RateLimitRequestsRemaining = int.Parse(
|
||||||
headers.TryGetValues(RateLimitRequestsRemainingHeaderKey, out var remainingRequestsValues)
|
headers.TryGetValues(RateLimitRequestsRemainingHeaderKey, out var remainingRequestsValues)
|
||||||
? remainingRequestsValues.FirstOrDefault()
|
? remainingRequestsValues.FirstOrDefault()
|
||||||
: "0"
|
: "0"
|
||||||
);
|
);
|
||||||
|
|
||||||
RateLimitRequestsReset = DateTimeOffset.Parse(
|
RateLimitRequestsReset = DateTimeOffset.Parse(
|
||||||
headers.TryGetValues(RateLimitRequestsResetHeaderKey, out var requestResetValues)
|
headers.TryGetValues(RateLimitRequestsResetHeaderKey, out var requestResetValues)
|
||||||
? requestResetValues.FirstOrDefault()
|
? requestResetValues.FirstOrDefault()
|
||||||
: DateTimeOffset.MinValue.ToString()
|
: DateTimeOffset.MinValue.ToString()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
RateLimitTokensLimit = int.Parse(
|
RateLimitTokensLimit = int.Parse(
|
||||||
headers.TryGetValues(RateLimitTokensLimitHeaderKey, out var limitValues)
|
headers.TryGetValues(RateLimitTokensLimitHeaderKey, out var limitValues)
|
||||||
? limitValues.FirstOrDefault()
|
? limitValues.FirstOrDefault()
|
||||||
: "0"
|
: "0"
|
||||||
);
|
);
|
||||||
|
|
||||||
RateLimitTokensRemaining = int.Parse(
|
RateLimitTokensRemaining = int.Parse(
|
||||||
headers.TryGetValues(RateLimitTokensRemainingHeaderKey, out var remainingTokensValues)
|
headers.TryGetValues(RateLimitTokensRemainingHeaderKey, out var remainingTokensValues)
|
||||||
? remainingTokensValues.FirstOrDefault()
|
? remainingTokensValues.FirstOrDefault()
|
||||||
: "0"
|
: "0"
|
||||||
);
|
);
|
||||||
|
|
||||||
RateLimitTokensReset = DateTimeOffset.Parse(
|
RateLimitTokensReset = DateTimeOffset.Parse(
|
||||||
headers.TryGetValues(RateLimitTokensResetHeaderKey, out var tokenResetValues)
|
headers.TryGetValues(RateLimitTokensResetHeaderKey, out var tokenResetValues)
|
||||||
? tokenResetValues.FirstOrDefault()
|
? tokenResetValues.FirstOrDefault()
|
||||||
: DateTimeOffset.MinValue.ToString()
|
: DateTimeOffset.MinValue.ToString()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
RetryAfter = int.Parse(
|
RetryAfter = int.Parse(
|
||||||
headers.TryGetValues(RetryAfterHeaderKey, out var retryValues)
|
headers.TryGetValues(RetryAfterHeaderKey, out var retryValues)
|
||||||
? retryValues.FirstOrDefault()
|
? retryValues.FirstOrDefault()
|
||||||
: "0"
|
: "0"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -63,4 +63,4 @@ public class AnthropicResult<T>
|
|||||||
{
|
{
|
||||||
return new AnthropicResult<T>(default!, error, false, headers);
|
return new AnthropicResult<T>(default!, error, false, headers);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,7 +7,7 @@ namespace AnthropicClient.Models;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Parameter)]
|
[AttributeUsage(AttributeTargets.Parameter)]
|
||||||
public sealed class FunctionParameterAttribute : Attribute
|
public sealed class FunctionParameterAttribute : Attribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The name of the parameter.
|
/// The name of the parameter.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -24,6 +24,6 @@ public static class ImageType
|
|||||||
/// Represents the WebP image type.
|
/// Represents the WebP image type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const string Webp = "image/webp";
|
public const string Webp = "image/webp";
|
||||||
|
|
||||||
internal static bool IsValidImageType(string imageType) => imageType is Jpg or Png or Gif or Webp;
|
internal static bool IsValidImageType(string imageType) => imageType is Jpg or Png or Gif or Webp;
|
||||||
}
|
}
|
||||||
@@ -9,7 +9,7 @@ namespace AnthropicClient.Models;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class MessageRequest
|
public abstract class MessageRequest
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the model ID to use for the request.
|
/// Gets the model ID to use for the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string Model { get; init; } = string.Empty;
|
public string Model { get; init; } = string.Empty;
|
||||||
@@ -66,7 +66,7 @@ public abstract class MessageRequest
|
|||||||
/// Gets the tools to use for the request.
|
/// Gets the tools to use for the request.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<Tool>? Tools { get; init; } = null;
|
public List<Tool>? Tools { get; init; } = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets a value indicating whether the message should be streamed.
|
/// Gets a value indicating whether the message should be streamed.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -14,6 +14,6 @@ public static class MessageRole
|
|||||||
/// Represents the assistant role.
|
/// Represents the assistant role.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const string Assistant = "assistant";
|
public const string Assistant = "assistant";
|
||||||
|
|
||||||
internal static bool IsValidRole(string role) => role is User or Assistant;
|
internal static bool IsValidRole(string role) => role is User or Assistant;
|
||||||
}
|
}
|
||||||
@@ -39,13 +39,13 @@ public class Tool
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public string DisplayName { get; }
|
public string DisplayName { get; }
|
||||||
|
|
||||||
internal Tool(string name, string description, AnthropicFunction function)
|
internal Tool(string name, string description, AnthropicFunction function)
|
||||||
{
|
{
|
||||||
ArgumentValidator.ThrowIfNullOrWhitespace(name, nameof(name));
|
ArgumentValidator.ThrowIfNullOrWhitespace(name, nameof(name));
|
||||||
ArgumentValidator.ThrowIfNullOrWhitespace(description, nameof(description));
|
ArgumentValidator.ThrowIfNullOrWhitespace(description, nameof(description));
|
||||||
ArgumentValidator.ThrowIfNull(function, nameof(function));
|
ArgumentValidator.ThrowIfNull(function, nameof(function));
|
||||||
|
|
||||||
// Anthropic imposes a limit on tool names. Tool names must be...
|
// Anthropic imposes a limit on tool names. Tool names must be...
|
||||||
// - between 1 and 64 characters long
|
// - between 1 and 64 characters long
|
||||||
// - contain only letters, numbers, underscores, and hyphens
|
// - contain only letters, numbers, underscores, and hyphens
|
||||||
@@ -76,7 +76,7 @@ public class Tool
|
|||||||
ArgumentValidator.ThrowIfNull(type, nameof(type));
|
ArgumentValidator.ThrowIfNull(type, nameof(type));
|
||||||
|
|
||||||
var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
|
var method = type.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static);
|
||||||
|
|
||||||
if (method is null)
|
if (method is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Method '{methodName}' not found in type '{type.FullName}'.", nameof(methodName));
|
throw new ArgumentException($"Method '{methodName}' not found in type '{type.FullName}'.", nameof(methodName));
|
||||||
@@ -103,7 +103,7 @@ public class Tool
|
|||||||
ArgumentValidator.ThrowIfNull(instance, nameof(instance));
|
ArgumentValidator.ThrowIfNull(instance, nameof(instance));
|
||||||
|
|
||||||
var method = instance.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
|
var method = instance.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance);
|
||||||
|
|
||||||
if (method is null)
|
if (method is null)
|
||||||
{
|
{
|
||||||
throw new ArgumentException($"Method '{methodName}' not found in type '{instance.GetType().FullName}'.", nameof(methodName));
|
throw new ArgumentException($"Method '{methodName}' not found in type '{instance.GetType().FullName}'.", nameof(methodName));
|
||||||
@@ -165,7 +165,7 @@ public class Tool
|
|||||||
|
|
||||||
return new Tool(name, description, new AnthropicFunction(func.Method, func.Target));
|
return new Tool(name, description, new AnthropicFunction(func.Method, func.Target));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static string SanitizeName(string name)
|
private static string SanitizeName(string name)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -60,9 +60,9 @@ public class ToolCall
|
|||||||
if (isAwaitable)
|
if (isAwaitable)
|
||||||
{
|
{
|
||||||
var task = (Task)Tool.Function.Method.Invoke(Tool.Function.Instance, arguments);
|
var task = (Task)Tool.Function.Method.Invoke(Tool.Function.Instance, arguments);
|
||||||
|
|
||||||
await task;
|
await task;
|
||||||
|
|
||||||
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;
|
result = resultProperty is not null ? (T)resultProperty.GetValue(task) : default;
|
||||||
@@ -89,9 +89,9 @@ public class ToolCall
|
|||||||
{
|
{
|
||||||
var parameter = parameters[i];
|
var parameter = parameters[i];
|
||||||
var attribute = parameter.GetCustomAttribute<FunctionParameterAttribute>();
|
var attribute = parameter.GetCustomAttribute<FunctionParameterAttribute>();
|
||||||
var parameterName = attribute is not null
|
var parameterName = attribute is not null
|
||||||
? string.IsNullOrWhiteSpace(attribute.Name)
|
? string.IsNullOrWhiteSpace(attribute.Name)
|
||||||
? parameter.Name
|
? parameter.Name
|
||||||
: attribute.Name
|
: attribute.Name
|
||||||
: parameter.Name;
|
: parameter.Name;
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,6 @@ public class ToolChoiceType
|
|||||||
/// Represents the specific tool choice type.
|
/// Represents the specific tool choice type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public const string Tool = "tool";
|
public const string Tool = "tool";
|
||||||
|
|
||||||
internal static bool IsValidType(string type) => type is Auto or Any or Tool;
|
internal static bool IsValidType(string type) => type is Auto or Any or Tool;
|
||||||
}
|
}
|
||||||
@@ -26,7 +26,7 @@ public class ClientTests(ConfigurationFixture configFixture) : EndToEndTest(conf
|
|||||||
);
|
);
|
||||||
|
|
||||||
var response = _client.CreateChatMessageAsync(request);
|
var response = _client.CreateChatMessageAsync(request);
|
||||||
|
|
||||||
var events = new List<AnthropicEvent>();
|
var events = new List<AnthropicEvent>();
|
||||||
|
|
||||||
await foreach (var e in response)
|
await foreach (var e in response)
|
||||||
@@ -46,7 +46,7 @@ public class ClientTests(ConfigurationFixture configFixture) : EndToEndTest(conf
|
|||||||
);
|
);
|
||||||
|
|
||||||
var response = _client.CreateChatMessageAsync(request);
|
var response = _client.CreateChatMessageAsync(request);
|
||||||
|
|
||||||
await foreach (var e in response)
|
await foreach (var e in response)
|
||||||
{
|
{
|
||||||
if (e.Data is MessageCompleteEventData messageCompleteData)
|
if (e.Data is MessageCompleteEventData messageCompleteData)
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace AnthropicClient.Tests.Unit.Models;
|
namespace AnthropicClient.Tests.Unit.Models;
|
||||||
|
|
||||||
public class AnthropicResultTests
|
public class AnthropicResultTests
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Success_WhenCalled_ItShouldReturnSuccessResult()
|
public void Success_WhenCalled_ItShouldReturnSuccessResult()
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ public class ChatMessageRequestTests : SerializationTest
|
|||||||
""tools"": [],
|
""tools"": [],
|
||||||
""stream"":false
|
""stream"":false
|
||||||
}";
|
}";
|
||||||
|
|
||||||
private readonly string _testJsonWithSpecificToolChoice = @"{
|
private readonly string _testJsonWithSpecificToolChoice = @"{
|
||||||
""model"": ""claude-3-sonnet-20240229"",
|
""model"": ""claude-3-sonnet-20240229"",
|
||||||
""system"": ""test-system"",
|
""system"": ""test-system"",
|
||||||
@@ -52,7 +52,7 @@ public class ChatMessageRequestTests : SerializationTest
|
|||||||
""tools"": [],
|
""tools"": [],
|
||||||
""stream"": false
|
""stream"": false
|
||||||
}";
|
}";
|
||||||
|
|
||||||
private readonly string _testJsonWithImageContent = @"{
|
private readonly string _testJsonWithImageContent = @"{
|
||||||
""model"": ""claude-3-sonnet-20240229"",
|
""model"": ""claude-3-sonnet-20240229"",
|
||||||
""system"": ""test-system"",
|
""system"": ""test-system"",
|
||||||
@@ -77,7 +77,7 @@ public class ChatMessageRequestTests : SerializationTest
|
|||||||
""tools"":[],
|
""tools"":[],
|
||||||
""stream"": false
|
""stream"": false
|
||||||
}";
|
}";
|
||||||
|
|
||||||
private readonly string _testJsonWithUnknownContent = @"{
|
private readonly string _testJsonWithUnknownContent = @"{
|
||||||
""model"": ""claude-3-sonnet-20240229"",
|
""model"": ""claude-3-sonnet-20240229"",
|
||||||
""system"": ""test-system"",
|
""system"": ""test-system"",
|
||||||
@@ -92,7 +92,7 @@ public class ChatMessageRequestTests : SerializationTest
|
|||||||
""tools"": [],
|
""tools"": [],
|
||||||
""stream"": false
|
""stream"": false
|
||||||
}";
|
}";
|
||||||
|
|
||||||
private readonly string _testJsonWithToolUseContent = @"{
|
private readonly string _testJsonWithToolUseContent = @"{
|
||||||
""model"": ""claude-3-sonnet-20240229"",
|
""model"": ""claude-3-sonnet-20240229"",
|
||||||
""system"": ""test-system"",
|
""system"": ""test-system"",
|
||||||
@@ -121,7 +121,7 @@ public class ChatMessageRequestTests : SerializationTest
|
|||||||
""tools"": [],
|
""tools"": [],
|
||||||
""stream"": false
|
""stream"": false
|
||||||
}";
|
}";
|
||||||
|
|
||||||
private readonly string _testJsonWithToolResultContent = @"{
|
private readonly string _testJsonWithToolResultContent = @"{
|
||||||
""model"": ""claude-3-sonnet-20240229"",
|
""model"": ""claude-3-sonnet-20240229"",
|
||||||
""system"": ""test-system"",
|
""system"": ""test-system"",
|
||||||
@@ -369,7 +369,7 @@ public class ChatMessageRequestTests : SerializationTest
|
|||||||
specificToolChoice!.Type.Should().Be("tool");
|
specificToolChoice!.Type.Should().Be("tool");
|
||||||
specificToolChoice.Name.Should().Be("test-tool");
|
specificToolChoice.Name.Should().Be("test-tool");
|
||||||
chatMessageRequest.Tools.Should().HaveCount(0);
|
chatMessageRequest.Tools.Should().HaveCount(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void JsonDeserialization_WhenDeserializedWithUnknownToolChoice_ItShouldThrowJsonException()
|
public void JsonDeserialization_WhenDeserializedWithUnknownToolChoice_ItShouldThrowJsonException()
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ public class ContentDeltaTypeTests
|
|||||||
public void TextDelta_WhenCalled_ItShouldReturnExpectedValue()
|
public void TextDelta_WhenCalled_ItShouldReturnExpectedValue()
|
||||||
{
|
{
|
||||||
var expected = "text_delta";
|
var expected = "text_delta";
|
||||||
|
|
||||||
var actual = ContentDeltaType.TextDelta;
|
var actual = ContentDeltaType.TextDelta;
|
||||||
|
|
||||||
actual.Should().Be(expected);
|
actual.Should().Be(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@ public class ContentDeltaTypeTests
|
|||||||
public void JsonDelta_WhenCalled_ItShouldReturnExpectedValue()
|
public void JsonDelta_WhenCalled_ItShouldReturnExpectedValue()
|
||||||
{
|
{
|
||||||
var expected = "input_json_delta";
|
var expected = "input_json_delta";
|
||||||
|
|
||||||
var actual = ContentDeltaType.JsonDelta;
|
var actual = ContentDeltaType.JsonDelta;
|
||||||
|
|
||||||
actual.Should().Be(expected);
|
actual.Should().Be(expected);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,11 +67,11 @@ public class ContentStartEventDataTests : SerializationTest
|
|||||||
Name = "get_weather",
|
Name = "get_weather",
|
||||||
Input = []
|
Input = []
|
||||||
};
|
};
|
||||||
|
|
||||||
var contentStartEventData = new ContentStartEventData(index, toolUseContent);
|
var contentStartEventData = new ContentStartEventData(index, toolUseContent);
|
||||||
|
|
||||||
var actual = Serialize(contentStartEventData);
|
var actual = Serialize(contentStartEventData);
|
||||||
|
|
||||||
JsonAssert.Equal(_testJsonWithToolUseBlock, actual);
|
JsonAssert.Equal(_testJsonWithToolUseBlock, actual);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -6,9 +6,9 @@ public class InvalidRequestErrorTests : SerializationTest
|
|||||||
public void Constructor_WhenCalled_ItShouldInitializeProperties()
|
public void Constructor_WhenCalled_ItShouldInitializeProperties()
|
||||||
{
|
{
|
||||||
var message = "message";
|
var message = "message";
|
||||||
|
|
||||||
var error = new InvalidRequestError(message);
|
var error = new InvalidRequestError(message);
|
||||||
|
|
||||||
error.Type.Should().Be("invalid_request_error");
|
error.Type.Should().Be("invalid_request_error");
|
||||||
error.Message.Should().Be(message);
|
error.Message.Should().Be(message);
|
||||||
}
|
}
|
||||||
@@ -17,11 +17,11 @@ public class InvalidRequestErrorTests : SerializationTest
|
|||||||
public void Serialization_WhenSerialized_ItShouldHaveExpectedShape()
|
public void Serialization_WhenSerialized_ItShouldHaveExpectedShape()
|
||||||
{
|
{
|
||||||
var message = "message";
|
var message = "message";
|
||||||
|
|
||||||
var error = new InvalidRequestError(message);
|
var error = new InvalidRequestError(message);
|
||||||
|
|
||||||
var serialized = Serialize(error);
|
var serialized = Serialize(error);
|
||||||
|
|
||||||
JsonAssert.Equal(
|
JsonAssert.Equal(
|
||||||
@"{
|
@"{
|
||||||
""type"": ""invalid_request_error"",
|
""type"": ""invalid_request_error"",
|
||||||
@@ -38,9 +38,9 @@ public class InvalidRequestErrorTests : SerializationTest
|
|||||||
""type"": ""invalid_request_error"",
|
""type"": ""invalid_request_error"",
|
||||||
""message"": ""message""
|
""message"": ""message""
|
||||||
}";
|
}";
|
||||||
|
|
||||||
var error = Deserialize<InvalidRequestError>(json);
|
var error = Deserialize<InvalidRequestError>(json);
|
||||||
|
|
||||||
error!.Type.Should().Be("invalid_request_error");
|
error!.Type.Should().Be("invalid_request_error");
|
||||||
error.Message.Should().Be("message");
|
error.Message.Should().Be("message");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ public class TextDeltaTests : SerializationTest
|
|||||||
public void Constructor_WhenCalled_ItShouldInitializeProperties()
|
public void Constructor_WhenCalled_ItShouldInitializeProperties()
|
||||||
{
|
{
|
||||||
var text = "Hello World!";
|
var text = "Hello World!";
|
||||||
|
|
||||||
var textDelta = new TextDelta(text);
|
var textDelta = new TextDelta(text);
|
||||||
|
|
||||||
textDelta.Type.Should().Be("text_delta");
|
textDelta.Type.Should().Be("text_delta");
|
||||||
textDelta.Text.Should().Be(text);
|
textDelta.Text.Should().Be(text);
|
||||||
}
|
}
|
||||||
@@ -24,7 +24,7 @@ public class TextDeltaTests : SerializationTest
|
|||||||
var textDelta = new TextDelta("Hello World!");
|
var textDelta = new TextDelta("Hello World!");
|
||||||
|
|
||||||
var actual = Serialize(textDelta);
|
var actual = Serialize(textDelta);
|
||||||
|
|
||||||
JsonAssert.Equal(_testJson, actual);
|
JsonAssert.Equal(_testJson, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -135,7 +135,7 @@ public class ToolCallTests : SerializationTest
|
|||||||
[Fact]
|
[Fact]
|
||||||
public async Task InvokeAsync_WhenCalledAndToolHasCustomParameterName_ItShouldReturnSuccessResult()
|
public async Task InvokeAsync_WhenCalledAndToolHasCustomParameterName_ItShouldReturnSuccessResult()
|
||||||
{
|
{
|
||||||
var func = ([FunctionParameter("Person's Age", "Age")]int i) => i.ToString();
|
var func = ([FunctionParameter("Person's Age", "Age")] int i) => i.ToString();
|
||||||
var anthropicFunction = new AnthropicFunction(func.Method, func.Target);
|
var anthropicFunction = new AnthropicFunction(func.Method, func.Target);
|
||||||
var tool = new Tool("tool", "description", anthropicFunction);
|
var tool = new Tool("tool", "description", anthropicFunction);
|
||||||
|
|
||||||
@@ -212,7 +212,7 @@ public class ToolCallTests : SerializationTest
|
|||||||
}";
|
}";
|
||||||
|
|
||||||
var input = Deserialize<Dictionary<string, object?>>(personJson);
|
var input = Deserialize<Dictionary<string, object?>>(personJson);
|
||||||
|
|
||||||
var toolCall = new ToolCall(tool, new ToolUseContent { Input = input! });
|
var toolCall = new ToolCall(tool, new ToolUseContent { Input = input! });
|
||||||
|
|
||||||
var result = await toolCall.InvokeAsync();
|
var result = await toolCall.InvokeAsync();
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ public class ToolTests : SerializationTest
|
|||||||
public void Constructor_WhenGivenInvalidNameValue_ItShouldThrowArgumentException(string name)
|
public void Constructor_WhenGivenInvalidNameValue_ItShouldThrowArgumentException(string name)
|
||||||
{
|
{
|
||||||
var method = () => true;
|
var method = () => true;
|
||||||
var function = new AnthropicFunction(method.Method);
|
var function = new AnthropicFunction(method.Method);
|
||||||
var action = () => new Tool(name, "description", function);
|
var action = () => new Tool(name, "description", function);
|
||||||
|
|
||||||
action.Should().Throw<ArgumentException>();
|
action.Should().Throw<ArgumentException>();
|
||||||
@@ -25,7 +25,7 @@ public class ToolTests : SerializationTest
|
|||||||
public void Constructor_WhenGivenInvalidDescriptionValue_ItShouldThrowArgumentException(string description)
|
public void Constructor_WhenGivenInvalidDescriptionValue_ItShouldThrowArgumentException(string description)
|
||||||
{
|
{
|
||||||
var method = () => true;
|
var method = () => true;
|
||||||
var function = new AnthropicFunction(method.Method);
|
var function = new AnthropicFunction(method.Method);
|
||||||
var action = () => new Tool("name", description, function);
|
var action = () => new Tool("name", description, function);
|
||||||
|
|
||||||
action.Should().Throw<ArgumentException>();
|
action.Should().Throw<ArgumentException>();
|
||||||
@@ -43,7 +43,7 @@ public class ToolTests : SerializationTest
|
|||||||
public void Constructor_WhenCalled_ItShouldInitializeProperties()
|
public void Constructor_WhenCalled_ItShouldInitializeProperties()
|
||||||
{
|
{
|
||||||
var method = () => true;
|
var method = () => true;
|
||||||
var function = new AnthropicFunction(method.Method);
|
var function = new AnthropicFunction(method.Method);
|
||||||
var tool = new Tool("test name", "description", function);
|
var tool = new Tool("test name", "description", function);
|
||||||
|
|
||||||
var expectedSchema = new JsonObject()
|
var expectedSchema = new JsonObject()
|
||||||
@@ -56,7 +56,7 @@ public class ToolTests : SerializationTest
|
|||||||
tool.Description.Should().Be("description");
|
tool.Description.Should().Be("description");
|
||||||
tool.Function.Should().Be(function);
|
tool.Function.Should().Be(function);
|
||||||
tool.InputSchema.Should().BeEquivalentTo(
|
tool.InputSchema.Should().BeEquivalentTo(
|
||||||
expectedSchema,
|
expectedSchema,
|
||||||
t => t.IgnoringCyclicReferences()
|
t => t.IgnoringCyclicReferences()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,7 @@ public class ToolTests : SerializationTest
|
|||||||
public void Constructor_WhenCalledAndGivenNameIsMoreThanMaxLength_ItShouldTruncateName()
|
public void Constructor_WhenCalledAndGivenNameIsMoreThanMaxLength_ItShouldTruncateName()
|
||||||
{
|
{
|
||||||
var method = () => true;
|
var method = () => true;
|
||||||
var function = new AnthropicFunction(method.Method);
|
var function = new AnthropicFunction(method.Method);
|
||||||
var tool = new Tool(new string('a', 65), "description", function);
|
var tool = new Tool(new string('a', 65), "description", function);
|
||||||
|
|
||||||
tool.Name.Should().HaveLength(64);
|
tool.Name.Should().HaveLength(64);
|
||||||
@@ -115,7 +115,7 @@ public class ToolTests : SerializationTest
|
|||||||
tool.Function.Method.Name.Should().Be(nameof(TestClass.TestStaticMethod));
|
tool.Function.Method.Name.Should().Be(nameof(TestClass.TestStaticMethod));
|
||||||
tool.Function.Instance.Should().BeNull();
|
tool.Function.Instance.Should().BeNull();
|
||||||
tool.InputSchema.Should().BeEquivalentTo(
|
tool.InputSchema.Should().BeEquivalentTo(
|
||||||
expectedSchema,
|
expectedSchema,
|
||||||
t => t.IgnoringCyclicReferences()
|
t => t.IgnoringCyclicReferences()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -173,7 +173,7 @@ public class ToolTests : SerializationTest
|
|||||||
tool.Function.Method.Name.Should().Be(nameof(TestClass.TestInstanceMethod));
|
tool.Function.Method.Name.Should().Be(nameof(TestClass.TestInstanceMethod));
|
||||||
tool.Function.Instance.Should().Be(instance);
|
tool.Function.Instance.Should().Be(instance);
|
||||||
tool.InputSchema.Should().BeEquivalentTo(
|
tool.InputSchema.Should().BeEquivalentTo(
|
||||||
expectedSchema,
|
expectedSchema,
|
||||||
t => t.IgnoringCyclicReferences()
|
t => t.IgnoringCyclicReferences()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -202,7 +202,7 @@ public class ToolTests : SerializationTest
|
|||||||
tool.Description.Should().Be("description");
|
tool.Description.Should().Be("description");
|
||||||
tool.Function.Method.Should().BeSameAs(func.Method);
|
tool.Function.Method.Should().BeSameAs(func.Method);
|
||||||
tool.InputSchema.Should().BeEquivalentTo(
|
tool.InputSchema.Should().BeEquivalentTo(
|
||||||
expectedSchema,
|
expectedSchema,
|
||||||
t => t.IgnoringCyclicReferences()
|
t => t.IgnoringCyclicReferences()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -242,7 +242,7 @@ public class ToolTests : SerializationTest
|
|||||||
tool.Description.Should().Be("description");
|
tool.Description.Should().Be("description");
|
||||||
tool.Function.Method.Should().BeSameAs(func.Method);
|
tool.Function.Method.Should().BeSameAs(func.Method);
|
||||||
tool.InputSchema.Should().BeEquivalentTo(
|
tool.InputSchema.Should().BeEquivalentTo(
|
||||||
expectedSchema,
|
expectedSchema,
|
||||||
t => t.IgnoringCyclicReferences()
|
t => t.IgnoringCyclicReferences()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ public class JsonSchemaGeneratorTests
|
|||||||
};
|
};
|
||||||
var testMethod = () => true;
|
var testMethod = () => true;
|
||||||
var function = new AnthropicFunction(testMethod.Method);
|
var function = new AnthropicFunction(testMethod.Method);
|
||||||
|
|
||||||
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
||||||
|
|
||||||
JsonAssert.Equal(expectedSchema, schema);
|
JsonAssert.Equal(expectedSchema, schema);
|
||||||
@@ -38,7 +38,7 @@ public class JsonSchemaGeneratorTests
|
|||||||
|
|
||||||
var testMethod = (int age = 0) => age;
|
var testMethod = (int age = 0) => age;
|
||||||
var function = new AnthropicFunction(testMethod.Method);
|
var function = new AnthropicFunction(testMethod.Method);
|
||||||
|
|
||||||
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
||||||
|
|
||||||
JsonAssert.Equal(expectedSchema, schema);
|
JsonAssert.Equal(expectedSchema, schema);
|
||||||
@@ -66,7 +66,7 @@ public class JsonSchemaGeneratorTests
|
|||||||
|
|
||||||
var testMethod = (string name) => name;
|
var testMethod = (string name) => name;
|
||||||
var function = new AnthropicFunction(testMethod.Method);
|
var function = new AnthropicFunction(testMethod.Method);
|
||||||
|
|
||||||
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
var schema = JsonSchemaGenerator.GenerateInputSchema(function);
|
||||||
|
|
||||||
JsonAssert.Equal(expectedSchema, schema);
|
JsonAssert.Equal(expectedSchema, schema);
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
global using FluentAssertions;
|
global using System.Text.Json;
|
||||||
|
global using System.Text.Json.JsonDiffPatch.Xunit;
|
||||||
|
|
||||||
global using AnthropicClient.Models;
|
global using AnthropicClient.Models;
|
||||||
global using AnthropicClient.Utils;
|
global using AnthropicClient.Utils;
|
||||||
|
|
||||||
global using System.Text.Json;
|
global using FluentAssertions;
|
||||||
global using System.Text.Json.JsonDiffPatch.Xunit;
|
|
||||||
|
|
||||||
global using Microsoft.Extensions.Configuration;
|
global using Microsoft.Extensions.Configuration;
|
||||||
Reference in New Issue
Block a user