docs: work on README

This commit is contained in:
Stevan Freeborn
2024-07-03 16:57:27 -05:00
parent bd8e4c70ab
commit dd380c9253
41 changed files with 847 additions and 523 deletions
@@ -0,0 +1,100 @@
namespace AnthropicClient.Tests.Data;
public class ErrorTestData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[]
{
HttpStatusCode.BadRequest,
@"{
""type"": ""error"",
""error"": {
""type"": ""invalid_request_error"",
""message"": ""Invalid request.""
}
}",
typeof(InvalidRequestError)
};
yield return new object[]
{
HttpStatusCode.Unauthorized,
@"{
""type"": ""error"",
""error"": {
""type"": ""authentication_error"",
""message"": ""Authentication error.""
}
}",
typeof(AuthenticationError)
};
yield return new object[]
{
HttpStatusCode.Forbidden,
@"{
""type"": ""error"",
""error"": {
""type"": ""permission_error"",
""message"": ""Permission error.""
}
}",
typeof(PermissionError)
};
yield return new object[]
{
HttpStatusCode.NotFound,
@"{
""type"": ""error"",
""error"": {
""type"": ""not_found_error"",
""message"": ""Not found error.""
}
}",
typeof(NotFoundError)
};
yield return new object[]
{
HttpStatusCode.TooManyRequests,
@"{
""type"": ""error"",
""error"": {
""type"": ""rate_limit_error"",
""message"": ""Rate limit error.""
}
}",
typeof(RateLimitError)
};
yield return new object[]
{
HttpStatusCode.InternalServerError,
@"{
""type"": ""error"",
""error"": {
""type"": ""api_error"",
""message"": ""Internal server error.""
}
}",
typeof(ApiError)
};
yield return new object[]
{
(HttpStatusCode)529,
@"{
""type"": ""error"",
""error"": {
""type"": ""overloaded_error"",
""message"": ""Overloaded error.""
}
}",
typeof(OverloadedError)
};
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
@@ -0,0 +1,666 @@
namespace AnthropicClient.Tests.Data;
public class EventTestData : IEnumerable<object[]>
{
public static MemoryStream GetEventStream()
{
var events = new EventTestData().Select(x => x[0].ToString())!;
var content = string.Join("\n\n", events);
return new MemoryStream(Encoding.UTF8.GetBytes(content));
}
public static List<AnthropicEvent> GetAllEvents() => new EventTestData().Select(x => x[1] as AnthropicEvent).Append(MessageCompleteEvent).ToList()!;
public static readonly AnthropicEvent MessageCompleteEvent = new()
{
Type = EventType.MessageComplete,
Data = new MessageCompleteEventData(
new()
{
Id = "msg_014p7gG3wDgGV9EUtLvnow3U",
Type = "message",
Role = MessageRole.Assistant,
Model = AnthropicModels.Claude3Haiku,
StopSequence = null,
Usage = new Usage { InputTokens = 472, OutputTokens = 91 },
StopReason = "tool_use",
Content = [
new TextContent("Okay, let's check the weather for San Francisco, CA:"),
new ToolUseContent()
{
Id = "toolu_01T1x1fJ34qAmk2tNTrN7Up6",
Name = "get_weather",
Input = new Dictionary<string, object?>
{
{ "location", "San Francisco, CA" },
{ "unit", "fahrenheit" }
},
},
]
},
new()
)
};
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[]
{
"""
event: message_start
data: {"type":"message_start","message":{"id":"msg_014p7gG3wDgGV9EUtLvnow3U","type":"message","role":"assistant","model":"claude-3-haiku-20240307","stop_sequence":null,"usage":{"input_tokens":472,"output_tokens":2},"content":[],"stop_reason":null}}
""",
new AnthropicEvent()
{
Type = EventType.MessageStart,
Data = new MessageStartEventData()
{
Message = new MessageResponse()
{
Id = "msg_014p7gG3wDgGV9EUtLvnow3U",
Type = "message",
Role = "assistant",
Model = "claude-3-haiku-20240307",
StopSequence = null,
Usage = new Usage()
{
InputTokens = 472,
OutputTokens = 2,
},
Content = [],
StopReason = null,
},
},
},
};
yield return new object[]
{
"""
event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockStart,
Data = new ContentStartEventData()
{
Index = 0,
ContentBlock = new TextContent()
{
Type = "text",
Text = "",
},
},
},
};
yield return new object[]
{
"""
event: ping
data: {"type": "ping"}
""",
new AnthropicEvent()
{
Type = EventType.Ping,
Data = new PingEventData(),
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Okay"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = "Okay",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = ",",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" let"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " let",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"'s"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = "'s",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" check"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " check",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" the"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " the",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" weather"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " weather",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" for"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " for",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" San"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " San",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" Francisco"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " Francisco",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":","}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " ,",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":" CA"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " CA",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":":"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 0,
Delta = new TextDelta()
{
Type = "text_delta",
Text = " :",
},
},
},
};
yield return new object[]
{
"""
event: content_block_stop
data: {"type":"content_block_stop","index":0}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockStop,
Data = new ContentStopEventData()
{
Index = 0,
},
},
};
yield return new object[]
{
"""
event: content_block_start
data: {"type":"content_block_start","index":1,"content_block":{"type":"tool_use","id":"toolu_01T1x1fJ34qAmk2tNTrN7Up6","name":"get_weather","input":{}}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockStart,
Data = new ContentStartEventData()
{
Index = 1,
ContentBlock = new ToolUseContent()
{
Type = "tool_use",
Id = "toolu_01T1x1fJ34qAmk2tNTrN7Up6",
Name = "get_weather",
Input = [],
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":""}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = "",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"{\"location\":"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = "{\"location\":",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" \"San"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = " \"San",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" Francisc"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = " Francisc",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"o,"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = "o,",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":" CA\""}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = " CA\"",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":", "}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = ", ",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"\"unit\": \"fah"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = "\"unit\": \"fah",
},
},
},
};
yield return new object[]
{
"""
event: content_block_delta
data: {"type":"content_block_delta","index":1,"delta":{"type":"input_json_delta","partial_json":"renheit\"}"}}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockDelta,
Data = new ContentDeltaEventData()
{
Index = 1,
Delta = new JsonDelta()
{
Type = "input_json_delta",
PartialJson = "renheit\"}",
},
},
},
};
yield return new object[]
{
"""
event: content_block_stop
data: {"type":"content_block_stop","index":1}
""",
new AnthropicEvent()
{
Type = EventType.ContentBlockStop,
Data = new ContentStopEventData()
{
Index = 1,
},
},
};
yield return new object[]
{
"""
event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"tool_use","stop_sequence":null},"usage":{"output_tokens":89}}
""",
new AnthropicEvent()
{
Type = EventType.MessageDelta,
Data = new MessageDeltaEventData()
{
Delta = new MessageDelta()
{
StopReason = "tool_use",
StopSequence = null,
},
Usage = new Usage()
{
OutputTokens = 89,
},
},
}
};
yield return new object[]
{
"""
event: message_stop
data: {"type":"message_stop"}
""",
new AnthropicEvent()
{
Type = EventType.MessageStop,
Data = new MessageStopEventData(),
},
};
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
@@ -0,0 +1,978 @@
namespace AnthropicClient.Tests.Data;
public class JsonSchemaGeneratorTestData : IEnumerable<object[]>
{
private const string TestToolName = "Test tool name";
private const string TestToolDescription = "Test tool description";
public IEnumerator<object[]> GetEnumerator()
{
// string parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(string name) => name),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"name"
},
}
};
// GUID parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(Guid id) => id),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["id"] = new JsonObject()
{
["type"] = "string",
["format"] = "uuid",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"id"
},
}
};
// char parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(char name) => name),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"name"
},
}
};
// int parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(int age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// uint parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(uint age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// long parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(long age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// ulong parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(ulong age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// short parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(short age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// ushort parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(ushort age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// byte parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(byte age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// sbyte parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(sbyte age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// bool parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(bool isAdult) => isAdult),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["isAdult"] = new JsonObject()
{
["type"] = "boolean",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"isAdult"
},
}
};
// double parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(double age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "number",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// float parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(float age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "number",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// decimal parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(decimal age) => age),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["age"] = new JsonObject()
{
["type"] = "number",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"age"
},
}
};
// date time parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(DateTime date) => date),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["date"] = new JsonObject()
{
["type"] = "string",
["format"] = "date-time",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"date"
},
}
};
// date time offset parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(DateTimeOffset date) => date),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["date"] = new JsonObject()
{
["type"] = "string",
["format"] = "date-time",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"date"
},
}
};
// enum parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(DayOfWeek day) => day),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["day"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty,
["enum"] = new JsonArray()
{
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
},
}
},
["required"] = new JsonArray()
{
"day"
},
}
};
// array parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(int[] numbers) => numbers),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["numbers"] = new JsonObject()
{
["type"] = "array",
["items"] = new JsonObject()
{
["type"] = "integer"
},
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"numbers"
},
}
};
// list parameter type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(List<int> numbers) => numbers),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["numbers"] = new JsonObject()
{
["type"] = "array",
["items"] = new JsonObject()
{
["type"] = "integer"
},
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"numbers"
},
}
};
// class parameter type with no attributes
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(Person person) => person),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Person).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
},
["Age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Name",
"Age"
}
}
},
["properties"] = new JsonObject()
{
["person"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"person",
},
}
};
// nested class parameter type with no attributes
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(Family family) => family),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Person).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
},
["Age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Name",
"Age"
}
},
[$"{typeof(Family).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Members"] = new JsonObject()
{
["type"] = "array",
["items"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}"
},
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Members"
}
}
},
["properties"] = new JsonObject()
{
["family"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Family).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"family",
},
}
};
var family = new Family();
// parameters from instance method
yield return new object[]
{
Tool.CreateFromInstanceMethod(TestToolName,TestToolDescription, family, nameof(family.AddMember)),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Person).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
},
["Age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Name",
"Age"
}
},
},
["properties"] = new JsonObject()
{
["member"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"member",
},
}
};
// parameters from static method
yield return new object[]
{
Tool.CreateFromStaticMethod(TestToolName,TestToolDescription, typeof(Person), nameof(Person.Create)),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
},
["age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"name",
"age",
},
}
};
// multiple parameters of same complex type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(Person person1, Person person2) => person1),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Person).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
},
["Age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Name",
"Age"
}
}
},
["properties"] = new JsonObject()
{
["person1"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}",
["description"] = string.Empty
},
["person2"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"person1",
"person2"
},
}
};
// complex type with field
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(Dad dad) => dad),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Dad).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Role"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Role"
}
}
},
["properties"] = new JsonObject()
{
["dad"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Dad).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"dad",
},
}
};
// complex type with members of same type
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(NuclearFamily family) => family),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Person).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Name"] = new JsonObject()
{
["type"] = "string",
["description"] = string.Empty
},
["Age"] = new JsonObject()
{
["type"] = "integer",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Name",
"Age"
}
},
[$"{typeof(NuclearFamily).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Father"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}",
["description"] = string.Empty
},
["Mother"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Person).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"Father",
"Mother"
}
}
},
["properties"] = new JsonObject()
{
["family"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(NuclearFamily).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"family",
},
}
};
// string parameter type with custom name
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,([FunctionParameter("Person's Name", "Name", true)]string name) => name),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Name"] = new JsonObject()
{
["type"] = "string",
["description"] = "Person's Name"
}
},
["required"] = new JsonArray()
{
"Name"
},
}
};
// string parameter with custom description
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,([FunctionParameter("The name of the person.", required: true)]string name) => name),
new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["name"] = new JsonObject()
{
["type"] = "string",
["description"] = "The name of the person."
}
},
["required"] = new JsonArray()
{
"name"
},
}
};
// complex type with attributes
yield return new object[]
{
Tool.CreateFromFunction(TestToolName,TestToolDescription,(Rule rule) => rule),
new JsonObject()
{
["type"] = "object",
["definitions"] = new JsonObject()
{
[$"{typeof(Rule).FullName}"] = new JsonObject()
{
["type"] = "object",
["properties"] = new JsonObject()
{
["Status"] = new JsonObject()
{
["type"] = "string",
["description"] = "Indicates the current status of the rule.",
["default"] = "Active",
["enum"] = new JsonArray()
{
"Inactive",
"Active",
}
},
["Type"] = new JsonObject()
{
["type"] = "string",
["description"] = "The type of the rule.",
["default"] = "Type A",
["enum"] = new JsonArray()
{
"Type B",
"Type A",
}
}
},
["required"] = new JsonArray()
{
"Status"
}
}
},
["properties"] = new JsonObject()
{
["rule"] = new JsonObject()
{
["$ref"] = $"#/definitions/{typeof(Rule).FullName}",
["description"] = string.Empty
}
},
["required"] = new JsonArray()
{
"rule",
},
}
};
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
class Family
{
public List<Person> Members { get; } = [];
public bool AddMember(Person member)
{
Members.Add(member);
return true;
}
}
class NuclearFamily
{
public Person Father { get; } = new Person("Father", 40);
public Person Mother { get; } = new Person("Mother", 38);
}
class Person(string name, int age)
{
public string Name { get; } = name;
public int Age { get; } = age;
public static Person Create(string name, int age) => new Person(name, age);
}
class Dad
{
public string Role = "Father";
}
class Rule
{
[FunctionProperty(
"Indicates the current status of the rule.",
true,
"Active",
new object[] { "Active", "Inactive" }
)]
public string Status { get; } = "Active";
[FunctionProperty(
"The type of the rule.",
false,
"Type A",
new object[] { "Type B" }
)]
public string Type { get; } = "Type A";
}