fix: copilot first attempt

This commit is contained in:
Stevan Freeborn
2025-05-19 19:52:16 +00:00
parent 02e34bb0ab
commit 0a1c6a0135
2 changed files with 75 additions and 76 deletions
+49 -63
View File
@@ -1,4 +1,5 @@
using System.Net.Http.Headers; using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
@@ -50,13 +51,12 @@ public class AnthropicApiClient : IAnthropicApiClient
_httpClient.DefaultRequestHeaders.Add(pair.Key, pair.Value); _httpClient.DefaultRequestHeaders.Add(pair.Key, pair.Value);
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request) public async Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(MessagesEndpoint, request); var response = await SendRequestAsync(MessagesEndpoint, request, cancellationToken);
var anthropicHeaders = new AnthropicHeaders(response.Headers); var anthropicHeaders = new AnthropicHeaders(response.Headers);
var responseContent = await response.Content.ReadAsStringAsync(); var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);
if (response.IsSuccessStatusCode is false) if (response.IsSuccessStatusCode is false)
{ {
@@ -75,13 +75,13 @@ public class AnthropicApiClient : IAnthropicApiClient
} }
/// <inheritdoc/> /// <inheritdoc/>
public async IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request) public async IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(MessagesEndpoint, request); var response = await SendRequestAsync(MessagesEndpoint, request, cancellationToken);
if (response.IsSuccessStatusCode is false) if (response.IsSuccessStatusCode is false)
{ {
var error = Deserialize<AnthropicError>(await response.Content.ReadAsStringAsync()) ?? new AnthropicError(); var error = Deserialize<AnthropicError>(await response.Content.ReadAsStringAsync(cancellationToken)) ?? new AnthropicError();
yield return new AnthropicEvent(EventType.Error, new ErrorEventData(error.Error)); yield return new AnthropicEvent(EventType.Error, new ErrorEventData(error.Error));
yield break; yield break;
} }
@@ -237,73 +237,66 @@ public class AnthropicApiClient : IAnthropicApiClient
} }
} while (true); } while (true);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request) public async Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(MessageBatchesEndpoint, request); var response = await SendRequestAsync(MessageBatchesEndpoint, request, cancellationToken);
return await CreateResultAsync<MessageBatchResponse>(response); return await CreateResultAsync<MessageBatchResponse>(response, cancellationToken);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId) public async Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}"); var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}", cancellationToken: cancellationToken);
return await CreateResultAsync<MessageBatchResponse>(response); return await CreateResultAsync<MessageBatchResponse>(response, cancellationToken);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null) public async Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null, CancellationToken cancellationToken = default)
{ {
var pagingRequest = request ?? new PagingRequest(); var pagingRequest = request ?? new PagingRequest();
var endpoint = $"{MessageBatchesEndpoint}?{pagingRequest.ToQueryParameters()}"; var endpoint = $"{MessageBatchesEndpoint}?{pagingRequest.ToQueryParameters()}";
var response = await SendRequestAsync(endpoint); var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
return await CreateResultAsync<Page<MessageBatchResponse>>(response); return await CreateResultAsync<Page<MessageBatchResponse>>(response, cancellationToken);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20) public async IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
await foreach (var result in GetAllPagesAsync<MessageBatchResponse>(MessageBatchesEndpoint, limit)) await foreach (var result in GetAllPagesAsync<MessageBatchResponse>(MessageBatchesEndpoint, limit, cancellationToken))
{ {
yield return result; yield return result;
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId) public async Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{MessageBatchesEndpoint}/{batchId}/cancel"; var endpoint = $"{MessageBatchesEndpoint}/{batchId}/cancel";
var response = await SendRequestAsync(endpoint, HttpMethod.Post); var response = await SendRequestAsync(endpoint, HttpMethod.Post, cancellationToken);
return await CreateResultAsync<MessageBatchResponse>(response); return await CreateResultAsync<MessageBatchResponse>(response, cancellationToken);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId) public async Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{MessageBatchesEndpoint}/{batchId}"; var endpoint = $"{MessageBatchesEndpoint}/{batchId}";
var response = await SendRequestAsync(endpoint, HttpMethod.Delete); var response = await SendRequestAsync(endpoint, HttpMethod.Delete, cancellationToken);
return await CreateResultAsync<MessageBatchDeleteResponse>(response); return await CreateResultAsync<MessageBatchDeleteResponse>(response, cancellationToken);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId) public async Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}/results"); var response = await SendRequestAsync($"{MessageBatchesEndpoint}/{batchId}/results", cancellationToken: cancellationToken);
var anthropicHeaders = new AnthropicHeaders(response.Headers); var anthropicHeaders = new AnthropicHeaders(response.Headers);
if (response.IsSuccessStatusCode is false) if (response.IsSuccessStatusCode is false)
{ {
var content = await response.Content.ReadAsStringAsync(); var content = await response.Content.ReadAsStringAsync(cancellationToken);
var error = Deserialize<AnthropicError>(content) ?? new AnthropicError(); var error = Deserialize<AnthropicError>(content) ?? new AnthropicError();
return AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>.Failure(error, anthropicHeaders); return AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>.Failure(error, anthropicHeaders);
} }
return AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>.Success(ReadResultsAsync(), anthropicHeaders); return AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>.Success(ReadResultsAsync(), anthropicHeaders);
async IAsyncEnumerable<MessageBatchResultItem> ReadResultsAsync() async IAsyncEnumerable<MessageBatchResultItem> ReadResultsAsync([EnumeratorCancellation] CancellationToken ct = default)
{ {
using var responseContent = await response.Content.ReadAsStreamAsync(); using var responseContent = await response.Content.ReadAsStreamAsync(ct);
using var streamReader = new StreamReader(responseContent); using var streamReader = new StreamReader(responseContent);
var line = await streamReader.ReadLineAsync(); var line = await streamReader.ReadLineAsync();
@@ -317,41 +310,36 @@ public class AnthropicApiClient : IAnthropicApiClient
} }
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request) public async Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request, CancellationToken cancellationToken = default)
{ {
var response = await SendRequestAsync(CountTokensEndpoint, request); var response = await SendRequestAsync(CountTokensEndpoint, request, cancellationToken);
return await CreateResultAsync<TokenCountResponse>(response); return await CreateResultAsync<TokenCountResponse>(response, cancellationToken);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null) public async Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null, CancellationToken cancellationToken = default)
{ {
var pagingRequest = request ?? new PagingRequest(); var pagingRequest = request ?? new PagingRequest();
var endpoint = $"{ModelsEndpoint}?{pagingRequest.ToQueryParameters()}"; var endpoint = $"{ModelsEndpoint}?{pagingRequest.ToQueryParameters()}";
var response = await SendRequestAsync(endpoint); var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
return await CreateResultAsync<Page<AnthropicModel>>(response); return await CreateResultAsync<Page<AnthropicModel>>(response, cancellationToken);
} }
/// <inheritdoc/> /// <inheritdoc/>
public async IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20) public async IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default)
{ {
await foreach (var result in GetAllPagesAsync<AnthropicModel>(ModelsEndpoint, limit)) await foreach (var result in GetAllPagesAsync<AnthropicModel>(ModelsEndpoint, limit, cancellationToken))
{ {
yield return result; yield return result;
} }
} }
/// <inheritdoc/> /// <inheritdoc/>
public async Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId) public async Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId, CancellationToken cancellationToken = default)
{ {
var endpoint = $"{ModelsEndpoint}/{modelId}"; var endpoint = $"{ModelsEndpoint}/{modelId}";
var response = await SendRequestAsync(endpoint); var response = await SendRequestAsync(endpoint, cancellationToken: cancellationToken);
return await CreateResultAsync<AnthropicModel>(response); return await CreateResultAsync<AnthropicModel>(response, cancellationToken);
} }
private async IAsyncEnumerable<AnthropicResult<Page<T>>> GetAllPagesAsync<T>(string endpoint, int limit = 20, [EnumeratorCancellation] CancellationToken cancellationToken = default)
private async IAsyncEnumerable<AnthropicResult<Page<T>>> GetAllPagesAsync<T>(string endpoint, int limit = 20)
{ {
var pagingRequest = new PagingRequest(limit: limit); var pagingRequest = new PagingRequest(limit: limit);
string Endpoint() => $"{endpoint}?{pagingRequest.ToQueryParameters()}"; string Endpoint() => $"{endpoint}?{pagingRequest.ToQueryParameters()}";
@@ -359,9 +347,9 @@ public class AnthropicApiClient : IAnthropicApiClient
do do
{ {
var response = await SendRequestAsync(Endpoint()); var response = await SendRequestAsync(Endpoint(), cancellationToken: cancellationToken);
var anthropicHeaders = new AnthropicHeaders(response.Headers); var anthropicHeaders = new AnthropicHeaders(response.Headers);
var responseContent = await response.Content.ReadAsStringAsync(); var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);
if (response.IsSuccessStatusCode is false) if (response.IsSuccessStatusCode is false)
{ {
@@ -404,11 +392,10 @@ public class AnthropicApiClient : IAnthropicApiClient
return new ToolCall(tool, toolUse); return new ToolCall(tool, toolUse);
} }
private async Task<AnthropicResult<T>> CreateResultAsync<T>(HttpResponseMessage response, CancellationToken cancellationToken = default) where T : new()
private async Task<AnthropicResult<T>> CreateResultAsync<T>(HttpResponseMessage response) where T : new()
{ {
var anthropicHeaders = new AnthropicHeaders(response.Headers); var anthropicHeaders = new AnthropicHeaders(response.Headers);
var responseContent = await response.Content.ReadAsStringAsync(); var responseContent = await response.Content.ReadAsStringAsync(cancellationToken);
if (response.IsSuccessStatusCode is false) if (response.IsSuccessStatusCode is false)
{ {
@@ -419,18 +406,17 @@ public class AnthropicApiClient : IAnthropicApiClient
var model = Deserialize<T>(responseContent) ?? new T(); var model = Deserialize<T>(responseContent) ?? new T();
return AnthropicResult<T>.Success(model, anthropicHeaders); return AnthropicResult<T>.Success(model, anthropicHeaders);
} }
private async Task<HttpResponseMessage> SendRequestAsync(string endpoint, HttpMethod? method = null, CancellationToken cancellationToken = default)
private async Task<HttpResponseMessage> SendRequestAsync(string endpoint, HttpMethod? method = null)
{ {
var request = new HttpRequestMessage(method ?? HttpMethod.Get, endpoint); var request = new HttpRequestMessage(method ?? HttpMethod.Get, endpoint);
return await _httpClient.SendAsync(request); return await _httpClient.SendAsync(request, cancellationToken);
} }
private async Task<HttpResponseMessage> SendRequestAsync<T>(string endpoint, T request) private async Task<HttpResponseMessage> SendRequestAsync<T>(string endpoint, T request, CancellationToken cancellationToken = default)
{ {
var requestJson = Serialize(request); var requestJson = Serialize(request);
var requestContent = new StringContent(requestJson, Encoding.UTF8, JsonContentType); var requestContent = new StringContent(requestJson, Encoding.UTF8, JsonContentType);
return await _httpClient.PostAsync(endpoint, requestContent); return await _httpClient.PostAsync(endpoint, requestContent, cancellationToken);
} }
private string Serialize<T>(T obj) => JsonSerializer.Serialize(obj, JsonSerializationOptions.DefaultOptions); private string Serialize<T>(T obj) => JsonSerializer.Serialize(obj, JsonSerializationOptions.DefaultOptions);
+26 -13
View File
@@ -11,91 +11,104 @@ public interface IAnthropicApiClient
/// Creates a message asynchronously. /// Creates a message asynchronously.
/// </summary> /// </summary>
/// <param name="request">The message request to create.</param> /// <param name="request">The message request to create.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/>.</returns>
Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request); Task<AnthropicResult<MessageResponse>> CreateMessageAsync(MessageRequest request, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Creates a message asynchronously and streams the response. /// Creates a message asynchronously and streams the response.
/// </summary> /// </summary>
/// <param name="request">The message request to create.</param> /// <param name="request">The message request to create.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>An asynchronous enumerable that yields the response event by event.</returns> /// <returns>An asynchronous enumerable that yields the response event by event.</returns>
IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request); IAsyncEnumerable<AnthropicEvent> CreateMessageAsync(StreamMessageRequest request, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Creates a batch of messages asynchronously. /// Creates a batch of messages asynchronously.
/// </summary> /// </summary>
/// <param name="request">The message batch request to create.</param> /// <param name="request">The message batch request to create.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request); Task<AnthropicResult<MessageBatchResponse>> CreateMessageBatchAsync(MessageBatchRequest request, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Gets a message batch asynchronously. /// Gets a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to get.</param> /// <param name="batchId">The ID of the message batch to get.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId); Task<AnthropicResult<MessageBatchResponse>> GetMessageBatchAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists the message batches asynchronously. /// Lists the message batches asynchronously.
/// </summary> /// </summary>
/// <param name="request">The paging request to use for listing the message batches.</param> /// <param name="request">The paging request to use for listing the message batches.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null); Task<AnthropicResult<Page<MessageBatchResponse>>> ListMessageBatchesAsync(PagingRequest? request = null, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists all message batches asynchronously. /// Lists all message batches asynchronously.
/// </summary> /// </summary>
/// <param name="limit">The maximum number of message batches to return in each page.</param> /// <param name="limit">The maximum number of message batches to return in each page.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns> /// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20); IAsyncEnumerable<AnthropicResult<Page<MessageBatchResponse>>> ListAllMessageBatchesAsync(int limit = 20, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Cancels a message batch asynchronously. /// Cancels a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to cancel.</param> /// <param name="batchId">The ID of the message batch to cancel.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchResponse"/>.</returns>
Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId); Task<AnthropicResult<MessageBatchResponse>> CancelMessageBatchAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Deletes a message batch asynchronously. /// Deletes a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to delete.</param> /// <param name="batchId">The ID of the message batch to delete.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchDeleteResponse"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="MessageBatchDeleteResponse"/>.</returns>
Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId); Task<AnthropicResult<MessageBatchDeleteResponse>> DeleteMessageBatchAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Gets the results of a message batch asynchronously. /// Gets the results of a message batch asynchronously.
/// </summary> /// </summary>
/// <param name="batchId">The ID of the message batch to get the results for.</param> /// <param name="batchId">The ID of the message batch to get the results for.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="IAsyncEnumerable{T}"/> where T is <see cref="MessageBatchResultItem"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="IAsyncEnumerable{T}"/> where T is <see cref="MessageBatchResultItem"/>.</returns>
Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId); Task<AnthropicResult<IAsyncEnumerable<MessageBatchResultItem>>> GetMessageBatchResultsAsync(string batchId, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Counts the tokens in a message asynchronously. /// Counts the tokens in a message asynchronously.
/// </summary> /// </summary>
/// <param name="request">The count message tokens request.</param> /// <param name="request">The count message tokens request.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="TokenCountResponse"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="TokenCountResponse"/>.</returns>
Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request); Task<AnthropicResult<TokenCountResponse>> CountMessageTokensAsync(CountMessageTokensRequest request, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists the models asynchronously. /// Lists the models asynchronously.
/// </summary> /// </summary>
/// <param name="request">The paging request to use for listing the models.</param> /// <param name="request">The paging request to use for listing the models.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null); Task<AnthropicResult<Page<AnthropicModel>>> ListModelsAsync(PagingRequest? request = null, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Lists the models asynchronously /// Lists the models asynchronously
/// </summary> /// </summary>
/// <param name="limit">The maximum number of models to return in each page.</param> /// <param name="limit">The maximum number of models to return in each page.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns> /// <returns>An asynchronous enumerable that yields the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="Page{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
/// ///
IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20); IAsyncEnumerable<AnthropicResult<Page<AnthropicModel>>> ListAllModelsAsync(int limit = 20, CancellationToken cancellationToken = default);
/// <summary> /// <summary>
/// Gets a model by its ID asynchronously. /// Gets a model by its ID asynchronously.
/// </summary> /// </summary>
/// <param name="modelId">The ID of the model to get.</param> /// <param name="modelId">The ID of the model to get.</param>
/// <param name="cancellationToken">A token to cancel the asynchronous operation.</param>
/// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="AnthropicModel"/>.</returns> /// <returns>A task that represents the asynchronous operation. The task result contains the response as an <see cref="AnthropicResult{T}"/> where T is <see cref="AnthropicModel"/>.</returns>
Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId); Task<AnthropicResult<AnthropicModel>> GetModelAsync(string modelId, CancellationToken cancellationToken = default);
} }