chore: run dotnet format

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