chore: run dotnet format

This commit is contained in:
Stevan Freeborn
2024-07-07 16:21:23 -05:00
parent c2e21ac574
commit 3851bf41c0
10 changed files with 111 additions and 111 deletions
@@ -12,8 +12,8 @@ public class AnthropicResult<T>
/// The value of the result. /// The value of the result.
/// </summary> /// </summary>
/// <exception cref="InvalidOperationException">Thrown when the result is not successful.</exception> /// <exception cref="InvalidOperationException">Thrown when the result is not successful.</exception>
public T Value public T Value
{ {
get get
{ {
return IsSuccess ? _value : throw new InvalidOperationException("The result is not successful. Check the error property for more information."); 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<T>
/// The error of the result. /// The error of the result.
/// </summary> /// </summary>
/// <exception cref="InvalidOperationException">Thrown when the result is successful.</exception> /// <exception cref="InvalidOperationException">Thrown when the result is successful.</exception>
public AnthropicError Error public AnthropicError Error
{ {
get get
{ {
return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error; return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error;
+1 -1
View File
@@ -64,7 +64,7 @@ public class Tool
[JsonConstructor] [JsonConstructor]
internal Tool() internal Tool()
{ {
var func = () => {}; var func = () => { };
Name = string.Empty; Name = string.Empty;
Description = string.Empty; Description = string.Empty;
+5 -5
View File
@@ -11,8 +11,8 @@ public class ToolCallResult<T>
/// 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 /// 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>
/// <exception cref="InvalidOperationException">Thrown when the result is not successful.</exception> /// <exception cref="InvalidOperationException">Thrown when the result is not successful.</exception>
public T? Value public T? Value
{ {
get get
{ {
return IsSuccess ? _value : throw new InvalidOperationException("The result is not successful. Check the error property for more information."); 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<T>
/// The error of the tool call result. /// The error of the tool call result.
/// </summary> /// </summary>
/// <exception cref="InvalidOperationException">Thrown when the result is successful.</exception> /// <exception cref="InvalidOperationException">Thrown when the result is successful.</exception>
public Exception Error public Exception Error
{ {
get get
{ {
return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error; return IsSuccess ? throw new InvalidOperationException("The result is successful. Check the value property for more information.") : _error;
@@ -40,7 +40,7 @@ public class ToolCallResult<T>
private set private set
{ {
_error = value; _error = value;
} }
} }
/// <summary> /// <summary>
@@ -2,8 +2,8 @@ using System.Reflection;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using AnthropicClient.Models;
using AnthropicClient.Json; using AnthropicClient.Json;
using AnthropicClient.Models;
namespace AnthropicClient.Utils; namespace AnthropicClient.Utils;
@@ -46,18 +46,18 @@ static class JsonSchemaGenerator
} }
var attribute = parameter.GetCustomAttribute<FunctionParameterAttribute>(); var attribute = parameter.GetCustomAttribute<FunctionParameterAttribute>();
var paramName = attribute is not null var paramName = attribute is not null
? string.IsNullOrWhiteSpace(attribute.Name) ? string.IsNullOrWhiteSpace(attribute.Name)
? parameter.Name ? parameter.Name
: attribute.Name : attribute.Name
: parameter.Name; : parameter.Name;
var paramDescription = attribute?.Description ?? string.Empty; var paramDescription = attribute?.Description ?? string.Empty;
var paramRequired = attribute?.Required ?? !parameter.HasDefaultValue; var paramRequired = attribute?.Required ?? !parameter.HasDefaultValue;
var paramSchema = GenerateParameterTypeSchema(parameter.ParameterType, inputSchema); var paramSchema = GenerateParameterTypeSchema(parameter.ParameterType, inputSchema);
paramSchema[DescriptionKey] = paramDescription; paramSchema[DescriptionKey] = paramDescription;
properties[paramName] = paramSchema; properties[paramName] = paramSchema;
if (paramRequired) if (paramRequired)
@@ -88,62 +88,62 @@ static class JsonSchemaGenerator
var paramSchema = type switch var paramSchema = type switch
{ {
var t when t == typeof(string) || t == typeof(char) => new JsonObject() var t when t == typeof(string) || t == typeof(char) => new JsonObject()
{ {
[TypeKey] = StringType [TypeKey] = StringType
}, },
var t when var t when
t == typeof(int) || t == typeof(int) ||
t == typeof(uint) || t == typeof(uint) ||
t == typeof(long) || t == typeof(long) ||
t == typeof(ulong) || t == typeof(ulong) ||
t == typeof(short) || t == typeof(short) ||
t == typeof(ushort) || t == typeof(ushort) ||
t == typeof(byte) || t == typeof(byte) ||
t == typeof(sbyte) => new JsonObject() t == typeof(sbyte) => new JsonObject()
{ {
[TypeKey] = "integer" [TypeKey] = "integer"
},
var t when t == typeof(bool) => new JsonObject()
{
[TypeKey] = "boolean"
}, },
var t when t == typeof(bool) => new JsonObject() var t when
{ t == typeof(double) ||
[TypeKey] = "boolean" 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 var t when t == typeof(Guid) => new JsonObject()
t == typeof(double) || {
t == typeof(float) || [TypeKey] = StringType,
t == typeof(decimal) => new JsonObject() [FormatKey] = "uuid"
{
[TypeKey] = "number"
}, },
var t when t == typeof(DateTime) || t == typeof(DateTimeOffset) => new JsonObject() var t when t.IsEnum => new JsonObject()
{ {
[TypeKey] = StringType, [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,
[EnumKey] = Enum.GetNames(t).Aggregate( [EnumKey] = Enum.GetNames(t).Aggregate(
new JsonArray(), (acc, name) => new JsonArray(), (acc, name) =>
{ {
acc.Add(name); acc.Add(name);
return acc; return acc;
} }
) )
}, },
var t when t.IsArray => new JsonObject() var t when t.IsArray => new JsonObject()
{ {
[TypeKey] = ArrayType, [TypeKey] = ArrayType,
[ItemsKey] = GenerateParameterTypeSchema(t.GetElementType()!, inputSchema) [ItemsKey] = GenerateParameterTypeSchema(t.GetElementType()!, inputSchema)
}, },
var t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => new JsonObject() var t when t.IsGenericType && t.GetGenericTypeDefinition() == typeof(List<>) => new JsonObject()
{ {
[TypeKey] = ArrayType, [TypeKey] = ArrayType,
[ItemsKey] = GenerateParameterTypeSchema(t.GetGenericArguments()[0], inputSchema) [ItemsKey] = GenerateParameterTypeSchema(t.GetGenericArguments()[0], inputSchema)
}, },
_ => GenerateTypeDefinitionSchema(type, inputSchema) _ => GenerateTypeDefinitionSchema(type, inputSchema)
@@ -189,9 +189,9 @@ static class JsonSchemaGenerator
memberProperty = definitions.AsObject().ContainsKey(memberType.FullName) memberProperty = definitions.AsObject().ContainsKey(memberType.FullName)
? new JsonObject() ? new JsonObject()
{ {
[RefKey] = GetDefinitionPath(memberType) [RefKey] = GetDefinitionPath(memberType)
} }
: GenerateParameterTypeSchema(memberType, inputSchema); : GenerateParameterTypeSchema(memberType, inputSchema);
if (memberRequired) if (memberRequired)
@@ -29,10 +29,10 @@ public class EventTestData : IEnumerable<object[]>
{ {
Id = "toolu_01T1x1fJ34qAmk2tNTrN7Up6", Id = "toolu_01T1x1fJ34qAmk2tNTrN7Up6",
Name = "get_weather", Name = "get_weather",
Input = new Dictionary<string, object?> Input = new Dictionary<string, object?>
{ {
{ "location", "San Francisco, CA" }, { "location", "San Francisco, CA" },
{ "unit", "fahrenheit" } { "unit", "fahrenheit" }
}, },
}, },
] ]
@@ -18,7 +18,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("Please write a haiku about the ocean.")] [new TextContent("Please write a haiku about the ocean.")]
) )
] ]
@@ -42,7 +42,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
} }
} }
} }
[Example] [Example]
public async Task CreateAndStreamMessage() public async Task CreateAndStreamMessage()
{ {
@@ -50,7 +50,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("Please write a haiku about the ocean.")] [new TextContent("Please write a haiku about the ocean.")]
) )
] ]
@@ -83,7 +83,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("Please write a haiku about the ocean.")] [new TextContent("Please write a haiku about the ocean.")]
) )
] ]
@@ -118,7 +118,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [new TextContent("What is the weather in New York?")]
) )
], ],
@@ -151,9 +151,9 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
public async Task CreateAToolFromAStaticMethod() public async Task CreateAToolFromAStaticMethod()
{ {
var getWeatherTool = Tool.CreateFromStaticMethod( var getWeatherTool = Tool.CreateFromStaticMethod(
"Get Weather", "Get Weather",
"Get the weather for a location in the specified units", "Get the weather for a location in the specified units",
typeof(GetWeatherTool), typeof(GetWeatherTool),
nameof(GetWeatherTool.GetWeatherStatically) nameof(GetWeatherTool.GetWeatherStatically)
); );
@@ -161,7 +161,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [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 toolInstance = new GetWeatherTool();
var getWeatherTool = Tool.CreateFromInstanceMethod( var getWeatherTool = Tool.CreateFromInstanceMethod(
"Get Weather", "Get Weather",
"Get the weather for a location in the specified units", "Get the weather for a location in the specified units",
toolInstance, toolInstance,
nameof(toolInstance.GetWeather) nameof(toolInstance.GetWeather)
); );
@@ -206,7 +206,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [new TextContent("What is the weather in New York?")]
) )
], ],
@@ -234,15 +234,15 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
} }
} }
} }
[Example] [Example]
public async Task CreateAToolFromDelegate() public async Task CreateAToolFromDelegate()
{ {
var tool = (string location, string units) => $"The weather in {location} is 72 degrees {units}"; var tool = (string location, string units) => $"The weather in {location} is 72 degrees {units}";
var getWeatherTool = Tool.CreateFromFunction( var getWeatherTool = Tool.CreateFromFunction(
"Get Weather", "Get Weather",
"Get the weather for a location in the specified units", "Get the weather for a location in the specified units",
tool tool
); );
@@ -250,7 +250,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [new TextContent("What is the weather in New York?")]
) )
], ],
@@ -283,14 +283,14 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
public async Task CreateAToolFromDelegateWithParameterAttributes() public async Task CreateAToolFromDelegateWithParameterAttributes()
{ {
var tool = ( var tool = (
[FunctionParameter(description: "The location of the weather being got", name: "Location", required: true)] [FunctionParameter(description: "The location of the weather being got", name: "Location", required: true)]
string location, string location,
string units string units
) => $"The weather in {location} is 72 degrees {units}"; ) => $"The weather in {location} is 72 degrees {units}";
var getWeatherTool = Tool.CreateFromFunction( var getWeatherTool = Tool.CreateFromFunction(
"Get Weather", "Get Weather",
"Get the weather for a location in the specified units", "Get the weather for a location in the specified units",
tool tool
); );
@@ -298,7 +298,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [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 tool = (GetWeatherInput input) => $"The weather in {input.Location} is 72 degrees {input.Units}";
var getWeatherTool = Tool.CreateFromFunction( var getWeatherTool = Tool.CreateFromFunction(
"Get Weather", "Get Weather",
"Get the weather for a location in the specified units", "Get the weather for a location in the specified units",
tool tool
); );
@@ -342,7 +342,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
AnthropicModels.Claude3Haiku, AnthropicModels.Claude3Haiku,
[ [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [new TextContent("What is the weather in New York?")]
) )
], ],
@@ -376,7 +376,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
{ {
List<Message> messages = [ List<Message> messages = [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [new TextContent("What is the weather in New York?")]
) )
]; ];
@@ -430,10 +430,10 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
messages.Add( messages.Add(
new( new(
MessageRole.User, MessageRole.User,
[ [
new ToolResultContent( new ToolResultContent(
response.Value.ToolCall.ToolUse.Id, response.Value.ToolCall.ToolUse.Id,
toolResultContent toolResultContent
) )
] ]
@@ -461,7 +461,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
{ {
case TextContent textContent: case TextContent textContent:
_console.WriteLine(textContent.Text); _console.WriteLine(textContent.Text);
break; break;
} }
} }
} }
@@ -473,14 +473,14 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
List<Message> messages = [ List<Message> messages = [
new( new(
MessageRole.User, MessageRole.User,
[new TextContent("What is the weather in New York?")] [new TextContent("What is the weather in New York?")]
) )
]; ];
List<Tool> tools = [Tool.CreateFromFunction( List<Tool> tools = [Tool.CreateFromFunction(
"Get Weather", "Get Weather",
"Get the weather for a location in the specified units", "Get the weather for a location in the specified units",
tool tool
)]; )];
@@ -529,10 +529,10 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
messages.Add( messages.Add(
new( new(
MessageRole.User, MessageRole.User,
[ [
new ToolResultContent( new ToolResultContent(
response.ToolCall.ToolUse.Id, response.ToolCall.ToolUse.Id,
toolResultContent toolResultContent
) )
] ]
@@ -560,7 +560,7 @@ public class Examples(ConfigurationFixture config, ITestOutputHelper console) :
{ {
case TextContent textContent: case TextContent textContent:
_console.WriteLine(textContent.Text); _console.WriteLine(textContent.Text);
break; break;
} }
} }
} }
@@ -27,7 +27,7 @@ public class AnthropicApiClientTests : IntegrationTest
result.IsSuccess.Should().BeFalse(); result.IsSuccess.Should().BeFalse();
result.Error.Should().BeOfType<AnthropicError>(); result.Error.Should().BeOfType<AnthropicError>();
var actualErrorType = result.Error.Error!.GetType(); var actualErrorType = result.Error.Error!.GetType();
actualErrorType.Should().Be(errorType); actualErrorType.Should().Be(errorType);
} }
@@ -81,7 +81,7 @@ public class AnthropicApiClientTests : IntegrationTest
message.Usage.OutputTokens.Should().Be(25); message.Usage.OutputTokens.Should().Be(25);
message.Content.Should().HaveCount(1); message.Content.Should().HaveCount(1);
message.ToolCall.Should().BeNull(); message.ToolCall.Should().BeNull();
var textContent = message.Content[0]; var textContent = message.Content[0];
textContent.Should().BeOfType<TextContent>(); textContent.Should().BeOfType<TextContent>();
textContent.As<TextContent>().Text.Should().Be("Hi! My name is Claude."); textContent.As<TextContent>().Text.Should().Be("Hi! My name is Claude.");
@@ -312,13 +312,13 @@ public class AnthropicApiClientTests : IntegrationTest
var msgCompleteEvent = await result var msgCompleteEvent = await result
.Where(e => e.Type is EventType.MessageComplete) .Where(e => e.Type is EventType.MessageComplete)
.FirstAsync(); .FirstAsync();
msgCompleteEvent.Data.Should().BeOfType<MessageCompleteEventData>(); msgCompleteEvent.Data.Should().BeOfType<MessageCompleteEventData>();
var toolCall = msgCompleteEvent.Data.As<MessageCompleteEventData>().Message.ToolCall; var toolCall = msgCompleteEvent.Data.As<MessageCompleteEventData>().Message.ToolCall;
var toolCallResult = await toolCall!.InvokeAsync<string>(); var toolCallResult = await toolCall!.InvokeAsync<string>();
toolCallResult.IsSuccess.Should().BeTrue(); toolCallResult.IsSuccess.Should().BeTrue();
toolCallResult.Value.Should().Be(getWeather("San Francisco, CA","fahrenheit")); toolCallResult.Value.Should().Be(getWeather("San Francisco, CA", "fahrenheit"));
} }
} }
@@ -31,7 +31,7 @@ public class AnthropicResultTests
actual.IsSuccess.Should().BeFalse(); actual.IsSuccess.Should().BeFalse();
actual.IsFailure.Should().BeTrue(); actual.IsFailure.Should().BeTrue();
actual.Error.Should().Be(error); actual.Error.Should().Be(error);
var action = () => { var _ = actual.Value; }; var action = () => { var _ = actual.Value; };
action.Should().Throw<InvalidOperationException>(); action.Should().Throw<InvalidOperationException>();
@@ -27,7 +27,7 @@ public class ToolCallResultTests
actual.IsSuccess.Should().BeFalse(); actual.IsSuccess.Should().BeFalse();
actual.IsFailure.Should().BeTrue(); actual.IsFailure.Should().BeTrue();
actual.Error.Should().Be(error); actual.Error.Should().Be(error);
var action = () => { var _ = actual.Value; }; var action = () => { var _ = actual.Value; };
action.Should().Throw<InvalidOperationException>(); action.Should().Throw<InvalidOperationException>();
} }
+5 -5
View File
@@ -1,18 +1,18 @@
global using System.Collections;
global using System.Net;
global using System.Text; global using System.Text;
global using System.Text.Json; global using System.Text.Json;
global using System.Text.Json.Nodes;
global using System.Text.Json.JsonDiffPatch.Xunit; 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.Json;
global using AnthropicClient.Models;
global using AnthropicClient.Tests.Data; global using AnthropicClient.Tests.Data;
global using AnthropicClient.Tests.Fixtures; global using AnthropicClient.Tests.Fixtures;
global using AnthropicClient.Utils;
global using FluentAssertions; global using FluentAssertions;
global using Microsoft.Extensions.Configuration; global using Microsoft.Extensions.Configuration;
global using RichardSzalay.MockHttp; global using RichardSzalay.MockHttp;
global using System.Net;
global using System.Collections;