From 3851bf41c02170e4de7022789ea7ca6dbacac3c0 Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Sun, 7 Jul 2024 16:21:23 -0500 Subject: [PATCH] chore: run dotnet format --- src/AnthropicClient/Models/AnthropicResult.cs | 8 +- src/AnthropicClient/Models/Tool.cs | 2 +- src/AnthropicClient/Models/ToolCallResult.cs | 10 +- .../Utils/JsonSchemaGenerator.cs | 102 +++++++++--------- .../Data/EventTestData.cs | 8 +- .../Examples/Examples.cs | 68 ++++++------ .../Integration/AnthropicApiClientTests.cs | 10 +- .../Unit/Models/AnthropicResultTests.cs | 2 +- .../Unit/Models/ToolCallResultTests.cs | 2 +- tests/AnthropicClient.Tests/Usings.cs | 10 +- 10 files changed, 111 insertions(+), 111 deletions(-) diff --git a/src/AnthropicClient/Models/AnthropicResult.cs b/src/AnthropicClient/Models/AnthropicResult.cs index dbde966..913afa8 100644 --- a/src/AnthropicClient/Models/AnthropicResult.cs +++ b/src/AnthropicClient/Models/AnthropicResult.cs @@ -12,8 +12,8 @@ public class AnthropicResult /// The value of the result. /// /// Thrown when the result is not successful. - public T Value - { + public T Value + { get { return IsSuccess ? _value : throw new InvalidOperationException("The result is not successful. Check the error property for more information."); @@ -31,8 +31,8 @@ public class AnthropicResult /// The error of the result. /// /// Thrown when the result is successful. - public AnthropicError Error - { + public AnthropicError Error + { get { return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error; diff --git a/src/AnthropicClient/Models/Tool.cs b/src/AnthropicClient/Models/Tool.cs index 3838b9c..0b1ba3f 100644 --- a/src/AnthropicClient/Models/Tool.cs +++ b/src/AnthropicClient/Models/Tool.cs @@ -64,7 +64,7 @@ public class Tool [JsonConstructor] internal Tool() { - var func = () => {}; + var func = () => { }; Name = string.Empty; Description = string.Empty; diff --git a/src/AnthropicClient/Models/ToolCallResult.cs b/src/AnthropicClient/Models/ToolCallResult.cs index 38df223..d9eba97 100644 --- a/src/AnthropicClient/Models/ToolCallResult.cs +++ b/src/AnthropicClient/Models/ToolCallResult.cs @@ -11,8 +11,8 @@ public class ToolCallResult /// 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 /// /// Thrown when the result is not successful. - public T? Value - { + public T? Value + { get { return IsSuccess ? _value : throw new InvalidOperationException("The result is not successful. Check the error property for more information."); @@ -30,8 +30,8 @@ public class ToolCallResult /// The error of the tool call result. /// /// Thrown when the result is successful. - public Exception Error - { + public Exception Error + { get { return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error; @@ -40,7 +40,7 @@ public class ToolCallResult private set { _error = value; - } + } } /// diff --git a/src/AnthropicClient/Utils/JsonSchemaGenerator.cs b/src/AnthropicClient/Utils/JsonSchemaGenerator.cs index 77d57a0..d3d4c0b 100644 --- a/src/AnthropicClient/Utils/JsonSchemaGenerator.cs +++ b/src/AnthropicClient/Utils/JsonSchemaGenerator.cs @@ -2,8 +2,8 @@ using System.Reflection; using System.Text.Json; using System.Text.Json.Nodes; -using AnthropicClient.Models; using AnthropicClient.Json; +using AnthropicClient.Models; namespace AnthropicClient.Utils; @@ -46,18 +46,18 @@ static class JsonSchemaGenerator } var attribute = parameter.GetCustomAttribute(); - var paramName = attribute is not null - ? string.IsNullOrWhiteSpace(attribute.Name) - ? parameter.Name + var paramName = attribute is not null + ? string.IsNullOrWhiteSpace(attribute.Name) + ? parameter.Name : attribute.Name : parameter.Name; - + var paramDescription = attribute?.Description ?? string.Empty; var paramRequired = attribute?.Required ?? !parameter.HasDefaultValue; var paramSchema = GenerateParameterTypeSchema(parameter.ParameterType, inputSchema); paramSchema[DescriptionKey] = paramDescription; - + properties[paramName] = paramSchema; if (paramRequired) @@ -88,62 +88,62 @@ static class JsonSchemaGenerator var paramSchema = type switch { - var t when t == typeof(string) || t == typeof(char) => new JsonObject() - { - [TypeKey] = StringType + var t when t == typeof(string) || t == typeof(char) => new JsonObject() + { + [TypeKey] = StringType }, - var t when - t == typeof(int) || + var t when + t == typeof(int) || t == typeof(uint) || t == typeof(long) || - t == typeof(ulong) || + t == typeof(ulong) || t == typeof(short) || - t == typeof(ushort) || - t == typeof(byte) || - t == typeof(sbyte) => new JsonObject() - { - [TypeKey] = "integer" + t == typeof(ushort) || + t == typeof(byte) || + t == typeof(sbyte) => new JsonObject() + { + [TypeKey] = "integer" + }, + var t when t == typeof(bool) => new JsonObject() + { + [TypeKey] = "boolean" }, - var t when t == typeof(bool) => new JsonObject() - { - [TypeKey] = "boolean" + var t when + t == typeof(double) || + t == typeof(float) || + t == typeof(decimal) => new JsonObject() + { + [TypeKey] = "number" + }, + var t when t == typeof(DateTime) || t == typeof(DateTimeOffset) => new JsonObject() + { + [TypeKey] = StringType, + [FormatKey] = "date-time" }, - var t when - t == typeof(double) || - t == typeof(float) || - t == typeof(decimal) => new JsonObject() - { - [TypeKey] = "number" + var t when t == typeof(Guid) => new JsonObject() + { + [TypeKey] = StringType, + [FormatKey] = "uuid" }, - var t when t == typeof(DateTime) || t == typeof(DateTimeOffset) => new JsonObject() - { - [TypeKey] = StringType, - [FormatKey] = "date-time" - }, - var t when t == typeof(Guid) => new JsonObject() - { - [TypeKey] = StringType, - [FormatKey] = "uuid" - }, - var t when t.IsEnum => new JsonObject() - { - [TypeKey] = StringType, + var t when t.IsEnum => new JsonObject() + { + [TypeKey] = StringType, [EnumKey] = Enum.GetNames(t).Aggregate( - new JsonArray(), (acc, name) => + new JsonArray(), (acc, name) => { - acc.Add(name); - return acc; + acc.Add(name); + return acc; } ) }, - var t when t.IsArray => new JsonObject() - { - [TypeKey] = ArrayType, + var t when t.IsArray => new JsonObject() + { + [TypeKey] = ArrayType, [ItemsKey] = GenerateParameterTypeSchema(t.GetElementType()!, inputSchema) }, - var t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => new JsonObject() - { - [TypeKey] = ArrayType, + var t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => new JsonObject() + { + [TypeKey] = ArrayType, [ItemsKey] = GenerateParameterTypeSchema(t.GetGenericArguments()[0], inputSchema) }, _ => GenerateTypeDefinitionSchema(type, inputSchema) @@ -189,9 +189,9 @@ static class JsonSchemaGenerator memberProperty = definitions.AsObject().ContainsKey(memberType.FullName) ? new JsonObject() - { - [RefKey] = GetDefinitionPath(memberType) - } + { + [RefKey] = GetDefinitionPath(memberType) + } : GenerateParameterTypeSchema(memberType, inputSchema); if (memberRequired) diff --git a/tests/AnthropicClient.Tests/Data/EventTestData.cs b/tests/AnthropicClient.Tests/Data/EventTestData.cs index 5c68347..6fe6a87 100644 --- a/tests/AnthropicClient.Tests/Data/EventTestData.cs +++ b/tests/AnthropicClient.Tests/Data/EventTestData.cs @@ -29,10 +29,10 @@ public class EventTestData : IEnumerable { Id = "toolu_01T1x1fJ34qAmk2tNTrN7Up6", Name = "get_weather", - Input = new Dictionary - { - { "location", "San Francisco, CA" }, - { "unit", "fahrenheit" } + Input = new Dictionary + { + { "location", "San Francisco, CA" }, + { "unit", "fahrenheit" } }, }, ] diff --git a/tests/AnthropicClient.Tests/Examples/Examples.cs b/tests/AnthropicClient.Tests/Examples/Examples.cs index 104f0d7..45ebea5 100644 --- a/tests/AnthropicClient.Tests/Examples/Examples.cs +++ b/tests/AnthropicClient.Tests/Examples/Examples.cs @@ -18,7 +18,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("Please write a haiku about the ocean.")] ) ] @@ -42,7 +42,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : } } } - + [Example] public async Task CreateAndStreamMessage() { @@ -50,7 +50,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("Please write a haiku about the ocean.")] ) ] @@ -83,7 +83,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("Please write a haiku about the ocean.")] ) ] @@ -118,7 +118,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ], @@ -151,9 +151,9 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : public async Task CreateAToolFromAStaticMethod() { var getWeatherTool = Tool.CreateFromStaticMethod( - "Get Weather", - "Get the weather for a location in the specified units", - typeof(GetWeatherTool), + "Get Weather", + "Get the weather for a location in the specified units", + typeof(GetWeatherTool), nameof(GetWeatherTool.GetWeatherStatically) ); @@ -161,7 +161,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ], @@ -196,8 +196,8 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : var toolInstance = new GetWeatherTool(); var getWeatherTool = Tool.CreateFromInstanceMethod( - "Get Weather", - "Get the weather for a location in the specified units", + "Get Weather", + "Get the weather for a location in the specified units", toolInstance, nameof(toolInstance.GetWeather) ); @@ -206,7 +206,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ], @@ -234,15 +234,15 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : } } } - + [Example] public async Task CreateAToolFromDelegate() { var tool = (string location, string units) => $"The weather in {location} is 72 degrees {units}"; var getWeatherTool = Tool.CreateFromFunction( - "Get Weather", - "Get the weather for a location in the specified units", + "Get Weather", + "Get the weather for a location in the specified units", tool ); @@ -250,7 +250,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ], @@ -283,14 +283,14 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : public async Task CreateAToolFromDelegateWithParameterAttributes() { var tool = ( - [FunctionParameter(description: "The location of the weather being got", name: "Location", required: true)] - string location, + [FunctionParameter(description: "The location of the weather being got", name: "Location", required: true)] + string location, string units ) => $"The weather in {location} is 72 degrees {units}"; var getWeatherTool = Tool.CreateFromFunction( - "Get Weather", - "Get the weather for a location in the specified units", + "Get Weather", + "Get the weather for a location in the specified units", tool ); @@ -298,7 +298,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ], @@ -333,8 +333,8 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : var tool = (GetWeatherInput input) => $"The weather in {input.Location} is 72 degrees {input.Units}"; var getWeatherTool = Tool.CreateFromFunction( - "Get Weather", - "Get the weather for a location in the specified units", + "Get Weather", + "Get the weather for a location in the specified units", tool ); @@ -342,7 +342,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : AnthropicModels.Claude3Haiku, [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ], @@ -376,7 +376,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : { List messages = [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ]; @@ -430,10 +430,10 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : messages.Add( new( - MessageRole.User, + MessageRole.User, [ new ToolResultContent( - response.Value.ToolCall.ToolUse.Id, + response.Value.ToolCall.ToolUse.Id, toolResultContent ) ] @@ -461,7 +461,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : { case TextContent textContent: _console.WriteLine(textContent.Text); - break; + break; } } } @@ -473,14 +473,14 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : List messages = [ new( - MessageRole.User, + MessageRole.User, [new TextContent("What is the weather in New York?")] ) ]; List tools = [Tool.CreateFromFunction( - "Get Weather", - "Get the weather for a location in the specified units", + "Get Weather", + "Get the weather for a location in the specified units", tool )]; @@ -529,10 +529,10 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : messages.Add( new( - MessageRole.User, + MessageRole.User, [ new ToolResultContent( - response.ToolCall.ToolUse.Id, + response.ToolCall.ToolUse.Id, toolResultContent ) ] @@ -560,7 +560,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) : { case TextContent textContent: _console.WriteLine(textContent.Text); - break; + break; } } } diff --git a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs index 644e923..0e3a353 100644 --- a/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs +++ b/tests/AnthropicClient.Tests/Integration/AnthropicApiClientTests.cs @@ -27,7 +27,7 @@ public class AnthropicApiClientTests : IntegrationTest result.IsSuccess.Should().BeFalse(); result.Error.Should().BeOfType(); - + var actualErrorType = result.Error.Error!.GetType(); actualErrorType.Should().Be(errorType); } @@ -81,7 +81,7 @@ public class AnthropicApiClientTests : IntegrationTest message.Usage.OutputTokens.Should().Be(25); message.Content.Should().HaveCount(1); message.ToolCall.Should().BeNull(); - + var textContent = message.Content[0]; textContent.Should().BeOfType(); textContent.As().Text.Should().Be("Hi! My name is Claude."); @@ -312,13 +312,13 @@ public class AnthropicApiClientTests : IntegrationTest var msgCompleteEvent = await result .Where(e => e.Type is EventType.MessageComplete) .FirstAsync(); - + msgCompleteEvent.Data.Should().BeOfType(); - + var toolCall = msgCompleteEvent.Data.As().Message.ToolCall; var toolCallResult = await toolCall!.InvokeAsync(); toolCallResult.IsSuccess.Should().BeTrue(); - toolCallResult.Value.Should().Be(getWeather("San Francisco, CA","fahrenheit")); + toolCallResult.Value.Should().Be(getWeather("San Francisco, CA", "fahrenheit")); } } \ No newline at end of file diff --git a/tests/AnthropicClient.Tests/Unit/Models/AnthropicResultTests.cs b/tests/AnthropicClient.Tests/Unit/Models/AnthropicResultTests.cs index 3e24801..ca38010 100644 --- a/tests/AnthropicClient.Tests/Unit/Models/AnthropicResultTests.cs +++ b/tests/AnthropicClient.Tests/Unit/Models/AnthropicResultTests.cs @@ -31,7 +31,7 @@ public class AnthropicResultTests actual.IsSuccess.Should().BeFalse(); actual.IsFailure.Should().BeTrue(); actual.Error.Should().Be(error); - + var action = () => { var _ = actual.Value; }; action.Should().Throw(); diff --git a/tests/AnthropicClient.Tests/Unit/Models/ToolCallResultTests.cs b/tests/AnthropicClient.Tests/Unit/Models/ToolCallResultTests.cs index 6a97018..a2be842 100644 --- a/tests/AnthropicClient.Tests/Unit/Models/ToolCallResultTests.cs +++ b/tests/AnthropicClient.Tests/Unit/Models/ToolCallResultTests.cs @@ -27,7 +27,7 @@ public class ToolCallResultTests actual.IsSuccess.Should().BeFalse(); actual.IsFailure.Should().BeTrue(); actual.Error.Should().Be(error); - + var action = () => { var _ = actual.Value; }; action.Should().Throw(); } diff --git a/tests/AnthropicClient.Tests/Usings.cs b/tests/AnthropicClient.Tests/Usings.cs index 57054e6..2310971 100644 --- a/tests/AnthropicClient.Tests/Usings.cs +++ b/tests/AnthropicClient.Tests/Usings.cs @@ -1,18 +1,18 @@ +global using System.Collections; +global using System.Net; global using System.Text; global using System.Text.Json; -global using System.Text.Json.Nodes; global using System.Text.Json.JsonDiffPatch.Xunit; +global using System.Text.Json.Nodes; -global using AnthropicClient.Models; -global using AnthropicClient.Utils; global using AnthropicClient.Json; +global using AnthropicClient.Models; global using AnthropicClient.Tests.Data; global using AnthropicClient.Tests.Fixtures; +global using AnthropicClient.Utils; global using FluentAssertions; global using Microsoft.Extensions.Configuration; global using RichardSzalay.MockHttp; -global using System.Net; -global using System.Collections;