diff --git a/.release-please-manifest.json b/.release-please-manifest.json index b6609d9f..895f6515 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "10.1.2" + ".": "10.2.0" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 61c0385b..08c53799 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,24 @@ # Changelog +## 10.2.0 (2025-11-20) + +Full Changelog: [v10.1.2...v10.2.0](https://github.com/anthropics/anthropic-sdk-csharp/compare/v10.1.2...v10.2.0) + +### Features + +* **client:** additional methods for positional params ([8bc6323](https://github.com/anthropics/anthropic-sdk-csharp/commit/8bc6323c38ce551f995bec5e4b1584460b7f037b)) + + +### Bug Fixes + +* **client:** return correct type for foundry#WithOptions ([#18](https://github.com/anthropics/anthropic-sdk-csharp/issues/18)) ([f814a46](https://github.com/anthropics/anthropic-sdk-csharp/commit/f814a460503abf7fdf7a824b5bf446ef74d60f28)) +* use correct versions ([c78c8db](https://github.com/anthropics/anthropic-sdk-csharp/commit/c78c8db4b6effa6b1438bb879bcafdad2d155808)) + + +### Refactors + +* **client:** make unknown variants implicit ([eb0e5b6](https://github.com/anthropics/anthropic-sdk-csharp/commit/eb0e5b628d7090adc34300775043ecd26ccfffaf)) + ## 10.1.2 (2025-11-18) Full Changelog: [v10.1.1...v10.1.2](https://github.com/anthropics/anthropic-sdk-csharp/compare/v10.1.1...v10.1.2) diff --git a/examples/MessagesExample/Program.cs b/examples/MessagesExample/Program.cs index 6fd7cfc8..6c8b0fc2 100644 --- a/examples/MessagesExample/Program.cs +++ b/examples/MessagesExample/Program.cs @@ -1,8 +1,7 @@ using System; using Anthropic; -using Anthropic.Models.Messages; using Anthropic.Foundry; - +using Anthropic.Models.Messages; // Configured using the ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL environment variables IAnthropicClient client = new AnthropicClient(); @@ -25,8 +24,7 @@ var message = String.Join( "", response - .Content - .Where(message => message.Value is TextBlock) + .Content.Where(message => message.Value is TextBlock) .Select(message => message.Value as TextBlock) .Select((textBlock) => textBlock.Text) ); diff --git a/src/Anthropic.Foundry/Anthropic.Foundry.csproj b/src/Anthropic.Foundry/Anthropic.Foundry.csproj index 7955e04b..8e87d087 100644 --- a/src/Anthropic.Foundry/Anthropic.Foundry.csproj +++ b/src/Anthropic.Foundry/Anthropic.Foundry.csproj @@ -1,58 +1,47 @@  - - - net8.0;net9.0;netstandard2.0 - enable - enable - Anthropic.Foundry - Anthropic.Foundry - - - - - - - - - - - - - true - true - Anthropic.Foundry - 0.0.1 - Stainless Software, Inc. - Stainless Software, Inc. - https://github.com/anthropics/anthropic-sdk-csharp - https://github.com/anthropics/anthropic-sdk-csharp - git - PRE-RELEASE NOT FOR DISTRIBUTION - Latest - - - true - - - true - - - true - snupkg - + true + + true + snupkg + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - - net8.0;net9.0;netstandard2.0 - - - - - %(RecursiveDir)/%(FileName)%(Extension) - PreserveNewest - - - + net8.0;net9.0;netstandard2.0 + + + + %(RecursiveDir)/%(FileName)%(Extension) + PreserveNewest + + diff --git a/src/Anthropic.Foundry/AnthropicFoundryBearerTokenCredentials.cs b/src/Anthropic.Foundry/AnthropicFoundryBearerTokenCredentials.cs index d735fd27..04977752 100644 --- a/src/Anthropic.Foundry/AnthropicFoundryBearerTokenCredentials.cs +++ b/src/Anthropic.Foundry/AnthropicFoundryBearerTokenCredentials.cs @@ -21,6 +21,7 @@ public AnthropicFoundryBearerTokenCredentials(string apiKey, string resourceName public void Apply(HttpRequestMessage requestMessage) { - requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", _apiKey); + requestMessage.Headers.Authorization = + new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", _apiKey); } } diff --git a/src/Anthropic.Foundry/AnthropicFoundryClient.cs b/src/Anthropic.Foundry/AnthropicFoundryClient.cs index 792cbd5c..04c02df4 100644 --- a/src/Anthropic.Foundry/AnthropicFoundryClient.cs +++ b/src/Anthropic.Foundry/AnthropicFoundryClient.cs @@ -2,8 +2,8 @@ global using ValueTask = System.Threading.Tasks.Task; #endif -using Anthropic.Core; using Anthropic; +using Anthropic.Core; namespace Anthropic.Foundry; @@ -21,21 +21,47 @@ public class AnthropicFoundryClient : AnthropicClient /// public AnthropicFoundryClient(IAnthropicFoundryCredentials azureCredentials) { - _azureCredentials = azureCredentials ?? throw new ArgumentNullException(nameof(azureCredentials)); + _azureCredentials = + azureCredentials ?? throw new ArgumentNullException(nameof(azureCredentials)); var url = $"https://{azureCredentials.ResourceName}.services.ai.azure.com/anthropic"; BaseUrl = new Uri(url, UriKind.Absolute); } + private AnthropicFoundryClient( + IAnthropicFoundryCredentials azureCredentials, + ClientOptions options + ) + : base(options) + { + _azureCredentials = + azureCredentials ?? throw new ArgumentNullException(nameof(azureCredentials)); + } + [Obsolete("The {nameof(APIKey)} property is not supported in this configuration.", true)] #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member public override string? APIKey #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member { - get => throw new NotSupportedException($"The {nameof(APIKey)} property is not supported in this configuration."); - init => throw new NotSupportedException($"The {nameof(APIKey)} property is not supported in this configuration."); + get => + throw new NotSupportedException( + $"The {nameof(APIKey)} property is not supported in this configuration." + ); + init => + throw new NotSupportedException( + $"The {nameof(APIKey)} property is not supported in this configuration." + ); + } + + public override IAnthropicClient WithOptions(Func modifier) + { + return new AnthropicFoundryClient(_azureCredentials, modifier(_options)); } - protected override ValueTask BeforeSend(HttpRequest request, HttpRequestMessage requestMessage, CancellationToken cancellationToken) + protected override ValueTask BeforeSend( + HttpRequest request, + HttpRequestMessage requestMessage, + CancellationToken cancellationToken + ) { _azureCredentials.Apply(requestMessage); diff --git a/src/Anthropic.Foundry/AnthropicFoundryIdentityTokenCredentials.cs b/src/Anthropic.Foundry/AnthropicFoundryIdentityTokenCredentials.cs index 1690ce88..ec17080f 100644 --- a/src/Anthropic.Foundry/AnthropicFoundryIdentityTokenCredentials.cs +++ b/src/Anthropic.Foundry/AnthropicFoundryIdentityTokenCredentials.cs @@ -18,7 +18,10 @@ public AnthropicFoundryIdentityTokenCredentials(AccessToken apiKey, string resou { if (apiKey.Equals(default) || string.IsNullOrWhiteSpace(apiKey.Token)) { - throw new ArgumentNullException(nameof(apiKey), "Invalid/Empty api key struct is not valid"); + throw new ArgumentNullException( + nameof(apiKey), + "Invalid/Empty api key struct is not valid" + ); } _apiKey = apiKey; ResourceName = resourceName ?? throw new ArgumentNullException(nameof(resourceName)); @@ -29,6 +32,7 @@ public AnthropicFoundryIdentityTokenCredentials(AccessToken apiKey, string resou public void Apply(HttpRequestMessage requestMessage) { //TODO implement token refresh - requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", _apiKey.Token); + requestMessage.Headers.Authorization = + new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", _apiKey.Token); } } diff --git a/src/Anthropic.Foundry/IAnthropicFoundryCredentials.cs b/src/Anthropic.Foundry/IAnthropicFoundryCredentials.cs index befcd680..763eb7e4 100644 --- a/src/Anthropic.Foundry/IAnthropicFoundryCredentials.cs +++ b/src/Anthropic.Foundry/IAnthropicFoundryCredentials.cs @@ -27,34 +27,47 @@ public class DefaultAnthropicFoundryCredentials /// ANTHROPIC_FOUNDRY_API_KEY=your_api_key /// /// or use Azure Identity to provide a token. - /// + /// /// If both are set, the API key environment variable will take precedence. - /// + /// /// /// The resource name or if null loaded from the environment variable ANTHROPIC_FOUNDRY_RESOURCE /// - public static async ValueTask FromEnv(string? resourceName = null) + public static async ValueTask FromEnv( + string? resourceName = null + ) { - if(Environment.GetEnvironmentVariable("ANTHROPIC_FOUNDRY_RESOURCE") is not string envResourceName - || (string.IsNullOrWhiteSpace(resourceName) && string.IsNullOrWhiteSpace(envResourceName))) + if ( + Environment.GetEnvironmentVariable("ANTHROPIC_FOUNDRY_RESOURCE") + is not string envResourceName + || ( + string.IsNullOrWhiteSpace(resourceName) + && string.IsNullOrWhiteSpace(envResourceName) + ) + ) { return null; } - if (Environment.GetEnvironmentVariable("ANTHROPIC_FOUNDRY_API_KEY") is string apiKey && !string.IsNullOrWhiteSpace(apiKey)) + if ( + Environment.GetEnvironmentVariable("ANTHROPIC_FOUNDRY_API_KEY") is string apiKey + && !string.IsNullOrWhiteSpace(apiKey) + ) { return new AnthropicFoundryApiKeyCredentials(apiKey, resourceName ?? envResourceName); } var defaultCredentialsProvider = new DefaultAzureCredential(); - var azureToken = await defaultCredentialsProvider.GetTokenAsync(new() - { - - }).ConfigureAwait(false); + var azureToken = await defaultCredentialsProvider + .GetTokenAsync(new() { }) + .ConfigureAwait(false); if (!azureToken.Equals(default)) { - return new AnthropicFoundryIdentityTokenCredentials(azureToken, resourceName ?? envResourceName); + return new AnthropicFoundryIdentityTokenCredentials( + azureToken, + resourceName ?? envResourceName + ); } return null; diff --git a/src/Anthropic.Tests/AnthropicTestClients.cs b/src/Anthropic.Tests/AnthropicTestClients.cs index 9bef486b..7172c476 100644 --- a/src/Anthropic.Tests/AnthropicTestClients.cs +++ b/src/Anthropic.Tests/AnthropicTestClients.cs @@ -18,29 +18,35 @@ public AnthropicTestClientsAttribute(TestSupportTypes testSupportTypes = TestSup public override IEnumerable GetData(MethodInfo testMethod) { - var dataServiceUrl = Environment.GetEnvironmentVariable("TEST_API_BASE_URL") ?? "http://localhost:4010"; + var dataServiceUrl = + Environment.GetEnvironmentVariable("TEST_API_BASE_URL") ?? "http://localhost:4010"; string apiKey = "YourApiKeyHere"; var resource = "YourRegionOrResourceHere"; var testData = testMethod.GetCustomAttributes().ToArray(); if (TestSupportTypes.HasFlag(TestSupportTypes.Anthropic)) { - yield return [ - new AnthropicClient() - { - BaseUrl = new Uri(dataServiceUrl), - APIKey = apiKey, - }, - ..testData.Where(e => e.TestSupport.HasFlag(TestSupportTypes.Anthropic)).Select(f => f.TestData).ToArray() + yield return + [ + new AnthropicClient() { BaseUrl = new Uri(dataServiceUrl), APIKey = apiKey }, + .. testData + .Where(e => e.TestSupport.HasFlag(TestSupportTypes.Anthropic)) + .Select(f => f.TestData) + .ToArray(), ]; } if (TestSupportTypes.HasFlag(TestSupportTypes.Foundry)) { - yield return [ - new AnthropicFoundryClient(new AnthropicFoundryApiKeyCredentials(apiKey, resource!)) { - BaseUrl = new Uri(dataServiceUrl) + yield return + [ + new AnthropicFoundryClient(new AnthropicFoundryApiKeyCredentials(apiKey, resource!)) + { + BaseUrl = new Uri(dataServiceUrl), }, - ..testData.Where(e => e.TestSupport.HasFlag(TestSupportTypes.Foundry)).Select(f => f.TestData).ToArray() + .. testData + .Where(e => e.TestSupport.HasFlag(TestSupportTypes.Foundry)) + .Select(f => f.TestData) + .ToArray(), ]; } } @@ -48,7 +54,7 @@ public override IEnumerable GetData(MethodInfo testMethod) [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = true)] sealed class AnthropicTestDataAttribute : Attribute -{ +{ public AnthropicTestDataAttribute(TestSupportTypes testSupport, object testData) { TestSupport = testSupport; @@ -64,5 +70,5 @@ public enum TestSupportTypes { All = Anthropic | Foundry, Anthropic = 1 << 1, - Foundry = 1 << 2 + Foundry = 1 << 2, } diff --git a/src/Anthropic.Tests/Services/Beta/FileServiceTest.cs b/src/Anthropic.Tests/Services/Beta/FileServiceTest.cs index 68ac21ae..789b1dbe 100644 --- a/src/Anthropic.Tests/Services/Beta/FileServiceTest.cs +++ b/src/Anthropic.Tests/Services/Beta/FileServiceTest.cs @@ -5,7 +5,6 @@ namespace Anthropic.Tests.Services.Beta; public class FileServiceTest { - [Theory] [AnthropicTestClients] public async Task List_Works(IAnthropicClient client) @@ -14,12 +13,11 @@ public async Task List_Works(IAnthropicClient client) page.Validate(); } - [Theory] [AnthropicTestClients] public async Task Delete_Works(IAnthropicClient client) { - var deletedFile = await client.Beta.Files.Delete(new() { FileID = "file_id" }); + var deletedFile = await client.Beta.Files.Delete("file_id"); deletedFile.Validate(); } @@ -27,16 +25,14 @@ public async Task Delete_Works(IAnthropicClient client) [AnthropicTestClients] public async Task Download_Works(IAnthropicClient client) { - await client.Beta.Files.Download(new() { FileID = "file_id" }); + await client.Beta.Files.Download("file_id"); } - + [Theory] [AnthropicTestClients] public async Task RetrieveMetadata_Works(IAnthropicClient client) { - var fileMetadata = await client.Beta.Files.RetrieveMetadata( - new() { FileID = "file_id" } - ); + var fileMetadata = await client.Beta.Files.RetrieveMetadata("file_id"); fileMetadata.Validate(); } } diff --git a/src/Anthropic.Tests/Services/Beta/MessageServiceTest.cs b/src/Anthropic.Tests/Services/Beta/MessageServiceTest.cs index fbab8814..4849f589 100644 --- a/src/Anthropic.Tests/Services/Beta/MessageServiceTest.cs +++ b/src/Anthropic.Tests/Services/Beta/MessageServiceTest.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; -using Anthropic.Tests; using Anthropic.Models.Beta.Messages; +using Anthropic.Tests; using Messages = Anthropic.Models.Messages; namespace Anthropic.Tests.Services.Beta; diff --git a/src/Anthropic.Tests/Services/Beta/Messages/BatchServiceTest.cs b/src/Anthropic.Tests/Services/Beta/Messages/BatchServiceTest.cs index f70f312d..f9b60870 100644 --- a/src/Anthropic.Tests/Services/Beta/Messages/BatchServiceTest.cs +++ b/src/Anthropic.Tests/Services/Beta/Messages/BatchServiceTest.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using System.Text.Json; using System.Threading.Tasks; -using Anthropic.Tests; using Anthropic.Models.Beta.Messages.Batches; +using Anthropic.Tests; using Messages = Anthropic.Models.Beta.Messages; namespace Anthropic.Tests.Services.Beta.Messages; @@ -140,18 +140,14 @@ public async Task Create_Works(IAnthropicClient client) betaMessageBatch.Validate(); } - [Theory] [AnthropicTestClients] public async Task Retrieve_Works(IAnthropicClient client) { - var betaMessageBatch = await client.Beta.Messages.Batches.Retrieve( - new() { MessageBatchID = "message_batch_id" } - ); + var betaMessageBatch = await client.Beta.Messages.Batches.Retrieve("message_batch_id"); betaMessageBatch.Validate(); } - [Theory] [AnthropicTestClients] public async Task List_Works(IAnthropicClient client) @@ -160,25 +156,19 @@ public async Task List_Works(IAnthropicClient client) page.Validate(); } - [Theory] [AnthropicTestClients] public async Task Delete_Works(IAnthropicClient client) { - var betaDeletedMessageBatch = await client.Beta.Messages.Batches.Delete( - new() { MessageBatchID = "message_batch_id" } - ); + var betaDeletedMessageBatch = await client.Beta.Messages.Batches.Delete("message_batch_id"); betaDeletedMessageBatch.Validate(); } - [Theory] [AnthropicTestClients] public async Task Cancel_Works(IAnthropicClient client) { - var betaMessageBatch = await client.Beta.Messages.Batches.Cancel( - new() { MessageBatchID = "message_batch_id" } - ); + var betaMessageBatch = await client.Beta.Messages.Batches.Cancel("message_batch_id"); betaMessageBatch.Validate(); } @@ -186,9 +176,7 @@ public async Task Cancel_Works(IAnthropicClient client) [AnthropicTestClients] public async Task ResultsStreaming_Works(IAnthropicClient client) { - var stream = client.Beta.Messages.Batches.ResultsStreaming( - new() { MessageBatchID = "message_batch_id" } - ); + var stream = client.Beta.Messages.Batches.ResultsStreaming("message_batch_id"); await foreach (var betaMessageBatchIndividualResponse in stream) { diff --git a/src/Anthropic.Tests/Services/Beta/ModelServiceTest.cs b/src/Anthropic.Tests/Services/Beta/ModelServiceTest.cs index 79969876..9fef26cb 100644 --- a/src/Anthropic.Tests/Services/Beta/ModelServiceTest.cs +++ b/src/Anthropic.Tests/Services/Beta/ModelServiceTest.cs @@ -5,16 +5,14 @@ namespace Anthropic.Tests.Services.Beta; public class ModelServiceTest { - [Theory] [AnthropicTestClients(TestSupportTypes.Anthropic)] public async Task Retrieve_Works(IAnthropicClient client) { - var betaModelInfo = await client.Beta.Models.Retrieve(new() { ModelID = "model_id" }); + var betaModelInfo = await client.Beta.Models.Retrieve("model_id"); betaModelInfo.Validate(); } - [Theory] [AnthropicTestClients] public async Task List_Works(IAnthropicClient client) diff --git a/src/Anthropic.Tests/Services/Beta/SkillServiceTest.cs b/src/Anthropic.Tests/Services/Beta/SkillServiceTest.cs index 1dde4588..ea6ffc89 100644 --- a/src/Anthropic.Tests/Services/Beta/SkillServiceTest.cs +++ b/src/Anthropic.Tests/Services/Beta/SkillServiceTest.cs @@ -17,11 +17,10 @@ public async Task Create_Works(IAnthropicClient client) [AnthropicTestClients] public async Task Retrieve_Works(IAnthropicClient client) { - var skill = await client.Beta.Skills.Retrieve(new() { SkillID = "skill_id" }); + var skill = await client.Beta.Skills.Retrieve("skill_id"); skill.Validate(); } - [Theory] [AnthropicTestClients] public async Task List_Works(IAnthropicClient client) @@ -30,12 +29,11 @@ public async Task List_Works(IAnthropicClient client) page.Validate(); } - [Theory] [AnthropicTestClients] public async Task Delete_Works(IAnthropicClient client) { - var skill = await client.Beta.Skills.Delete(new() { SkillID = "skill_id" }); + var skill = await client.Beta.Skills.Delete("skill_id"); skill.Validate(); } } diff --git a/src/Anthropic.Tests/Services/Beta/Skills/VersionServiceTest.cs b/src/Anthropic.Tests/Services/Beta/Skills/VersionServiceTest.cs index 5682fcea..ba30bcfc 100644 --- a/src/Anthropic.Tests/Services/Beta/Skills/VersionServiceTest.cs +++ b/src/Anthropic.Tests/Services/Beta/Skills/VersionServiceTest.cs @@ -9,37 +9,36 @@ public class VersionServiceTest [AnthropicTestClients] public async Task Create_Works(IAnthropicClient client) { - var version = await client.Beta.Skills.Versions.Create(new() { SkillID = "skill_id" }); + var version = await client.Beta.Skills.Versions.Create("skill_id"); version.Validate(); } - [Theory] [AnthropicTestClients] public async Task Retrieve_Works(IAnthropicClient client) { var version = await client.Beta.Skills.Versions.Retrieve( - new() { SkillID = "skill_id", Version = "version" } + "version", + new() { SkillID = "skill_id" } ); version.Validate(); } - [Theory] [AnthropicTestClients] public async Task List_Works(IAnthropicClient client) { - var page = await client.Beta.Skills.Versions.List(new() { SkillID = "skill_id" }); + var page = await client.Beta.Skills.Versions.List("skill_id"); page.Validate(); } - [Theory] [AnthropicTestClients] public async Task Delete_Works(IAnthropicClient client) { var version = await client.Beta.Skills.Versions.Delete( - new() { SkillID = "skill_id", Version = "version" } + "version", + new() { SkillID = "skill_id" } ); version.Validate(); } diff --git a/src/Anthropic.Tests/Services/MessageServiceTest.cs b/src/Anthropic.Tests/Services/MessageServiceTest.cs index 559df9f7..d2ecd568 100644 --- a/src/Anthropic.Tests/Services/MessageServiceTest.cs +++ b/src/Anthropic.Tests/Services/MessageServiceTest.cs @@ -1,7 +1,7 @@ using System.Linq; using System.Threading.Tasks; -using Anthropic.Tests; using Anthropic.Models.Messages; +using Anthropic.Tests; namespace Anthropic.Tests.Services; @@ -18,7 +18,7 @@ public async Task Create_Works(IAnthropicClient client, string modelName) { MaxTokens = 1024, Messages = [new() { Content = "Hello, world", Role = Role.User }], - Model = modelName + Model = modelName, } ); message.Validate(); @@ -52,11 +52,7 @@ public async Task CreateStreaming_Works(IAnthropicClient client, string modelNam public async Task CountTokens_Works(IAnthropicClient client, string modelName) { var messageTokensCount = await client.Messages.CountTokens( - new() - { - Messages = [new() { Content = "string", Role = Role.User }], - Model = modelName, - } + new() { Messages = [new() { Content = "string", Role = Role.User }], Model = modelName } ); messageTokensCount.Validate(); } diff --git a/src/Anthropic.Tests/Services/Messages/BatchServiceTest.cs b/src/Anthropic.Tests/Services/Messages/BatchServiceTest.cs index d8658837..cff59e79 100644 --- a/src/Anthropic.Tests/Services/Messages/BatchServiceTest.cs +++ b/src/Anthropic.Tests/Services/Messages/BatchServiceTest.cs @@ -1,8 +1,8 @@ using System.Collections.Generic; using System.Text.Json; using System.Threading.Tasks; -using Anthropic.Tests; using Anthropic.Models.Messages.Batches; +using Anthropic.Tests; using Messages = Anthropic.Models.Messages; namespace Anthropic.Tests.Services.Messages; @@ -10,7 +10,7 @@ namespace Anthropic.Tests.Services.Messages; public class BatchServiceTest { [Theory] - [AnthropicTestClients] + [AnthropicTestClients] [AnthropicTestData(TestSupportTypes.Anthropic, "Claude3_7SonnetLatest")] [AnthropicTestData(TestSupportTypes.Foundry, "claude-sonnet-45-2")] public async Task Create_Works(IAnthropicClient client, string modelName) @@ -28,7 +28,11 @@ public async Task Create_Works(IAnthropicClient client, string modelName) MaxTokens = 1024, Messages = [ - new() { Content = "Hello, world", Role = Models.Messages.Role.User }, + new() + { + Content = "Hello, world", + Role = Models.Messages.Role.User, + }, ], Model = modelName, Metadata = new() { UserID = "13803d75-b4b5-4c3e-b2a2-6f21399b021b" }, @@ -97,9 +101,7 @@ public async Task Create_Works(IAnthropicClient client, string modelName) [AnthropicTestClients] public async Task Retrieve_Works(IAnthropicClient client) { - var messageBatch = await client.Messages.Batches.Retrieve( - new() { MessageBatchID = "message_batch_id" } - ); + var messageBatch = await client.Messages.Batches.Retrieve("message_batch_id"); messageBatch.Validate(); } @@ -115,9 +117,7 @@ public async Task List_Works(IAnthropicClient client) [AnthropicTestClients] public async Task Delete_Works(IAnthropicClient client) { - var deletedMessageBatch = await client.Messages.Batches.Delete( - new() { MessageBatchID = "message_batch_id" } - ); + var deletedMessageBatch = await client.Messages.Batches.Delete("message_batch_id"); deletedMessageBatch.Validate(); } @@ -125,9 +125,7 @@ public async Task Delete_Works(IAnthropicClient client) [AnthropicTestClients] public async Task Cancel_Works(IAnthropicClient client) { - var messageBatch = await client.Messages.Batches.Cancel( - new() { MessageBatchID = "message_batch_id" } - ); + var messageBatch = await client.Messages.Batches.Cancel("message_batch_id"); messageBatch.Validate(); } @@ -135,9 +133,7 @@ public async Task Cancel_Works(IAnthropicClient client) [AnthropicTestClients] public async Task ResultsStreaming_Works(IAnthropicClient client) { - var stream = client.Messages.Batches.ResultsStreaming( - new() { MessageBatchID = "message_batch_id" } - ); + var stream = client.Messages.Batches.ResultsStreaming("message_batch_id"); await foreach (var messageBatchIndividualResponse in stream) { diff --git a/src/Anthropic.Tests/Services/ModelServiceTest.cs b/src/Anthropic.Tests/Services/ModelServiceTest.cs index 9060e273..fc6f8ba4 100644 --- a/src/Anthropic.Tests/Services/ModelServiceTest.cs +++ b/src/Anthropic.Tests/Services/ModelServiceTest.cs @@ -4,16 +4,15 @@ namespace Anthropic.Tests.Services; public class ModelServiceTest -{ +{ [Theory] [AnthropicTestClients(TestSupportTypes.Anthropic)] public async Task Retrieve_Works(IAnthropicClient client) { - var modelInfo = await client.Models.Retrieve(new() { ModelID = "model_id" }); + var modelInfo = await client.Models.Retrieve("model_id"); modelInfo.Validate(); } - [Theory] [AnthropicTestClients] public async Task List_Works(IAnthropicClient client) diff --git a/src/Anthropic/Anthropic.csproj b/src/Anthropic/Anthropic.csproj index 82d63e7d..3510cf0b 100644 --- a/src/Anthropic/Anthropic.csproj +++ b/src/Anthropic/Anthropic.csproj @@ -6,7 +6,7 @@ SDK Code Generation Anthropic C# MIT enable - 10.1.2 + 10.2.0 net8.0;net9.0;netstandard2.0 latest @@ -29,33 +29,27 @@ true true disable - Anthropic true - true - 10.1.2 - Stainless Software, Inc. - Stainless Software, Inc. - https://github.com/anthropics/anthropic-sdk-csharp - https://github.com/anthropics/anthropic-sdk-csharp - git - PRE-RELEASE NOT FOR DISTRIBUTION - Latest - - - true - - - true - - - true - snupkg - $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb - - net8.0;net9.0;netstandard2.0 + true + 10.2.0 + Stainless Software, Inc. + Stainless Software, Inc. + https://github.com/anthropics/anthropic-sdk-csharp + https://github.com/anthropics/anthropic-sdk-csharp + git + PRE-RELEASE NOT FOR DISTRIBUTION + Latest + + true + + true + + true + snupkg + $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb + net8.0;net9.0;netstandard2.0 - diff --git a/src/Anthropic/AnthropicClient.cs b/src/Anthropic/AnthropicClient.cs index aa3de14b..c367c846 100644 --- a/src/Anthropic/AnthropicClient.cs +++ b/src/Anthropic/AnthropicClient.cs @@ -21,7 +21,7 @@ static Random Random get { return _threadLocalRandom.Value!; } } - readonly ClientOptions _options; + protected readonly ClientOptions _options; public HttpClient HttpClient { @@ -65,7 +65,7 @@ public virtual string? AuthToken init { this._options.AuthToken = value; } } - public IAnthropicClient WithOptions(Func modifier) + public virtual IAnthropicClient WithOptions(Func modifier) { return new AnthropicClient(modifier(this._options)); } @@ -146,13 +146,21 @@ await response.ReadAsString(cancellationToken).ConfigureAwait(false) } } - protected virtual ValueTask BeforeSend(HttpRequest request, HttpRequestMessage requestMessage, CancellationToken cancellationToken) + protected virtual ValueTask BeforeSend( + HttpRequest request, + HttpRequestMessage requestMessage, + CancellationToken cancellationToken + ) where T : ParamsBase { return ValueTask.CompletedTask; } - protected virtual ValueTask AfterSend(HttpRequest request, HttpResponseMessage httpResponseMessage, CancellationToken cancellationToken) + protected virtual ValueTask AfterSend( + HttpRequest request, + HttpResponseMessage httpResponseMessage, + CancellationToken cancellationToken + ) where T : ParamsBase { return ValueTask.CompletedTask; @@ -227,9 +235,9 @@ static TimeSpan ComputeRetryBackoff(int retries, HttpResponse? response) if (float.TryParse(headerValue #if NET5_0_OR_GREATER - .AsSpan() + .AsSpan() #endif - , out var retryAfterMs)) + , out var retryAfterMs)) { return TimeSpan.FromMilliseconds(retryAfterMs); } @@ -249,17 +257,17 @@ static TimeSpan ComputeRetryBackoff(int retries, HttpResponse? response) if (float.TryParse(headerValue #if NET5_0_OR_GREATER - .AsSpan() + .AsSpan() #endif - , out var retryAfterSeconds)) + , out var retryAfterSeconds)) { return TimeSpan.FromSeconds(retryAfterSeconds); } else if (DateTimeOffset.TryParse(headerValue #if NET5_0_OR_GREATER - .AsSpan() + .AsSpan() #endif - , out var retryAfterDate)) + , out var retryAfterDate)) { return retryAfterDate - DateTimeOffset.Now; } @@ -288,7 +296,7 @@ static bool ShouldRetry(HttpResponse response) or #if !NETSTANDARD2_0_OR_GREATER // Retry on rate limits - HttpStatusCode.TooManyRequests + HttpStatusCode.TooManyRequests or #endif // Retry internal errors diff --git a/src/Anthropic/Core/ApiEnum.cs b/src/Anthropic/Core/ApiEnum.cs index f8122766..f20b3632 100644 --- a/src/Anthropic/Core/ApiEnum.cs +++ b/src/Anthropic/Core/ApiEnum.cs @@ -20,12 +20,11 @@ public readonly TEnum Value() => public readonly void Validate() { - #if NET7_0_OR_GREATER +#if NET7_0_OR_GREATER if (!Enum.IsDefined(Value())) - #else +#else if (!Enum.GetValues(typeof(TEnum)).OfType().Contains(Value())) - #endif - +#endif { throw new AnthropicInvalidDataException("Invalid enum value"); } diff --git a/src/Anthropic/Core/FreezableDictionary.cs b/src/Anthropic/Core/FreezableDictionary.cs index 5f3fafb3..b27b40b2 100644 --- a/src/Anthropic/Core/FreezableDictionary.cs +++ b/src/Anthropic/Core/FreezableDictionary.cs @@ -126,6 +126,7 @@ public bool Remove(KeyValuePair item) { return _mutableDictionary.Remove(item.Key); } + #if NETSTANDARD2_0 public bool TryGetValue(K key, out V value) { @@ -138,7 +139,6 @@ public bool TryGetValue(K key, [MaybeNullWhenAttribute(false)] out V value) } #endif - Collections::IEnumerator Collections::IEnumerable.GetEnumerator() { return _dictionary.GetEnumerator(); diff --git a/src/Anthropic/Core/ModelConverter.cs b/src/Anthropic/Core/ModelConverter.cs index 5db0290a..017601b2 100644 --- a/src/Anthropic/Core/ModelConverter.cs +++ b/src/Anthropic/Core/ModelConverter.cs @@ -24,8 +24,7 @@ JsonSerializerOptions options #if NET5_0_OR_GREATER return TModel.FromRawUnchecked(properties); #else - return (TModel)ModelConverterConstructionShim - .FromRawFactories[typeof(TModel)](properties); + return (TModel)ModelConverterConstructionShim.FromRawFactories[typeof(TModel)](properties); #endif } diff --git a/src/Anthropic/Core/ParamsBase.cs b/src/Anthropic/Core/ParamsBase.cs index a1eab3c4..81df81ed 100644 --- a/src/Anthropic/Core/ParamsBase.cs +++ b/src/Anthropic/Core/ParamsBase.cs @@ -215,7 +215,7 @@ static string GetOSArch() => Architecture.Arm => "arm", Architecture.Arm64 #if NET5_0_OR_GREATER - or Architecture.Armv6 + or Architecture.Armv6 #endif => "arm64", #if NET5_0_OR_GREATER diff --git a/src/Anthropic/Core/SseMessage.cs b/src/Anthropic/Core/SseMessage.cs index 5a5596e0..5acf8544 100644 --- a/src/Anthropic/Core/SseMessage.cs +++ b/src/Anthropic/Core/SseMessage.cs @@ -19,11 +19,10 @@ internal static async IAsyncEnumerable GetEnumerable( using var stream = await response .Content - #if NET5_0_OR_GREATER .ReadAsStreamAsync(cancellationToken) #else - .ReadAsStreamAsync() + .ReadAsStreamAsync() #endif .ConfigureAwait(false); using var reader = new StreamReader(stream); diff --git a/src/Anthropic/GlobalShims.cs b/src/Anthropic/GlobalShims.cs index e1f74325..47a85eaa 100644 --- a/src/Anthropic/GlobalShims.cs +++ b/src/Anthropic/GlobalShims.cs @@ -15,7 +15,6 @@ using System.Threading.Tasks; using System.Text.Json; - /* ValueTask and related async interface types are provided by referenced compatibility packages (e.g. System.Threading.Tasks.Extensions, Microsoft.Bcl.AsyncInterfaces). Defining them here can conflict with @@ -38,7 +37,14 @@ public static class IsExternalInit { } // Supports `required` members feature: marks members that must be // initialized during object construction. - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event, Inherited = false)] + [AttributeUsage( + AttributeTargets.Class + | AttributeTargets.Struct + | AttributeTargets.Property + | AttributeTargets.Field + | AttributeTargets.Event, + Inherited = false + )] public sealed class RequiredMemberAttribute : Attribute { } // Applied to constructors (or factory methods) that ensure required @@ -57,7 +63,11 @@ namespace System.Runtime.CompilerServices [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = false)] public sealed class CompilerFeatureRequiredAttribute : Attribute { - public CompilerFeatureRequiredAttribute(string feature) { Feature = feature; } + public CompilerFeatureRequiredAttribute(string feature) + { + Feature = feature; + } + public string Feature { get; } } } @@ -70,7 +80,14 @@ public sealed class SetsRequiredMembersAttribute : Attribute public SetsRequiredMembersAttribute() { } } - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event, Inherited = false)] + [AttributeUsage( + AttributeTargets.Class + | AttributeTargets.Struct + | AttributeTargets.Property + | AttributeTargets.Field + | AttributeTargets.Event, + Inherited = false + )] public sealed class RequiredMemberAttribute : Attribute { public RequiredMemberAttribute() { } @@ -95,31 +112,47 @@ namespace System.Collections.Frozen { // Provide a FrozenDictionary in the System.Collections.Frozen namespace // to match usages of that API in generated code. - public class FrozenDictionary : System.Collections.Generic.Dictionary + public class FrozenDictionary + : System.Collections.Generic.Dictionary { - public FrozenDictionary() : base() { } - public FrozenDictionary(System.Collections.Generic.IDictionary source) : base(source) { } + public FrozenDictionary() + : base() { } + + public FrozenDictionary(System.Collections.Generic.IDictionary source) + : base(source) { } - public static FrozenDictionary ToFrozenDictionary(System.Collections.Generic.IDictionary source) + public static FrozenDictionary ToFrozenDictionary( + System.Collections.Generic.IDictionary source + ) { - if (source is FrozenDictionary fd) return fd; + if (source is FrozenDictionary fd) + return fd; return new FrozenDictionary(source); } } + // Provide a FrozenDictionary in the System.Collections.Frozen namespace // to match usages of that API in generated code. public class FrozenDictionary { - public static FrozenDictionary ToFrozenDictionary(System.Collections.Generic.IDictionary source) + public static FrozenDictionary ToFrozenDictionary( + System.Collections.Generic.IDictionary source + ) { - if (source is FrozenDictionary fd) return fd; + if (source is FrozenDictionary fd) + return fd; return new FrozenDictionary(source); } - public static FrozenDictionary ToFrozenDictionary(System.Collections.Generic.IReadOnlyDictionary source) + public static FrozenDictionary ToFrozenDictionary( + System.Collections.Generic.IReadOnlyDictionary source + ) { - if (source is FrozenDictionary fd) return fd; - return new FrozenDictionary(source.ToDictionary(e => e.Key, e => e.Value)); + if (source is FrozenDictionary fd) + return fd; + return new FrozenDictionary( + source.ToDictionary(e => e.Key, e => e.Value) + ); } } } @@ -158,7 +191,9 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() return GetEnumerator(); } - public static ImmutableArray ToImmutableArray(System.Collections.Generic.IEnumerable source) + public static ImmutableArray ToImmutableArray( + System.Collections.Generic.IEnumerable source + ) { return new ImmutableArray(source.ToArray()); } @@ -166,7 +201,9 @@ public static ImmutableArray ToImmutableArray(System.Collections.Generic.IEnu public class ImmutableArray { - public static ImmutableArray ToImmutableArray(System.Collections.Generic.IEnumerable source) + public static ImmutableArray ToImmutableArray( + System.Collections.Generic.IEnumerable source + ) { return ImmutableArray.ToImmutableArray(source); } @@ -194,20 +231,24 @@ public static class ModelConverterConstructionShim static ModelConverterConstructionShim() { - Assembly.GetCallingAssembly() + Assembly + .GetCallingAssembly() .GetTypes() .Where(t => t.IsSubclassOf(typeof(Anthropic.Core.ModelBase))) .ToList() .ForEach(t => { var converterType = typeof(Anthropic.Core.ModelConverter<>).MakeGenericType(t); - var converterMethod = converterType.GetMethod(FromRawUncheckedMethodName, BindingFlags.Static | BindingFlags.Public); + var converterMethod = converterType.GetMethod( + FromRawUncheckedMethodName, + BindingFlags.Static | BindingFlags.Public + ); if (converterMethod is null) { return; } var fromRaw = - (Func, object>) + (Func, object>) Delegate.CreateDelegate( typeof(Func, object>), converterMethod @@ -216,8 +257,11 @@ static ModelConverterConstructionShim() }); } - internal static Dictionary, object>> FromRawFactories { get; } - = new Dictionary, object>>(); + internal static Dictionary< + Type, + Func, object> + > FromRawFactories { get; } = + new Dictionary, object>>(); } #endif diff --git a/src/Anthropic/Models/Beta/BetaError.cs b/src/Anthropic/Models/Beta/BetaError.cs index 27af02b1..23c79353 100644 --- a/src/Anthropic/Models/Beta/BetaError.cs +++ b/src/Anthropic/Models/Beta/BetaError.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta; [JsonConverter(typeof(BetaErrorConverter))] public record class BetaError { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string Message { @@ -48,59 +54,63 @@ public JsonElement Type } } - public BetaError(BetaInvalidRequestError value) - { - Value = value; - } - - public BetaError(BetaAuthenticationError value) + public BetaError(BetaInvalidRequestError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaError(BetaBillingError value) + public BetaError(BetaAuthenticationError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaError(BetaPermissionError value) + public BetaError(BetaBillingError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaError(BetaNotFoundError value) + public BetaError(BetaPermissionError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaError(BetaRateLimitError value) + public BetaError(BetaNotFoundError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaError(BetaGatewayTimeoutError value) + public BetaError(BetaRateLimitError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaError(BetaAPIError value) + public BetaError(BetaGatewayTimeoutError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaError(BetaOverloadedError value) + public BetaError(BetaAPIError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaError(UnknownVariant value) + public BetaError(BetaOverloadedError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaError CreateUnknownVariant(JsonElement value) + public BetaError(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickInvalidRequest([NotNullWhen(true)] out BetaInvalidRequestError? value) @@ -254,13 +264,11 @@ Func overloaded public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of BetaError"); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaErrorConverter : JsonConverter @@ -286,8 +294,6 @@ JsonSerializerOptions options { case "invalid_request_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -297,25 +303,18 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaInvalidRequestError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "authentication_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -325,50 +324,36 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaAuthenticationError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "billing_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBillingError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "permission_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -378,50 +363,36 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaPermissionError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "not_found_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaNotFoundError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "rate_limit_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -431,25 +402,18 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRateLimitError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "timeout_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -459,50 +423,36 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaGatewayTimeoutError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "api_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaAPIError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "overloaded_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -512,26 +462,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaError(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaOverloadedError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaError(json); } } } @@ -542,7 +485,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Files/FileDeleteParams.cs b/src/Anthropic/Models/Beta/Files/FileDeleteParams.cs index da657521..63221e15 100644 --- a/src/Anthropic/Models/Beta/Files/FileDeleteParams.cs +++ b/src/Anthropic/Models/Beta/Files/FileDeleteParams.cs @@ -14,7 +14,7 @@ namespace Anthropic.Models.Beta.Files; /// public sealed record class FileDeleteParams : ParamsBase { - public required string FileID { get; init; } + public string? FileID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Files/FileDownloadParams.cs b/src/Anthropic/Models/Beta/Files/FileDownloadParams.cs index ea160600..c51a33d8 100644 --- a/src/Anthropic/Models/Beta/Files/FileDownloadParams.cs +++ b/src/Anthropic/Models/Beta/Files/FileDownloadParams.cs @@ -14,7 +14,7 @@ namespace Anthropic.Models.Beta.Files; /// public sealed record class FileDownloadParams : ParamsBase { - public required string FileID { get; init; } + public string? FileID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Files/FileRetrieveMetadataParams.cs b/src/Anthropic/Models/Beta/Files/FileRetrieveMetadataParams.cs index 4422a2da..5bd5183c 100644 --- a/src/Anthropic/Models/Beta/Files/FileRetrieveMetadataParams.cs +++ b/src/Anthropic/Models/Beta/Files/FileRetrieveMetadataParams.cs @@ -14,7 +14,7 @@ namespace Anthropic.Models.Beta.Files; /// public sealed record class FileRetrieveMetadataParams : ParamsBase { - public required string FileID { get; init; } + public string? FileID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Messages/Batches/BatchCancelParams.cs b/src/Anthropic/Models/Beta/Messages/Batches/BatchCancelParams.cs index a845f5dd..0fba8383 100644 --- a/src/Anthropic/Models/Beta/Messages/Batches/BatchCancelParams.cs +++ b/src/Anthropic/Models/Beta/Messages/Batches/BatchCancelParams.cs @@ -22,7 +22,7 @@ namespace Anthropic.Models.Beta.Messages.Batches; /// public sealed record class BatchCancelParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Messages/Batches/BatchCreateParams.cs b/src/Anthropic/Models/Beta/Messages/Batches/BatchCreateParams.cs index e18f97a5..fc2238c2 100644 --- a/src/Anthropic/Models/Beta/Messages/Batches/BatchCreateParams.cs +++ b/src/Anthropic/Models/Beta/Messages/Batches/BatchCreateParams.cs @@ -953,28 +953,30 @@ public static Params FromRawUnchecked(IReadOnlyDictionary p [JsonConverter(typeof(global::Anthropic.Models.Beta.Messages.Batches.ContainerConverter))] public record class Container { - public object Value { get; private init; } + public object? Value { get; } = null; - public Container(BetaContainerParams value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public Container(string value) + public Container(BetaContainerParams value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Container(UnknownVariant value) + public Container(string value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static global::Anthropic.Models.Beta.Messages.Batches.Container CreateUnknownVariant( - JsonElement value - ) + public Container(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaContainerParams([NotNullWhen(true)] out BetaContainerParams? value) @@ -1034,13 +1036,11 @@ string value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Container"); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContainerConverter @@ -1052,43 +1052,35 @@ sealed class ContainerConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new global::Anthropic.Models.Beta.Messages.Batches.Container(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaContainerParams'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new global::Anthropic.Models.Beta.Messages.Batches.Container(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -1097,8 +1089,7 @@ public override void Write( JsonSerializerOptions options ) { - object? variant = value?.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value?.Json, options); } } @@ -1170,26 +1161,30 @@ JsonSerializerOptions options [JsonConverter(typeof(ParamsSystemConverter))] public record class ParamsSystem { - public object Value { get; private init; } + public object? Value { get; } = null; - public ParamsSystem(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public ParamsSystem(IReadOnlyList value) + public ParamsSystem(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - ParamsSystem(UnknownVariant value) + public ParamsSystem(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static ParamsSystem CreateUnknownVariant(JsonElement value) + public ParamsSystem(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -1248,15 +1243,13 @@ public static implicit operator ParamsSystem(List value) => public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ParamsSystem" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ParamsSystemConverter : JsonConverter @@ -1267,45 +1260,34 @@ sealed class ParamsSystemConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new ParamsSystem(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new ParamsSystem(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -1314,7 +1296,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/Batches/BatchDeleteParams.cs b/src/Anthropic/Models/Beta/Messages/Batches/BatchDeleteParams.cs index b7088d60..d2ff4a80 100644 --- a/src/Anthropic/Models/Beta/Messages/Batches/BatchDeleteParams.cs +++ b/src/Anthropic/Models/Beta/Messages/Batches/BatchDeleteParams.cs @@ -19,7 +19,7 @@ namespace Anthropic.Models.Beta.Messages.Batches; /// public sealed record class BatchDeleteParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Messages/Batches/BatchResultsParams.cs b/src/Anthropic/Models/Beta/Messages/Batches/BatchResultsParams.cs index ee8928dc..1339cabf 100644 --- a/src/Anthropic/Models/Beta/Messages/Batches/BatchResultsParams.cs +++ b/src/Anthropic/Models/Beta/Messages/Batches/BatchResultsParams.cs @@ -20,7 +20,7 @@ namespace Anthropic.Models.Beta.Messages.Batches; /// public sealed record class BatchResultsParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Messages/Batches/BatchRetrieveParams.cs b/src/Anthropic/Models/Beta/Messages/Batches/BatchRetrieveParams.cs index 1c0e51ad..cdfc312c 100644 --- a/src/Anthropic/Models/Beta/Messages/Batches/BatchRetrieveParams.cs +++ b/src/Anthropic/Models/Beta/Messages/Batches/BatchRetrieveParams.cs @@ -18,7 +18,7 @@ namespace Anthropic.Models.Beta.Messages.Batches; /// public sealed record class BatchRetrieveParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Messages/Batches/BetaMessageBatchResult.cs b/src/Anthropic/Models/Beta/Messages/Batches/BetaMessageBatchResult.cs index 41a4bf6d..33f6f5c8 100644 --- a/src/Anthropic/Models/Beta/Messages/Batches/BetaMessageBatchResult.cs +++ b/src/Anthropic/Models/Beta/Messages/Batches/BetaMessageBatchResult.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -17,7 +16,14 @@ namespace Anthropic.Models.Beta.Messages.Batches; [JsonConverter(typeof(BetaMessageBatchResultConverter))] public record class BetaMessageBatchResult { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -32,34 +38,33 @@ public JsonElement Type } } - public BetaMessageBatchResult(BetaMessageBatchSucceededResult value) + public BetaMessageBatchResult(BetaMessageBatchSucceededResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaMessageBatchResult(BetaMessageBatchErroredResult value) + public BetaMessageBatchResult(BetaMessageBatchErroredResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaMessageBatchResult(BetaMessageBatchCanceledResult value) + public BetaMessageBatchResult(BetaMessageBatchCanceledResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaMessageBatchResult(BetaMessageBatchExpiredResult value) + public BetaMessageBatchResult(BetaMessageBatchExpiredResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaMessageBatchResult(UnknownVariant value) + public BetaMessageBatchResult(JsonElement json) { - Value = value; - } - - public static BetaMessageBatchResult CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickSucceeded([NotNullWhen(true)] out BetaMessageBatchSucceededResult? value) @@ -147,15 +152,13 @@ public static implicit operator BetaMessageBatchResult(BetaMessageBatchExpiredRe public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaMessageBatchResult" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaMessageBatchResultConverter : JsonConverter @@ -181,8 +184,6 @@ JsonSerializerOptions options { case "succeeded": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -192,26 +193,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMessageBatchSucceededResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "errored": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -221,26 +215,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMessageBatchErroredResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "canceled": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -250,26 +237,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMessageBatchCanceledResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "expired": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -279,27 +259,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMessageBatchExpiredResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaMessageBatchResult(json); } } } @@ -310,7 +283,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlock.cs b/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlock.cs index 7564b896..950288d3 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlock.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlock.cs @@ -134,7 +134,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(ContentConverter))] public record class Content { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -147,24 +154,21 @@ public JsonElement Type } } - public Content(BetaBashCodeExecutionToolResultError value) + public Content(BetaBashCodeExecutionToolResultError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Content(BetaBashCodeExecutionResultBlock value) + public Content(BetaBashCodeExecutionResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Content(UnknownVariant value) + public Content(JsonElement json) { - Value = value; - } - - public static Content CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaBashCodeExecutionToolResultError( @@ -227,13 +231,11 @@ public static implicit operator Content(BetaBashCodeExecutionToolResultError val public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Content"); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContentConverter : JsonConverter @@ -244,58 +246,46 @@ sealed class ContentConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Content(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBashCodeExecutionToolResultError'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Content(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBashCodeExecutionResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write(Utf8JsonWriter writer, Content value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlockParam.cs b/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlockParam.cs index d8cf786e..33a45e8d 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlockParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaBashCodeExecutionToolResultBlockParam.cs @@ -164,7 +164,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(BetaBashCodeExecutionToolResultBlockParamContentConverter))] public record class BetaBashCodeExecutionToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -178,29 +185,26 @@ public JsonElement Type } public BetaBashCodeExecutionToolResultBlockParamContent( - BetaBashCodeExecutionToolResultErrorParam value + BetaBashCodeExecutionToolResultErrorParam value, + JsonElement? json = null ) { - Value = value; + this.Value = value; + this._json = json; } public BetaBashCodeExecutionToolResultBlockParamContent( - BetaBashCodeExecutionResultBlockParam value + BetaBashCodeExecutionResultBlockParam value, + JsonElement? json = null ) { - Value = value; - } - - BetaBashCodeExecutionToolResultBlockParamContent(UnknownVariant value) - { - Value = value; + this.Value = value; + this._json = json; } - public static BetaBashCodeExecutionToolResultBlockParamContent CreateUnknownVariant( - JsonElement value - ) + public BetaBashCodeExecutionToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaBashCodeExecutionToolResultErrorParam( @@ -270,15 +274,13 @@ BetaBashCodeExecutionResultBlockParam value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaBashCodeExecutionToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaBashCodeExecutionToolResultBlockParamContentConverter @@ -290,54 +292,43 @@ sealed class BetaBashCodeExecutionToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaBashCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBashCodeExecutionToolResultErrorParam'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaBashCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBashCodeExecutionResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -346,7 +337,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaCitationsDelta.cs b/src/Anthropic/Models/Beta/Messages/BetaCitationsDelta.cs index 2db4e272..bcf93c66 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaCitationsDelta.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaCitationsDelta.cs @@ -110,7 +110,14 @@ public BetaCitationsDelta(Citation citation) [JsonConverter(typeof(CitationConverter))] public record class Citation { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string CitedText { @@ -224,39 +231,39 @@ public string? Title } } - public Citation(BetaCitationCharLocation value) - { - Value = value; - } - - public Citation(BetaCitationPageLocation value) + public Citation(BetaCitationCharLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Citation(BetaCitationContentBlockLocation value) + public Citation(BetaCitationPageLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Citation(BetaCitationsWebSearchResultLocation value) + public Citation(BetaCitationContentBlockLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Citation(BetaCitationSearchResultLocation value) + public Citation(BetaCitationsWebSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Citation(UnknownVariant value) + public Citation(BetaCitationSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Citation CreateUnknownVariant(JsonElement value) + public Citation(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaCitationCharLocation( @@ -367,13 +374,11 @@ public static implicit operator Citation(BetaCitationsWebSearchResultLocation va public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Citation"); } } - - record struct UnknownVariant(JsonElement value); } sealed class CitationConverter : JsonConverter @@ -399,8 +404,6 @@ JsonSerializerOptions options { case "char_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -410,26 +413,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationCharLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "page_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -439,26 +435,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationPageLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -468,26 +457,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationContentBlockLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_result_location": { - List exceptions = []; - try { var deserialized = @@ -498,26 +480,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationsWebSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -527,34 +502,26 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Citation(json); } } } public override void Write(Utf8JsonWriter writer, Citation value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaClearThinking20251015Edit.cs b/src/Anthropic/Models/Beta/Messages/BetaClearThinking20251015Edit.cs index 4444efd7..ea41fe37 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaClearThinking20251015Edit.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaClearThinking20251015Edit.cs @@ -111,7 +111,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(KeepConverter))] public record class Keep { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement? Type { @@ -125,29 +132,27 @@ public JsonElement? Type } } - public Keep(BetaThinkingTurns value) - { - Value = value; - } - - public Keep(BetaAllThinkingTurns value) + public Keep(BetaThinkingTurns value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Keep(UnionMember2 value) + public Keep(BetaAllThinkingTurns value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Keep(UnknownVariant value) + public Keep(UnionMember2 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Keep CreateUnknownVariant(JsonElement value) + public Keep(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaThinkingTurns([NotNullWhen(true)] out BetaThinkingTurns? value) @@ -213,13 +218,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Keep"); } } - - record struct UnknownVariant(JsonElement value); } sealed class KeepConverter : JsonConverter @@ -230,75 +233,55 @@ sealed class KeepConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Keep(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'UnionMember2'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Keep(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaThinkingTurns'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Keep(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaAllThinkingTurns'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write(Utf8JsonWriter writer, Keep value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaClearToolUses20250919Edit.cs b/src/Anthropic/Models/Beta/Messages/BetaClearToolUses20250919Edit.cs index 56b9be6d..b274bb54 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaClearToolUses20250919Edit.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaClearToolUses20250919Edit.cs @@ -213,26 +213,30 @@ IReadOnlyDictionary properties [JsonConverter(typeof(ClearToolInputsConverter))] public record class ClearToolInputs { - public object Value { get; private init; } + public object? Value { get; } = null; - public ClearToolInputs(bool value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public ClearToolInputs(IReadOnlyList value) + public ClearToolInputs(bool value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - ClearToolInputs(UnknownVariant value) + public ClearToolInputs(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static ClearToolInputs CreateUnknownVariant(JsonElement value) + public ClearToolInputs(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBool([NotNullWhen(true)] out bool? value) @@ -283,15 +287,13 @@ public static implicit operator ClearToolInputs(List value) => public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ClearToolInputs" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ClearToolInputsConverter : JsonConverter @@ -302,38 +304,30 @@ sealed class ClearToolInputsConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - return new ClearToolInputs(JsonSerializer.Deserialize(ref reader, options)); + return new(JsonSerializer.Deserialize(json, options)); } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'bool'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>(ref reader, options); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new ClearToolInputs(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -342,8 +336,7 @@ public override void Write( JsonSerializerOptions options ) { - object? variant = value?.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value?.Json, options); } } @@ -353,7 +346,14 @@ JsonSerializerOptions options [JsonConverter(typeof(TriggerConverter))] public record class Trigger { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -365,24 +365,21 @@ public long Value1 get { return Match(betaInputTokens: (x) => x.Value, betaToolUses: (x) => x.Value); } } - public Trigger(BetaInputTokensTrigger value) + public Trigger(BetaInputTokensTrigger value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Trigger(BetaToolUsesTrigger value) + public Trigger(BetaToolUsesTrigger value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Trigger(UnknownVariant value) + public Trigger(JsonElement json) { - Value = value; - } - - public static Trigger CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaInputTokens([NotNullWhen(true)] out BetaInputTokensTrigger? value) @@ -438,13 +435,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Trigger"); } } - - record struct UnknownVariant(JsonElement value); } sealed class TriggerConverter : JsonConverter @@ -470,8 +465,6 @@ JsonSerializerOptions options { case "input_tokens": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -481,26 +474,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Trigger(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaInputTokensTrigger'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_uses": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -510,34 +496,26 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Trigger(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolUsesTrigger'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Trigger(json); } } } public override void Write(Utf8JsonWriter writer, Trigger value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockContent.cs b/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockContent.cs index e1f52af3..c1502887 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockContent.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockContent.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,31 +9,41 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaCodeExecutionToolResultBlockContentConverter))] public record class BetaCodeExecutionToolResultBlockContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public JsonElement Type + JsonElement? _json = null; + + public JsonElement Json { - get { return Match(error: (x) => x.Type, resultBlock: (x) => x.Type); } + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaCodeExecutionToolResultBlockContent(BetaCodeExecutionToolResultError value) + public JsonElement Type { - Value = value; + get { return Match(error: (x) => x.Type, resultBlock: (x) => x.Type); } } - public BetaCodeExecutionToolResultBlockContent(BetaCodeExecutionResultBlock value) + public BetaCodeExecutionToolResultBlockContent( + BetaCodeExecutionToolResultError value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - BetaCodeExecutionToolResultBlockContent(UnknownVariant value) + public BetaCodeExecutionToolResultBlockContent( + BetaCodeExecutionResultBlock value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaCodeExecutionToolResultBlockContent CreateUnknownVariant(JsonElement value) + public BetaCodeExecutionToolResultBlockContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickError([NotNullWhen(true)] out BetaCodeExecutionToolResultError? value) @@ -94,15 +103,13 @@ BetaCodeExecutionResultBlock value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaCodeExecutionToolResultBlockContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaCodeExecutionToolResultBlockContentConverter @@ -114,53 +121,42 @@ sealed class BetaCodeExecutionToolResultBlockContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaCodeExecutionToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionToolResultError'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaCodeExecutionToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -169,7 +165,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockParamContent.cs b/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockParamContent.cs index 7571b69d..678d86c8 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockParamContent.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaCodeExecutionToolResultBlockParamContent.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,33 +9,41 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaCodeExecutionToolResultBlockParamContentConverter))] public record class BetaCodeExecutionToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public JsonElement Type + JsonElement? _json = null; + + public JsonElement Json { - get { return Match(errorParam: (x) => x.Type, resultBlockParam: (x) => x.Type); } + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaCodeExecutionToolResultBlockParamContent(BetaCodeExecutionToolResultErrorParam value) + public JsonElement Type { - Value = value; + get { return Match(errorParam: (x) => x.Type, resultBlockParam: (x) => x.Type); } } - public BetaCodeExecutionToolResultBlockParamContent(BetaCodeExecutionResultBlockParam value) + public BetaCodeExecutionToolResultBlockParamContent( + BetaCodeExecutionToolResultErrorParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - BetaCodeExecutionToolResultBlockParamContent(UnknownVariant value) + public BetaCodeExecutionToolResultBlockParamContent( + BetaCodeExecutionResultBlockParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaCodeExecutionToolResultBlockParamContent CreateUnknownVariant( - JsonElement value - ) + public BetaCodeExecutionToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickErrorParam( @@ -100,15 +107,13 @@ BetaCodeExecutionResultBlockParam value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaCodeExecutionToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaCodeExecutionToolResultBlockParamContentConverter @@ -120,53 +125,42 @@ sealed class BetaCodeExecutionToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionToolResultErrorParam'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -175,7 +169,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaContentBlock.cs b/src/Anthropic/Models/Beta/Messages/BetaContentBlock.cs index d2799d79..6dbf6ce7 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaContentBlock.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaContentBlock.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -13,7 +12,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaContentBlockConverter))] public record class BetaContentBlock { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -81,79 +87,90 @@ public string? ToolUseID } } - public BetaContentBlock(BetaTextBlock value) - { - Value = value; - } - - public BetaContentBlock(BetaThinkingBlock value) + public BetaContentBlock(BetaTextBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaRedactedThinkingBlock value) + public BetaContentBlock(BetaThinkingBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaToolUseBlock value) + public BetaContentBlock(BetaRedactedThinkingBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaServerToolUseBlock value) + public BetaContentBlock(BetaToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaWebSearchToolResultBlock value) + public BetaContentBlock(BetaServerToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaWebFetchToolResultBlock value) + public BetaContentBlock(BetaWebSearchToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaCodeExecutionToolResultBlock value) + public BetaContentBlock(BetaWebFetchToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaBashCodeExecutionToolResultBlock value) + public BetaContentBlock(BetaCodeExecutionToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaTextEditorCodeExecutionToolResultBlock value) + public BetaContentBlock(BetaBashCodeExecutionToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaMCPToolUseBlock value) + public BetaContentBlock( + BetaTextEditorCodeExecutionToolResultBlock value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaMCPToolResultBlock value) + public BetaContentBlock(BetaMCPToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlock(BetaContainerUploadBlock value) + public BetaContentBlock(BetaMCPToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaContentBlock(UnknownVariant value) + public BetaContentBlock(BetaContainerUploadBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaContentBlock CreateUnknownVariant(JsonElement value) + public BetaContentBlock(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickText([NotNullWhen(true)] out BetaTextBlock? value) @@ -384,15 +401,13 @@ BetaTextEditorCodeExecutionToolResultBlock value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaContentBlock" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaContentBlockConverter : JsonConverter @@ -418,60 +433,44 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "redacted_thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -481,52 +480,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRedactedThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "server_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -536,26 +521,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaServerToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -565,26 +543,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebSearchToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_fetch_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -594,26 +565,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "code_execution_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -623,26 +587,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "bash_code_execution_tool_result": { - List exceptions = []; - try { var deserialized = @@ -653,26 +610,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBashCodeExecutionToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "text_editor_code_execution_tool_result": { - List exceptions = []; - try { var deserialized = @@ -683,26 +633,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "mcp_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -712,26 +655,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMCPToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "mcp_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -741,26 +677,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMCPToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "container_upload": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -770,27 +699,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaContainerUploadBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaContentBlock(json); } } } @@ -801,7 +723,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaContentBlockParam.cs b/src/Anthropic/Models/Beta/Messages/BetaContentBlockParam.cs index 64f82f88..e34aac40 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaContentBlockParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaContentBlockParam.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -13,7 +12,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaContentBlockParamConverter))] public record class BetaContentBlockParam { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -171,99 +177,120 @@ public bool? IsError } } - public BetaContentBlockParam(BetaTextBlockParam value) - { - Value = value; - } - - public BetaContentBlockParam(BetaImageBlockParam value) + public BetaContentBlockParam(BetaTextBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaRequestDocumentBlock value) + public BetaContentBlockParam(BetaImageBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaSearchResultBlockParam value) + public BetaContentBlockParam(BetaRequestDocumentBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaThinkingBlockParam value) + public BetaContentBlockParam(BetaSearchResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaRedactedThinkingBlockParam value) + public BetaContentBlockParam(BetaThinkingBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaToolUseBlockParam value) + public BetaContentBlockParam(BetaRedactedThinkingBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaToolResultBlockParam value) + public BetaContentBlockParam(BetaToolUseBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaServerToolUseBlockParam value) + public BetaContentBlockParam(BetaToolResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaWebSearchToolResultBlockParam value) + public BetaContentBlockParam(BetaServerToolUseBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaWebFetchToolResultBlockParam value) + public BetaContentBlockParam(BetaWebSearchToolResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaCodeExecutionToolResultBlockParam value) + public BetaContentBlockParam(BetaWebFetchToolResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaBashCodeExecutionToolResultBlockParam value) + public BetaContentBlockParam( + BetaCodeExecutionToolResultBlockParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaTextEditorCodeExecutionToolResultBlockParam value) + public BetaContentBlockParam( + BetaBashCodeExecutionToolResultBlockParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaMCPToolUseBlockParam value) + public BetaContentBlockParam( + BetaTextEditorCodeExecutionToolResultBlockParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaRequestMCPToolResultBlockParam value) + public BetaContentBlockParam(BetaMCPToolUseBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaContentBlockParam(BetaContainerUploadBlockParam value) + public BetaContentBlockParam(BetaRequestMCPToolResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaContentBlockParam(UnknownVariant value) + public BetaContentBlockParam(BetaContainerUploadBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaContentBlockParam CreateUnknownVariant(JsonElement value) + public BetaContentBlockParam(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickText([NotNullWhen(true)] out BetaTextBlockParam? value) @@ -567,15 +594,13 @@ public static implicit operator BetaContentBlockParam(BetaContainerUploadBlockPa public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaContentBlockParam" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaContentBlockParamConverter : JsonConverter @@ -601,8 +626,6 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -612,26 +635,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "image": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -641,26 +657,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaImageBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "document": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -670,26 +679,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRequestDocumentBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -699,26 +701,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaSearchResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -728,26 +723,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaThinkingBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "redacted_thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -757,26 +745,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRedactedThinkingBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -786,26 +767,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolUseBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -815,26 +789,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "server_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -844,26 +811,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaServerToolUseBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_tool_result": { - List exceptions = []; - try { var deserialized = @@ -874,26 +834,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebSearchToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_fetch_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -903,26 +856,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "code_execution_tool_result": { - List exceptions = []; - try { var deserialized = @@ -933,26 +879,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "bash_code_execution_tool_result": { - List exceptions = []; - try { var deserialized = @@ -963,26 +902,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBashCodeExecutionToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "text_editor_code_execution_tool_result": { - List exceptions = []; - try { var deserialized = @@ -993,26 +925,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "mcp_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -1022,26 +947,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMCPToolUseBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "mcp_tool_result": { - List exceptions = []; - try { var deserialized = @@ -1052,26 +970,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRequestMCPToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "container_upload": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -1081,27 +992,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaContainerUploadBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaContentBlockParam(json); } } } @@ -1112,7 +1016,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaContentBlockSource.cs b/src/Anthropic/Models/Beta/Messages/BetaContentBlockSource.cs index acd24be3..36f0b180 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaContentBlockSource.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaContentBlockSource.cs @@ -114,26 +114,33 @@ public BetaContentBlockSource(BetaContentBlockSourceContent content) [JsonConverter(typeof(BetaContentBlockSourceContentConverter))] public record class BetaContentBlockSourceContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public BetaContentBlockSourceContent(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaContentBlockSourceContent(IReadOnlyList value) + public BetaContentBlockSourceContent(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - BetaContentBlockSourceContent(UnknownVariant value) + public BetaContentBlockSourceContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static BetaContentBlockSourceContent CreateUnknownVariant(JsonElement value) + public BetaContentBlockSourceContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -199,15 +206,13 @@ List value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaContentBlockSourceContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaContentBlockSourceContentConverter : JsonConverter @@ -218,44 +223,36 @@ sealed class BetaContentBlockSourceContentConverter : JsonConverter exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new BetaContentBlockSourceContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize< List - >(ref reader, options); + >(json, options); if (deserialized != null) { - return new BetaContentBlockSourceContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -264,7 +261,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaContextManagementConfig.cs b/src/Anthropic/Models/Beta/Messages/BetaContextManagementConfig.cs index b7a3bb32..401eaac2 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaContextManagementConfig.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaContextManagementConfig.cs @@ -74,7 +74,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(EditConverter))] public record class Edit { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -87,24 +94,21 @@ public JsonElement Type } } - public Edit(BetaClearToolUses20250919Edit value) - { - Value = value; - } - - public Edit(BetaClearThinking20251015Edit value) + public Edit(BetaClearToolUses20250919Edit value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Edit(UnknownVariant value) + public Edit(BetaClearThinking20251015Edit value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Edit CreateUnknownVariant(JsonElement value) + public Edit(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaClearToolUses20250919( @@ -160,13 +164,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Edit"); } } - - record struct UnknownVariant(JsonElement value); } sealed class EditConverter : JsonConverter @@ -192,8 +194,6 @@ JsonSerializerOptions options { case "clear_tool_uses_20250919": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -203,26 +203,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Edit(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaClearToolUses20250919Edit'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "clear_thinking_20251015": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -232,34 +225,26 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Edit(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaClearThinking20251015Edit'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Edit(json); } } } public override void Write(Utf8JsonWriter writer, Edit value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaContextManagementResponse.cs b/src/Anthropic/Models/Beta/Messages/BetaContextManagementResponse.cs index 46514ec9..db2d3f03 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaContextManagementResponse.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaContextManagementResponse.cs @@ -89,7 +89,14 @@ public BetaContextManagementResponse(List appliedEdits) [JsonConverter(typeof(AppliedEditConverter))] public record class AppliedEdit { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public long ClearedInputTokens { @@ -113,24 +120,21 @@ public JsonElement Type } } - public AppliedEdit(BetaClearToolUses20250919EditResponse value) - { - Value = value; - } - - public AppliedEdit(BetaClearThinking20251015EditResponse value) + public AppliedEdit(BetaClearToolUses20250919EditResponse value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - AppliedEdit(UnknownVariant value) + public AppliedEdit(BetaClearThinking20251015EditResponse value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static AppliedEdit CreateUnknownVariant(JsonElement value) + public AppliedEdit(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaClearToolUses20250919EditResponse( @@ -199,15 +203,13 @@ public static implicit operator AppliedEdit(BetaClearThinking20251015EditRespons public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of AppliedEdit" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class AppliedEditConverter : JsonConverter @@ -233,8 +235,6 @@ JsonSerializerOptions options { case "clear_tool_uses_20250919": { - List exceptions = []; - try { var deserialized = @@ -245,26 +245,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new AppliedEdit(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaClearToolUses20250919EditResponse'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "clear_thinking_20251015": { - List exceptions = []; - try { var deserialized = @@ -275,27 +268,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new AppliedEdit(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaClearThinking20251015EditResponse'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new AppliedEdit(json); } } } @@ -306,7 +292,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaDocumentBlock.cs b/src/Anthropic/Models/Beta/Messages/BetaDocumentBlock.cs index 927ea88d..65c00b70 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaDocumentBlock.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaDocumentBlock.cs @@ -150,7 +150,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(SourceConverter))] public record class Source { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string Data { @@ -167,24 +174,21 @@ public JsonElement Type get { return Match(betaBase64PDF: (x) => x.Type, betaPlainText: (x) => x.Type); } } - public Source(BetaBase64PDFSource value) - { - Value = value; - } - - public Source(BetaPlainTextSource value) + public Source(BetaBase64PDFSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Source(UnknownVariant value) + public Source(BetaPlainTextSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Source CreateUnknownVariant(JsonElement value) + public Source(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaBase64PDF([NotNullWhen(true)] out BetaBase64PDFSource? value) @@ -238,13 +242,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Source"); } } - - record struct UnknownVariant(JsonElement value); } sealed class SourceConverter : JsonConverter @@ -270,8 +272,6 @@ JsonSerializerOptions options { case "base64": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -281,26 +281,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Source(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBase64PDFSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -310,34 +303,26 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Source(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaPlainTextSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Source(json); } } } public override void Write(Utf8JsonWriter writer, Source value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaImageBlockParam.cs b/src/Anthropic/Models/Beta/Messages/BetaImageBlockParam.cs index 4102b16d..683e3988 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaImageBlockParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaImageBlockParam.cs @@ -135,7 +135,14 @@ public BetaImageBlockParam(BetaImageBlockParamSource source) [JsonConverter(typeof(BetaImageBlockParamSourceConverter))] public record class BetaImageBlockParamSource { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -149,29 +156,27 @@ public JsonElement Type } } - public BetaImageBlockParamSource(BetaBase64ImageSource value) + public BetaImageBlockParamSource(BetaBase64ImageSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaImageBlockParamSource(BetaURLImageSource value) + public BetaImageBlockParamSource(BetaURLImageSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaImageBlockParamSource(BetaFileImageSource value) + public BetaImageBlockParamSource(BetaFileImageSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaImageBlockParamSource(UnknownVariant value) + public BetaImageBlockParamSource(JsonElement json) { - Value = value; - } - - public static BetaImageBlockParamSource CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaBase64Image([NotNullWhen(true)] out BetaBase64ImageSource? value) @@ -244,15 +249,13 @@ public static implicit operator BetaImageBlockParamSource(BetaFileImageSource va public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaImageBlockParamSource" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaImageBlockParamSourceConverter : JsonConverter @@ -278,8 +281,6 @@ JsonSerializerOptions options { case "base64": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -289,26 +290,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaImageBlockParamSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBase64ImageSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "url": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -318,26 +312,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaImageBlockParamSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaURLImageSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "file": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -347,27 +334,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaImageBlockParamSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaFileImageSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaImageBlockParamSource(json); } } } @@ -378,7 +358,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaMCPToolResultBlock.cs b/src/Anthropic/Models/Beta/Messages/BetaMCPToolResultBlock.cs index ab482aee..1dbd487f 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaMCPToolResultBlock.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaMCPToolResultBlock.cs @@ -158,26 +158,33 @@ IReadOnlyDictionary properties [JsonConverter(typeof(BetaMCPToolResultBlockContentConverter))] public record class BetaMCPToolResultBlockContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public BetaMCPToolResultBlockContent(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaMCPToolResultBlockContent(IReadOnlyList value) + public BetaMCPToolResultBlockContent(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - BetaMCPToolResultBlockContent(UnknownVariant value) + public BetaMCPToolResultBlockContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static BetaMCPToolResultBlockContent CreateUnknownVariant(JsonElement value) + public BetaMCPToolResultBlockContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -236,15 +243,13 @@ public static implicit operator BetaMCPToolResultBlockContent(List @@ -255,42 +260,34 @@ sealed class BetaMCPToolResultBlockContentConverter : JsonConverter exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new BetaMCPToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>(ref reader, options); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new BetaMCPToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -299,7 +296,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaMemoryTool20250818Command.cs b/src/Anthropic/Models/Beta/Messages/BetaMemoryTool20250818Command.cs index a76ac4f8..5e3df66c 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaMemoryTool20250818Command.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaMemoryTool20250818Command.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaMemoryTool20250818CommandConverter))] public record class BetaMemoryTool20250818Command { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Command { @@ -42,44 +48,63 @@ public string? Path } } - public BetaMemoryTool20250818Command(BetaMemoryTool20250818ViewCommand value) - { - Value = value; - } - - public BetaMemoryTool20250818Command(BetaMemoryTool20250818CreateCommand value) + public BetaMemoryTool20250818Command( + BetaMemoryTool20250818ViewCommand value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaMemoryTool20250818Command(BetaMemoryTool20250818StrReplaceCommand value) + public BetaMemoryTool20250818Command( + BetaMemoryTool20250818CreateCommand value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaMemoryTool20250818Command(BetaMemoryTool20250818InsertCommand value) + public BetaMemoryTool20250818Command( + BetaMemoryTool20250818StrReplaceCommand value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaMemoryTool20250818Command(BetaMemoryTool20250818DeleteCommand value) + public BetaMemoryTool20250818Command( + BetaMemoryTool20250818InsertCommand value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaMemoryTool20250818Command(BetaMemoryTool20250818RenameCommand value) + public BetaMemoryTool20250818Command( + BetaMemoryTool20250818DeleteCommand value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - BetaMemoryTool20250818Command(UnknownVariant value) + public BetaMemoryTool20250818Command( + BetaMemoryTool20250818RenameCommand value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaMemoryTool20250818Command CreateUnknownVariant(JsonElement value) + public BetaMemoryTool20250818Command(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickTool20250818View( @@ -215,15 +240,13 @@ BetaMemoryTool20250818RenameCommand value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaMemoryTool20250818Command" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaMemoryTool20250818CommandConverter : JsonConverter @@ -249,8 +272,6 @@ JsonSerializerOptions options { case "view": { - List exceptions = []; - try { var deserialized = @@ -261,26 +282,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMemoryTool20250818Command(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818ViewCommand'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "create": { - List exceptions = []; - try { var deserialized = @@ -291,26 +305,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMemoryTool20250818Command(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818CreateCommand'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "str_replace": { - List exceptions = []; - try { var deserialized = @@ -321,26 +328,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMemoryTool20250818Command(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818StrReplaceCommand'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "insert": { - List exceptions = []; - try { var deserialized = @@ -351,26 +351,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMemoryTool20250818Command(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818InsertCommand'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "delete": { - List exceptions = []; - try { var deserialized = @@ -381,26 +374,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMemoryTool20250818Command(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818DeleteCommand'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "rename": { - List exceptions = []; - try { var deserialized = @@ -411,27 +397,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaMemoryTool20250818Command(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818RenameCommand'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaMemoryTool20250818Command(json); } } } @@ -442,7 +421,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaMessageParam.cs b/src/Anthropic/Models/Beta/Messages/BetaMessageParam.cs index f313c1fe..e4d67bdc 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaMessageParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaMessageParam.cs @@ -97,26 +97,33 @@ IReadOnlyDictionary properties [JsonConverter(typeof(BetaMessageParamContentConverter))] public record class BetaMessageParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public BetaMessageParamContent(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaMessageParamContent(IReadOnlyList value) + public BetaMessageParamContent(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - BetaMessageParamContent(UnknownVariant value) + public BetaMessageParamContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static BetaMessageParamContent CreateUnknownVariant(JsonElement value) + public BetaMessageParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -175,15 +182,13 @@ public static implicit operator BetaMessageParamContent(List @@ -194,45 +199,37 @@ sealed class BetaMessageParamContentConverter : JsonConverter exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new BetaMessageParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize>( - ref reader, + json, options ); if (deserialized != null) { - return new BetaMessageParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -241,8 +238,7 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockDelta.cs b/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockDelta.cs index 4da737c5..eeeb32a6 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockDelta.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockDelta.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaRawContentBlockDeltaConverter))] public record class BetaRawContentBlockDelta { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -26,39 +32,39 @@ public JsonElement Type } } - public BetaRawContentBlockDelta(BetaTextDelta value) - { - Value = value; - } - - public BetaRawContentBlockDelta(BetaInputJSONDelta value) + public BetaRawContentBlockDelta(BetaTextDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawContentBlockDelta(BetaCitationsDelta value) + public BetaRawContentBlockDelta(BetaInputJSONDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawContentBlockDelta(BetaThinkingDelta value) + public BetaRawContentBlockDelta(BetaCitationsDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawContentBlockDelta(BetaSignatureDelta value) + public BetaRawContentBlockDelta(BetaThinkingDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaRawContentBlockDelta(UnknownVariant value) + public BetaRawContentBlockDelta(BetaSignatureDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaRawContentBlockDelta CreateUnknownVariant(JsonElement value) + public BetaRawContentBlockDelta(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickText([NotNullWhen(true)] out BetaTextDelta? value) @@ -159,15 +165,13 @@ public static implicit operator BetaRawContentBlockDelta(BetaSignatureDelta valu public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaRawContentBlockDelta" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaRawContentBlockDeltaConverter : JsonConverter @@ -193,34 +197,25 @@ JsonSerializerOptions options { case "text_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaRawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "input_json_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -230,26 +225,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaInputJSONDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "citations_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -259,52 +247,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationsDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaRawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaThinkingDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "signature_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -314,27 +288,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaSignatureDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaRawContentBlockDelta(json); } } } @@ -345,7 +312,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockStartEvent.cs b/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockStartEvent.cs index 314ac5f9..79331fab 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockStartEvent.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaRawContentBlockStartEvent.cs @@ -136,7 +136,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(ContentBlockConverter))] public record class ContentBlock { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -204,79 +211,87 @@ public string? ToolUseID } } - public ContentBlock(BetaTextBlock value) + public ContentBlock(BetaTextBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaThinkingBlock value) + public ContentBlock(BetaThinkingBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaRedactedThinkingBlock value) + public ContentBlock(BetaRedactedThinkingBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaToolUseBlock value) + public ContentBlock(BetaToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaServerToolUseBlock value) + public ContentBlock(BetaServerToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaWebSearchToolResultBlock value) + public ContentBlock(BetaWebSearchToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaWebFetchToolResultBlock value) + public ContentBlock(BetaWebFetchToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaCodeExecutionToolResultBlock value) + public ContentBlock(BetaCodeExecutionToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaBashCodeExecutionToolResultBlock value) + public ContentBlock(BetaBashCodeExecutionToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaTextEditorCodeExecutionToolResultBlock value) + public ContentBlock(BetaTextEditorCodeExecutionToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaMCPToolUseBlock value) + public ContentBlock(BetaMCPToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaMCPToolResultBlock value) + public ContentBlock(BetaMCPToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(BetaContainerUploadBlock value) + public ContentBlock(BetaContainerUploadBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ContentBlock(UnknownVariant value) + public ContentBlock(JsonElement json) { - Value = value; - } - - public static ContentBlock CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaText([NotNullWhen(true)] out BetaTextBlock? value) @@ -506,15 +521,13 @@ BetaTextEditorCodeExecutionToolResultBlock value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ContentBlock" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContentBlockConverter : JsonConverter @@ -540,60 +553,44 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "redacted_thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -603,52 +600,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRedactedThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "server_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -658,26 +641,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaServerToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -687,26 +663,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebSearchToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_fetch_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -716,26 +685,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "code_execution_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -745,26 +707,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "bash_code_execution_tool_result": { - List exceptions = []; - try { var deserialized = @@ -775,26 +730,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBashCodeExecutionToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "text_editor_code_execution_tool_result": { - List exceptions = []; - try { var deserialized = @@ -805,26 +753,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "mcp_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -834,26 +775,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMCPToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "mcp_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -863,26 +797,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMCPToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "container_upload": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -892,27 +819,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaContainerUploadBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ContentBlock(json); } } } @@ -923,7 +843,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaRawMessageStreamEvent.cs b/src/Anthropic/Models/Beta/Messages/BetaRawMessageStreamEvent.cs index 24284afb..02a86b2e 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaRawMessageStreamEvent.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaRawMessageStreamEvent.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaRawMessageStreamEventConverter))] public record class BetaRawMessageStreamEvent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -42,44 +48,45 @@ public long? Index } } - public BetaRawMessageStreamEvent(BetaRawMessageStartEvent value) + public BetaRawMessageStreamEvent(BetaRawMessageStartEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawMessageStreamEvent(BetaRawMessageDeltaEvent value) + public BetaRawMessageStreamEvent(BetaRawMessageDeltaEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawMessageStreamEvent(BetaRawMessageStopEvent value) + public BetaRawMessageStreamEvent(BetaRawMessageStopEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawMessageStreamEvent(BetaRawContentBlockStartEvent value) + public BetaRawMessageStreamEvent(BetaRawContentBlockStartEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawMessageStreamEvent(BetaRawContentBlockDeltaEvent value) + public BetaRawMessageStreamEvent(BetaRawContentBlockDeltaEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRawMessageStreamEvent(BetaRawContentBlockStopEvent value) + public BetaRawMessageStreamEvent(BetaRawContentBlockStopEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaRawMessageStreamEvent(UnknownVariant value) + public BetaRawMessageStreamEvent(JsonElement json) { - Value = value; - } - - public static BetaRawMessageStreamEvent CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickStart([NotNullWhen(true)] out BetaRawMessageStartEvent? value) @@ -203,15 +210,13 @@ public static implicit operator BetaRawMessageStreamEvent(BetaRawContentBlockSto public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaRawMessageStreamEvent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaRawMessageStreamEventConverter : JsonConverter @@ -237,8 +242,6 @@ JsonSerializerOptions options { case "message_start": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -248,26 +251,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRawMessageStartEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "message_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -277,26 +273,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRawMessageDeltaEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "message_stop": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -306,26 +295,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRawMessageStopEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_start": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -335,26 +317,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRawContentBlockStartEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -364,26 +339,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRawContentBlockDeltaEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_stop": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -393,27 +361,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRawContentBlockStopEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaRawMessageStreamEvent(json); } } } @@ -424,7 +385,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaRequestDocumentBlock.cs b/src/Anthropic/Models/Beta/Messages/BetaRequestDocumentBlock.cs index 663ce184..2de4cac0 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaRequestDocumentBlock.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaRequestDocumentBlock.cs @@ -198,7 +198,14 @@ public BetaRequestDocumentBlock(BetaRequestDocumentBlockSource source) [JsonConverter(typeof(BetaRequestDocumentBlockSourceConverter))] public record class BetaRequestDocumentBlockSource { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string? Data { @@ -242,39 +249,39 @@ public JsonElement Type } } - public BetaRequestDocumentBlockSource(BetaBase64PDFSource value) - { - Value = value; - } - - public BetaRequestDocumentBlockSource(BetaPlainTextSource value) + public BetaRequestDocumentBlockSource(BetaBase64PDFSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRequestDocumentBlockSource(BetaContentBlockSource value) + public BetaRequestDocumentBlockSource(BetaPlainTextSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRequestDocumentBlockSource(BetaURLPDFSource value) + public BetaRequestDocumentBlockSource(BetaContentBlockSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaRequestDocumentBlockSource(BetaFileDocumentSource value) + public BetaRequestDocumentBlockSource(BetaURLPDFSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaRequestDocumentBlockSource(UnknownVariant value) + public BetaRequestDocumentBlockSource(BetaFileDocumentSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaRequestDocumentBlockSource CreateUnknownVariant(JsonElement value) + public BetaRequestDocumentBlockSource(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaBase64PDF([NotNullWhen(true)] out BetaBase64PDFSource? value) @@ -377,15 +384,13 @@ public static implicit operator BetaRequestDocumentBlockSource(BetaFileDocumentS public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaRequestDocumentBlockSource" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaRequestDocumentBlockSourceConverter : JsonConverter @@ -411,8 +416,6 @@ JsonSerializerOptions options { case "base64": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -422,26 +425,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRequestDocumentBlockSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaBase64PDFSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -451,26 +447,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRequestDocumentBlockSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaPlainTextSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -480,52 +469,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRequestDocumentBlockSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaContentBlockSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "url": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaRequestDocumentBlockSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaURLPDFSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "file": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -535,27 +510,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaRequestDocumentBlockSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaFileDocumentSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaRequestDocumentBlockSource(json); } } } @@ -566,7 +534,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaRequestMCPToolResultBlockParam.cs b/src/Anthropic/Models/Beta/Messages/BetaRequestMCPToolResultBlockParam.cs index 98990377..e2e695db 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaRequestMCPToolResultBlockParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaRequestMCPToolResultBlockParam.cs @@ -192,26 +192,33 @@ public BetaRequestMCPToolResultBlockParam(string toolUseID) [JsonConverter(typeof(BetaRequestMCPToolResultBlockParamContentConverter))] public record class BetaRequestMCPToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public BetaRequestMCPToolResultBlockParamContent(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaRequestMCPToolResultBlockParamContent(IReadOnlyList value) + public BetaRequestMCPToolResultBlockParamContent(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - BetaRequestMCPToolResultBlockParamContent(UnknownVariant value) + public BetaRequestMCPToolResultBlockParamContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static BetaRequestMCPToolResultBlockParamContent CreateUnknownVariant(JsonElement value) + public BetaRequestMCPToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -272,15 +279,13 @@ List value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaRequestMCPToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaRequestMCPToolResultBlockParamContentConverter @@ -292,45 +297,34 @@ sealed class BetaRequestMCPToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new BetaRequestMCPToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new BetaRequestMCPToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -339,7 +333,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaTextCitation.cs b/src/Anthropic/Models/Beta/Messages/BetaTextCitation.cs index 1fc5d109..c309c7a7 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaTextCitation.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaTextCitation.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaTextCitationConverter))] public record class BetaTextCitation { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string CitedText { @@ -124,39 +130,39 @@ public string? Title } } - public BetaTextCitation(BetaCitationCharLocation value) - { - Value = value; - } - - public BetaTextCitation(BetaCitationPageLocation value) + public BetaTextCitation(BetaCitationCharLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaTextCitation(BetaCitationContentBlockLocation value) + public BetaTextCitation(BetaCitationPageLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaTextCitation(BetaCitationsWebSearchResultLocation value) + public BetaTextCitation(BetaCitationContentBlockLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaTextCitation(BetaCitationSearchResultLocation value) + public BetaTextCitation(BetaCitationsWebSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaTextCitation(UnknownVariant value) + public BetaTextCitation(BetaCitationSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaTextCitation CreateUnknownVariant(JsonElement value) + public BetaTextCitation(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickCitationCharLocation([NotNullWhen(true)] out BetaCitationCharLocation? value) @@ -263,15 +269,13 @@ public static implicit operator BetaTextCitation(BetaCitationSearchResultLocatio public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaTextCitation" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaTextCitationConverter : JsonConverter @@ -297,8 +301,6 @@ JsonSerializerOptions options { case "char_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -308,26 +310,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationCharLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "page_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -337,26 +332,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationPageLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -366,26 +354,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationContentBlockLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_result_location": { - List exceptions = []; - try { var deserialized = @@ -396,26 +377,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationsWebSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -425,27 +399,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaTextCitation(json); } } } @@ -456,7 +423,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaTextCitationParam.cs b/src/Anthropic/Models/Beta/Messages/BetaTextCitationParam.cs index 632008d9..8f2cf0a6 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaTextCitationParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaTextCitationParam.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaTextCitationParamConverter))] public record class BetaTextCitationParam { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string CitedText { @@ -110,39 +116,48 @@ public string? Title } } - public BetaTextCitationParam(BetaCitationCharLocationParam value) - { - Value = value; - } - - public BetaTextCitationParam(BetaCitationPageLocationParam value) + public BetaTextCitationParam(BetaCitationCharLocationParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaTextCitationParam(BetaCitationContentBlockLocationParam value) + public BetaTextCitationParam(BetaCitationPageLocationParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaTextCitationParam(BetaCitationWebSearchResultLocationParam value) + public BetaTextCitationParam( + BetaCitationContentBlockLocationParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public BetaTextCitationParam(BetaCitationSearchResultLocationParam value) + public BetaTextCitationParam( + BetaCitationWebSearchResultLocationParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - BetaTextCitationParam(UnknownVariant value) + public BetaTextCitationParam( + BetaCitationSearchResultLocationParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaTextCitationParam CreateUnknownVariant(JsonElement value) + public BetaTextCitationParam(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickCitationCharLocation( @@ -260,15 +275,13 @@ BetaCitationSearchResultLocationParam value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaTextCitationParam" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaTextCitationParamConverter : JsonConverter @@ -294,8 +307,6 @@ JsonSerializerOptions options { case "char_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -305,26 +316,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationCharLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "page_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -334,26 +338,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationPageLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_location": { - List exceptions = []; - try { var deserialized = @@ -364,26 +361,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationContentBlockLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_result_location": { - List exceptions = []; - try { var deserialized = @@ -394,26 +384,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationWebSearchResultLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result_location": { - List exceptions = []; - try { var deserialized = @@ -424,27 +407,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaTextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCitationSearchResultLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaTextCitationParam(json); } } } @@ -455,7 +431,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlock.cs b/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlock.cs index d6eaf140..4f0c238b 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlock.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlock.cs @@ -145,7 +145,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(BetaTextEditorCodeExecutionToolResultBlockContentConverter))] public record class BetaTextEditorCodeExecutionToolResultBlockContent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -161,43 +168,44 @@ public JsonElement Type } public BetaTextEditorCodeExecutionToolResultBlockContent( - BetaTextEditorCodeExecutionToolResultError value + BetaTextEditorCodeExecutionToolResultError value, + JsonElement? json = null ) { - Value = value; + this.Value = value; + this._json = json; } public BetaTextEditorCodeExecutionToolResultBlockContent( - BetaTextEditorCodeExecutionViewResultBlock value + BetaTextEditorCodeExecutionViewResultBlock value, + JsonElement? json = null ) { - Value = value; + this.Value = value; + this._json = json; } public BetaTextEditorCodeExecutionToolResultBlockContent( - BetaTextEditorCodeExecutionCreateResultBlock value + BetaTextEditorCodeExecutionCreateResultBlock value, + JsonElement? json = null ) { - Value = value; + this.Value = value; + this._json = json; } public BetaTextEditorCodeExecutionToolResultBlockContent( - BetaTextEditorCodeExecutionStrReplaceResultBlock value + BetaTextEditorCodeExecutionStrReplaceResultBlock value, + JsonElement? json = null ) { - Value = value; - } - - BetaTextEditorCodeExecutionToolResultBlockContent(UnknownVariant value) - { - Value = value; + this.Value = value; + this._json = json; } - public static BetaTextEditorCodeExecutionToolResultBlockContent CreateUnknownVariant( - JsonElement value - ) + public BetaTextEditorCodeExecutionToolResultBlockContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaTextEditorCodeExecutionToolResultError( @@ -313,15 +321,13 @@ BetaTextEditorCodeExecutionStrReplaceResultBlock value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaTextEditorCodeExecutionToolResultBlockContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaTextEditorCodeExecutionToolResultBlockContentConverter @@ -333,101 +339,80 @@ sealed class BetaTextEditorCodeExecutionToolResultBlockContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionToolResultError'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionViewResultBlock'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionCreateResultBlock'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionStrReplaceResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -436,7 +421,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlockParam.cs b/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlockParam.cs index 644d2627..4cec6c53 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlockParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaTextEditorCodeExecutionToolResultBlockParam.cs @@ -172,7 +172,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(BetaTextEditorCodeExecutionToolResultBlockParamContentConverter))] public record class BetaTextEditorCodeExecutionToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -188,43 +195,44 @@ public JsonElement Type } public BetaTextEditorCodeExecutionToolResultBlockParamContent( - BetaTextEditorCodeExecutionToolResultErrorParam value + BetaTextEditorCodeExecutionToolResultErrorParam value, + JsonElement? json = null ) { - Value = value; + this.Value = value; + this._json = json; } public BetaTextEditorCodeExecutionToolResultBlockParamContent( - BetaTextEditorCodeExecutionViewResultBlockParam value + BetaTextEditorCodeExecutionViewResultBlockParam value, + JsonElement? json = null ) { - Value = value; + this.Value = value; + this._json = json; } public BetaTextEditorCodeExecutionToolResultBlockParamContent( - BetaTextEditorCodeExecutionCreateResultBlockParam value + BetaTextEditorCodeExecutionCreateResultBlockParam value, + JsonElement? json = null ) { - Value = value; + this.Value = value; + this._json = json; } public BetaTextEditorCodeExecutionToolResultBlockParamContent( - BetaTextEditorCodeExecutionStrReplaceResultBlockParam value + BetaTextEditorCodeExecutionStrReplaceResultBlockParam value, + JsonElement? json = null ) { - Value = value; - } - - BetaTextEditorCodeExecutionToolResultBlockParamContent(UnknownVariant value) - { - Value = value; + this.Value = value; + this._json = json; } - public static BetaTextEditorCodeExecutionToolResultBlockParamContent CreateUnknownVariant( - JsonElement value - ) + public BetaTextEditorCodeExecutionToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaTextEditorCodeExecutionToolResultErrorParam( @@ -340,15 +348,13 @@ BetaTextEditorCodeExecutionStrReplaceResultBlockParam value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaTextEditorCodeExecutionToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaTextEditorCodeExecutionToolResultBlockParamContentConverter @@ -360,101 +366,80 @@ sealed class BetaTextEditorCodeExecutionToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionToolResultErrorParam'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionViewResultBlockParam'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionCreateResultBlockParam'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaTextEditorCodeExecutionToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextEditorCodeExecutionStrReplaceResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -463,7 +448,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaThinkingConfigParam.cs b/src/Anthropic/Models/Beta/Messages/BetaThinkingConfigParam.cs index 58179be2..1e05a5a9 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaThinkingConfigParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaThinkingConfigParam.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -20,31 +19,35 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaThinkingConfigParamConverter))] public record class BetaThinkingConfigParam { - public object Value { get; private init; } + public object? Value { get; } = null; - public JsonElement Type + JsonElement? _json = null; + + public JsonElement Json { - get { return Match(enabled: (x) => x.Type, disabled: (x) => x.Type); } + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaThinkingConfigParam(BetaThinkingConfigEnabled value) + public JsonElement Type { - Value = value; + get { return Match(enabled: (x) => x.Type, disabled: (x) => x.Type); } } - public BetaThinkingConfigParam(BetaThinkingConfigDisabled value) + public BetaThinkingConfigParam(BetaThinkingConfigEnabled value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaThinkingConfigParam(UnknownVariant value) + public BetaThinkingConfigParam(BetaThinkingConfigDisabled value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaThinkingConfigParam CreateUnknownVariant(JsonElement value) + public BetaThinkingConfigParam(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickEnabled([NotNullWhen(true)] out BetaThinkingConfigEnabled? value) @@ -102,15 +105,13 @@ public static implicit operator BetaThinkingConfigParam(BetaThinkingConfigDisabl public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaThinkingConfigParam" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaThinkingConfigParamConverter : JsonConverter @@ -136,8 +137,6 @@ JsonSerializerOptions options { case "enabled": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -147,26 +146,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaThinkingConfigParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaThinkingConfigEnabled'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "disabled": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -176,27 +168,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaThinkingConfigParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaThinkingConfigDisabled'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaThinkingConfigParam(json); } } } @@ -207,7 +192,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaToolChoice.cs b/src/Anthropic/Models/Beta/Messages/BetaToolChoice.cs index 63e05902..1c8332c7 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaToolChoice.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaToolChoice.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -14,7 +13,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaToolChoiceConverter))] public record class BetaToolChoice { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -42,34 +48,33 @@ public bool? DisableParallelToolUse } } - public BetaToolChoice(BetaToolChoiceAuto value) + public BetaToolChoice(BetaToolChoiceAuto value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolChoice(BetaToolChoiceAny value) + public BetaToolChoice(BetaToolChoiceAny value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolChoice(BetaToolChoiceTool value) + public BetaToolChoice(BetaToolChoiceTool value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolChoice(BetaToolChoiceNone value) + public BetaToolChoice(BetaToolChoiceNone value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaToolChoice(UnknownVariant value) + public BetaToolChoice(JsonElement json) { - Value = value; - } - - public static BetaToolChoice CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickAuto([NotNullWhen(true)] out BetaToolChoiceAuto? value) @@ -153,15 +158,13 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaToolChoice" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaToolChoiceConverter : JsonConverter @@ -187,8 +190,6 @@ JsonSerializerOptions options { case "auto": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -198,52 +199,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolChoiceAuto'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "any": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolChoiceAny'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -253,26 +240,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolChoiceTool'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "none": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -282,27 +262,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new BetaToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolChoiceNone'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new BetaToolChoice(json); } } } @@ -313,7 +286,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaToolResultBlockParam.cs b/src/Anthropic/Models/Beta/Messages/BetaToolResultBlockParam.cs index 77a41acb..abc24fd3 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaToolResultBlockParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaToolResultBlockParam.cs @@ -190,26 +190,30 @@ public BetaToolResultBlockParam(string toolUseID) [JsonConverter(typeof(BetaToolResultBlockParamContentConverter))] public record class BetaToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public BetaToolResultBlockParamContent(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaToolResultBlockParamContent(IReadOnlyList value) + public BetaToolResultBlockParamContent(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - BetaToolResultBlockParamContent(UnknownVariant value) + public BetaToolResultBlockParamContent(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static BetaToolResultBlockParamContent CreateUnknownVariant(JsonElement value) + public BetaToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -260,15 +264,13 @@ public static implicit operator BetaToolResultBlockParamContent(List valu public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaToolResultBlockParamContentConverter @@ -280,42 +282,34 @@ sealed class BetaToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new BetaToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>(ref reader, options); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new BetaToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -324,15 +318,21 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } [JsonConverter(typeof(BlockConverter))] public record class Block { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -373,34 +373,33 @@ public string? Title } } - public Block(BetaTextBlockParam value) - { - Value = value; - } - - public Block(BetaImageBlockParam value) + public Block(BetaTextBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Block(BetaSearchResultBlockParam value) + public Block(BetaImageBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Block(BetaRequestDocumentBlock value) + public Block(BetaSearchResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Block(UnknownVariant value) + public Block(BetaRequestDocumentBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Block CreateUnknownVariant(JsonElement value) + public Block(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaTextBlockParam([NotNullWhen(true)] out BetaTextBlockParam? value) @@ -482,13 +481,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Block"); } } - - record struct UnknownVariant(JsonElement value); } sealed class BlockConverter : JsonConverter @@ -514,8 +511,6 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -525,26 +520,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "image": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -554,26 +542,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaImageBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -583,26 +564,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaSearchResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "document": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -612,34 +586,26 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaRequestDocumentBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Block(json); } } } public override void Write(Utf8JsonWriter writer, Block value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaToolUnion.cs b/src/Anthropic/Models/Beta/Messages/BetaToolUnion.cs index b397f200..9bb0f455 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaToolUnion.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaToolUnion.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaToolUnionConverter))] public record class BetaToolUnion { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public BetaCacheControlEphemeral? CacheControl { @@ -150,84 +156,93 @@ public long? MaxUses } } - public BetaToolUnion(BetaTool value) + public BetaToolUnion(BetaTool value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolBash20241022 value) + public BetaToolUnion(BetaToolBash20241022 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolBash20250124 value) + public BetaToolUnion(BetaToolBash20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaCodeExecutionTool20250522 value) + public BetaToolUnion(BetaCodeExecutionTool20250522 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaCodeExecutionTool20250825 value) + public BetaToolUnion(BetaCodeExecutionTool20250825 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolComputerUse20241022 value) + public BetaToolUnion(BetaToolComputerUse20241022 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaMemoryTool20250818 value) + public BetaToolUnion(BetaMemoryTool20250818 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolComputerUse20250124 value) + public BetaToolUnion(BetaToolComputerUse20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolTextEditor20241022 value) + public BetaToolUnion(BetaToolTextEditor20241022 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolTextEditor20250124 value) + public BetaToolUnion(BetaToolTextEditor20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolTextEditor20250429 value) + public BetaToolUnion(BetaToolTextEditor20250429 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaToolTextEditor20250728 value) + public BetaToolUnion(BetaToolTextEditor20250728 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaWebSearchTool20250305 value) + public BetaToolUnion(BetaWebSearchTool20250305 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public BetaToolUnion(BetaWebFetchTool20250910 value) + public BetaToolUnion(BetaWebFetchTool20250910 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - BetaToolUnion(UnknownVariant value) + public BetaToolUnion(JsonElement json) { - Value = value; - } - - public static BetaToolUnion CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaTool([NotNullWhen(true)] out BetaTool? value) @@ -463,15 +478,13 @@ public static implicit operator BetaToolUnion(BetaCodeExecutionTool20250825 valu public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaToolUnion" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaToolUnionConverter : JsonConverter @@ -482,311 +495,228 @@ sealed class BetaToolUnionConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'BetaTool'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolBash20241022'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolBash20250124'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionTool20250522'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionTool20250825'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolComputerUse20241022'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolComputerUse20250124'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20241022'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20250124'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20250429'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20250728'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebSearchTool20250305'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchTool20250910'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -795,7 +725,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlock.cs b/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlock.cs index 78881149..091af5d4 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlock.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlock.cs @@ -137,7 +137,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(BetaWebFetchToolResultBlockContentConverter))] public record class BetaWebFetchToolResultBlockContent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -150,24 +157,24 @@ public JsonElement Type } } - public BetaWebFetchToolResultBlockContent(BetaWebFetchToolResultErrorBlock value) - { - Value = value; - } - - public BetaWebFetchToolResultBlockContent(BetaWebFetchBlock value) + public BetaWebFetchToolResultBlockContent( + BetaWebFetchToolResultErrorBlock value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - BetaWebFetchToolResultBlockContent(UnknownVariant value) + public BetaWebFetchToolResultBlockContent(BetaWebFetchBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaWebFetchToolResultBlockContent CreateUnknownVariant(JsonElement value) + public BetaWebFetchToolResultBlockContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaWebFetchToolResultErrorBlock( @@ -228,15 +235,13 @@ public static implicit operator BetaWebFetchToolResultBlockContent(BetaWebFetchB public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaWebFetchToolResultBlockContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaWebFetchToolResultBlockContentConverter @@ -248,50 +253,39 @@ sealed class BetaWebFetchToolResultBlockContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaWebFetchToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchToolResultErrorBlock'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaWebFetchToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -300,7 +294,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlockParam.cs b/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlockParam.cs index f9fb3033..a14f2046 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlockParam.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaWebFetchToolResultBlockParam.cs @@ -162,7 +162,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(BetaWebFetchToolResultBlockParamContentConverter))] public record class BetaWebFetchToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -175,24 +182,27 @@ public JsonElement Type } } - public BetaWebFetchToolResultBlockParamContent(BetaWebFetchToolResultErrorBlockParam value) - { - Value = value; - } - - public BetaWebFetchToolResultBlockParamContent(BetaWebFetchBlockParam value) + public BetaWebFetchToolResultBlockParamContent( + BetaWebFetchToolResultErrorBlockParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - BetaWebFetchToolResultBlockParamContent(UnknownVariant value) + public BetaWebFetchToolResultBlockParamContent( + BetaWebFetchBlockParam value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaWebFetchToolResultBlockParamContent CreateUnknownVariant(JsonElement value) + public BetaWebFetchToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaWebFetchToolResultErrorBlockParam( @@ -259,15 +269,13 @@ BetaWebFetchBlockParam value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaWebFetchToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaWebFetchToolResultBlockParamContentConverter @@ -279,53 +287,39 @@ sealed class BetaWebFetchToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaWebFetchToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchToolResultErrorBlockParam'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new BetaWebFetchToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -334,7 +328,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockContent.cs b/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockContent.cs index 6c2709f5..e64f2e8e 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockContent.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockContent.cs @@ -11,26 +11,36 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaWebSearchToolResultBlockContentConverter))] public record class BetaWebSearchToolResultBlockContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public BetaWebSearchToolResultBlockContent(BetaWebSearchToolResultError value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaWebSearchToolResultBlockContent(IReadOnlyList value) + public BetaWebSearchToolResultBlockContent( + BetaWebSearchToolResultError value, + JsonElement? json = null + ) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - BetaWebSearchToolResultBlockContent(UnknownVariant value) + public BetaWebSearchToolResultBlockContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static BetaWebSearchToolResultBlockContent CreateUnknownVariant(JsonElement value) + public BetaWebSearchToolResultBlockContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickError([NotNullWhen(true)] out BetaWebSearchToolResultError? value) @@ -92,15 +102,13 @@ List value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaWebSearchToolResultBlockContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaWebSearchToolResultBlockContentConverter @@ -112,52 +120,41 @@ sealed class BetaWebSearchToolResultBlockContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaWebSearchToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebSearchToolResultError'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize>( - ref reader, + json, options ); if (deserialized != null) { - return new BetaWebSearchToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -166,7 +163,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockParamContent.cs b/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockParamContent.cs index b45ade6f..c0c682a2 100644 --- a/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockParamContent.cs +++ b/src/Anthropic/Models/Beta/Messages/BetaWebSearchToolResultBlockParamContent.cs @@ -11,28 +11,36 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(BetaWebSearchToolResultBlockParamContentConverter))] public record class BetaWebSearchToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public BetaWebSearchToolResultBlockParamContent( - IReadOnlyList value - ) + JsonElement? _json = null; + + public JsonElement Json { - Value = ImmutableArray.ToImmutableArray(value); + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public BetaWebSearchToolResultBlockParamContent(BetaWebSearchToolRequestError value) + public BetaWebSearchToolResultBlockParamContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - BetaWebSearchToolResultBlockParamContent(UnknownVariant value) + public BetaWebSearchToolResultBlockParamContent( + BetaWebSearchToolRequestError value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static BetaWebSearchToolResultBlockParamContent CreateUnknownVariant(JsonElement value) + public BetaWebSearchToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickResultBlock( @@ -94,15 +102,13 @@ BetaWebSearchToolRequestError value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of BetaWebSearchToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class BetaWebSearchToolResultBlockParamContentConverter @@ -114,52 +120,41 @@ sealed class BetaWebSearchToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new BetaWebSearchToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebSearchToolRequestError'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize>( - ref reader, + json, options ); if (deserialized != null) { - return new BetaWebSearchToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -168,7 +163,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/MessageBetaContentBlockSourceContent.cs b/src/Anthropic/Models/Beta/Messages/MessageBetaContentBlockSourceContent.cs index 7e1245aa..ffc2dd8b 100644 --- a/src/Anthropic/Models/Beta/Messages/MessageBetaContentBlockSourceContent.cs +++ b/src/Anthropic/Models/Beta/Messages/MessageBetaContentBlockSourceContent.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Beta.Messages; [JsonConverter(typeof(MessageBetaContentBlockSourceContentConverter))] public record class MessageBetaContentBlockSourceContent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -28,24 +34,21 @@ public BetaCacheControlEphemeral? CacheControl } } - public MessageBetaContentBlockSourceContent(BetaTextBlockParam value) - { - Value = value; - } - - public MessageBetaContentBlockSourceContent(BetaImageBlockParam value) + public MessageBetaContentBlockSourceContent(BetaTextBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - MessageBetaContentBlockSourceContent(UnknownVariant value) + public MessageBetaContentBlockSourceContent(BetaImageBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static MessageBetaContentBlockSourceContent CreateUnknownVariant(JsonElement value) + public MessageBetaContentBlockSourceContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickTextBlockParam([NotNullWhen(true)] out BetaTextBlockParam? value) @@ -105,15 +108,13 @@ BetaImageBlockParam value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of MessageBetaContentBlockSourceContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class MessageBetaContentBlockSourceContentConverter @@ -140,8 +141,6 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -151,26 +150,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new MessageBetaContentBlockSourceContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaTextBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "image": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -180,27 +172,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new MessageBetaContentBlockSourceContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaImageBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new MessageBetaContentBlockSourceContent(json); } } } @@ -211,7 +196,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/MessageCountTokensParams.cs b/src/Anthropic/Models/Beta/Messages/MessageCountTokensParams.cs index d55a9cf2..ab40e9ff 100644 --- a/src/Anthropic/Models/Beta/Messages/MessageCountTokensParams.cs +++ b/src/Anthropic/Models/Beta/Messages/MessageCountTokensParams.cs @@ -488,26 +488,30 @@ internal override void AddHeadersToRequest(HttpRequestMessage request, ClientOpt [JsonConverter(typeof(System1Converter))] public record class System1 { - public object Value { get; private init; } + public object? Value { get; } = null; - public System1(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public System1(IReadOnlyList value) + public System1(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - System1(UnknownVariant value) + public System1(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static System1 CreateUnknownVariant(JsonElement value) + public System1(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -566,13 +570,11 @@ public static implicit operator System1(List value) => public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of System1"); } } - - record struct UnknownVariant(JsonElement value); } sealed class System1Converter : JsonConverter @@ -583,58 +585,53 @@ sealed class System1Converter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new System1(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new System1(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write(Utf8JsonWriter writer, System1 value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } [JsonConverter(typeof(ToolConverter))] public record class Tool { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public BetaCacheControlEphemeral? CacheControl { @@ -774,84 +771,93 @@ public long? MaxUses } } - public Tool(BetaTool value) + public Tool(BetaTool value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolBash20241022 value) + public Tool(BetaToolBash20241022 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolBash20250124 value) + public Tool(BetaToolBash20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaCodeExecutionTool20250522 value) + public Tool(BetaCodeExecutionTool20250522 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaCodeExecutionTool20250825 value) + public Tool(BetaCodeExecutionTool20250825 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolComputerUse20241022 value) + public Tool(BetaToolComputerUse20241022 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaMemoryTool20250818 value) + public Tool(BetaMemoryTool20250818 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolComputerUse20250124 value) + public Tool(BetaToolComputerUse20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolTextEditor20241022 value) + public Tool(BetaToolTextEditor20241022 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolTextEditor20250124 value) + public Tool(BetaToolTextEditor20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolTextEditor20250429 value) + public Tool(BetaToolTextEditor20250429 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaToolTextEditor20250728 value) + public Tool(BetaToolTextEditor20250728 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaWebSearchTool20250305 value) + public Tool(BetaWebSearchTool20250305 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Tool(BetaWebFetchTool20250910 value) + public Tool(BetaWebFetchTool20250910 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Tool(UnknownVariant value) + public Tool(JsonElement json) { - Value = value; - } - - public static Tool CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBeta([NotNullWhen(true)] out BetaTool? value) @@ -1091,13 +1097,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Tool"); } } - - record struct UnknownVariant(JsonElement value); } sealed class ToolConverter : JsonConverter @@ -1108,316 +1112,232 @@ sealed class ToolConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'BetaTool'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolBash20241022'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolBash20250124'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionTool20250522'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaCodeExecutionTool20250825'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolComputerUse20241022'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaMemoryTool20250818'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolComputerUse20250124'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20241022'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20250124'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20250429'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize( - ref reader, + json, options ); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaToolTextEditor20250728'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebSearchTool20250305'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Tool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaWebFetchTool20250910'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write(Utf8JsonWriter writer, Tool value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Messages/MessageCreateParams.cs b/src/Anthropic/Models/Beta/Messages/MessageCreateParams.cs index fecd0ea7..8f0d2ed2 100644 --- a/src/Anthropic/Models/Beta/Messages/MessageCreateParams.cs +++ b/src/Anthropic/Models/Beta/Messages/MessageCreateParams.cs @@ -733,26 +733,30 @@ internal override void AddHeadersToRequest(HttpRequestMessage request, ClientOpt [JsonConverter(typeof(ContainerConverter))] public record class Container { - public object Value { get; private init; } + public object? Value { get; } = null; - public Container(BetaContainerParams value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public Container(string value) + public Container(BetaContainerParams value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Container(UnknownVariant value) + public Container(string value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Container CreateUnknownVariant(JsonElement value) + public Container(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBetaContainerParams([NotNullWhen(true)] out BetaContainerParams? value) @@ -808,13 +812,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Container"); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContainerConverter : JsonConverter @@ -825,43 +827,35 @@ sealed class ContainerConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Container(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BetaContainerParams'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new Container(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -870,8 +864,7 @@ public override void Write( JsonSerializerOptions options ) { - object? variant = value?.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value?.Json, options); } } @@ -935,26 +928,30 @@ JsonSerializerOptions options [JsonConverter(typeof(SystemModelConverter))] public record class SystemModel { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; - public SystemModel(string value) + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public SystemModel(IReadOnlyList value) + public SystemModel(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - SystemModel(UnknownVariant value) + public SystemModel(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static SystemModel CreateUnknownVariant(JsonElement value) + public SystemModel(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -1013,15 +1010,13 @@ public static implicit operator SystemModel(List value) => public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of SystemModel" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class SystemModelConverter : JsonConverter @@ -1032,45 +1027,34 @@ sealed class SystemModelConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new SystemModel(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new SystemModel(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -1079,7 +1063,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Beta/Models/ModelRetrieveParams.cs b/src/Anthropic/Models/Beta/Models/ModelRetrieveParams.cs index 3061fead..5d41b68b 100644 --- a/src/Anthropic/Models/Beta/Models/ModelRetrieveParams.cs +++ b/src/Anthropic/Models/Beta/Models/ModelRetrieveParams.cs @@ -16,7 +16,7 @@ namespace Anthropic.Models.Beta.Models; /// public sealed record class ModelRetrieveParams : ParamsBase { - public required string ModelID { get; init; } + public string? ModelID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Skills/SkillDeleteParams.cs b/src/Anthropic/Models/Beta/Skills/SkillDeleteParams.cs index a7a1c906..fee78e95 100644 --- a/src/Anthropic/Models/Beta/Skills/SkillDeleteParams.cs +++ b/src/Anthropic/Models/Beta/Skills/SkillDeleteParams.cs @@ -14,7 +14,7 @@ namespace Anthropic.Models.Beta.Skills; /// public sealed record class SkillDeleteParams : ParamsBase { - public required string SkillID { get; init; } + public string? SkillID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Skills/SkillRetrieveParams.cs b/src/Anthropic/Models/Beta/Skills/SkillRetrieveParams.cs index 6007ddbb..c5c27af5 100644 --- a/src/Anthropic/Models/Beta/Skills/SkillRetrieveParams.cs +++ b/src/Anthropic/Models/Beta/Skills/SkillRetrieveParams.cs @@ -14,7 +14,7 @@ namespace Anthropic.Models.Beta.Skills; /// public sealed record class SkillRetrieveParams : ParamsBase { - public required string SkillID { get; init; } + public string? SkillID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Skills/Versions/VersionCreateParams.cs b/src/Anthropic/Models/Beta/Skills/Versions/VersionCreateParams.cs index 4177fede..d382dfd8 100644 --- a/src/Anthropic/Models/Beta/Skills/Versions/VersionCreateParams.cs +++ b/src/Anthropic/Models/Beta/Skills/Versions/VersionCreateParams.cs @@ -21,7 +21,7 @@ public IReadOnlyDictionary BodyProperties get { return this._bodyProperties.Freeze(); } } - public required string SkillID { get; init; } + public string? SkillID { get; init; } /// /// Files to upload for the skill. diff --git a/src/Anthropic/Models/Beta/Skills/Versions/VersionDeleteParams.cs b/src/Anthropic/Models/Beta/Skills/Versions/VersionDeleteParams.cs index cef05685..c1c4b723 100644 --- a/src/Anthropic/Models/Beta/Skills/Versions/VersionDeleteParams.cs +++ b/src/Anthropic/Models/Beta/Skills/Versions/VersionDeleteParams.cs @@ -16,7 +16,7 @@ public sealed record class VersionDeleteParams : ParamsBase { public required string SkillID { get; init; } - public required string Version { get; init; } + public string? Version { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/Beta/Skills/Versions/VersionListParams.cs b/src/Anthropic/Models/Beta/Skills/Versions/VersionListParams.cs index 330651df..2aa7cd48 100644 --- a/src/Anthropic/Models/Beta/Skills/Versions/VersionListParams.cs +++ b/src/Anthropic/Models/Beta/Skills/Versions/VersionListParams.cs @@ -14,7 +14,7 @@ namespace Anthropic.Models.Beta.Skills.Versions; /// public sealed record class VersionListParams : ParamsBase { - public required string SkillID { get; init; } + public string? SkillID { get; init; } /// /// Number of items to return per page. diff --git a/src/Anthropic/Models/Beta/Skills/Versions/VersionRetrieveParams.cs b/src/Anthropic/Models/Beta/Skills/Versions/VersionRetrieveParams.cs index adb6f6bd..0038a66d 100644 --- a/src/Anthropic/Models/Beta/Skills/Versions/VersionRetrieveParams.cs +++ b/src/Anthropic/Models/Beta/Skills/Versions/VersionRetrieveParams.cs @@ -16,7 +16,7 @@ public sealed record class VersionRetrieveParams : ParamsBase { public required string SkillID { get; init; } - public required string Version { get; init; } + public string? Version { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Models/ErrorObject.cs b/src/Anthropic/Models/ErrorObject.cs index 4fddb339..df996177 100644 --- a/src/Anthropic/Models/ErrorObject.cs +++ b/src/Anthropic/Models/ErrorObject.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models; [JsonConverter(typeof(ErrorObjectConverter))] public record class ErrorObject { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string Message { @@ -48,59 +54,63 @@ public JsonElement Type } } - public ErrorObject(InvalidRequestError value) - { - Value = value; - } - - public ErrorObject(AuthenticationError value) + public ErrorObject(InvalidRequestError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ErrorObject(BillingError value) + public ErrorObject(AuthenticationError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ErrorObject(PermissionError value) + public ErrorObject(BillingError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ErrorObject(NotFoundError value) + public ErrorObject(PermissionError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ErrorObject(RateLimitError value) + public ErrorObject(NotFoundError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ErrorObject(GatewayTimeoutError value) + public ErrorObject(RateLimitError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ErrorObject(APIErrorObject value) + public ErrorObject(GatewayTimeoutError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ErrorObject(OverloadedError value) + public ErrorObject(APIErrorObject value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ErrorObject(UnknownVariant value) + public ErrorObject(OverloadedError value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static ErrorObject CreateUnknownVariant(JsonElement value) + public ErrorObject(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickInvalidRequestError([NotNullWhen(true)] out InvalidRequestError? value) @@ -254,15 +264,13 @@ Func overloadedError public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ErrorObject" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ErrorObjectConverter : JsonConverter @@ -288,8 +296,6 @@ JsonSerializerOptions options { case "invalid_request_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -299,25 +305,18 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'InvalidRequestError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "authentication_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -327,125 +326,90 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'AuthenticationError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "billing_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'BillingError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "permission_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'PermissionError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "not_found_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'NotFoundError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "rate_limit_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RateLimitError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "timeout_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -455,76 +419,55 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'GatewayTimeoutError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "api_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'APIErrorObject'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } case "overloaded_error": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ErrorObject(deserialized); + return new(deserialized, json); } } catch (Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'OverloadedError'", - e - ) - ); + // ignore } - throw new AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ErrorObject(json); } } } @@ -535,7 +478,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/Batches/BatchCancelParams.cs b/src/Anthropic/Models/Messages/Batches/BatchCancelParams.cs index a067b551..cf9db99c 100644 --- a/src/Anthropic/Models/Messages/Batches/BatchCancelParams.cs +++ b/src/Anthropic/Models/Messages/Batches/BatchCancelParams.cs @@ -21,7 +21,7 @@ namespace Anthropic.Models.Messages.Batches; /// public sealed record class BatchCancelParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } public BatchCancelParams() { } diff --git a/src/Anthropic/Models/Messages/Batches/BatchCreateParams.cs b/src/Anthropic/Models/Messages/Batches/BatchCreateParams.cs index bfc2944e..35581228 100644 --- a/src/Anthropic/Models/Messages/Batches/BatchCreateParams.cs +++ b/src/Anthropic/Models/Messages/Batches/BatchCreateParams.cs @@ -863,26 +863,30 @@ JsonSerializerOptions options [JsonConverter(typeof(ParamsSystemConverter))] public record class ParamsSystem { - public object Value { get; private init; } + public object? Value { get; } = null; - public ParamsSystem(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public ParamsSystem(IReadOnlyList value) + public ParamsSystem(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - ParamsSystem(UnknownVariant value) + public ParamsSystem(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static ParamsSystem CreateUnknownVariant(JsonElement value) + public ParamsSystem(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -939,15 +943,13 @@ public static implicit operator ParamsSystem(List value) => public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ParamsSystem" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ParamsSystemConverter : JsonConverter @@ -958,45 +960,34 @@ sealed class ParamsSystemConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new ParamsSystem(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new ParamsSystem(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -1005,7 +996,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/Batches/BatchDeleteParams.cs b/src/Anthropic/Models/Messages/Batches/BatchDeleteParams.cs index a074e783..490d1d1e 100644 --- a/src/Anthropic/Models/Messages/Batches/BatchDeleteParams.cs +++ b/src/Anthropic/Models/Messages/Batches/BatchDeleteParams.cs @@ -18,7 +18,7 @@ namespace Anthropic.Models.Messages.Batches; /// public sealed record class BatchDeleteParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } public BatchDeleteParams() { } diff --git a/src/Anthropic/Models/Messages/Batches/BatchResultsParams.cs b/src/Anthropic/Models/Messages/Batches/BatchResultsParams.cs index 6bc0dd04..328e8bb4 100644 --- a/src/Anthropic/Models/Messages/Batches/BatchResultsParams.cs +++ b/src/Anthropic/Models/Messages/Batches/BatchResultsParams.cs @@ -19,7 +19,7 @@ namespace Anthropic.Models.Messages.Batches; /// public sealed record class BatchResultsParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } public BatchResultsParams() { } diff --git a/src/Anthropic/Models/Messages/Batches/BatchRetrieveParams.cs b/src/Anthropic/Models/Messages/Batches/BatchRetrieveParams.cs index ac5b7e91..27727555 100644 --- a/src/Anthropic/Models/Messages/Batches/BatchRetrieveParams.cs +++ b/src/Anthropic/Models/Messages/Batches/BatchRetrieveParams.cs @@ -17,7 +17,7 @@ namespace Anthropic.Models.Messages.Batches; /// public sealed record class BatchRetrieveParams : ParamsBase { - public required string MessageBatchID { get; init; } + public string? MessageBatchID { get; init; } public BatchRetrieveParams() { } diff --git a/src/Anthropic/Models/Messages/Batches/MessageBatchResult.cs b/src/Anthropic/Models/Messages/Batches/MessageBatchResult.cs index 1ffcb990..5d8218fd 100644 --- a/src/Anthropic/Models/Messages/Batches/MessageBatchResult.cs +++ b/src/Anthropic/Models/Messages/Batches/MessageBatchResult.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -17,7 +16,14 @@ namespace Anthropic.Models.Messages.Batches; [JsonConverter(typeof(MessageBatchResultConverter))] public record class MessageBatchResult { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -32,34 +38,33 @@ public JsonElement Type } } - public MessageBatchResult(MessageBatchSucceededResult value) + public MessageBatchResult(MessageBatchSucceededResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public MessageBatchResult(MessageBatchErroredResult value) + public MessageBatchResult(MessageBatchErroredResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public MessageBatchResult(MessageBatchCanceledResult value) + public MessageBatchResult(MessageBatchCanceledResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public MessageBatchResult(MessageBatchExpiredResult value) + public MessageBatchResult(MessageBatchExpiredResult value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - MessageBatchResult(UnknownVariant value) + public MessageBatchResult(JsonElement json) { - Value = value; - } - - public static MessageBatchResult CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickSucceeded([NotNullWhen(true)] out MessageBatchSucceededResult? value) @@ -147,15 +152,13 @@ public static implicit operator MessageBatchResult(MessageBatchExpiredResult val public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of MessageBatchResult" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class MessageBatchResultConverter : JsonConverter @@ -181,8 +184,6 @@ JsonSerializerOptions options { case "succeeded": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -192,26 +193,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new MessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'MessageBatchSucceededResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "errored": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -221,26 +215,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new MessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'MessageBatchErroredResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "canceled": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -250,26 +237,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new MessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'MessageBatchCanceledResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "expired": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -279,27 +259,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new MessageBatchResult(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'MessageBatchExpiredResult'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new MessageBatchResult(json); } } } @@ -310,7 +283,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/CitationsDelta.cs b/src/Anthropic/Models/Messages/CitationsDelta.cs index f141c282..7725e130 100644 --- a/src/Anthropic/Models/Messages/CitationsDelta.cs +++ b/src/Anthropic/Models/Messages/CitationsDelta.cs @@ -110,7 +110,14 @@ public CitationsDelta(Citation citation) [JsonConverter(typeof(CitationConverter))] public record class Citation { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string CitedText { @@ -224,39 +231,39 @@ public string? Title } } - public Citation(CitationCharLocation value) - { - Value = value; - } - - public Citation(CitationPageLocation value) + public Citation(CitationCharLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Citation(CitationContentBlockLocation value) + public Citation(CitationPageLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Citation(CitationsWebSearchResultLocation value) + public Citation(CitationContentBlockLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Citation(CitationsSearchResultLocation value) + public Citation(CitationsWebSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Citation(UnknownVariant value) + public Citation(CitationsSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Citation CreateUnknownVariant(JsonElement value) + public Citation(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickCharLocation([NotNullWhen(true)] out CitationCharLocation? value) @@ -360,13 +367,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Citation"); } } - - record struct UnknownVariant(JsonElement value); } sealed class CitationConverter : JsonConverter @@ -392,8 +397,6 @@ JsonSerializerOptions options { case "char_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -403,26 +406,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationCharLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "page_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -432,26 +428,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationPageLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -461,26 +450,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationContentBlockLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_result_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -490,26 +472,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationsWebSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -519,34 +494,26 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Citation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationsSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Citation(json); } } } public override void Write(Utf8JsonWriter writer, Citation value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ContentBlock.cs b/src/Anthropic/Models/Messages/ContentBlock.cs index 26df1506..c9e07ef9 100644 --- a/src/Anthropic/Models/Messages/ContentBlock.cs +++ b/src/Anthropic/Models/Messages/ContentBlock.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(ContentBlockConverter))] public record class ContentBlock { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -42,44 +48,45 @@ public string? ID } } - public ContentBlock(TextBlock value) + public ContentBlock(TextBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(ThinkingBlock value) + public ContentBlock(ThinkingBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(RedactedThinkingBlock value) + public ContentBlock(RedactedThinkingBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(ToolUseBlock value) + public ContentBlock(ToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(ServerToolUseBlock value) + public ContentBlock(ServerToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlock(WebSearchToolResultBlock value) + public ContentBlock(WebSearchToolResultBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ContentBlock(UnknownVariant value) + public ContentBlock(JsonElement json) { - Value = value; - } - - public static ContentBlock CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickText([NotNullWhen(true)] out TextBlock? value) @@ -191,15 +198,13 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ContentBlock" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContentBlockConverter : JsonConverter @@ -225,60 +230,44 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'TextBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "redacted_thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -288,52 +277,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RedactedThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "server_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -343,26 +318,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ServerToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -372,27 +340,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'WebSearchToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ContentBlock(json); } } } @@ -403,7 +364,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ContentBlockParam.cs b/src/Anthropic/Models/Messages/ContentBlockParam.cs index e81ccf9c..2f7a6f26 100644 --- a/src/Anthropic/Models/Messages/ContentBlockParam.cs +++ b/src/Anthropic/Models/Messages/ContentBlockParam.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -13,7 +12,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(ContentBlockParamConverter))] public record class ContentBlockParam { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -110,64 +116,69 @@ public string? ToolUseID } } - public ContentBlockParam(TextBlockParam value) + public ContentBlockParam(TextBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(ImageBlockParam value) + public ContentBlockParam(ImageBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(DocumentBlockParam value) + public ContentBlockParam(DocumentBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(SearchResultBlockParam value) + public ContentBlockParam(SearchResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(ThinkingBlockParam value) + public ContentBlockParam(ThinkingBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(RedactedThinkingBlockParam value) + public ContentBlockParam(RedactedThinkingBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(ToolUseBlockParam value) + public ContentBlockParam(ToolUseBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(ToolResultBlockParam value) + public ContentBlockParam(ToolResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(ServerToolUseBlockParam value) + public ContentBlockParam(ServerToolUseBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ContentBlockParam(WebSearchToolResultBlockParam value) + public ContentBlockParam(WebSearchToolResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ContentBlockParam(UnknownVariant value) + public ContentBlockParam(JsonElement json) { - Value = value; - } - - public static ContentBlockParam CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickText([NotNullWhen(true)] out TextBlockParam? value) @@ -339,15 +350,13 @@ public static implicit operator ContentBlockParam(WebSearchToolResultBlockParam public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ContentBlockParam" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContentBlockParamConverter : JsonConverter @@ -373,60 +382,44 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'TextBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "image": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ImageBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "document": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -436,26 +429,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'DocumentBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -465,26 +451,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'SearchResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -494,26 +473,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ThinkingBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "redacted_thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -523,52 +495,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RedactedThinkingBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolUseBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -578,26 +536,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "server_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -607,26 +558,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ServerToolUseBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -636,27 +580,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ContentBlockParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'WebSearchToolResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ContentBlockParam(json); } } } @@ -667,7 +604,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ContentBlockSource.cs b/src/Anthropic/Models/Messages/ContentBlockSource.cs index b5e3c8f7..ac834d42 100644 --- a/src/Anthropic/Models/Messages/ContentBlockSource.cs +++ b/src/Anthropic/Models/Messages/ContentBlockSource.cs @@ -111,26 +111,30 @@ public ContentBlockSource(Content content) [JsonConverter(typeof(ContentConverter))] public record class Content { - public object Value { get; private init; } + public object? Value { get; } = null; - public Content(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public Content(IReadOnlyList value) + public Content(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - Content(UnknownVariant value) + public Content(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static Content CreateUnknownVariant(JsonElement value) + public Content(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -189,13 +193,11 @@ public static implicit operator Content(List value) = public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Content"); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContentConverter : JsonConverter @@ -206,50 +208,41 @@ sealed class ContentConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new Content(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize>( - ref reader, + json, options ); if (deserialized != null) { - return new Content(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write(Utf8JsonWriter writer, Content value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ContentBlockSourceContent.cs b/src/Anthropic/Models/Messages/ContentBlockSourceContent.cs index f95088f0..c6f0bf05 100644 --- a/src/Anthropic/Models/Messages/ContentBlockSourceContent.cs +++ b/src/Anthropic/Models/Messages/ContentBlockSourceContent.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(ContentBlockSourceContentConverter))] public record class ContentBlockSourceContent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -28,24 +34,21 @@ public CacheControlEphemeral? CacheControl } } - public ContentBlockSourceContent(TextBlockParam value) - { - Value = value; - } - - public ContentBlockSourceContent(ImageBlockParam value) + public ContentBlockSourceContent(TextBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ContentBlockSourceContent(UnknownVariant value) + public ContentBlockSourceContent(ImageBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static ContentBlockSourceContent CreateUnknownVariant(JsonElement value) + public ContentBlockSourceContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickTextBlockParam([NotNullWhen(true)] out TextBlockParam? value) @@ -101,15 +104,13 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ContentBlockSourceContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ContentBlockSourceContentConverter : JsonConverter @@ -135,61 +136,45 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlockSourceContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'TextBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "image": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ContentBlockSourceContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ImageBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ContentBlockSourceContent(json); } } } @@ -200,7 +185,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/DocumentBlockParam.cs b/src/Anthropic/Models/Messages/DocumentBlockParam.cs index a990e129..86ec4453 100644 --- a/src/Anthropic/Models/Messages/DocumentBlockParam.cs +++ b/src/Anthropic/Models/Messages/DocumentBlockParam.cs @@ -195,7 +195,14 @@ public DocumentBlockParam(Source source) [JsonConverter(typeof(SourceConverter))] public record class Source { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string? Data { @@ -236,34 +243,33 @@ public JsonElement Type } } - public Source(Base64PDFSource value) + public Source(Base64PDFSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Source(PlainTextSource value) + public Source(PlainTextSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Source(ContentBlockSource value) + public Source(ContentBlockSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Source(URLPDFSource value) + public Source(URLPDFSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Source(UnknownVariant value) + public Source(JsonElement json) { - Value = value; - } - - public static Source CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBase64PDF([NotNullWhen(true)] out Base64PDFSource? value) @@ -345,13 +351,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Source"); } } - - record struct UnknownVariant(JsonElement value); } sealed class SourceConverter : JsonConverter @@ -377,60 +381,44 @@ JsonSerializerOptions options { case "base64": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Source(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'Base64PDFSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Source(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'PlainTextSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -440,60 +428,45 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Source(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ContentBlockSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "url": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Source(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'URLPDFSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Source(json); } } } public override void Write(Utf8JsonWriter writer, Source value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ImageBlockParam.cs b/src/Anthropic/Models/Messages/ImageBlockParam.cs index 10438d45..3cddd100 100644 --- a/src/Anthropic/Models/Messages/ImageBlockParam.cs +++ b/src/Anthropic/Models/Messages/ImageBlockParam.cs @@ -135,31 +135,35 @@ public ImageBlockParam(ImageBlockParamSource source) [JsonConverter(typeof(ImageBlockParamSourceConverter))] public record class ImageBlockParamSource { - public object Value { get; private init; } + public object? Value { get; } = null; - public JsonElement Type + JsonElement? _json = null; + + public JsonElement Json { - get { return Match(base64Image: (x) => x.Type, urlImage: (x) => x.Type); } + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public ImageBlockParamSource(Base64ImageSource value) + public JsonElement Type { - Value = value; + get { return Match(base64Image: (x) => x.Type, urlImage: (x) => x.Type); } } - public ImageBlockParamSource(URLImageSource value) + public ImageBlockParamSource(Base64ImageSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ImageBlockParamSource(UnknownVariant value) + public ImageBlockParamSource(URLImageSource value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static ImageBlockParamSource CreateUnknownVariant(JsonElement value) + public ImageBlockParamSource(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickBase64Image([NotNullWhen(true)] out Base64ImageSource? value) @@ -215,15 +219,13 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ImageBlockParamSource" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ImageBlockParamSourceConverter : JsonConverter @@ -249,61 +251,45 @@ JsonSerializerOptions options { case "base64": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ImageBlockParamSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'Base64ImageSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "url": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ImageBlockParamSource(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'URLImageSource'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ImageBlockParamSource(json); } } } @@ -314,7 +300,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/MessageCountTokensParams.cs b/src/Anthropic/Models/Messages/MessageCountTokensParams.cs index 51883a14..7ea461d0 100644 --- a/src/Anthropic/Models/Messages/MessageCountTokensParams.cs +++ b/src/Anthropic/Models/Messages/MessageCountTokensParams.cs @@ -378,26 +378,30 @@ internal override void AddHeadersToRequest(HttpRequestMessage request, ClientOpt [JsonConverter(typeof(System1Converter))] public record class System1 { - public object Value { get; private init; } + public object? Value { get; } = null; - public System1(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public System1(IReadOnlyList value) + public System1(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - System1(UnknownVariant value) + public System1(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static System1 CreateUnknownVariant(JsonElement value) + public System1(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -454,13 +458,11 @@ public static implicit operator System1(List value) => public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of System1"); } } - - record struct UnknownVariant(JsonElement value); } sealed class System1Converter : JsonConverter @@ -471,50 +473,38 @@ sealed class System1Converter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new System1(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new System1(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write(Utf8JsonWriter writer, System1 value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/MessageCountTokensTool.cs b/src/Anthropic/Models/Messages/MessageCountTokensTool.cs index 7b766189..0aaba3d8 100644 --- a/src/Anthropic/Models/Messages/MessageCountTokensTool.cs +++ b/src/Anthropic/Models/Messages/MessageCountTokensTool.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(MessageCountTokensToolConverter))] public record class MessageCountTokensTool { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public CacheControlEphemeral? CacheControl { @@ -27,44 +33,45 @@ public CacheControlEphemeral? CacheControl } } - public MessageCountTokensTool(Tool value) - { - Value = value; - } - - public MessageCountTokensTool(ToolBash20250124 value) + public MessageCountTokensTool(Tool value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public MessageCountTokensTool(ToolTextEditor20250124 value) + public MessageCountTokensTool(ToolBash20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public MessageCountTokensTool(ToolTextEditor20250429 value) + public MessageCountTokensTool(ToolTextEditor20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public MessageCountTokensTool(ToolTextEditor20250728 value) + public MessageCountTokensTool(ToolTextEditor20250429 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public MessageCountTokensTool(WebSearchTool20250305 value) + public MessageCountTokensTool(ToolTextEditor20250728 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - MessageCountTokensTool(UnknownVariant value) + public MessageCountTokensTool(WebSearchTool20250305 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static MessageCountTokensTool CreateUnknownVariant(JsonElement value) + public MessageCountTokensTool(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickTool([NotNullWhen(true)] out Tool? value) @@ -180,15 +187,13 @@ public static implicit operator MessageCountTokensTool(WebSearchTool20250305 val public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of MessageCountTokensTool" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class MessageCountTokensToolConverter : JsonConverter @@ -199,132 +204,92 @@ sealed class MessageCountTokensToolConverter : JsonConverter exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new MessageCountTokensTool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'Tool'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new MessageCountTokensTool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolBash20250124'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new MessageCountTokensTool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolTextEditor20250124'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new MessageCountTokensTool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolTextEditor20250429'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new MessageCountTokensTool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolTextEditor20250728'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new MessageCountTokensTool(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'WebSearchTool20250305'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -333,7 +298,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/MessageCreateParams.cs b/src/Anthropic/Models/Messages/MessageCreateParams.cs index 5f1e5a6e..fcc92aea 100644 --- a/src/Anthropic/Models/Messages/MessageCreateParams.cs +++ b/src/Anthropic/Models/Messages/MessageCreateParams.cs @@ -651,26 +651,30 @@ JsonSerializerOptions options [JsonConverter(typeof(SystemModelConverter))] public record class SystemModel { - public object Value { get; private init; } + public object? Value { get; } = null; - public SystemModel(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public SystemModel(IReadOnlyList value) + public SystemModel(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - SystemModel(UnknownVariant value) + public SystemModel(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static SystemModel CreateUnknownVariant(JsonElement value) + public SystemModel(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -727,15 +731,13 @@ public static implicit operator SystemModel(List value) => public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of SystemModel" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class SystemModelConverter : JsonConverter @@ -746,45 +748,34 @@ sealed class SystemModelConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new SystemModel(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new SystemModel(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -793,7 +784,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/MessageParam.cs b/src/Anthropic/Models/Messages/MessageParam.cs index 1caaad82..75bbf2fa 100644 --- a/src/Anthropic/Models/Messages/MessageParam.cs +++ b/src/Anthropic/Models/Messages/MessageParam.cs @@ -95,26 +95,30 @@ public static MessageParam FromRawUnchecked(IReadOnlyDictionary value) + public MessageParamContent(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - MessageParamContent(UnknownVariant value) + public MessageParamContent(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static MessageParamContent CreateUnknownVariant(JsonElement value) + public MessageParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -173,15 +177,13 @@ public static implicit operator MessageParamContent(List valu public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of MessageParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class MessageParamContentConverter : JsonConverter @@ -192,45 +194,34 @@ sealed class MessageParamContentConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new MessageParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new MessageParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -239,8 +230,7 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/RawContentBlockDelta.cs b/src/Anthropic/Models/Messages/RawContentBlockDelta.cs index 1569310f..6064cada 100644 --- a/src/Anthropic/Models/Messages/RawContentBlockDelta.cs +++ b/src/Anthropic/Models/Messages/RawContentBlockDelta.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(RawContentBlockDeltaConverter))] public record class RawContentBlockDelta { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -26,39 +32,39 @@ public JsonElement Type } } - public RawContentBlockDelta(TextDelta value) + public RawContentBlockDelta(TextDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockDelta(InputJSONDelta value) + public RawContentBlockDelta(InputJSONDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockDelta(CitationsDelta value) + public RawContentBlockDelta(CitationsDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockDelta(ThinkingDelta value) + public RawContentBlockDelta(ThinkingDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockDelta(SignatureDelta value) + public RawContentBlockDelta(SignatureDelta value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - RawContentBlockDelta(UnknownVariant value) + public RawContentBlockDelta(JsonElement json) { - Value = value; - } - - public static RawContentBlockDelta CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickText([NotNullWhen(true)] out TextDelta? value) @@ -156,15 +162,13 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of RawContentBlockDelta" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class RawContentBlockDeltaConverter : JsonConverter @@ -190,139 +194,102 @@ JsonSerializerOptions options { case "text_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'TextDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "input_json_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'InputJSONDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "citations_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationsDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ThinkingDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "signature_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockDelta(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'SignatureDelta'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new RawContentBlockDelta(json); } } } @@ -333,7 +300,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/RawContentBlockStartEvent.cs b/src/Anthropic/Models/Messages/RawContentBlockStartEvent.cs index ca5ce21f..f501de96 100644 --- a/src/Anthropic/Models/Messages/RawContentBlockStartEvent.cs +++ b/src/Anthropic/Models/Messages/RawContentBlockStartEvent.cs @@ -133,7 +133,14 @@ IReadOnlyDictionary properties [JsonConverter(typeof(RawContentBlockStartEventContentBlockConverter))] public record class RawContentBlockStartEventContentBlock { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -165,44 +172,51 @@ public string? ID } } - public RawContentBlockStartEventContentBlock(TextBlock value) - { - Value = value; - } - - public RawContentBlockStartEventContentBlock(ThinkingBlock value) + public RawContentBlockStartEventContentBlock(TextBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockStartEventContentBlock(RedactedThinkingBlock value) + public RawContentBlockStartEventContentBlock(ThinkingBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockStartEventContentBlock(ToolUseBlock value) + public RawContentBlockStartEventContentBlock( + RedactedThinkingBlock value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockStartEventContentBlock(ServerToolUseBlock value) + public RawContentBlockStartEventContentBlock(ToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawContentBlockStartEventContentBlock(WebSearchToolResultBlock value) + public RawContentBlockStartEventContentBlock(ServerToolUseBlock value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - RawContentBlockStartEventContentBlock(UnknownVariant value) + public RawContentBlockStartEventContentBlock( + WebSearchToolResultBlock value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static RawContentBlockStartEventContentBlock CreateUnknownVariant(JsonElement value) + public RawContentBlockStartEventContentBlock(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickText([NotNullWhen(true)] out TextBlock? value) @@ -323,15 +337,13 @@ WebSearchToolResultBlock value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of RawContentBlockStartEventContentBlock" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class RawContentBlockStartEventContentBlockConverter @@ -358,60 +370,44 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockStartEventContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'TextBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockStartEventContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "redacted_thinking": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -421,52 +417,38 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockStartEventContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RedactedThinkingBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockStartEventContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "server_tool_use": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -476,26 +458,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockStartEventContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ServerToolUseBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_tool_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -505,27 +480,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawContentBlockStartEventContentBlock(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'WebSearchToolResultBlock'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new RawContentBlockStartEventContentBlock(json); } } } @@ -536,7 +504,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/RawMessageStreamEvent.cs b/src/Anthropic/Models/Messages/RawMessageStreamEvent.cs index 3e73e3ea..b1771a04 100644 --- a/src/Anthropic/Models/Messages/RawMessageStreamEvent.cs +++ b/src/Anthropic/Models/Messages/RawMessageStreamEvent.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(RawMessageStreamEventConverter))] public record class RawMessageStreamEvent { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -42,44 +48,45 @@ public long? Index } } - public RawMessageStreamEvent(RawMessageStartEvent value) + public RawMessageStreamEvent(RawMessageStartEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawMessageStreamEvent(RawMessageDeltaEvent value) + public RawMessageStreamEvent(RawMessageDeltaEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawMessageStreamEvent(RawMessageStopEvent value) + public RawMessageStreamEvent(RawMessageStopEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawMessageStreamEvent(RawContentBlockStartEvent value) + public RawMessageStreamEvent(RawContentBlockStartEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawMessageStreamEvent(RawContentBlockDeltaEvent value) + public RawMessageStreamEvent(RawContentBlockDeltaEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public RawMessageStreamEvent(RawContentBlockStopEvent value) + public RawMessageStreamEvent(RawContentBlockStopEvent value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - RawMessageStreamEvent(UnknownVariant value) + public RawMessageStreamEvent(JsonElement json) { - Value = value; - } - - public static RawMessageStreamEvent CreateUnknownVariant(JsonElement value) - { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickStart([NotNullWhen(true)] out RawMessageStartEvent? value) @@ -194,15 +201,13 @@ public static implicit operator RawMessageStreamEvent(RawContentBlockStopEvent v public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of RawMessageStreamEvent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class RawMessageStreamEventConverter : JsonConverter @@ -228,8 +233,6 @@ JsonSerializerOptions options { case "message_start": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -239,26 +242,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RawMessageStartEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "message_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -268,26 +264,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RawMessageDeltaEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "message_stop": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -297,26 +286,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RawMessageStopEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_start": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -326,26 +308,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RawContentBlockStartEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_delta": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -355,26 +330,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RawContentBlockDeltaEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_stop": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -384,27 +352,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new RawMessageStreamEvent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'RawContentBlockStopEvent'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new RawMessageStreamEvent(json); } } } @@ -415,7 +376,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/TextCitation.cs b/src/Anthropic/Models/Messages/TextCitation.cs index 28042965..bbfe8277 100644 --- a/src/Anthropic/Models/Messages/TextCitation.cs +++ b/src/Anthropic/Models/Messages/TextCitation.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(TextCitationConverter))] public record class TextCitation { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string CitedText { @@ -124,39 +130,39 @@ public string? Title } } - public TextCitation(CitationCharLocation value) - { - Value = value; - } - - public TextCitation(CitationPageLocation value) + public TextCitation(CitationCharLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public TextCitation(CitationContentBlockLocation value) + public TextCitation(CitationPageLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public TextCitation(CitationsWebSearchResultLocation value) + public TextCitation(CitationContentBlockLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public TextCitation(CitationsSearchResultLocation value) + public TextCitation(CitationsWebSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - TextCitation(UnknownVariant value) + public TextCitation(CitationsSearchResultLocation value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static TextCitation CreateUnknownVariant(JsonElement value) + public TextCitation(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickCitationCharLocation([NotNullWhen(true)] out CitationCharLocation? value) @@ -261,15 +267,13 @@ public static implicit operator TextCitation(CitationsWebSearchResultLocation va public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of TextCitation" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class TextCitationConverter : JsonConverter @@ -295,8 +299,6 @@ JsonSerializerOptions options { case "char_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -306,26 +308,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationCharLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "page_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -335,26 +330,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationPageLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -364,26 +352,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationContentBlockLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_result_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -393,26 +374,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationsWebSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -422,27 +396,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitation(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationsSearchResultLocation'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new TextCitation(json); } } } @@ -453,7 +420,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/TextCitationParam.cs b/src/Anthropic/Models/Messages/TextCitationParam.cs index 0801ea54..33a2a9b2 100644 --- a/src/Anthropic/Models/Messages/TextCitationParam.cs +++ b/src/Anthropic/Models/Messages/TextCitationParam.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(TextCitationParamConverter))] public record class TextCitationParam { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public string CitedText { @@ -110,39 +116,39 @@ public string? Title } } - public TextCitationParam(CitationCharLocationParam value) - { - Value = value; - } - - public TextCitationParam(CitationPageLocationParam value) + public TextCitationParam(CitationCharLocationParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public TextCitationParam(CitationContentBlockLocationParam value) + public TextCitationParam(CitationPageLocationParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public TextCitationParam(CitationWebSearchResultLocationParam value) + public TextCitationParam(CitationContentBlockLocationParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public TextCitationParam(CitationSearchResultLocationParam value) + public TextCitationParam(CitationWebSearchResultLocationParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - TextCitationParam(UnknownVariant value) + public TextCitationParam(CitationSearchResultLocationParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static TextCitationParam CreateUnknownVariant(JsonElement value) + public TextCitationParam(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickCitationCharLocation( @@ -255,15 +261,13 @@ public static implicit operator TextCitationParam(CitationSearchResultLocationPa public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of TextCitationParam" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class TextCitationParamConverter : JsonConverter @@ -289,8 +293,6 @@ JsonSerializerOptions options { case "char_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -300,26 +302,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationCharLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "page_location": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -329,26 +324,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationPageLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "content_block_location": { - List exceptions = []; - try { var deserialized = @@ -359,26 +347,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationContentBlockLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "web_search_result_location": { - List exceptions = []; - try { var deserialized = @@ -389,26 +370,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationWebSearchResultLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result_location": { - List exceptions = []; - try { var deserialized = @@ -419,27 +393,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new TextCitationParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'CitationSearchResultLocationParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new TextCitationParam(json); } } } @@ -450,7 +417,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ThinkingConfigParam.cs b/src/Anthropic/Models/Messages/ThinkingConfigParam.cs index 0cb82385..7b750473 100644 --- a/src/Anthropic/Models/Messages/ThinkingConfigParam.cs +++ b/src/Anthropic/Models/Messages/ThinkingConfigParam.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -20,31 +19,35 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(ThinkingConfigParamConverter))] public record class ThinkingConfigParam { - public object Value { get; private init; } + public object? Value { get; } = null; - public JsonElement Type + JsonElement? _json = null; + + public JsonElement Json { - get { return Match(enabled: (x) => x.Type, disabled: (x) => x.Type); } + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public ThinkingConfigParam(ThinkingConfigEnabled value) + public JsonElement Type { - Value = value; + get { return Match(enabled: (x) => x.Type, disabled: (x) => x.Type); } } - public ThinkingConfigParam(ThinkingConfigDisabled value) + public ThinkingConfigParam(ThinkingConfigEnabled value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ThinkingConfigParam(UnknownVariant value) + public ThinkingConfigParam(ThinkingConfigDisabled value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static ThinkingConfigParam CreateUnknownVariant(JsonElement value) + public ThinkingConfigParam(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickEnabled([NotNullWhen(true)] out ThinkingConfigEnabled? value) @@ -100,15 +103,13 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ThinkingConfigParam" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ThinkingConfigParamConverter : JsonConverter @@ -134,8 +135,6 @@ JsonSerializerOptions options { case "enabled": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -145,26 +144,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ThinkingConfigParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ThinkingConfigEnabled'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "disabled": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -174,27 +166,20 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new ThinkingConfigParam(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ThinkingConfigDisabled'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ThinkingConfigParam(json); } } } @@ -205,7 +190,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ToolChoice.cs b/src/Anthropic/Models/Messages/ToolChoice.cs index db2f497e..a375d3bc 100644 --- a/src/Anthropic/Models/Messages/ToolChoice.cs +++ b/src/Anthropic/Models/Messages/ToolChoice.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -14,7 +13,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(ToolChoiceConverter))] public record class ToolChoice { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -42,34 +48,33 @@ public bool? DisableParallelToolUse } } - public ToolChoice(ToolChoiceAuto value) - { - Value = value; - } - - public ToolChoice(ToolChoiceAny value) + public ToolChoice(ToolChoiceAuto value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ToolChoice(ToolChoiceTool value) + public ToolChoice(ToolChoiceAny value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ToolChoice(ToolChoiceNone value) + public ToolChoice(ToolChoiceTool value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ToolChoice(UnknownVariant value) + public ToolChoice(ToolChoiceNone value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static ToolChoice CreateUnknownVariant(JsonElement value) + public ToolChoice(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickAuto([NotNullWhen(true)] out ToolChoiceAuto? value) @@ -153,13 +158,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of ToolChoice"); } } - - record struct UnknownVariant(JsonElement value); } sealed class ToolChoiceConverter : JsonConverter @@ -185,113 +188,83 @@ JsonSerializerOptions options { case "auto": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolChoiceAuto'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "any": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolChoiceAny'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "tool": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolChoiceTool'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "none": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolChoice(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolChoiceNone'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new ToolChoice(json); } } } @@ -302,7 +275,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ToolResultBlockParam.cs b/src/Anthropic/Models/Messages/ToolResultBlockParam.cs index 06ffbd20..178b851d 100644 --- a/src/Anthropic/Models/Messages/ToolResultBlockParam.cs +++ b/src/Anthropic/Models/Messages/ToolResultBlockParam.cs @@ -190,26 +190,30 @@ public ToolResultBlockParam(string toolUseID) [JsonConverter(typeof(ToolResultBlockParamContentConverter))] public record class ToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public ToolResultBlockParamContent(string value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public ToolResultBlockParamContent(IReadOnlyList value) + public ToolResultBlockParamContent(string value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - ToolResultBlockParamContent(UnknownVariant value) + public ToolResultBlockParamContent(IReadOnlyList value, JsonElement? json = null) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static ToolResultBlockParamContent CreateUnknownVariant(JsonElement value) + public ToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickString([NotNullWhen(true)] out string? value) @@ -260,15 +264,13 @@ public static implicit operator ToolResultBlockParamContent(List value) = public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of ToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class ToolResultBlockParamContentConverter : JsonConverter @@ -279,42 +281,34 @@ sealed class ToolResultBlockParamContentConverter : JsonConverter exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { - return new ToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'string'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize>(ref reader, options); + var deserialized = JsonSerializer.Deserialize>(json, options); if (deserialized != null) { - return new ToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -323,15 +317,21 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } [JsonConverter(typeof(BlockConverter))] public record class Block { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public JsonElement Type { @@ -372,34 +372,33 @@ public string? Title } } - public Block(TextBlockParam value) - { - Value = value; - } - - public Block(ImageBlockParam value) + public Block(TextBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Block(SearchResultBlockParam value) + public Block(ImageBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public Block(DocumentBlockParam value) + public Block(SearchResultBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - Block(UnknownVariant value) + public Block(DocumentBlockParam value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static Block CreateUnknownVariant(JsonElement value) + public Block(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickTextBlockParam([NotNullWhen(true)] out TextBlockParam? value) @@ -479,13 +478,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of Block"); } } - - record struct UnknownVariant(JsonElement value); } sealed class BlockConverter : JsonConverter @@ -511,60 +508,44 @@ JsonSerializerOptions options { case "text": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'TextBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "image": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ImageBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "search_result": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -574,26 +555,19 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'SearchResultBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } case "document": { - List exceptions = []; - try { var deserialized = JsonSerializer.Deserialize( @@ -603,34 +577,26 @@ JsonSerializerOptions options if (deserialized != null) { deserialized.Validate(); - return new Block(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'DocumentBlockParam'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } default: { - throw new AnthropicInvalidDataException( - "Could not find valid union variant to represent data" - ); + return new Block(json); } } } public override void Write(Utf8JsonWriter writer, Block value, JsonSerializerOptions options) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/ToolUnion.cs b/src/Anthropic/Models/Messages/ToolUnion.cs index f5eb445e..6b3e9029 100644 --- a/src/Anthropic/Models/Messages/ToolUnion.cs +++ b/src/Anthropic/Models/Messages/ToolUnion.cs @@ -1,4 +1,3 @@ -using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Text.Json; using System.Text.Json.Serialization; @@ -10,7 +9,14 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(ToolUnionConverter))] public record class ToolUnion { - public object Value { get; private init; } + public object? Value { get; } = null; + + JsonElement? _json = null; + + public JsonElement Json + { + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } + } public CacheControlEphemeral? CacheControl { @@ -27,44 +33,45 @@ public CacheControlEphemeral? CacheControl } } - public ToolUnion(Tool value) - { - Value = value; - } - - public ToolUnion(ToolBash20250124 value) + public ToolUnion(Tool value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ToolUnion(ToolTextEditor20250124 value) + public ToolUnion(ToolBash20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ToolUnion(ToolTextEditor20250429 value) + public ToolUnion(ToolTextEditor20250124 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ToolUnion(ToolTextEditor20250728 value) + public ToolUnion(ToolTextEditor20250429 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public ToolUnion(WebSearchTool20250305 value) + public ToolUnion(ToolTextEditor20250728 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - ToolUnion(UnknownVariant value) + public ToolUnion(WebSearchTool20250305 value, JsonElement? json = null) { - Value = value; + this.Value = value; + this._json = json; } - public static ToolUnion CreateUnknownVariant(JsonElement value) + public ToolUnion(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickTool([NotNullWhen(true)] out Tool? value) @@ -176,13 +183,11 @@ public T Match( public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException("Data did not match any variant of ToolUnion"); } } - - record struct UnknownVariant(JsonElement value); } sealed class ToolUnionConverter : JsonConverter @@ -193,132 +198,92 @@ sealed class ToolUnionConverter : JsonConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException("Data does not match union variant 'Tool'", e) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize(ref reader, options); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolBash20250124'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolTextEditor20250124'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolTextEditor20250429'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'ToolTextEditor20250728'", - e - ) - ); + // ignore } try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new ToolUnion(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'WebSearchTool20250305'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -327,7 +292,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/WebSearchToolResultBlockContent.cs b/src/Anthropic/Models/Messages/WebSearchToolResultBlockContent.cs index 46d0cfc4..f63215fd 100644 --- a/src/Anthropic/Models/Messages/WebSearchToolResultBlockContent.cs +++ b/src/Anthropic/Models/Messages/WebSearchToolResultBlockContent.cs @@ -11,26 +11,33 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(WebSearchToolResultBlockContentConverter))] public record class WebSearchToolResultBlockContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public WebSearchToolResultBlockContent(WebSearchToolResultError value) + JsonElement? _json = null; + + public JsonElement Json { - Value = value; + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public WebSearchToolResultBlockContent(IReadOnlyList value) + public WebSearchToolResultBlockContent(WebSearchToolResultError value, JsonElement? json = null) { - Value = ImmutableArray.ToImmutableArray(value); + this.Value = value; + this._json = json; } - WebSearchToolResultBlockContent(UnknownVariant value) + public WebSearchToolResultBlockContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - public static WebSearchToolResultBlockContent CreateUnknownVariant(JsonElement value) + public WebSearchToolResultBlockContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickError([NotNullWhen(true)] out WebSearchToolResultError? value) @@ -92,15 +99,13 @@ List value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of WebSearchToolResultBlockContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class WebSearchToolResultBlockContentConverter @@ -112,52 +117,38 @@ sealed class WebSearchToolResultBlockContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new WebSearchToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'WebSearchToolResultError'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize>( - ref reader, + json, options ); if (deserialized != null) { - return new WebSearchToolResultBlockContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -166,7 +157,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Messages/WebSearchToolResultBlockParamContent.cs b/src/Anthropic/Models/Messages/WebSearchToolResultBlockParamContent.cs index 37b3d2f4..22e4e727 100644 --- a/src/Anthropic/Models/Messages/WebSearchToolResultBlockParamContent.cs +++ b/src/Anthropic/Models/Messages/WebSearchToolResultBlockParamContent.cs @@ -11,26 +11,36 @@ namespace Anthropic.Models.Messages; [JsonConverter(typeof(WebSearchToolResultBlockParamContentConverter))] public record class WebSearchToolResultBlockParamContent { - public object Value { get; private init; } + public object? Value { get; } = null; - public WebSearchToolResultBlockParamContent(IReadOnlyList value) + JsonElement? _json = null; + + public JsonElement Json { - Value = ImmutableArray.ToImmutableArray(value); + get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); } } - public WebSearchToolResultBlockParamContent(WebSearchToolRequestError value) + public WebSearchToolResultBlockParamContent( + IReadOnlyList value, + JsonElement? json = null + ) { - Value = value; + this.Value = ImmutableArray.ToImmutableArray(value); + this._json = json; } - WebSearchToolResultBlockParamContent(UnknownVariant value) + public WebSearchToolResultBlockParamContent( + WebSearchToolRequestError value, + JsonElement? json = null + ) { - Value = value; + this.Value = value; + this._json = json; } - public static WebSearchToolResultBlockParamContent CreateUnknownVariant(JsonElement value) + public WebSearchToolResultBlockParamContent(JsonElement json) { - return new(new UnknownVariant(value)); + this._json = json; } public bool TryPickItem([NotNullWhen(true)] out IReadOnlyList? value) @@ -90,15 +100,13 @@ WebSearchToolRequestError value public void Validate() { - if (this.Value is UnknownVariant) + if (this.Value == null) { throw new AnthropicInvalidDataException( "Data did not match any variant of WebSearchToolResultBlockParamContent" ); } } - - record struct UnknownVariant(JsonElement value); } sealed class WebSearchToolResultBlockParamContentConverter @@ -110,52 +118,38 @@ sealed class WebSearchToolResultBlockParamContentConverter JsonSerializerOptions options ) { - List exceptions = []; - + var json = JsonSerializer.Deserialize(ref reader, options); try { - var deserialized = JsonSerializer.Deserialize( - ref reader, - options - ); + var deserialized = JsonSerializer.Deserialize(json, options); if (deserialized != null) { deserialized.Validate(); - return new WebSearchToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'WebSearchToolRequestError'", - e - ) - ); + // ignore } try { var deserialized = JsonSerializer.Deserialize>( - ref reader, + json, options ); if (deserialized != null) { - return new WebSearchToolResultBlockParamContent(deserialized); + return new(deserialized, json); } } catch (System::Exception e) when (e is JsonException || e is AnthropicInvalidDataException) { - exceptions.Add( - new AnthropicInvalidDataException( - "Data does not match union variant 'List'", - e - ) - ); + // ignore } - throw new System::AggregateException(exceptions); + return new(json); } public override void Write( @@ -164,7 +158,6 @@ public override void Write( JsonSerializerOptions options ) { - object variant = value.Value; - JsonSerializer.Serialize(writer, variant, options); + JsonSerializer.Serialize(writer, value.Json, options); } } diff --git a/src/Anthropic/Models/Models/ModelRetrieveParams.cs b/src/Anthropic/Models/Models/ModelRetrieveParams.cs index 950930f4..056f4d2b 100644 --- a/src/Anthropic/Models/Models/ModelRetrieveParams.cs +++ b/src/Anthropic/Models/Models/ModelRetrieveParams.cs @@ -17,7 +17,7 @@ namespace Anthropic.Models.Models; /// public sealed record class ModelRetrieveParams : ParamsBase { - public required string ModelID { get; init; } + public string? ModelID { get; init; } /// /// Optional header to specify the beta version(s) you want to use. diff --git a/src/Anthropic/Services/Beta/FileService.cs b/src/Anthropic/Services/Beta/FileService.cs index ed50cef5..bb81c4be 100644 --- a/src/Anthropic/Services/Beta/FileService.cs +++ b/src/Anthropic/Services/Beta/FileService.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Anthropic.Core; +using Anthropic.Exceptions; using Anthropic.Models.Beta.Files; namespace Anthropic.Services.Beta; @@ -56,6 +57,11 @@ public async Task Delete( CancellationToken cancellationToken = default ) { + if (parameters.FileID == null) + { + throw new AnthropicInvalidDataException("'parameters.FileID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Delete, @@ -74,11 +80,27 @@ public async Task Delete( return deletedFile; } + public async Task Delete( + string fileID, + FileDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Delete(parameters with { FileID = fileID }, cancellationToken); + } + public async Task Download( FileDownloadParams parameters, CancellationToken cancellationToken = default ) { + if (parameters.FileID == null) + { + throw new AnthropicInvalidDataException("'parameters.FileID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -90,11 +112,27 @@ public async Task Download( return response; } + public async Task Download( + string fileID, + FileDownloadParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Download(parameters with { FileID = fileID }, cancellationToken); + } + public async Task RetrieveMetadata( FileRetrieveMetadataParams parameters, CancellationToken cancellationToken = default ) { + if (parameters.FileID == null) + { + throw new AnthropicInvalidDataException("'parameters.FileID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -112,4 +150,15 @@ public async Task RetrieveMetadata( } return fileMetadata; } + + public async Task RetrieveMetadata( + string fileID, + FileRetrieveMetadataParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.RetrieveMetadata(parameters with { FileID = fileID }, cancellationToken); + } } diff --git a/src/Anthropic/Services/Beta/IFileService.cs b/src/Anthropic/Services/Beta/IFileService.cs index df3dbe8f..1d2f2c3e 100644 --- a/src/Anthropic/Services/Beta/IFileService.cs +++ b/src/Anthropic/Services/Beta/IFileService.cs @@ -31,6 +31,15 @@ Task Delete( CancellationToken cancellationToken = default ); + /// + /// Delete File + /// + Task Delete( + string fileID, + FileDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// Download File /// @@ -39,6 +48,15 @@ Task Download( CancellationToken cancellationToken = default ); + /// + /// Download File + /// + Task Download( + string fileID, + FileDownloadParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// Get File Metadata /// @@ -46,4 +64,13 @@ Task RetrieveMetadata( FileRetrieveMetadataParams parameters, CancellationToken cancellationToken = default ); + + /// + /// Get File Metadata + /// + Task RetrieveMetadata( + string fileID, + FileRetrieveMetadataParams? parameters = null, + CancellationToken cancellationToken = default + ); } diff --git a/src/Anthropic/Services/Beta/IModelService.cs b/src/Anthropic/Services/Beta/IModelService.cs index 60d93429..1d5ba1af 100644 --- a/src/Anthropic/Services/Beta/IModelService.cs +++ b/src/Anthropic/Services/Beta/IModelService.cs @@ -28,6 +28,18 @@ Task Retrieve( CancellationToken cancellationToken = default ); + /// + /// Get a specific model. + /// + /// The Models API response can be used to determine information about a + /// specific model or resolve a model alias to a model ID. + /// + Task Retrieve( + string modelID, + ModelRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// List available models. /// diff --git a/src/Anthropic/Services/Beta/ISkillService.cs b/src/Anthropic/Services/Beta/ISkillService.cs index 2c0baa18..a55b63ae 100644 --- a/src/Anthropic/Services/Beta/ISkillService.cs +++ b/src/Anthropic/Services/Beta/ISkillService.cs @@ -34,6 +34,15 @@ Task Retrieve( CancellationToken cancellationToken = default ); + /// + /// Get Skill + /// + Task Retrieve( + string skillID, + SkillRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// List Skills /// @@ -49,4 +58,13 @@ Task Delete( SkillDeleteParams parameters, CancellationToken cancellationToken = default ); + + /// + /// Delete Skill + /// + Task Delete( + string skillID, + SkillDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ); } diff --git a/src/Anthropic/Services/Beta/MessageService.cs b/src/Anthropic/Services/Beta/MessageService.cs index f50ee9a3..531aa7cb 100644 --- a/src/Anthropic/Services/Beta/MessageService.cs +++ b/src/Anthropic/Services/Beta/MessageService.cs @@ -80,6 +80,11 @@ public async IAsyncEnumerable CreateStreaming( var bodyProperties = parameters.BodyProperties.ToDictionary(e => e.Key, e => e.Value); bodyProperties["stream"] = JsonSerializer.Deserialize("true"); #endif + parameters = MessageCreateParams.FromRawUnchecked( + parameters.HeaderProperties, + parameters.QueryProperties, + bodyProperties + ); HttpRequest request = new() { Method = HttpMethod.Post, diff --git a/src/Anthropic/Services/Beta/Messages/BatchService.cs b/src/Anthropic/Services/Beta/Messages/BatchService.cs index c411dfb7..4c074b26 100644 --- a/src/Anthropic/Services/Beta/Messages/BatchService.cs +++ b/src/Anthropic/Services/Beta/Messages/BatchService.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using Anthropic.Core; +using Anthropic.Exceptions; using Anthropic.Models.Beta.Messages.Batches; namespace Anthropic.Services.Beta.Messages; @@ -56,6 +57,11 @@ public async Task Retrieve( CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -74,6 +80,23 @@ public async Task Retrieve( return betaMessageBatch; } + public async Task Retrieve( + string messageBatchID, + BatchRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Retrieve( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ); + } + public async Task List( BatchListParams? parameters = null, CancellationToken cancellationToken = default @@ -104,6 +127,11 @@ public async Task Delete( CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Delete, @@ -122,11 +150,33 @@ public async Task Delete( return betaDeletedMessageBatch; } + public async Task Delete( + string messageBatchID, + BatchDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Delete( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ); + } + public async Task Cancel( BatchCancelParams parameters, CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Post, @@ -145,11 +195,33 @@ public async Task Cancel( return betaMessageBatch; } + public async Task Cancel( + string messageBatchID, + BatchCancelParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Cancel( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ); + } + public async IAsyncEnumerable ResultsStreaming( BatchResultsParams parameters, [EnumeratorCancellation] CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -169,4 +241,26 @@ public async IAsyncEnumerable ResultsStreami yield return betaMessageBatchIndividualResponse; } } + + public async IAsyncEnumerable ResultsStreaming( + string messageBatchID, + BatchResultsParams? parameters = null, + [EnumeratorCancellation] CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + await foreach ( + var item in this.ResultsStreaming( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ) + ) + { + yield return item; + } + } } diff --git a/src/Anthropic/Services/Beta/Messages/IBatchService.cs b/src/Anthropic/Services/Beta/Messages/IBatchService.cs index e57e65da..3c29e637 100644 --- a/src/Anthropic/Services/Beta/Messages/IBatchService.cs +++ b/src/Anthropic/Services/Beta/Messages/IBatchService.cs @@ -42,6 +42,19 @@ Task Retrieve( CancellationToken cancellationToken = default ); + /// + /// This endpoint is idempotent and can be used to poll for Message Batch completion. + /// To access the results of a Message Batch, make a request to the `results_url` + /// field in the response. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + Task Retrieve( + string messageBatchID, + BatchRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// List all Message Batches within a Workspace. Most recently created batches /// are returned first. @@ -66,6 +79,20 @@ Task Delete( CancellationToken cancellationToken = default ); + /// + /// Delete a Message Batch. + /// + /// Message Batches can only be deleted once they've finished processing. + /// If you'd like to delete an in-progress batch, you must first cancel it. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + Task Delete( + string messageBatchID, + BatchDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// Batches may be canceled any time before processing ends. Once cancellation /// is initiated, the batch enters a `canceling` state, at which time the system @@ -83,6 +110,24 @@ Task Cancel( CancellationToken cancellationToken = default ); + /// + /// Batches may be canceled any time before processing ends. Once cancellation + /// is initiated, the batch enters a `canceling` state, at which time the system + /// may complete any in-progress, non-interruptible requests before finalizing cancellation. + /// + /// The number of canceled requests is specified in `request_counts`. To + /// determine which requests were canceled, check the individual results within + /// the batch. Note that cancellation may not result in any canceled requests + /// if they were non-interruptible. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + Task Cancel( + string messageBatchID, + BatchCancelParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// Streams the results of a Message Batch as a `.jsonl` file. /// @@ -96,4 +141,19 @@ IAsyncEnumerable ResultsStreaming( BatchResultsParams parameters, CancellationToken cancellationToken = default ); + + /// + /// Streams the results of a Message Batch as a `.jsonl` file. + /// + /// Each line in the file is a JSON object containing the result of a single + /// request in the Message Batch. Results are not guaranteed to be in the same + /// order as requests. Use the `custom_id` field to match results to requests. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + IAsyncEnumerable ResultsStreaming( + string messageBatchID, + BatchResultsParams? parameters = null, + CancellationToken cancellationToken = default + ); } diff --git a/src/Anthropic/Services/Beta/ModelService.cs b/src/Anthropic/Services/Beta/ModelService.cs index de6e9271..74f8d621 100644 --- a/src/Anthropic/Services/Beta/ModelService.cs +++ b/src/Anthropic/Services/Beta/ModelService.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Anthropic.Core; +using Anthropic.Exceptions; using Anthropic.Models.Beta.Models; namespace Anthropic.Services.Beta; @@ -28,6 +29,11 @@ public async Task Retrieve( CancellationToken cancellationToken = default ) { + if (parameters.ModelID == null) + { + throw new AnthropicInvalidDataException("'parameters.ModelID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -46,6 +52,17 @@ public async Task Retrieve( return betaModelInfo; } + public async Task Retrieve( + string modelID, + ModelRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Retrieve(parameters with { ModelID = modelID }, cancellationToken); + } + public async Task List( ModelListParams? parameters = null, CancellationToken cancellationToken = default diff --git a/src/Anthropic/Services/Beta/SkillService.cs b/src/Anthropic/Services/Beta/SkillService.cs index be403db5..e845f721 100644 --- a/src/Anthropic/Services/Beta/SkillService.cs +++ b/src/Anthropic/Services/Beta/SkillService.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Anthropic.Core; +using Anthropic.Exceptions; using Anthropic.Models.Beta.Skills; using Anthropic.Services.Beta.Skills; @@ -64,6 +65,11 @@ public async Task Retrieve( CancellationToken cancellationToken = default ) { + if (parameters.SkillID == null) + { + throw new AnthropicInvalidDataException("'parameters.SkillID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -82,6 +88,17 @@ public async Task Retrieve( return skill; } + public async Task Retrieve( + string skillID, + SkillRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Retrieve(parameters with { SkillID = skillID }, cancellationToken); + } + public async Task List( SkillListParams? parameters = null, CancellationToken cancellationToken = default @@ -112,6 +129,11 @@ public async Task Delete( CancellationToken cancellationToken = default ) { + if (parameters.SkillID == null) + { + throw new AnthropicInvalidDataException("'parameters.SkillID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Delete, @@ -129,4 +151,15 @@ public async Task Delete( } return skill; } + + public async Task Delete( + string skillID, + SkillDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Delete(parameters with { SkillID = skillID }, cancellationToken); + } } diff --git a/src/Anthropic/Services/Beta/Skills/IVersionService.cs b/src/Anthropic/Services/Beta/Skills/IVersionService.cs index a3c7c711..9223e4cc 100644 --- a/src/Anthropic/Services/Beta/Skills/IVersionService.cs +++ b/src/Anthropic/Services/Beta/Skills/IVersionService.cs @@ -23,10 +23,28 @@ Task Create( CancellationToken cancellationToken = default ); + /// + /// Create Skill Version + /// + Task Create( + string skillID, + VersionCreateParams? parameters = null, + CancellationToken cancellationToken = default + ); + + /// + /// Get Skill Version + /// + Task Retrieve( + VersionRetrieveParams parameters, + CancellationToken cancellationToken = default + ); + /// /// Get Skill Version /// Task Retrieve( + string version, VersionRetrieveParams parameters, CancellationToken cancellationToken = default ); @@ -39,10 +57,28 @@ Task List( CancellationToken cancellationToken = default ); + /// + /// List Skill Versions + /// + Task List( + string skillID, + VersionListParams? parameters = null, + CancellationToken cancellationToken = default + ); + + /// + /// Delete Skill Version + /// + Task Delete( + VersionDeleteParams parameters, + CancellationToken cancellationToken = default + ); + /// /// Delete Skill Version /// Task Delete( + string version, VersionDeleteParams parameters, CancellationToken cancellationToken = default ); diff --git a/src/Anthropic/Services/Beta/Skills/VersionService.cs b/src/Anthropic/Services/Beta/Skills/VersionService.cs index b65b81f1..b40d0e26 100644 --- a/src/Anthropic/Services/Beta/Skills/VersionService.cs +++ b/src/Anthropic/Services/Beta/Skills/VersionService.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Anthropic.Core; +using Anthropic.Exceptions; using Anthropic.Models.Beta.Skills.Versions; namespace Anthropic.Services.Beta.Skills; @@ -31,6 +32,11 @@ public async Task Create( CancellationToken cancellationToken = default ) { + if (parameters.SkillID == null) + { + throw new AnthropicInvalidDataException("'parameters.SkillID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Post, @@ -49,11 +55,27 @@ public async Task Create( return version; } + public async Task Create( + string skillID, + VersionCreateParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Create(parameters with { SkillID = skillID }, cancellationToken); + } + public async Task Retrieve( VersionRetrieveParams parameters, CancellationToken cancellationToken = default ) { + if (parameters.Version == null) + { + throw new AnthropicInvalidDataException("'parameters.Version' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -72,11 +94,25 @@ public async Task Retrieve( return version; } + public async Task Retrieve( + string version, + VersionRetrieveParams parameters, + CancellationToken cancellationToken = default + ) + { + return await this.Retrieve(parameters with { Version = version }, cancellationToken); + } + public async Task List( VersionListParams parameters, CancellationToken cancellationToken = default ) { + if (parameters.SkillID == null) + { + throw new AnthropicInvalidDataException("'parameters.SkillID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -95,11 +131,27 @@ public async Task List( return page; } + public async Task List( + string skillID, + VersionListParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.List(parameters with { SkillID = skillID }, cancellationToken); + } + public async Task Delete( VersionDeleteParams parameters, CancellationToken cancellationToken = default ) { + if (parameters.Version == null) + { + throw new AnthropicInvalidDataException("'parameters.Version' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Delete, @@ -117,4 +169,13 @@ public async Task Delete( } return version; } + + public async Task Delete( + string version, + VersionDeleteParams parameters, + CancellationToken cancellationToken = default + ) + { + return await this.Delete(parameters with { Version = version }, cancellationToken); + } } diff --git a/src/Anthropic/Services/IModelService.cs b/src/Anthropic/Services/IModelService.cs index c5d927c9..e718801d 100644 --- a/src/Anthropic/Services/IModelService.cs +++ b/src/Anthropic/Services/IModelService.cs @@ -26,6 +26,18 @@ Task Retrieve( CancellationToken cancellationToken = default ); + /// + /// Get a specific model. + /// + /// The Models API response can be used to determine information about a + /// specific model or resolve a model alias to a model ID. + /// + Task Retrieve( + string modelID, + ModelRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// List available models. /// diff --git a/src/Anthropic/Services/MessageService.cs b/src/Anthropic/Services/MessageService.cs index 80ee36ae..1dd526d0 100644 --- a/src/Anthropic/Services/MessageService.cs +++ b/src/Anthropic/Services/MessageService.cs @@ -79,6 +79,7 @@ public async IAsyncEnumerable CreateStreaming( parameters.QueryProperties, bodyProperties ); + HttpRequest request = new() { Method = HttpMethod.Post, diff --git a/src/Anthropic/Services/Messages/BatchService.cs b/src/Anthropic/Services/Messages/BatchService.cs index 206222dc..10fb9b1c 100644 --- a/src/Anthropic/Services/Messages/BatchService.cs +++ b/src/Anthropic/Services/Messages/BatchService.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using Anthropic.Core; +using Anthropic.Exceptions; using Anthropic.Models.Messages.Batches; namespace Anthropic.Services.Messages; @@ -51,6 +52,11 @@ public async Task Retrieve( CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -69,6 +75,23 @@ public async Task Retrieve( return messageBatch; } + public async Task Retrieve( + string messageBatchID, + BatchRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Retrieve( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ); + } + public async Task List( BatchListParams? parameters = null, CancellationToken cancellationToken = default @@ -99,6 +122,11 @@ public async Task Delete( CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Delete, @@ -117,11 +145,33 @@ public async Task Delete( return deletedMessageBatch; } + public async Task Delete( + string messageBatchID, + BatchDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Delete( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ); + } + public async Task Cancel( BatchCancelParams parameters, CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Post, @@ -140,11 +190,33 @@ public async Task Cancel( return messageBatch; } + public async Task Cancel( + string messageBatchID, + BatchCancelParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Cancel( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ); + } + public async IAsyncEnumerable ResultsStreaming( BatchResultsParams parameters, [EnumeratorCancellation] CancellationToken cancellationToken = default ) { + if (parameters.MessageBatchID == null) + { + throw new AnthropicInvalidDataException("'parameters.MessageBatchID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -164,4 +236,26 @@ public async IAsyncEnumerable ResultsStreaming( yield return messageBatchIndividualResponse; } } + + public async IAsyncEnumerable ResultsStreaming( + string messageBatchID, + BatchResultsParams? parameters = null, + [EnumeratorCancellation] CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + await foreach ( + var item in this.ResultsStreaming( + parameters with + { + MessageBatchID = messageBatchID, + }, + cancellationToken + ) + ) + { + yield return item; + } + } } diff --git a/src/Anthropic/Services/Messages/IBatchService.cs b/src/Anthropic/Services/Messages/IBatchService.cs index 77d4c965..3d977dc6 100644 --- a/src/Anthropic/Services/Messages/IBatchService.cs +++ b/src/Anthropic/Services/Messages/IBatchService.cs @@ -42,6 +42,19 @@ Task Retrieve( CancellationToken cancellationToken = default ); + /// + /// This endpoint is idempotent and can be used to poll for Message Batch completion. + /// To access the results of a Message Batch, make a request to the `results_url` + /// field in the response. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + Task Retrieve( + string messageBatchID, + BatchRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// List all Message Batches within a Workspace. Most recently created batches /// are returned first. @@ -66,6 +79,20 @@ Task Delete( CancellationToken cancellationToken = default ); + /// + /// Delete a Message Batch. + /// + /// Message Batches can only be deleted once they've finished processing. + /// If you'd like to delete an in-progress batch, you must first cancel it. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + Task Delete( + string messageBatchID, + BatchDeleteParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// Batches may be canceled any time before processing ends. Once cancellation /// is initiated, the batch enters a `canceling` state, at which time the system @@ -83,6 +110,24 @@ Task Cancel( CancellationToken cancellationToken = default ); + /// + /// Batches may be canceled any time before processing ends. Once cancellation + /// is initiated, the batch enters a `canceling` state, at which time the system + /// may complete any in-progress, non-interruptible requests before finalizing cancellation. + /// + /// The number of canceled requests is specified in `request_counts`. To + /// determine which requests were canceled, check the individual results within + /// the batch. Note that cancellation may not result in any canceled requests + /// if they were non-interruptible. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + Task Cancel( + string messageBatchID, + BatchCancelParams? parameters = null, + CancellationToken cancellationToken = default + ); + /// /// Streams the results of a Message Batch as a `.jsonl` file. /// @@ -96,4 +141,19 @@ IAsyncEnumerable ResultsStreaming( BatchResultsParams parameters, CancellationToken cancellationToken = default ); + + /// + /// Streams the results of a Message Batch as a `.jsonl` file. + /// + /// Each line in the file is a JSON object containing the result of a single + /// request in the Message Batch. Results are not guaranteed to be in the same + /// order as requests. Use the `custom_id` field to match results to requests. + /// + /// Learn more about the Message Batches API in our [user guide](https://docs.claude.com/en/docs/build-with-claude/batch-processing) + /// + IAsyncEnumerable ResultsStreaming( + string messageBatchID, + BatchResultsParams? parameters = null, + CancellationToken cancellationToken = default + ); } diff --git a/src/Anthropic/Services/ModelService.cs b/src/Anthropic/Services/ModelService.cs index a465740f..aa3af116 100644 --- a/src/Anthropic/Services/ModelService.cs +++ b/src/Anthropic/Services/ModelService.cs @@ -3,6 +3,7 @@ using System.Threading; using System.Threading.Tasks; using Anthropic.Core; +using Anthropic.Exceptions; using Anthropic.Models.Models; namespace Anthropic.Services; @@ -26,6 +27,11 @@ public async Task Retrieve( CancellationToken cancellationToken = default ) { + if (parameters.ModelID == null) + { + throw new AnthropicInvalidDataException("'parameters.ModelID' cannot be null"); + } + HttpRequest request = new() { Method = HttpMethod.Get, @@ -44,6 +50,17 @@ public async Task Retrieve( return modelInfo; } + public async Task Retrieve( + string modelID, + ModelRetrieveParams? parameters = null, + CancellationToken cancellationToken = default + ) + { + parameters ??= new(); + + return await this.Retrieve(parameters with { ModelID = modelID }, cancellationToken); + } + public async Task List( ModelListParams? parameters = null, CancellationToken cancellationToken = default